BsRTTIField.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include <string>
  5. #include <type_traits>
  6. #include "BsPrerequisitesUtil.h"
  7. #include "BsIReflectable.h"
  8. #include "BsManagedDataBlock.h"
  9. #include "BsException.h"
  10. #include "BsAny.h"
  11. namespace BansheeEngine
  12. {
  13. class RTTITypeBase;
  14. /** @addtogroup Internal-Utility
  15. * @{
  16. */
  17. /** @addtogroup RTTI-Internal
  18. * @{
  19. */
  20. /**
  21. * Types of fields we can serialize:
  22. *
  23. * - Plain - Native data types, POD (Plain old data) structures, or in general types we don't want to (or can't) inherit from IReflectable.
  24. * Type must be copyable by memcpy.
  25. *
  26. * - DataBlock - Array of bytes of a certain size. When returning a data block you may specify if its managed or unmanaged.
  27. * Managed data blocks have their buffers deleted after they go out of scope. This is useful if you need to return some
  28. * temporary data. On the other hand if the data in the block belongs to your class, and isn't temporary, keep the data unmanaged.
  29. *
  30. * - Reflectable - Field that is of IReflectable type. Cannot be a pointer to IReflectable and must be actual value type.
  31. * Type and its fields are serialized recursively. Supports versioning so you may add/remove fields from the type
  32. * without breaking previously serialized data.
  33. *
  34. * - ReflectablePtr - A pointer to IReflectable. Same as "Reflectable" except that data isn't serialized as a value type,
  35. * but as a pointer, which may be referenced by multiple other instances. All references are saved upon
  36. * serialization and restored upon deserialization.
  37. */
  38. enum SerializableFieldType
  39. {
  40. SerializableFT_Plain,
  41. SerializableFT_DataBlock,
  42. SerializableFT_Reflectable,
  43. SerializableFT_ReflectablePtr
  44. };
  45. /** Various flags you can assign to RTTI fields. */
  46. enum RTTIFieldFlag
  47. {
  48. /**
  49. * This flag is only used on field types of ReflectablePtr type, and it is used
  50. * to solve circular references. Circular references cause an issue when deserializing,
  51. * as the algorithm doesn't know which object to deserialize first. By making one of
  52. * the references weak, you tell the algorithm that it doesn't have to guarantee
  53. * the object will be fully deserialized before being assigned to the field.
  54. *
  55. * In short: If you make a reference weak, when "set" method of that field is called,
  56. * it is not guaranteed the value provided is fully initialized, so you should not access any of its
  57. * data until deserialization is fully complete. You only need to use this flag if the RTTI system
  58. * complains that is has found a circular reference.
  59. */
  60. RTTI_Flag_WeakRef = 0x01,
  61. /**
  62. * This flags signals various systems that the flagged field should not be searched when looking for
  63. * object references. This normally means the value of this field will no be retrieved during reference
  64. * searches but it will likely still be retrieved during other operations (for example serialization).
  65. * This is used as an optimization to avoid retrieving values of potentially very expensive fields that
  66. * would not contribute to the reference search anyway. Whether or not a field contributes to the reference
  67. * search depends on the search and should be handled on a case by case basis.
  68. */
  69. RTTI_Flag_SkipInReferenceSearch = 0x02
  70. };
  71. /**
  72. * Structure that keeps meta-data concerning a single class field. You can use this data for setting and getting values
  73. * for that field on a specific class instance.
  74. *
  75. * Class also contains an unique field name, and an unique field ID. Fields may contain single types or an array of types.
  76. * See SerializableFieldType for information about specific field types.
  77. *
  78. * @note
  79. * Most of the methods for retrieving and setting data accept "void *" for both the data and the owning class instance.
  80. * It is up to the caller to ensure that pointer is of proper type.
  81. */
  82. struct BS_UTILITY_EXPORT RTTIField
  83. {
  84. Any valueGetter;
  85. Any valueSetter;
  86. Any arraySizeGetter;
  87. Any arraySizeSetter;
  88. String mName;
  89. UINT16 mUniqueId;
  90. bool mIsVectorType;
  91. SerializableFieldType mType;
  92. UINT64 mFlags;
  93. bool isPlainType() const { return mType == SerializableFT_Plain; }
  94. bool isDataBlockType() const { return mType == SerializableFT_DataBlock; }
  95. bool isReflectableType() const { return mType == SerializableFT_Reflectable; }
  96. bool isReflectablePtrType() const { return mType == SerializableFT_ReflectablePtr; }
  97. bool isArray() const { return mIsVectorType; }
  98. /** Returns flags that were set in the field meta-data. */
  99. UINT64 getFlags() const { return mFlags; }
  100. /**
  101. * Gets the size of an array contained by the field, if the field represents an array. Throws exception if field
  102. * is not an array.
  103. */
  104. virtual UINT32 getArraySize(void* object) = 0;
  105. /**
  106. * Changes the size of an array contained by the field, if the field represents an array. Throws exception if field
  107. * is not an array.
  108. */
  109. virtual void setArraySize(void* object, UINT32 size) = 0;
  110. /** Returns the type id for the type used in this field. */
  111. virtual UINT32 getTypeSize() = 0;
  112. /**
  113. * Query if the field has dynamic size.
  114. *
  115. * @note
  116. * Field should have dynamic size if:
  117. * - The field can have varying size
  118. * - The field size is over 255
  119. * @note
  120. * Types like integers, floats, bools, POD structs dont have dynamic size.
  121. * Types like strings, vectors, maps do.
  122. * @note
  123. * If your type has a static size but that size exceeds 255 bytes you also need to
  124. * use dynamic field size. (You will be warned during compilation if you don't follow this rule)
  125. */
  126. virtual bool hasDynamicSize() = 0;
  127. /**
  128. * Throws an exception if this field doesn't contain a plain value.
  129. *
  130. * @param[in] array If true then the field must support plain array type.
  131. */
  132. void checkIsPlain(bool array);
  133. /**
  134. * Throws an exception if this field doesn't contain a complex value.
  135. *
  136. * @param[in] array If true then the field must support complex array type.
  137. */
  138. void checkIsComplex(bool array);
  139. /**
  140. * Throws an exception if this field doesn't contain a complex pointer value.
  141. *
  142. * @param[in] array If true then the field must support complex pointer array type.
  143. */
  144. void checkIsComplexPtr(bool array);
  145. /**
  146. * Throws an exception depending if the field is or isn't an array.
  147. *
  148. * @param[in] array If true, then exception will be thrown if field is not an array.
  149. * If false, then it will be thrown if field is an array.
  150. */
  151. void checkIsArray(bool array);
  152. /** Throws an exception if this field doesn't contain a data block value. */
  153. void checkIsDataBlock();
  154. protected:
  155. void initAll(Any valueGetter, Any valueSetter, Any arraySizeGetter, Any arraySizeSetter,
  156. String mName, UINT16 mUniqueId, bool mIsVectorType, SerializableFieldType type, UINT64 flags)
  157. {
  158. this->valueGetter = valueGetter;
  159. this->valueSetter = valueSetter;
  160. this->arraySizeGetter = arraySizeGetter;
  161. this->arraySizeSetter = arraySizeSetter;
  162. this->mName = mName;
  163. this->mUniqueId = mUniqueId;
  164. this->mIsVectorType = mIsVectorType;
  165. this->mType = type;
  166. this->mFlags = flags;
  167. }
  168. };
  169. /** @} */
  170. /** @} */
  171. }