CmRTTIReflectableField.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmRTTIField.h"
  4. #include "CmIReflectable.h"
  5. namespace CamelotFramework
  6. {
  7. /**
  8. * @brief Base class containing common functionality for a reflectable class field.
  9. *
  10. * @note Reflectable fields are fields containing complex types deriving from IReflectable. They
  11. * are serialized recursively and you may add/remove fields from them without breaking
  12. * the serialized data.
  13. */
  14. struct RTTIReflectableFieldBase : public RTTIField
  15. {
  16. /**
  17. * @brief Retrieves the IReflectable value from the provided instance.
  18. *
  19. * @note Field type must not be an array.
  20. */
  21. virtual IReflectable& getValue(void* object) = 0;
  22. /**
  23. * @brief Retrieves the IReflectable value from an array on the provided instance
  24. * and index.
  25. *
  26. * @note Field type must be an array.
  27. */
  28. virtual IReflectable& getArrayValue(void* object, UINT32 index) = 0;
  29. /**
  30. * @brief Sets the IReflectable value in the provided instance.
  31. *
  32. * @note Field type must not be an array.
  33. */
  34. virtual void setValue(void* object, IReflectable& value) = 0;
  35. /**
  36. * @brief Sets the IReflectable value in an array on the provided instance
  37. * and index.
  38. *
  39. * @note Field type must be an array.
  40. */
  41. virtual void setArrayValue(void* object, UINT32 index, IReflectable& value) = 0;
  42. /**
  43. * @brief Creates a new object of the field type.
  44. */
  45. virtual std::shared_ptr<IReflectable> newObject() = 0;
  46. /**
  47. * @copydoc RTTIField::hasDynamicSize
  48. */
  49. virtual bool hasDynamicSize() { return true; }
  50. };
  51. /**
  52. * @brief Class containing a reflectable field containing a specific type.
  53. */
  54. template <class DataType, class ObjectType>
  55. struct RTTIReflectableField : public RTTIReflectableFieldBase
  56. {
  57. /**
  58. * @brief Initializes a field containing a single data type implementing IReflectable interface.
  59. *
  60. * @param name Name of the field.
  61. * @param uniqueId Unique identifier for this field. Although name is also a unique
  62. * identifier we want a small data type that can be used for efficiently
  63. * serializing data to disk and similar. It is primarily used for compatibility
  64. * between different versions of serialized data.
  65. * @param getter The getter method for the field. Must be a specific signature: DataType&(ObjectType*)
  66. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, DataType)
  67. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  68. */
  69. void initSingle(const String& name, UINT16 uniqueId, boost::any getter, boost::any setter, UINT64 flags)
  70. {
  71. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_Reflectable, flags);
  72. }
  73. /**
  74. * @brief Initializes a field containing an array of data types implementing IReflectable interface.
  75. *
  76. * @param name Name of the field.
  77. * @param uniqueId Unique identifier for this field. Although name is also a unique
  78. * identifier we want a small data type that can be used for efficiently
  79. * serializing data to disk and similar. It is primarily used for compatibility
  80. * between different versions of serialized data.
  81. * @param getter The getter method for the field. Must be a specific signature: DataType&(ObjectType*, UINT32)
  82. * @param getSize Getter method that returns the size of an array. Must be a specific signature: UINT32(ObjectType*)
  83. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, UINT32, DataType)
  84. * @param setSize Setter method that allows you to resize an array. Must be a specific signature: void(ObjectType*, UINT32)
  85. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  86. */
  87. void initArray(const String& name, UINT16 uniqueId, boost::any getter,
  88. boost::any getSize, boost::any setter, boost::any setSize, UINT64 flags)
  89. {
  90. initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_Reflectable, flags);
  91. }
  92. /**
  93. * @copydoc RTTIField::getTypeSize
  94. */
  95. virtual UINT32 getTypeSize()
  96. {
  97. return 0; // Complex types don't store size the conventional way
  98. }
  99. /**
  100. * @copydoc RTTIReflectableFieldBase::getValue
  101. */
  102. virtual IReflectable& getValue(void* object)
  103. {
  104. checkIsArray(false);
  105. ObjectType* castObjType = static_cast<ObjectType*>(object);
  106. std::function<DataType&(ObjectType*)> f = boost::any_cast<std::function<DataType&(ObjectType*)>>(valueGetter);
  107. IReflectable& castDataType = f(castObjType);
  108. return castDataType;
  109. }
  110. /**
  111. * @copydoc RTTIReflectableFieldBase::getArrayValue
  112. */
  113. virtual IReflectable& getArrayValue(void* object, UINT32 index)
  114. {
  115. checkIsArray(true);
  116. ObjectType* castObjType = static_cast<ObjectType*>(object);
  117. std::function<DataType&(ObjectType*, UINT32)> f = boost::any_cast<std::function<DataType&(ObjectType*, UINT32)>>(valueGetter);
  118. IReflectable& castDataType = f(castObjType, index);
  119. return castDataType;
  120. }
  121. /**
  122. * @copydoc RTTIReflectableFieldBase::setValue
  123. */
  124. virtual void setValue(void* object, IReflectable& value)
  125. {
  126. checkIsArray(false);
  127. if(valueSetter.empty())
  128. {
  129. CM_EXCEPT(InternalErrorException,
  130. "Specified field (" + mName + ") has no setter.");
  131. }
  132. ObjectType* castObjType = static_cast<ObjectType*>(object);
  133. DataType& castDataObj = static_cast<DataType&>(value);
  134. std::function<void(ObjectType*, DataType&)> f = boost::any_cast<std::function<void(ObjectType*, DataType&)>>(valueSetter);
  135. f(castObjType, castDataObj);
  136. }
  137. /**
  138. * @copydoc RTTIReflectableFieldBase::setArrayValue
  139. */
  140. virtual void setArrayValue(void* object, UINT32 index, IReflectable& value)
  141. {
  142. checkIsArray(true);
  143. if(valueSetter.empty())
  144. {
  145. CM_EXCEPT(InternalErrorException,
  146. "Specified field (" + mName + ") has no setter.");
  147. }
  148. ObjectType* castObjType = static_cast<ObjectType*>(object);
  149. DataType& castDataObj = static_cast<DataType&>(value);
  150. std::function<void(ObjectType*, UINT32, DataType&)> f = boost::any_cast<std::function<void(ObjectType*, UINT32, DataType&)>>(valueSetter);
  151. f(castObjType, index, castDataObj);
  152. }
  153. /**
  154. * @copydoc RTTIField::getArraySize
  155. */
  156. virtual UINT32 getArraySize(void* object)
  157. {
  158. checkIsArray(true);
  159. std::function<UINT32(ObjectType*)> f = boost::any_cast<std::function<UINT32(ObjectType*)>>(arraySizeGetter);
  160. ObjectType* castObject = static_cast<ObjectType*>(object);
  161. return f(castObject);
  162. }
  163. /**
  164. * @copydoc RTTIField::setArraySize
  165. */
  166. virtual void setArraySize(void* object, UINT32 size)
  167. {
  168. checkIsArray(true);
  169. if(arraySizeSetter.empty())
  170. {
  171. CM_EXCEPT(InternalErrorException,
  172. "Specified field (" + mName + ") has no array size setter.");
  173. }
  174. std::function<void(ObjectType*, UINT32)> f = boost::any_cast<std::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
  175. ObjectType* castObject = static_cast<ObjectType*>(object);
  176. f(castObject, size);
  177. }
  178. /**
  179. * @copydoc RTTIReflectableFieldBase::newObject
  180. */
  181. virtual std::shared_ptr<IReflectable> newObject()
  182. {
  183. return DataType::getRTTIStatic()->newRTTIObject();
  184. }
  185. };
  186. }