BsRTTIField.h 7.3 KB

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