GenericClasses
This example demonstrates how generic classes are translated to C++. They become C++ template classes which are inherited from System::Object and have RTTI declared.
Additional command-line options passed to CodePorting.Translator.Cs2Cpp: none.
Source C# Code
using System;
namespace TypesPorting
{
public class GenericClass<TInner>
{
}
public class GenericClassWithTypeConstraint<TInner> where TInner : ICloneable
{
}
public class GenericClassWithClassConstraint<TInner> where TInner : class
{
}
public class GenericClassWithStructConstraint<TInner> where TInner : struct
{
}
public class GenericClassWithNewConstraint<TInner> where TInner : new()
{
}
public class GenericClassWithSeveralConstraints<TInner> where TInner : class, ICloneable, new()
{
}
}
Translated Code
C++ Header
#pragma once
#include <system/object.h>
#include <system/icloneable.h>
#include <system/details/pointer_collection_helpers.h>
#include <system/constraints.h>
#include <cstdint>
namespace TypesPorting {
template<typename TInner>
class GenericClass : public System::Object
{
typedef GenericClass<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClass;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
template<typename TInner>
class GenericClassWithTypeConstraint : public System::Object
{
typedef System::ICloneable BaseT_ICloneable;
assert_is_base_of(BaseT_ICloneable, TInner);
typedef GenericClassWithTypeConstraint<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClassWithTypeConstraint;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
template<typename TInner>
class GenericClassWithClassConstraint : public System::Object
{
assert_is_cs_class(TInner);
typedef GenericClassWithClassConstraint<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClassWithClassConstraint;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
template<typename TInner>
class GenericClassWithStructConstraint : public System::Object
{
assert_is_cs_struct(TInner);
typedef GenericClassWithStructConstraint<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClassWithStructConstraint;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
template<typename TInner>
class GenericClassWithNewConstraint : public System::Object
{
assert_is_constructable(TInner);
typedef GenericClassWithNewConstraint<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClassWithNewConstraint;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
template<typename TInner>
class GenericClassWithSeveralConstraints : public System::Object
{
assert_is_cs_class(TInner);
typedef System::ICloneable BaseT_ICloneable;
assert_is_base_of(BaseT_ICloneable, TInner);
assert_is_constructable(TInner);
typedef GenericClassWithSeveralConstraints<TInner> ThisType;
typedef System::Object BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
template<typename FT0> friend class GenericClassWithSeveralConstraints;
public:
void SetTemplateWeakPtr(uint32_t argument) override
{
switch (argument)
{
case 0:
break;
}
}
};
} // namespace TypesPorting