BsRTTIField.h 7.4 KB

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