You.i Engine
YiRtti.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_RTTI_H_
3 #define _YI_RTTI_H_
4 
6 #include "utility/YiStringView.h"
7 
8 class CYIString;
9 
59 {
60 public:
61  struct InstantiableTag;
62  struct NonInstantiableTag;
63  struct AutoDetectInstantiatabilityTag;
64 
65  virtual ~CYIRuntimeTypeInfo();
66 
76  template<typename T>
77  inline std::unique_ptr<T> CreateInstance() const;
78 
82  virtual bool CanCastTo(const CYIRuntimeTypeInfo &baseType) const = 0;
83 
91  template<typename T>
92  inline bool CanCastTo() const;
93 
97  CYIString GetName() const;
98 
103 
107  virtual const std::vector<const CYIRuntimeTypeInfo *> &GetBasesTypeInfos() const = 0;
108 
112  virtual bool IsInstantiable() const = 0;
113 
117  void RegisterWithLinker() const;
118 
132  template<typename T>
133  inline const T *CastVoidPointer(const void *pObject) const;
134 
135  inline bool operator==(const CYIRuntimeTypeInfo &other) const;
136  inline bool operator!=(const CYIRuntimeTypeInfo &other) const;
137  inline bool operator<(const CYIRuntimeTypeInfo &other) const;
138 
139 protected:
140  explicit CYIRuntimeTypeInfo(CYIStringView name);
141 
142 private:
143  CYIRuntimeTypeInfo(const CYIRuntimeTypeInfo &) = delete;
145  virtual const void *CastVoidToVoid(const CYIRuntimeTypeInfo &baseType, const void *pObject) const = 0;
146  CYIRuntimeTypeInfo &operator=(const CYIRuntimeTypeInfo &) = delete;
147  CYIRuntimeTypeInfo &operator=(CYIRuntimeTypeInfo &&) = delete;
148 
149  virtual void *CreateVoidInstance() const = 0;
150 
151  const CYIStringView m_name;
152 };
153 
164 template<typename CLASS, typename... BASES>
166 {
167 public:
171  typedef CLASS ClassType;
172 
182  typedef std::tuple<BASES...> BaseTypes;
183 
191  explicit CYIRuntimeTypeInfoTyped(const char *pName);
192 
198  inline bool CanCastToNonVirtual(const CYIRuntimeTypeInfo &baseType) const;
199 
211  template<typename T>
212  inline bool CanCastTo() const;
213 
219  template<typename T, typename... TBASES>
220  inline bool CanCastTo(const CYIRuntimeTypeInfoTyped<T, TBASES...> &baseType) const;
221 
227  inline std::unique_ptr<CLASS> CreateInstance() const;
228 
229  virtual bool CanCastTo(const CYIRuntimeTypeInfo &baseType) const override;
230  virtual const std::vector<const CYIRuntimeTypeInfo *> &GetBasesTypeInfos() const override;
231  inline const void *CastVoidToVoidNonVirtual(const CYIRuntimeTypeInfo &baseType, const void *pObject) const;
232  virtual bool IsInstantiable() const override;
233 
234 private:
235  virtual const void *CastVoidToVoid(const CYIRuntimeTypeInfo &baseType, const void *pObject) const override;
236  virtual void *CreateVoidInstance() const override;
237 };
238 
239 #if defined(DOXY)
240 
259 # define YI_TYPE_BASES(CLASS, BASES...)
260 
280 # define YI_TYPE_DEF(CLASS, BASES...)
281 
297 # define YI_TYPE_DEF_WITH_NAME(CLASS, NAME, BASES...)
298 
321 # define YI_TYPE_DEF_INST(CLASS, BASES...)
322 
341 # define YI_TYPE_DEF_INST_WITH_NAME(CLASS, NAME, BASES...)
342 
369 template<typename TARGET, typename SOURCE>
370 inline TARGET *YiDynamicCast(SOURCE *pObject);
371 
390 template<typename TARGET, typename SOURCE>
391 inline std::shared_ptr<TARGET> YiDynamicPointerCast(const std::shared_ptr<SOURCE> &pObject);
392 
396 template<typename TARGET, typename SOURCE>
397 inline std::shared_ptr<TARGET> YiDynamicCast(const std::shared_ptr<SOURCE> &pObject);
398 #endif
399 
413 template<typename T>
414 std::unique_ptr<T> YiRTTINew(CYIStringView name);
415 
419 const CYIRuntimeTypeInfo *YiGetTypeInfo(const char *pTypeName);
420 
425 
429 template<typename T>
430 inline const CYIRuntimeTypeInfo *YiGetTypeInfo(T *pObject);
431 
432 #if defined(DOXY)
433 
437 class RTTISampleClass : public Base1, public Base2, public Base3
438 {
439 public:
453  static const CYIRuntimeTypeInfoTyped<RTTISampleClass, Base1, Base2, Base3> &GetClassTypeInfo();
454 
464  const CYIRuntimeTypeInfo &GetRuntimeTypeInfo() const;
465 };
466 
467 #endif
468 
471 #include "utility/internal/YiRtti.inl"
472 
473 #endif /* _YI_RTTI_H_ */
CLASS ClassType
Definition: YiRtti.h:171
std::tuple< BASES... > BaseTypes
Definition: YiRtti.h:182
const T * CastVoidPointer(const void *pObject) const
bool operator<(const CYIRuntimeTypeInfo &other) const
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:32
bool operator!=(const CYIRuntimeTypeInfo &other) const
bool operator==(const CYIRuntimeTypeInfo &other) const
const CYIRuntimeTypeInfo * YiGetTypeInfo(const char *pTypeName)
CYIStringView GetNameAsView() const
bool CanCastTo() const
virtual ~CYIRuntimeTypeInfo()
TARGET * YiDynamicCast(SOURCE *pObject)
Casts the pointer pObject to type T*.
std::shared_ptr< TARGET > YiDynamicPointerCast(const std::shared_ptr< SOURCE > &pObject)
Casts the shared pointer pObject to shared pointer type T*.
std::unique_ptr< T > CreateInstance() const
This is a sample class to document the RTTI functions inserted into classes by the YI_TYPE_BASES(...
Definition: YiRtti.h:437
std::unique_ptr< T > YiRTTINew(CYIStringView name)
Creates a new instance of the type identifed by name.
The abstract runtime representation of a C++ type.
Definition: YiRtti.h:58
virtual bool IsInstantiable() const =0
void RegisterWithLinker() const
virtual const std::vector< const CYIRuntimeTypeInfo * > & GetBasesTypeInfos() const =0
CYIRuntimeTypeInfo(CYIStringView name)
CYIString GetName() const
The runtime representation of a C++ type.
Definition: YiRtti.h:165
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:56