CmRTTIField.h 6.6 KB

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