You.i Engine
YiRtti.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_RTTI_H_
3 #define _YI_RTTI_H_
4 
6 #include "utility/YiString.h"
7 
57 {
58 public:
59  struct InstantiableTag;
60  struct NonInstantiableTag;
61  struct AutoDetectInstantiatabilityTag;
62 
63  virtual ~CYIRuntimeTypeInfo();
64 
74  template<typename T>
75  inline std::unique_ptr<T> CreateInstance() const;
76 
80  virtual bool CanCastTo(const CYIRuntimeTypeInfo &baseType) const = 0;
81 
89  template<typename T>
90  inline bool CanCastTo() const;
91 
95  CYIString GetName() const;
96 
101 
105  virtual const std::vector<const CYIRuntimeTypeInfo *> &GetBasesTypeInfos() const = 0;
106 
110  virtual bool IsInstantiable() const = 0;
111 
115  void RegisterWithLinker() const;
116 
130  template<typename T>
131  inline const T *CastVoidPointer(const void *pObject) const;
132 
133  inline bool operator==(const CYIRuntimeTypeInfo &other) const;
134  inline bool operator!=(const CYIRuntimeTypeInfo &other) const;
135  inline bool operator<(const CYIRuntimeTypeInfo &other) const;
136 
137 protected:
138  explicit CYIRuntimeTypeInfo(CYIStringView name);
139 
140 private:
141  CYIRuntimeTypeInfo(const CYIRuntimeTypeInfo &) = delete;
143  virtual const void *CastVoidToVoid(const CYIRuntimeTypeInfo &baseType, const void *pObject) const = 0;
144  CYIRuntimeTypeInfo &operator=(const CYIRuntimeTypeInfo &) = delete;
145  CYIRuntimeTypeInfo &operator=(CYIRuntimeTypeInfo &&) = delete;
146 
147  virtual void *CreateVoidInstance() const = 0;
148 
149  const CYIStringView m_name;
150 };
151 
162 template<typename CLASS, typename... BASES>
164 {
165 public:
169  typedef CLASS ClassType;
170 
180  typedef std::tuple<BASES...> BaseTypes;
181 
189  explicit CYIRuntimeTypeInfoTyped(const char *pName);
190 
196  inline bool CanCastToNonVirtual(const CYIRuntimeTypeInfo &baseType) const;
197 
209  template<typename T>
210  inline bool CanCastTo() const;
211 
217  template<typename T, typename... TBASES>
218  inline bool CanCastTo(const CYIRuntimeTypeInfoTyped<T, TBASES...> &baseType) const;
219 
225  inline std::unique_ptr<CLASS> CreateInstance() const;
226 
227  virtual bool CanCastTo(const CYIRuntimeTypeInfo &baseType) const override;
228  virtual const std::vector<const CYIRuntimeTypeInfo *> &GetBasesTypeInfos() const override;
229  inline const void *CastVoidToVoidNonVirtual(const CYIRuntimeTypeInfo &baseType, const void *pObject) const;
230  virtual bool IsInstantiable() const override;
231 
232 private:
233  virtual const void *CastVoidToVoid(const CYIRuntimeTypeInfo &baseType, const void *pObject) const override;
234  virtual void *CreateVoidInstance() const override;
235 };
236 
237 #if defined(DOXY)
238 
257 # define YI_TYPE_BASES(CLASS, BASES...)
258 
278 # define YI_TYPE_DEF(CLASS, BASES...)
279 
295 # define YI_TYPE_DEF_WITH_NAME(CLASS, NAME, BASES...)
296 
319 # define YI_TYPE_DEF_INST(CLASS, BASES...)
320 
339 # define YI_TYPE_DEF_INST_WITH_NAME(CLASS, NAME, BASES...)
340 
367 template<typename TARGET, typename SOURCE>
368 inline TARGET *YiDynamicCast(SOURCE *pObject);
369 
388 template<typename TARGET, typename SOURCE>
389 inline std::shared_ptr<TARGET> YiDynamicPointerCast(const std::shared_ptr<SOURCE> &pObject);
390 
394 template<typename TARGET, typename SOURCE>
395 inline std::shared_ptr<TARGET> YiDynamicCast(const std::shared_ptr<SOURCE> &pObject);
396 #endif
397 
411 template<typename T>
412 std::unique_ptr<T> YiRTTINew(CYIStringView name);
413 
417 const CYIRuntimeTypeInfo *YiGetTypeInfo(const char *pTypeName);
418 
423 
427 template<typename T>
428 inline const CYIRuntimeTypeInfo *YiGetTypeInfo(T *pObject);
429 
430 #if defined(DOXY)
431 
435 class RTTISampleClass : public Base1, public Base2, public Base3
436 {
437 public:
451  static const CYIRuntimeTypeInfoTyped<RTTISampleClass, Base1, Base2, Base3> &GetClassTypeInfo();
452 
462  const CYIRuntimeTypeInfo &GetRuntimeTypeInfo() const;
463 };
464 
465 #endif
466 
469 #include "utility/YiRtti.inl"
470 
471 #endif /* _YI_RTTI_H_ */
CLASS ClassType
Definition: YiRtti.h:169
std::tuple< BASES... > BaseTypes
Definition: YiRtti.h:180
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:36
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:435
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:56
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:163
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57