CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
type_info.h
1
3#ifndef _TypeInfo_h_
4#define _TypeInfo_h_
5
6#include <system/shared_ptr.h>
7#include <system/reflection/binding_flags.h>
8#include <system/reflection.h>
9
10#include <memory>
11#include <vector>
12#include <functional>
13#include <type_traits>
14#include <cwchar>
15#include <iosfwd>
16#include <map>
17
18#include <fwd.h>
19
20namespace System { namespace Reflection { class Assembly; } }
21namespace System { namespace Reflection { class MemberInfo; }}
22namespace System { namespace Reflection { class MethodInfo; }}
23namespace System { namespace Reflection { class PropertyInfo; } }
24namespace System { namespace Reflection { class ConstructorInfo; }}
25namespace System { namespace Reflection { class FieldInfo; } }
26
27namespace System
28{
29
30class Object;
31class String;
32
35template<unsigned i>
37{
38public:
43 static void Hash(uint32_t &hash, int strLen, const char_t *input)
44 {
45 hash = input[strLen - i] + (hash << 6) + (hash << 16) - hash;
46 StringHashCompiletime<i - 1>::Hash(hash, strLen, input);
47 }
48};
49
52template<>
53class StringHashCompiletime<0>
54{
55public:
57 static void Hash(uint32_t &hash, int strLen, const char_t* input)
58 {
59 ASPOSE_UNUSED(hash);
60 ASPOSE_UNUSED(strLen);
61 ASPOSE_UNUSED(input);
62 }
63};
65
66class TypeInfo;
67
72{
74 std::unique_ptr<TypeInfo> ptr;
76 operator TypeInfo*()
77 {
78 return ptr.get();
79 }
80
82 TypeInfoPtr() = default;
83
86 ASPOSECPP_SHARED_API TypeInfoPtr(const std::type_info& info);
87
91 ASPOSECPP_SHARED_API TypeInfoPtr(const char_t* name, uint32_t hash);
92
95 ASPOSECPP_SHARED_API TypeInfoPtr(const char_t* name);
96
99 ASPOSECPP_SHARED_API TypeInfoPtr(const String& name);
100
102 ASPOSECPP_SHARED_API ~TypeInfoPtr();
103};
104
105class TypeInfoImpl;
106
108class ASPOSECPP_SHARED_CLASS TypeInfo
109{
110 friend class TypeInfoImpl;
111
113 typedef const TypeInfo& (*GetTypeInfoFunPtr)();
114
115 std::shared_ptr<TypeInfoImpl> m_pimpl;
116 TypeInfoImpl *getPipml() const;
117
118
119public:
123 static uint32_t StringHash(const char_t *str)
124 {
125 uint32_t hash = 0;
126
127 while (char_t c = *str++)
128 hash = c + (hash << 6) + (hash << 16) - hash;
129
130 return hash;
131 }
132
134 ASPOSECPP_SHARED_API TypeInfo();
136 ASPOSECPP_SHARED_API TypeInfo(std::nullptr_t);
139 ASPOSECPP_SHARED_API TypeInfo(const char_t* name);
143 ASPOSECPP_SHARED_API TypeInfo(const char_t* name, uint32_t hash);
146 ASPOSECPP_SHARED_API TypeInfo(const std::type_info& info);
147
149 ASPOSECPP_SHARED_API String get_Name() const;
150
152 ASPOSECPP_SHARED_API String get_Namespace() const;
153
156 ASPOSECPP_SHARED_API String get_FullName() const;
157
161
164 ASPOSECPP_SHARED_API String get_AssemblyQualifiedName() const;
165
168 ASPOSECPP_SHARED_API bool IsSubclassOf(const TypeInfo& type) const;
169
173 ASPOSECPP_SHARED_API bool IsInstanceOfType(const SharedPtr<Object>& obj) const;
174
177 ASPOSECPP_SHARED_API bool IsAssignableFrom(const TypeInfo& type) const;
178
185 ASPOSECPP_SHARED_API bool IsDefined(const TypeInfo& attributeType, bool inherit) const;
186
188 ASPOSECPP_SHARED_API bool get_IsSealed() const;
190 ASPOSECPP_SHARED_API bool get_IsInterface() const;
192 ASPOSECPP_SHARED_API bool get_IsAbstract() const;
194 ASPOSECPP_SHARED_API bool get_IsEnum() const;
196 ASPOSECPP_SHARED_API bool get_IsArray() const;
198 ASPOSECPP_SHARED_API bool get_IsClass() const;
200 ASPOSECPP_SHARED_API bool get_IsValueType() const;
202 ASPOSECPP_SHARED_API bool get_IsVisible() const;
204 ASPOSECPP_SHARED_API bool get_ContainsGenericParameters() const;
205 // /Gets a value indicating whether the current type is a generic type
206 ASPOSECPP_SHARED_API bool get_IsGenericType() const;
208 ASPOSECPP_SHARED_API bool get_IsGenericTypeDefinition() const;
210 ASPOSECPP_SHARED_API ArrayPtr<TypeInfo> get_GenericTypeArguments() const;
212 ASPOSECPP_SHARED_API ArrayPtr<TypeInfo> GetGenericArguments() const;
213
215 ASPOSECPP_SHARED_API ArrayPtr<TypeInfo> GetInterfaces() const;
216
219 ASPOSECPP_SHARED_API TypeInfo GetElementType() const;
220
227
229 ASPOSECPP_SHARED_API String ToString() const;
230
232 ASPOSECPP_SHARED_API uint32_t Hash() const;
233
235 int GetHashCode() const
236 {
237 return Hash();
238 }
239
240 bool Equals(const TypeInfo& other) const
241 {
242 return *this == other;
243 }
244
248 bool operator == (const TypeInfo& info) const
249 {
250 if (*this == nullptr && info == nullptr)
251 {
252 return true;
253 }
254
255 if (*this == nullptr || info == nullptr)
256 {
257 return false;
258 }
259
260 return Hash() == info.Hash();
261 }
262
266 bool operator != (const TypeInfo& info) const
267 {
268 return !operator==(info);
269 }
270
273 bool operator == (std::nullptr_t) const
274 {
275 return !m_pimpl;
276 }
277
280 bool operator != (std::nullptr_t) const
281 {
282 return !!m_pimpl;
283 }
284
287 ASPOSECPP_SHARED_API void AddAttribute(const ObjectPtr& attribute);
290 ASPOSECPP_SHARED_API void AddMember(const SharedPtr<System::Reflection::MemberInfo>& member);
302 ASPOSECPP_SHARED_API SharedPtr<System::Reflection::MethodInfo> GetMethod(const String& name) const;
303
312
319 ASPOSECPP_SHARED_API ObjectPtr GetCustomAttribute(const TypeInfo& attributeType) const;
320
322 ASPOSECPP_SHARED_API ArrayPtr<ObjectPtr> GetCustomAttributes() const;
326 ASPOSECPP_SHARED_API ArrayPtr<ObjectPtr> GetCustomAttributes(const TypeInfo& attributeType, bool inherit) const;
327
330 ASPOSECPP_SHARED_API TypeInfo get_BaseType() const;
333 ASPOSECPP_SHARED_API void SetBaseType(GetTypeInfoFunPtr basetype);
336 ASPOSECPP_SHARED_API void SetTemplParamType(const TypeInfo& templParam);
337
339 ASPOSECPP_SHARED_API TypeInfo GetTemplParamType() const;
340
342 ASPOSECPP_SHARED_API void reset();
343
345 template <class T> inline void AddDefaultConstructor();
346
348 typedef std::function<ObjectPtr()> DefaultConstructor;
349
352 ASPOSECPP_SHARED_API void AddDefaultConstructor(DefaultConstructor method);
353
355 ASPOSECPP_SHARED_API void set_IsValueType(bool value);
356
360 template <class T> static inline const TypeInfo& BoxedValueType()
361 {
362 return *System::static_holder<TypeInfo::BoxedValue<T>>();
363 }
364
366 template <class T> struct BoxedValue : TypeInfoPtr
367 {
370 : TypeInfoPtr(typeid(BoxedValue<T>))
371 {}
372 };
373
375 ASPOSECPP_SHARED_API static ArrayPtr<TypeInfo> EmptyTypes;
376
378 ASPOSECPP_SHARED_API static TypeInfo EmptyType;
379
381 static const TypeInfo& Type();
382
383};
384
389ASPOSECPP_SHARED_API std::ostream& operator<<(std::ostream& stream, const TypeInfo& type_info);
390
395ASPOSECPP_SHARED_API std::wostream& operator<<(std::wostream& stream, const TypeInfo& type_info);
396
397} // namespace System
398
399namespace std
400{
402template<>
403struct hash<System::TypeInfo> {
404public:
408 size_t operator()(const System::TypeInfo &ti) const
409 {
410 return ti.Hash();
411 }
412};
413}
414
415#endif // _TypeInfo_h_
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
A helper class that generates a hash value from a c-string.
Definition: type_info.h:37
static void Hash(uint32_t &hash, int strLen, const char_t *input)
Generates a hash value from the specified c-string of the specified length.
Definition: type_info.h:43
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Represents a particular type and provides information about it.
Definition: type_info.h:109
String ToString() const
Returns a string containing the name of the type represented by the current object.
ArrayPtr< SharedPtr< System::Reflection::PropertyInfo > > GetProperties(System::Reflection::BindingFlags bindingAttr) const
Searches for the properties of the current Type, using the specified binding constraints.
bool get_IsValueType() const
Gets a value indicating whether the Type is a value type.
System::SharedPtr< System::Reflection::Assembly > get_Assembly() const
NOT IMPLEMENTED. Returns a pointer to the assembly in which the type represented by the current objec...
bool get_IsSealed() const
Gets a value indicating whether the Type is declared sealed.
bool IsInstanceOfType(const SharedPtr< Object > &obj) const
Determines whether the specified object is an instance of the current type.
TypeInfo(const char_t *name, uint32_t hash)
Constructor.
SharedPtr< System::Reflection::FieldInfo > GetField(const System::String &name, System::Reflection::BindingFlags bindingAttr) const
Searches for the specified field, using the specified binding constraints.
static TypeInfo EmptyType
Constant representing empty list of TypeInfo.
Definition: type_info.h:378
String get_Namespace() const
Gets the namespace of the Type.
bool get_IsClass() const
Gets a value indicating whether the Type is a class or a delegate; that is, not a value type or inter...
String get_FullName() const
Returns the fully qualified name (but without the assembly name) of the type represented by the curre...
static const TypeInfo & BoxedValueType()
Provides unique TypeInfo structure for BoxedValue type to be shared by multiple Boxed* classes.
Definition: type_info.h:360
void reset()
Sets TypeInfo to null.
bool get_IsArray() const
Gets a value that indicates whether the type is an array.
ArrayPtr< ObjectPtr > GetCustomAttributes(const TypeInfo &attributeType, bool inherit) const
Returns an array containing objects that represent specific attributes applied to the type.
TypeInfo()
Default constructor (no type is set).
String get_AssemblyQualifiedName() const
NOT IMPLEMENTED. Returns the fully qualified name including the assembly name of the type represented...
bool get_IsAbstract() const
Gets a value indicating whether the Type is abstract and must be overridden.
bool get_IsGenericTypeDefinition() const
Gets a value indicating whether the current Type represents a generic type definition,...
static uint32_t StringHash(const char_t *str)
Calculates hash for specified string.
Definition: type_info.h:123
void set_IsValueType(bool value)
Sets a value indicating whether the Type is a value type.
bool get_IsGenericType() const
String get_Name() const
Returns the name of the type represented by the current object.
void AddAttribute(const ObjectPtr &attribute)
Adds the specified attribute to the list of type's attributes.
ArrayPtr< SharedPtr< System::Reflection::MemberInfo > > get_DeclaredMember(const String &name) const
Gets list of the members with specified name.
ArrayPtr< SharedPtr< System::Reflection::MemberInfo > > GetMember(const String &name) const
Gets list of the members with specified name.
void SetTemplParamType(const TypeInfo &templParam)
Sets template parameter type descritor.
void AddDefaultConstructor(DefaultConstructor method)
Sets default constructor by the functor that creates class instanse.
bool IsAssignableFrom(const TypeInfo &type) const
Determines whether an instance of a specified type can be assigned to a variable of the current type.
ObjectPtr GetCustomAttribute(const TypeInfo &attributeType) const
Searches for the custom attribute applied having the specified type and applied to the type reprsente...
static ArrayPtr< TypeInfo > EmptyTypes
Constant representing empty list of TypeInfo.
Definition: type_info.h:375
SharedPtr< System::Reflection::ConstructorInfo > GetConstructor(const ArrayPtr< TypeInfo > &types) const
Searches for a public instance constructor whose parameters match the types in the specified array.
uint32_t Hash() const
Returns a hash value associated with the type represented by the current object.
std::function< ObjectPtr()> DefaultConstructor
Function pointer to construct type.
Definition: type_info.h:348
bool get_ContainsGenericParameters() const
Gets a value indicating whether the current Type object has type parameters that have not been replac...
bool get_IsEnum() const
Gets a value indicating whether the current Type represents an enumeration.
bool Equals(const TypeInfo &other) const
Definition: type_info.h:240
ArrayPtr< TypeInfo > GetInterfaces() const
Gets all the interfaces implemented or inherited by the current Type.
TypeInfo(const std::type_info &info)
Constructor.
static const TypeInfo & Type()
Returns a TypeInfo object that represent TypeInfo class.
ArrayPtr< ObjectPtr > GetCustomAttributes() const
Returns an array containing objects that represent all custom attributes applied to the type.
int GetHashCode() const
Returns a hash code associated with this instance.
Definition: type_info.h:235
bool IsSubclassOf(const TypeInfo &type) const
Determines whether the type represented by the current object is a subclass of the specified class.
TypeInfo(const char_t *name)
Constructor.
ArrayPtr< TypeInfo > GetGenericArguments() const
Gets an array of the generic type arguments for this type.
SharedPtr< System::Reflection::MethodInfo > GetMethod(const String &name) const
Gets method with specified name.
TypeInfo get_BaseType() const
Returns base type descritor.
ArrayPtr< SharedPtr< System::Reflection::ConstructorInfo > > GetConstructors(System::Reflection::BindingFlags bindingAttr) const
searches for the constructors defined for the current Type, using the specified BindingFlags.
TypeInfo(std::nullptr_t)
Null object constructor (no type is set).
void AddMember(const SharedPtr< System::Reflection::MemberInfo > &member)
Adds the specified member to the list of type's members.
ArrayPtr< SharedPtr< System::Reflection::PropertyInfo > > GetProperties() const
Returns all the public properties of the current Type.
bool IsDefined(const TypeInfo &attributeType, bool inherit) const
NOT IMPLEMENTED. Indicates whether one or more attributes of the specified type or of its derived typ...
bool get_IsVisible() const
Gets a value indicating whether the Type can be accessed by code outside the assembly.
TypeInfo GetElementType() const
NOT IMPLEMENTED.
ArrayPtr< TypeInfo > get_GenericTypeArguments() const
Gets an array of the generic type arguments for this type.
bool get_IsInterface() const
Gets a value indicating whether the Type is an interface; that is, not a class or a value type.
ArrayPtr< SharedPtr< System::Reflection::ConstructorInfo > > GetConstructors() const
Returns all the public constructors defined for the current Type.
TypeInfo GetTemplParamType() const
Gets template parameter type descritor.
void SetBaseType(GetTypeInfoFunPtr basetype)
Sets base type descritor.
ArrayPtr< SharedPtr< System::Reflection::FieldInfo > > GetFields(System::Reflection::BindingFlags bindingAttr) const
Searches for the fields defined for the current Type, using the specified binding constraints.
BindingFlags
Degines members and types lookup modes and bindings.
Definition: binding_flags.h:10
@ Assembly
Assembly-scoped members.
@ TypeInfo
Specifies that the member is a type.
Definition: db_command.h:9
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:157
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:151
TypeInfo structure for BoxedValue class.
Definition: type_info.h:367
BoxedValue()
Fills appropriate type name.
Definition: type_info.h:369
Wrapper for a pointer to an instance of TypeInfo class. This type should be allocated on stack and pa...
Definition: type_info.h:72
TypeInfoPtr(const char_t *name)
Constructor.
TypeInfoPtr(const String &name)
Constructor.
~TypeInfoPtr()
Destructor.
std::unique_ptr< TypeInfo > ptr
Unique pointer to the TypeInfo object.
Definition: type_info.h:74
TypeInfoPtr(const char_t *name, uint32_t hash)
Constructor.
TypeInfoPtr()=default
Default constructor.
TypeInfoPtr(const std::type_info &info)
Constructor.