BsRTTIType.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #pragma once
  2. #include <string>
  3. #include <algorithm>
  4. #include <unordered_map>
  5. #include "BsPrerequisitesUtil.h"
  6. #include "BsManagedDataBlock.h"
  7. #include "BsRTTIField.h"
  8. #include "BsRTTIPlainField.h"
  9. #include "BsRTTIReflectableField.h"
  10. #include "BsRTTIReflectablePtrField.h"
  11. #include "BsRTTIManagedDataBlockField.h"
  12. #include "BsIReflectable.h"
  13. namespace BansheeEngine
  14. {
  15. #define BS_SETGET_MEMBER(name, type, parentType) \
  16. type##& get##name(parentType##* obj) { return obj->##name; } \
  17. void Set##name(parentType##* obj, type##& val) { obj->##name = val; }
  18. #define BS_ADD_PLAINFIELD(name, id, parentType) \
  19. addPlainField(#name, id##, &##parentType##::get##name, &##parentType##::Set##name);
  20. /**
  21. * @brief Provides an interface for accessing fields of a certain class.
  22. * Data can be easily accessed by getter and setter methods.
  23. *
  24. * Supported data types:
  25. * - Plain types - All types defined in BsRTTIField.h, mostly native types and POD (plain old data) structs. Data is parsed byte by byte.
  26. * No pointers to plain types are supported. Data is passed around by value.
  27. * - Reflectable types - Any class deriving from IReflectable. Data is parsed based on fields in its RTTI class. Can be pointer or value type.
  28. * - Arrays of both plain and reflectable types are supported
  29. * - Data blocks - A managed or unmanaged block of data. See BsManagedDataBlock.h
  30. */
  31. class BS_UTILITY_EXPORT RTTITypeBase
  32. {
  33. public:
  34. RTTITypeBase();
  35. virtual ~RTTITypeBase();
  36. /**
  37. * @brief Returns RTTI type information for all classes that derive from the class
  38. * that owns this RTTI type.
  39. */
  40. virtual Vector<RTTITypeBase*>& getDerivedClasses() = 0;
  41. /**
  42. * @brief Returns RTTI type information for the class that owns this RTTI type.
  43. * If the class has not base type, null is returned instead.
  44. */
  45. virtual RTTITypeBase* getBaseClass() = 0;
  46. /**
  47. * @brief Returns true if current RTTI class is derived from "base".
  48. * (Or if it is the same type as base)
  49. */
  50. virtual bool isDerivedFrom(RTTITypeBase* base) = 0;
  51. /**
  52. * @brief Internal method. Called by the RTTI system when a class is first found in
  53. * order to form child/parent class hierarchy.
  54. */
  55. virtual void _registerDerivedClass(RTTITypeBase* derivedClass) = 0;
  56. /**
  57. * @brief Creates a new instance of the class owning this RTTI type.
  58. */
  59. virtual std::shared_ptr<IReflectable> newRTTIObject() = 0;
  60. /**
  61. * @brief Returns the name of the class owning this RTTI type.
  62. */
  63. virtual const String& getRTTIName() = 0;
  64. /**
  65. * @brief Returns an RTTI id that uniquely represents each class in the RTTI
  66. * system.
  67. */
  68. virtual UINT32 getRTTIId() = 0;
  69. /**
  70. * @brief Called by the serializers when serialization for this object has started.
  71. * Use this to do any preprocessing on data you might need during serialization itself.
  72. */
  73. virtual void onSerializationStarted(IReflectable* obj) {}
  74. /**
  75. * @brief Called by the serializers when serialization for this object has ended.
  76. * After serialization has ended you can be sure that the type has been fully serialized,
  77. * and you may clean up any temporary data.
  78. */
  79. virtual void onSerializationEnded(IReflectable* obj) {}
  80. /**
  81. * @brief Called by the serializers when deserialization for this object has started.
  82. * Use this to do any preprocessing on data you might need during deserialization itself.
  83. */
  84. virtual void onDeserializationStarted(IReflectable* obj) {}
  85. /**
  86. * @brief Called by the serializers when deserialization for this object has ended.
  87. * At this point you can be sure the instance has been fully deserialized and you
  88. * may safely use it.
  89. *
  90. * One exception being are fields you marked with "WeakRef" flag, as they might be resolved
  91. * only after deserialization has fully completed for all objects.
  92. */
  93. virtual void onDeserializationEnded(IReflectable* obj) {}
  94. /**
  95. * @brief Allows you to assign a value to a plain field with the specified name on
  96. * the provided instance.
  97. *
  98. * @note Caller must ensure instance and value types are valid for this field.
  99. */
  100. template <class ObjectType, class DataType>
  101. void setPlainValue(ObjectType* object, const String& name, DataType& value)
  102. {
  103. RTTIField* genericField = findField(name);
  104. genericField->checkIsPlain(false);
  105. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(genericField);
  106. UINT32 typeSize = 0;
  107. if(RTTIPlainType<DataType>::hasDynamicSize)
  108. typeSize = RTTIPlainType<DataType>::getDynamicSize(value);
  109. else
  110. typeSize = sizeof(DataType);
  111. UINT8* tempBuffer = (UINT8*)stackAlloc(typeSize);
  112. RTTIPlainType<DataType>::toMemory(value, (char*)tempBuffer);
  113. field->fromBuffer(object, tempBuffer);
  114. stackDeallocLast(tempBuffer);
  115. }
  116. /**
  117. * @brief Allows you to assign a value to a plain field array element with the
  118. * specified name and index on the provided instance.
  119. *
  120. * @note Caller must ensure instance and value types are valid for this field.
  121. */
  122. template <class ObjectType, class DataType>
  123. void setPlainArrayValue(ObjectType* object, const String& name, UINT32 index, DataType& value)
  124. {
  125. RTTIField* genericField = findField(name);
  126. genericField->checkIsPlain(true);
  127. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(genericField);
  128. UINT32 typeSize = 0;
  129. if(RTTIPlainType<DataType>::hasDynamicSize)
  130. typeSize = RTTIPlainType<DataType>::getDynamicSize(value);
  131. else
  132. typeSize = sizeof(DataType);
  133. UINT8* tempBuffer = (UINT8*)stackAlloc(typeSize);
  134. RTTIPlainType<DataType>::toMemory(value, (char*)tempBuffer);
  135. field->arrayElemFromBuffer(object, index, tempBuffer);
  136. stackDeallocLast(tempBuffer);
  137. }
  138. /**
  139. * @brief Allows you to assign a value to a reflectable field with the specified name on
  140. * the provided instance.
  141. *
  142. * @note Caller must ensure instance and value types are valid for this field.
  143. */
  144. template <class ObjectType, class DataType>
  145. void setReflectableValue(ObjectType* object, const String& name, DataType& value)
  146. {
  147. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  148. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  149. RTTIField* genericField = findField(name);
  150. genericField->checkIsComplex(false);
  151. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  152. field->setValue(object, value);
  153. }
  154. /**
  155. * @brief Allows you to assign a value to a reflectable field array element with the
  156. * specified name and index on the provided instance.
  157. *
  158. * @note Caller must ensure instance and value types are valid for this field.
  159. */
  160. template <class ObjectType, class DataType>
  161. void setReflectableArrayValue(ObjectType* object, const String& name, UINT32 index, DataType& value)
  162. {
  163. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  164. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  165. RTTIField* genericField = findField(name);
  166. genericField->checkIsComplex(true);
  167. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  168. field->setArrayValue(object, index, value);
  169. }
  170. /**
  171. * @brief Allows you to assign a value to a managed data block field with the specified name on
  172. * the provided instance.
  173. *
  174. * @note Caller must ensure instance type is valid for this field.
  175. */
  176. template <class ObjectType>
  177. void setDataBlockValue(ObjectType* object, const String& name, ManagedDataBlock value)
  178. {
  179. RTTIField* genericField = findField(name);
  180. genericField->checkIsDataBlock();
  181. RTTIManagedDataBlockFieldBase* field = static_cast<RTTIManagedDataBlockFieldBase*>(genericField);
  182. field->setValue(object, value);
  183. }
  184. /**
  185. * @brief Allows you to assign a value to a reflectable pointer field with the specified name on
  186. * the provided instance.
  187. *
  188. * @note Caller must ensure instance and value types are valid for this field.
  189. */
  190. template <class ObjectType, class DataType>
  191. void setReflectablePtrValue(ObjectType* object, const String& name, std::shared_ptr<DataType> value)
  192. {
  193. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  194. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  195. RTTIField* genericField = findField(name);
  196. genericField->checkIsComplexPtr(false);
  197. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  198. field->setValue(object, value);
  199. }
  200. /**
  201. * @brief Allows you to assign a value to a reflectable pointer field array element with the
  202. * specified name and index on the provided instance.
  203. *
  204. * @note Caller must ensure instance and value types are valid for this field.
  205. */
  206. template <class ObjectType, class DataType>
  207. void setReflectablePtrArrayValue(ObjectType* object, const String& name, UINT32 index, std::shared_ptr<DataType> value)
  208. {
  209. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  210. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  211. RTTIField* genericField = findField(name);
  212. genericField->checkIsComplexPtr(true);
  213. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  214. field->setArrayValue(object, index, value);
  215. }
  216. /**
  217. * @brief Reads a value from a plain field with the specified name from the provided instance.
  218. *
  219. * @note Caller must ensure instance and value types are valid for this field.
  220. */
  221. template <class ObjectType, class DataType>
  222. void getPlainValue(ObjectType* object, const String& name, DataType& value)
  223. {
  224. RTTIField* genericField = findField(name);
  225. genericField->checkIsPlain(false);
  226. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(genericField);
  227. UINT32 typeSize = 0;
  228. if(field->hasDynamicSize())
  229. typeSize = field->getDynamicSize(object);
  230. else
  231. typeSize = field->getTypeSize();
  232. UINT8* tempBuffer = (UINT8*)stackAlloc(typeSize);
  233. field->toBuffer(object, tempBuffer);
  234. RTTIPlainType<DataType>::fromMemory(value, (char*)tempBuffer);
  235. stackDeallocLast(tempBuffer);
  236. }
  237. /**
  238. * @brief Reads a value from a plain array field with the specified name and index from the provided instance.
  239. *
  240. * @note Caller must ensure instance and value types are valid for this field.
  241. */
  242. template <class ObjectType, class DataType>
  243. void getPlainArrayValue(ObjectType* object, const String& name, UINT32 index, DataType& value)
  244. {
  245. RTTIField* genericField = findField(name);
  246. genericField->checkIsPlain(true);
  247. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(genericField);
  248. UINT32 typeSize = 0;
  249. if(field->hasDynamicSize())
  250. typeSize = field->getArrayElemDynamicSize(object, arrIdx);
  251. else
  252. typeSize = field->getTypeSize();
  253. UINT8* tempBuffer = (UINT8*)stackAlloc(typeSize);
  254. field->arrayElemToBuffer(object, index, tempBuffer);
  255. RTTIPlainType<DataType>::fromMemory(value, (char*)tempBuffer);
  256. stackDeallocLast(tempBuffer);
  257. }
  258. /**
  259. * @brief Reads a value from a reflectable object field with the specified name from the provided instance.
  260. *
  261. * @note Caller must ensure instance and value types are valid for this field.
  262. */
  263. template <class ObjectType>
  264. IReflectable& getReflectableValue(ObjectType* object, const String& name)
  265. {
  266. RTTIField* genericField = findField(name);
  267. genericField->checkIsComplex(false);
  268. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  269. return field->getValue(object);
  270. }
  271. /**
  272. * @brief Reads a value from a reflectable object array field with the specified name and index from the provided instance.
  273. *
  274. * @note Caller must ensure instance and value types are valid for this field.
  275. */
  276. template <class ObjectType>
  277. IReflectable& getReflectableArrayValue(ObjectType* object, const String& name, UINT32 index)
  278. {
  279. RTTIField* genericField = findField(name);
  280. genericField->checkIsComplex(true);
  281. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  282. return field->getArrayValue(object, index);
  283. }
  284. /**
  285. * @brief Reads a managed data block field with the specified name from the provided instance.
  286. *
  287. * @note Caller must ensure instance type is valid for this field.
  288. */
  289. template <class ObjectType>
  290. ManagedDataBlock getDataBlockValue(ObjectType* object, const String& name)
  291. {
  292. RTTIField* genericField = findField(name);
  293. genericField->checkIsDataBlock();
  294. RTTIManagedDataBlockFieldBase* field = static_cast<RTTIManagedDataBlockFieldBase*>(genericField);
  295. return field->getValue(object);
  296. }
  297. /**
  298. * @brief Reads a value from a reflectable object pointer field with the specified name from the provided instance.
  299. *
  300. * @note Caller must ensure instance and value types are valid for this field.
  301. */
  302. template <class ObjectType>
  303. std::shared_ptr<IReflectable> getReflectablePtrValue(ObjectType* object, const String& name)
  304. {
  305. RTTIField* genericField = findField(name);
  306. genericField->checkIsComplexPtr(false);
  307. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  308. return field->getValue(object);
  309. }
  310. /**
  311. * @brief Reads a value from a reflectable pointer array field with the specified name and index from the provided instance.
  312. *
  313. * @note Caller must ensure instance and value types are valid for this field.
  314. */
  315. template <class ObjectType>
  316. std::shared_ptr<IReflectable> getReflectablePtrArrayValue(ObjectType* object, const String& name, UINT32 index)
  317. {
  318. RTTIField* genericField = findField(name);
  319. genericField->checkIsComplexPtr(true);
  320. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  321. return field->getArrayValue(object, index);
  322. }
  323. /**
  324. * @brief Returns the size of the array of the field with the specified name on the provided instance.
  325. *
  326. * @note Caller must ensure instance type is valid and that the field as an array.
  327. */
  328. template <class ObjectType>
  329. UINT32 getArraySize(ObjectType* object, const String& name)
  330. {
  331. RTTIField* field = findField(name);
  332. return field->getArraySize(object);
  333. }
  334. /**
  335. * @brief Sets the size of the array of the field with the specified name on the provided instance.
  336. *
  337. * @note Caller must ensure instance type is valid and that the field as an array.
  338. * This might clear any existing data from the array.
  339. */
  340. template <class ObjectType>
  341. void setArraySize(ObjectType* object, const String& name, UINT32 size)
  342. {
  343. RTTIField* field = findField(name);
  344. field->setArraySize(object, size);
  345. }
  346. /**
  347. * @brief Returns the total number of fields in this RTTI type.
  348. */
  349. UINT32 getNumFields() { return (UINT32)mFields.size(); }
  350. /**
  351. * @brief Returns a field based on the field index. Use "getNumFields" to
  352. * get total number of fields available.
  353. */
  354. RTTIField* getField(UINT32 idx) { return mFields.at(idx); }
  355. /**
  356. * @brief Tries to find a field with the specified name. Throws an exception if it can't.
  357. *
  358. * @param name The name of the field.
  359. */
  360. RTTIField* findField(const String& name);
  361. /**
  362. * @brief Tries to find a field with the specified unique ID. Doesn't throw an exception
  363. * if it can't find the field (Unlike findField(name)).
  364. *
  365. * @param uniqueFieldId Unique identifier for the field.
  366. *
  367. * @return nullptr if it can't find the field.
  368. */
  369. RTTIField* findField(int uniqueFieldId);
  370. protected:
  371. /**
  372. * @brief Tries to add a new field to the fields array, and throws an exception
  373. * if a field with the same name or id already exists.
  374. *
  375. * @param [in] field Field, must be non-null.
  376. */
  377. void addNewField(RTTIField* field);
  378. /**
  379. * @brief Checks if the templated DataType has any references back to us, that aren't weak.
  380. *
  381. * @note This method assumes this class holds a non-weak reference to DataType.
  382. * DataType must derive from IReflectable and implement getRTTIStatic method.
  383. */
  384. template<class DataType>
  385. void checkForCircularReferences()
  386. {
  387. RTTITypeBase* type = DataType::getRTTIStatic();
  388. for(UINT32 i = 0; i < type->getNumFields(); i++)
  389. {
  390. RTTIField* field = type->getField(i);
  391. if(!field->isReflectablePtrType())
  392. continue;
  393. RTTIReflectablePtrFieldBase* reflectablePtrField = static_cast<RTTIReflectablePtrFieldBase*>(field);
  394. if(reflectablePtrField->getRTTIId() == getRTTIId() && ((reflectablePtrField->getFlags() & RTTI_Flag_WeakRef) == 0))
  395. {
  396. throwCircularRefException(getRTTIName(), reflectablePtrField->getRTTIName());
  397. }
  398. }
  399. }
  400. /**
  401. * @brief Throws an exception warning the user that a circular reference was found.
  402. *
  403. * @note Only a separate function so I don't need to include BsException header.
  404. */
  405. void throwCircularRefException(const String& myType, const String& otherType) const;
  406. private:
  407. Vector<RTTIField*> mFields;
  408. };
  409. /**
  410. * @brief Used for initializing a certain type as soon as the program is loaded.
  411. */
  412. template<typename Type, typename BaseType>
  413. struct InitRTTIOnStart
  414. {
  415. public:
  416. InitRTTIOnStart()
  417. {
  418. BaseType::getRTTIStatic()->_registerDerivedClass(Type::getRTTIStatic());
  419. }
  420. void makeSureIAmInstantiated() { }
  421. };
  422. /**
  423. * @brief Specialization for root class of RTTI hierarchy - IReflectable
  424. */
  425. template<typename Type>
  426. struct InitRTTIOnStart<Type, IReflectable>
  427. {
  428. public:
  429. InitRTTIOnStart()
  430. {
  431. IReflectable::_registerDerivedClass(Type::getRTTIStatic());
  432. }
  433. void makeSureIAmInstantiated() { }
  434. };
  435. /**
  436. * @brief Template that returns RTTI type of the specified type, unless the specified
  437. * type is IReflectable in which case it returns a null.
  438. */
  439. template<typename Type>
  440. struct GetRTTIType
  441. {
  442. RTTITypeBase* operator()() { return Type::getRTTIStatic(); }
  443. };
  444. /**
  445. * @brief Specialization for root class of RTTI hierarchy - IReflectable
  446. */
  447. template<>
  448. struct GetRTTIType<IReflectable>
  449. {
  450. RTTITypeBase* operator()() { return nullptr; }
  451. };
  452. /**
  453. * @brief Allows you to provide a run-time type information for a specific class, along with
  454. * support for serialization/deserialization.
  455. *
  456. * Derive from this class and return the that class from IReflectable::getRTTI.
  457. * This way you can separate serialization logic from the actual class you're serializing.
  458. *
  459. * This class will provide a way to register individual fields in the class, together with ways to
  460. * read and write them, as well a providing information about class hierarchy, and run-time type checking.
  461. */
  462. template <typename Type, typename BaseType, typename MyRTTIType>
  463. class RTTIType : public RTTITypeBase
  464. {
  465. protected:
  466. /************************************************************************/
  467. /* RTTI CLASS META DATA */
  468. /************************************************************************/
  469. static InitRTTIOnStart<Type, BaseType> initOnStart;
  470. public:
  471. RTTIType()
  472. {
  473. // Compiler will only generate code for stuff that is directly used, including static data members,
  474. // so we fool it here like we're using the class directly. Otherwise compiler won't generate the code for the member
  475. // and our type won't get initialized on start (Actual behavior is a bit more random)
  476. initOnStart.makeSureIAmInstantiated();
  477. }
  478. virtual ~RTTIType() {}
  479. /**
  480. * @brief Returns a singleton of this RTTI type.
  481. */
  482. static MyRTTIType* instance()
  483. {
  484. static MyRTTIType inst;
  485. return &inst;
  486. }
  487. /**
  488. * @copydoc RTTITypeBase::getDerivedClasses
  489. */
  490. virtual Vector<RTTITypeBase*>& getDerivedClasses()
  491. {
  492. static Vector<RTTITypeBase*> mRTTIDerivedClasses;
  493. return mRTTIDerivedClasses;
  494. }
  495. /**
  496. * @copydoc RTTITypeBase::getBaseClass
  497. */
  498. virtual RTTITypeBase* getBaseClass()
  499. {
  500. return GetRTTIType<BaseType>()();
  501. }
  502. /**
  503. * @copydoc RTTITypeBase::isDerivedFrom
  504. */
  505. bool RTTITypeBase::isDerivedFrom(RTTITypeBase* base)
  506. {
  507. assert(base != nullptr);
  508. Stack<RTTITypeBase*> todo;
  509. todo.push(base);
  510. while (!todo.empty())
  511. {
  512. RTTITypeBase* currentType = todo.top();
  513. todo.pop();
  514. if (currentType->getRTTIId() == getRTTIId())
  515. return true;
  516. Vector<RTTITypeBase*>& derivedClasses = currentType->getDerivedClasses();
  517. for (auto iter = derivedClasses.begin(); iter != derivedClasses.end(); ++iter)
  518. todo.push(*iter);
  519. }
  520. return false;
  521. }
  522. /**
  523. * @copydoc RTTITypeBase::_registerDerivedClass
  524. */
  525. virtual void _registerDerivedClass(RTTITypeBase* derivedClass)
  526. {
  527. if(IReflectable::_isTypeIdDuplicate(derivedClass->getRTTIId()))
  528. {
  529. BS_EXCEPT(InternalErrorException, "RTTI type \"" + derivedClass->getRTTIName() +
  530. "\" has a duplicate ID: " + toString(derivedClass->getRTTIId()));
  531. }
  532. getDerivedClasses().push_back(derivedClass);
  533. }
  534. /************************************************************************/
  535. /* FIELDS OPERATING DIRECTLY ON SERIALIZABLE OBJECT */
  536. /************************************************************************/
  537. /**
  538. * @brief Registers a new plain field. This field can then be accessed dynamically from the RTTI system and
  539. * used for automatic serialization. See RTTIField for more information about field types.
  540. *
  541. * @param name Name of the field.
  542. * @param uniqueId Unique identifier for this field. Although name is also a unique
  543. * identifier we want a small data type that can be used for efficiently
  544. * serializing data to disk and similar. It is primarily used for compatibility
  545. * between different versions of serialized data.
  546. * @param getter Method used for retrieving the value of this field.
  547. * @param setter Method used for setting the value of this field.
  548. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  549. */
  550. template<class ObjectType, class DataType>
  551. void addPlainField(const String& name, UINT32 uniqueId, DataType& (ObjectType::*getter)(),
  552. void (ObjectType::*setter)(DataType&) = nullptr, UINT64 flags = 0)
  553. {
  554. addPlainField<ObjectType, DataType>(name, uniqueId,
  555. std::function<DataType&(ObjectType*)>(getter),
  556. std::function<void(ObjectType*, DataType&)>(setter), flags);
  557. }
  558. /**
  559. * @brief Registers a new reflectable object field. This field can then be accessed dynamically from the RTTI system and
  560. * used for automatic serialization. See RTTIField for more information about field types.
  561. *
  562. * @param name Name of the field.
  563. * @param uniqueId Unique identifier for this field. Although name is also a unique
  564. * identifier we want a small data type that can be used for efficiently
  565. * serializing data to disk and similar. It is primarily used for compatibility
  566. * between different versions of serialized data.
  567. * @param getter Method used for retrieving the value of this field.
  568. * @param setter Method used for setting the value of this field.
  569. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  570. */
  571. template<class ObjectType, class DataType>
  572. void addReflectableField(const String& name, UINT32 uniqueId, DataType& (ObjectType::*getter)(),
  573. void (ObjectType::*setter)(DataType&) = nullptr, UINT64 flags = 0)
  574. {
  575. addReflectableField<ObjectType, DataType>(name, uniqueId,
  576. std::function<DataType&(ObjectType*)>(getter),
  577. std::function<void(ObjectType*, DataType&)>(setter), flags);
  578. }
  579. /**
  580. * @brief Registers a new reflectable object pointer field. This field can then be accessed dynamically from the RTTI system and
  581. * used for automatic serialization. See RTTIField for more information about field types.
  582. *
  583. * @param name Name of the field.
  584. * @param uniqueId Unique identifier for this field. Although name is also a unique
  585. * identifier we want a small data type that can be used for efficiently
  586. * serializing data to disk and similar. It is primarily used for compatibility
  587. * between different versions of serialized data.
  588. * @param getter Method used for retrieving the value of this field.
  589. * @param setter Method used for setting the value of this field.
  590. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  591. */
  592. template<class ObjectType, class DataType>
  593. void addReflectablePtrField(const String& name, UINT32 uniqueId, std::shared_ptr<DataType> (ObjectType::*getter)(),
  594. void (ObjectType::*setter)(std::shared_ptr<DataType>) = nullptr, UINT64 flags = 0)
  595. {
  596. addReflectablePtrField<ObjectType, DataType>(name, uniqueId,
  597. std::function<std::shared_ptr<DataType>(ObjectType*)>(getter),
  598. std::function<void(ObjectType*, std::shared_ptr<DataType>)>(setter), flags);
  599. }
  600. /**
  601. * @brief Registers a new field containg an array of plain values. This field can then be accessed dynamically from the RTTI system and
  602. * used for automatic serialization. See RTTIField for more information about field types.
  603. *
  604. * @param name Name of the field.
  605. * @param uniqueId Unique identifier for this field. Although name is also a unique
  606. * identifier we want a small data type that can be used for efficiently
  607. * serializing data to disk and similar. It is primarily used for compatibility
  608. * between different versions of serialized data.
  609. * @param getter Method used for retrieving a single element of the array.
  610. * @param getSize Getter method that returns the size of the array.
  611. * @param setter Method used for setting the a single element of the field.
  612. * @param setSize Setter method that allows you to resize the array.
  613. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  614. */
  615. template<class ObjectType, class DataType>
  616. void addPlainArrayField(const String& name, UINT32 uniqueId, DataType& (ObjectType::*getter)(UINT32), UINT32 (ObjectType::*getSize)(),
  617. void (ObjectType::*setter)(UINT32, DataType&) = nullptr, void(ObjectType::*setSize)(UINT32) = nullptr, UINT64 flags = 0)
  618. {
  619. addPlainArrayField<ObjectType, DataType>(name, uniqueId,
  620. std::function<DataType&(ObjectType*, UINT32)>(getter),
  621. std::function<UINT32(ObjectType*)>(getSize),
  622. std::function<void(ObjectType*, UINT32, DataType&)>(setter),
  623. std::function<void(ObjectType*, UINT32)>(setSize), flags);
  624. }
  625. /**
  626. * @brief Registers a new field containg an array of reflectable object values. This field can then be accessed
  627. * dynamically from the RTTI system and used for automatic serialization. See RTTIField for more information
  628. * about field types.
  629. *
  630. * @param name Name of the field.
  631. * @param uniqueId Unique identifier for this field. Although name is also a unique
  632. * identifier we want a small data type that can be used for efficiently
  633. * serializing data to disk and similar. It is primarily used for compatibility
  634. * between different versions of serialized data.
  635. * @param getter Method used for retrieving a single element of the array.
  636. * @param getSize Getter method that returns the size of the array.
  637. * @param setter Method used for setting the a single element of the field.
  638. * @param setSize Setter method that allows you to resize the array.
  639. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  640. */
  641. template<class ObjectType, class DataType>
  642. void addReflectableArrayField(const String& name, UINT32 uniqueId, DataType& (ObjectType::*getter)(UINT32), UINT32 (ObjectType::*getSize)(),
  643. void (ObjectType::*setter)(UINT32, DataType&) = nullptr, void(ObjectType::*setSize)(UINT32) = nullptr, UINT64 flags = 0)
  644. {
  645. addReflectableArrayField<ObjectType, DataType>(name, uniqueId,
  646. std::function<DataType&(ObjectType*, UINT32)>(getter),
  647. std::function<UINT32(ObjectType*)>(getSize),
  648. std::function<void(ObjectType*, UINT32, DataType&)>(setter),
  649. std::function<void(ObjectType*, UINT32)>(setSize), flags);
  650. }
  651. /**
  652. * @brief Registers a new field containg an array of reflectable obejct pointers. This field can then be accessed
  653. * dynamically from the RTTI system and used for automatic serialization. See RTTIField for more information
  654. * about field types.
  655. *
  656. * @param name Name of the field.
  657. * @param uniqueId Unique identifier for this field. Although name is also a unique
  658. * identifier we want a small data type that can be used for efficiently
  659. * serializing data to disk and similar. It is primarily used for compatibility
  660. * between different versions of serialized data.
  661. * @param getter Method used for retrieving a single element of the array.
  662. * @param getSize Getter method that returns the size of the array.
  663. * @param setter Method used for setting the a single element of the field.
  664. * @param setSize Setter method that allows you to resize the array.
  665. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  666. */
  667. template<class ObjectType, class DataType>
  668. void addReflectablePtrArrayField(const String& name, UINT32 uniqueId, std::shared_ptr<DataType> (ObjectType::*getter)(UINT32), UINT32 (ObjectType::*getSize)(),
  669. void (ObjectType::*setter)(UINT32, std::shared_ptr<DataType>) = nullptr, void(ObjectType::*setSize)(UINT32) = nullptr, UINT64 flags = 0)
  670. {
  671. addReflectablePtrArrayField<ObjectType, DataType>(name, uniqueId,
  672. std::function<std::shared_ptr<DataType>(ObjectType*, UINT32)>(getter),
  673. std::function<UINT32(ObjectType*)>(getSize),
  674. std::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)>(setter),
  675. std::function<void(ObjectType*, UINT32)>(setSize), flags);
  676. }
  677. /**
  678. * @brief Registers a new managed data block field. This field can then be accessed dynamically from the RTTI system and
  679. * used for automatic serialization. See RTTIField for more information about field types.
  680. *
  681. * @param name Name of the field.
  682. * @param uniqueId Unique identifier for this field. Although name is also a unique
  683. * identifier we want a small data type that can be used for efficiently
  684. * serializing data to disk and similar. It is primarily used for compatibility
  685. * between different versions of serialized data.
  686. * @param getter Method used for retrieving the value of this field.
  687. * @param setter Method used for setting the value of this field.
  688. * @param flags Various flags you can use to specialize how systems handle this field. See RTTIFieldFlag.
  689. */
  690. template<class ObjectType>
  691. void addDataBlockField(const String& name, UINT32 uniqueId, ManagedDataBlock (ObjectType::*getter)(),
  692. void (ObjectType::*setter)(ManagedDataBlock) = nullptr, UINT64 flags = 0, UINT8* (customAllocator)(ObjectType*, UINT32) = 0)
  693. {
  694. addDataBlockField<ObjectType>(name, uniqueId,
  695. std::function<ManagedDataBlock(ObjectType*)>(getter),
  696. std::function<void(ObjectType*, ManagedDataBlock)>(setter), flags, customAllocator);
  697. }
  698. protected:
  699. virtual void initSerializableFields() {}
  700. /************************************************************************/
  701. /* FIELDS OPERATING ON DERIVED SERIALIZATION INTERFACE */
  702. /* (Needs an extra pointer to the actual object) */
  703. /************************************************************************/
  704. template<class InterfaceType, class ObjectType, class DataType>
  705. void addPlainField(const String& name, UINT32 uniqueId,
  706. DataType& (InterfaceType::*getter)(ObjectType*),
  707. void (InterfaceType::*setter)(ObjectType*, DataType&), UINT64 flags = 0)
  708. {
  709. using namespace std::placeholders;
  710. static_assert((std::is_base_of<BansheeEngine::RTTIType<Type, BaseType, MyRTTIType>, InterfaceType>::value),
  711. "Class with the get/set methods must derive from BansheeEngine::RTTIType.");
  712. static_assert(!(std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  713. "Data type derives from IReflectable but it is being added as a plain field.");
  714. addPlainField<ObjectType, DataType>(name, uniqueId,
  715. std::function<DataType&(ObjectType*)>(std::bind(getter, static_cast<InterfaceType*>(this), _1)),
  716. std::function<void(ObjectType*, DataType&)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags);
  717. }
  718. template<class InterfaceType, class ObjectType, class DataType>
  719. void addReflectableField(const String& name, UINT32 uniqueId,
  720. DataType& (InterfaceType::*getter)(ObjectType*),
  721. void (InterfaceType::*setter)(ObjectType*, DataType&), UINT64 flags = 0)
  722. {
  723. using namespace std::placeholders;
  724. addReflectableField<ObjectType, DataType>(name, uniqueId,
  725. std::function<DataType&(ObjectType*)>(std::bind(getter, static_cast<InterfaceType*>(this), _1)),
  726. std::function<void(ObjectType*, DataType&)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags);
  727. }
  728. template<class InterfaceType, class ObjectType, class DataType>
  729. void addReflectablePtrField(const String& name, UINT32 uniqueId,
  730. std::shared_ptr<DataType> (InterfaceType::*getter)(ObjectType*),
  731. void (InterfaceType::*setter)(ObjectType*, std::shared_ptr<DataType>), UINT64 flags = 0)
  732. {
  733. using namespace std::placeholders;
  734. addReflectablePtrField<ObjectType, DataType>(name, uniqueId,
  735. std::function<std::shared_ptr<DataType>(ObjectType*)>(std::bind(getter, static_cast<InterfaceType*>(this), _1)),
  736. std::function<void(ObjectType*, std::shared_ptr<DataType>)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags);
  737. }
  738. template<class InterfaceType, class ObjectType, class DataType>
  739. void addPlainArrayField(const String& name, UINT32 uniqueId,
  740. DataType& (InterfaceType::*getter)(ObjectType*, UINT32),
  741. UINT32 (InterfaceType::*getSize)(ObjectType*),
  742. void (InterfaceType::*setter)(ObjectType*, UINT32, DataType&),
  743. void(InterfaceType::*setSize)(ObjectType*, UINT32), UINT64 flags = 0)
  744. {
  745. using namespace std::placeholders;
  746. static_assert((std::is_base_of<BansheeEngine::RTTIType<Type, BaseType, MyRTTIType>, InterfaceType>::value),
  747. "Class with the get/set methods must derive from BansheeEngine::RTTIType.");
  748. static_assert(!(std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  749. "Data type derives from IReflectable but it is being added as a plain field.");
  750. addPlainArrayField<ObjectType, DataType>(name, uniqueId,
  751. std::function<DataType&(ObjectType*, UINT32)>(std::bind(getter, static_cast<InterfaceType*>(this), _1, _2)),
  752. std::function<UINT32(ObjectType*)>(std::bind(getSize, static_cast<InterfaceType*>(this), _1)),
  753. std::function<void(ObjectType*, UINT32, DataType&)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2, _3)),
  754. std::function<void(ObjectType*, UINT32)>(std::bind(setSize, static_cast<InterfaceType*>(this), _1, _2)), flags);
  755. }
  756. template<class InterfaceType, class ObjectType, class DataType>
  757. void addReflectableArrayField(const String& name, UINT32 uniqueId,
  758. DataType& (InterfaceType::*getter)(ObjectType*, UINT32),
  759. UINT32 (InterfaceType::*getSize)(ObjectType*),
  760. void (InterfaceType::*setter)(ObjectType*, UINT32, DataType&),
  761. void(InterfaceType::*setSize)(ObjectType*, UINT32), UINT64 flags = 0)
  762. {
  763. using namespace std::placeholders;
  764. addReflectableArrayField<ObjectType, DataType>(name, uniqueId,
  765. std::function<DataType&(ObjectType*, UINT32)>(std::bind(getter, static_cast<InterfaceType*>(this), _1, _2)),
  766. std::function<UINT32(ObjectType*)>(std::bind(getSize, static_cast<InterfaceType*>(this), _1)),
  767. std::function<void(ObjectType*, UINT32, DataType&)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2, _3)),
  768. std::function<void(ObjectType*, UINT32)>(std::bind(setSize, static_cast<InterfaceType*>(this), _1, _2)), flags);
  769. }
  770. template<class InterfaceType, class ObjectType, class DataType>
  771. void addReflectablePtrArrayField(const String& name, UINT32 uniqueId,
  772. std::shared_ptr<DataType> (InterfaceType::*getter)(ObjectType*, UINT32),
  773. UINT32 (InterfaceType::*getSize)(ObjectType*),
  774. void (InterfaceType::*setter)(ObjectType*, UINT32, std::shared_ptr<DataType>),
  775. void(InterfaceType::*setSize)(ObjectType*, UINT32), UINT64 flags = 0)
  776. {
  777. using namespace std::placeholders;
  778. addReflectablePtrArrayField<ObjectType, DataType>(name, uniqueId,
  779. std::function<std::shared_ptr<DataType>(ObjectType*, UINT32)>(std::bind(getter, static_cast<InterfaceType*>(this), _1, _2)),
  780. std::function<UINT32(ObjectType*)>(std::bind(getSize, static_cast<InterfaceType*>(this), _1)),
  781. std::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2, _3)),
  782. std::function<void(ObjectType*, UINT32)>(std::bind(setSize, static_cast<InterfaceType*>(this), _1, _2)), flags);
  783. }
  784. template<class InterfaceType, class ObjectType>
  785. void addDataBlockField(const String& name, UINT32 uniqueId, ManagedDataBlock (InterfaceType::*getter)(ObjectType*),
  786. void (InterfaceType::*setter)(ObjectType*, ManagedDataBlock), UINT64 flags = 0,
  787. UINT8* (customAllocator)(ObjectType*, UINT32) = 0)
  788. {
  789. using namespace std::placeholders;
  790. if(customAllocator != 0)
  791. {
  792. std::function<UINT8*(ObjectType*, UINT32)> customAllocFunc = std::bind(customAllocator, _1, _2);
  793. addDataBlockField<ObjectType>(name, uniqueId,
  794. std::function<ManagedDataBlock(ObjectType*)>(std::bind(getter, static_cast<InterfaceType*>(this), _1)),
  795. std::function<void(ObjectType*, ManagedDataBlock)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags,
  796. customAllocFunc);
  797. }
  798. else
  799. {
  800. addDataBlockField<ObjectType>(name, uniqueId,
  801. std::function<ManagedDataBlock(ObjectType*)>(std::bind(getter, static_cast<InterfaceType*>(this), _1)),
  802. std::function<void(ObjectType*, ManagedDataBlock)>(std::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags);
  803. }
  804. }
  805. private:
  806. template<class ObjectType, class DataType>
  807. void addPlainField(const String& name, UINT32 uniqueId, Any getter, Any setter, UINT64 flags)
  808. {
  809. RTTIPlainField<DataType, ObjectType>* newField =
  810. bs_new<RTTIPlainField<DataType, ObjectType>>();
  811. newField->initSingle(name, uniqueId, getter, setter, flags);
  812. addNewField(newField);
  813. }
  814. template<class ObjectType, class DataType>
  815. void addReflectableField(const String& name, UINT32 uniqueId, Any getter, Any setter, UINT64 flags)
  816. {
  817. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  818. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  819. RTTIReflectableField<DataType, ObjectType>* newField =
  820. bs_new<RTTIReflectableField<DataType, ObjectType>>();
  821. newField->initSingle(name, uniqueId, getter, setter, flags);
  822. addNewField(newField);
  823. }
  824. template<class ObjectType, class DataType>
  825. void addReflectablePtrField(const String& name, UINT32 uniqueId, Any getter, Any setter, UINT64 flags)
  826. {
  827. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  828. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  829. if((flags & RTTI_Flag_WeakRef) == 0)
  830. checkForCircularReferences<DataType>();
  831. RTTIReflectablePtrField<DataType, ObjectType>* newField =
  832. bs_new<RTTIReflectablePtrField<DataType, ObjectType>>();
  833. newField->initSingle(name, uniqueId, getter, setter, flags);
  834. addNewField(newField);
  835. }
  836. template<class ObjectType, class DataType>
  837. void addPlainArrayField(const String& name, UINT32 uniqueId, Any getter, Any getSize,
  838. Any setter, Any setSize, UINT64 flags)
  839. {
  840. RTTIPlainField<DataType, ObjectType>* newField =
  841. bs_new<RTTIPlainField<DataType, ObjectType>>();
  842. newField->initArray(name, uniqueId, getter, getSize, setter, setSize, flags);
  843. addNewField(newField);
  844. }
  845. template<class ObjectType, class DataType>
  846. void addReflectableArrayField(const String& name, UINT32 uniqueId, Any getter, Any getSize,
  847. Any setter, Any setSize, UINT64 flags)
  848. {
  849. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  850. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  851. RTTIReflectableField<DataType, ObjectType>* newField =
  852. bs_new<RTTIReflectableField<DataType, ObjectType>>();
  853. newField->initArray(name, uniqueId, getter, getSize, setter, setSize, flags);
  854. addNewField(newField);
  855. }
  856. template<class ObjectType, class DataType>
  857. void addReflectablePtrArrayField(const String& name, UINT32 uniqueId, Any getter, Any getSize,
  858. Any setter, Any setSize, UINT64 flags)
  859. {
  860. static_assert((std::is_base_of<BansheeEngine::IReflectable, DataType>::value),
  861. "Invalid data type for complex field. It needs to derive from BansheeEngine::IReflectable.");
  862. if((flags & RTTI_Flag_WeakRef) == 0)
  863. checkForCircularReferences<DataType>();
  864. RTTIReflectablePtrField<DataType, ObjectType>* newField =
  865. bs_new<RTTIReflectablePtrField<DataType, ObjectType>>();
  866. newField->initArray(name, uniqueId, getter, getSize, setter, setSize, flags);
  867. addNewField(newField);
  868. }
  869. template<class ObjectType>
  870. void addDataBlockField(const String& name, UINT32 uniqueId, Any getter, Any setter, UINT64 flags,
  871. Any customAllocator = Any())
  872. {
  873. RTTIManagedDataBlockField<ManagedDataBlock, ObjectType>* newField =
  874. bs_new<RTTIManagedDataBlockField<ManagedDataBlock, ObjectType>>();
  875. newField->initSingle(name, uniqueId, getter, setter, flags, customAllocator);
  876. addNewField(newField);
  877. }
  878. };
  879. template <typename Type, typename BaseType, typename MyRTTIType>
  880. InitRTTIOnStart<Type, BaseType> RTTIType<Type, BaseType, MyRTTIType>::initOnStart;
  881. /**
  882. * @brief Returns true if the provided object can be safely cast into type T.
  883. */
  884. template<class T>
  885. bool rtti_is_of_type(IReflectable* object)
  886. {
  887. static_assert((std::is_base_of<BansheeEngine::IReflectable, T>::value),
  888. "Invalid data type for type checking. It needs to derive from BansheeEngine::IReflectable.");
  889. return object->getTypeId() == T::getRTTIStatic()->getRTTIId();
  890. }
  891. /**
  892. * @brief Returns true if the provided object can be safely cast into type T.
  893. */
  894. template<class T>
  895. bool rtti_is_of_type(std::shared_ptr<IReflectable> object)
  896. {
  897. static_assert((std::is_base_of<BansheeEngine::IReflectable, T>::value),
  898. "Invalid data type for type checking. It needs to derive from BansheeEngine::IReflectable.");
  899. return object->getTypeId() == T::getRTTIStatic()->getRTTIId();
  900. }
  901. }