BsRTTIField.h 7.4 KB

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