BsRTTIPlainField.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Reflection/BsRTTIField.h"
  6. #include "Error/BsException.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Internal-Utility
  10. * @{
  11. */
  12. /** @addtogroup RTTI-Internal
  13. * @{
  14. */
  15. /**
  16. * Base class containing common functionality for a plain class field.
  17. *
  18. * @note
  19. * Plain fields are considered those that may be serialized directly by copying their memory. (All built-in types,
  20. * strings, etc.)
  21. */
  22. struct RTTIPlainFieldBase : public RTTIField
  23. {
  24. virtual ~RTTIPlainFieldBase() { }
  25. /** Throws an exception if the current field type and provided template types don't match. */
  26. template<class DataType>
  27. void checkType()
  28. {
  29. // TODO: Low priority. Because I wanted to get rid of SerializableType I have no way of checking the actual
  30. // type of the field and the type provided to get/set methods matches
  31. /*if(mType.id != SerializableType<DataType>().id)
  32. {
  33. BS_EXCEPT(InternalErrorException,
  34. "Invalid field type.",
  35. "SerializableSimpleTypeFieldBase::checkType()");
  36. }*/
  37. }
  38. /** Returns the unique identifier for the type owned by the field. */
  39. virtual UINT32 getTypeId()
  40. {
  41. return 0;
  42. }
  43. /** @copydoc RTTIField::hasDynamicSize */
  44. bool hasDynamicSize() override
  45. {
  46. return false;
  47. }
  48. /** Gets the dynamic size of the object. If object has no dynamic size, static size of the object is returned. */
  49. virtual UINT32 getDynamicSize(void* object)
  50. {
  51. return 0;
  52. }
  53. /**
  54. * Gets the dynamic size of an array element. If the element has no dynamic size, static size of the element
  55. * is returned.
  56. */
  57. virtual UINT32 getArrayElemDynamicSize(void* object, int index)
  58. {
  59. return 0;
  60. }
  61. /**
  62. * Retrieves the value from the provided field of the provided object, and copies it into the buffer. It does not
  63. * check if buffer is large enough.
  64. */
  65. virtual void toBuffer(void* object, void* buffer) = 0;
  66. /**
  67. * Retrieves the value at the specified array index on the provided field of the provided object, and copies it into
  68. * the buffer. It does not check if buffer is large enough.
  69. */
  70. virtual void arrayElemToBuffer(void* object, int index, void* buffer) = 0;
  71. /**
  72. * Sets the value on the provided field of the provided object. Value is copied from the buffer. It does not check
  73. * the value in the buffer in any way. You must make sure buffer points to the proper location and contains the
  74. * proper type.
  75. */
  76. virtual void fromBuffer(void* object, void* buffer) = 0;
  77. /**
  78. * Sets the value at the specified array index on the provided field of the provided object. Value is copied from
  79. * the buffer. It does not check the value in the buffer in any way. You must make sure buffer points to the proper
  80. * location and contains the proper type.
  81. */
  82. virtual void arrayElemFromBuffer(void* object, int index, void* buffer) = 0;
  83. };
  84. /** Represents a plain class field containing a specific type. */
  85. template <class DataType, class ObjectType>
  86. struct RTTIPlainField : public RTTIPlainFieldBase
  87. {
  88. /**
  89. * Initializes a plain field containing a single value.
  90. *
  91. * @param[in] name Name of the field.
  92. * @param[in] uniqueId Unique identifier for this field. Although name is also a unique identifier we want a
  93. * small data type that can be used for efficiently serializing data to disk and similar.
  94. * It is primarily used for compatibility between different versions of serialized data.
  95. * @param[in] getter The getter method for the field. Must be a specific signature: DataType(ObjectType*).
  96. * @param[in] setter The setter method for the field. Must be a specific signature: void(ObjectType*, DataType)
  97. * @param[in] flags Various flags you can use to specialize how outside systems handle this field. See "RTTIFieldFlag".
  98. */
  99. void initSingle(const String& name, UINT16 uniqueId, Any getter, Any setter, UINT64 flags)
  100. {
  101. static_assert(sizeof(RTTIPlainType<DataType>::id) > 0, "Type has no RTTI ID."); // Just making sure provided type has a type ID
  102. static_assert((RTTIPlainType<DataType>::hasDynamicSize != 0 || (sizeof(DataType) <= 255)),
  103. "Trying to create a plain RTTI field with size larger than 255. In order to use larger sizes for plain types please specialize " \
  104. " RTTIPlainType, set hasDynamicSize to true.");
  105. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_Plain, flags);
  106. }
  107. /**
  108. * Initializes a plain field containing multiple values in an array.
  109. *
  110. * @param[in] name Name of the field.
  111. * @param[in] uniqueId Unique identifier for this field. Although name is also a unique identifier we want a
  112. * small data type that can be used for efficiently serializing data to disk and similar.
  113. * It is primarily used for compatibility between different versions of serialized data.
  114. * @param[in] getter The getter method for the field. Must be a specific signature: DataType(ObjectType*, UINT32)
  115. * @param[in] getSize Getter method that returns the size of an array. Must be a specific signature: UINT32(ObjectType*)
  116. * @param[in] setter The setter method for the field. Must be a specific signature: void(ObjectType*, UINT32, DataType)
  117. * @param[in] setSize Setter method that allows you to resize an array. Can be null. Must be a specific signature: void(ObjectType*, UINT32)
  118. * @param[in] flags Various flags you can use to specialize how outside systems handle this field. See "RTTIFieldFlag".
  119. */
  120. void initArray(const String& name, UINT16 uniqueId, Any getter,
  121. Any getSize, Any setter, Any setSize, UINT64 flags)
  122. {
  123. static_assert((RTTIPlainType<DataType>::id != 0) || true, ""); // Just making sure provided type has a type ID
  124. static_assert((RTTIPlainType<DataType>::hasDynamicSize != 0 || (sizeof(DataType) <= 255)),
  125. "Trying to create a plain RTTI field with size larger than 255. In order to use larger sizes for plain types please specialize " \
  126. " RTTIPlainType, set hasDynamicSize to true.");
  127. initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_Plain, flags);
  128. }
  129. /** @copydoc RTTIField::getTypeSize */
  130. UINT32 getTypeSize() override
  131. {
  132. return sizeof(DataType);
  133. }
  134. /** @copydoc RTTIPlainFieldBase::getTypeId */
  135. UINT32 getTypeId() override
  136. {
  137. return RTTIPlainType<DataType>::id;
  138. }
  139. /** @copydoc RTTIPlainFieldBase::hasDynamicSize */
  140. bool hasDynamicSize() override
  141. {
  142. return RTTIPlainType<DataType>::hasDynamicSize != 0;
  143. }
  144. /** @copydoc RTTIPlainFieldBase::getDynamicSize */
  145. UINT32 getDynamicSize(void* object) override
  146. {
  147. checkIsArray(false);
  148. checkType<DataType>();
  149. ObjectType* castObject = static_cast<ObjectType*>(object);
  150. std::function<DataType&(ObjectType*)> f = any_cast<std::function<DataType&(ObjectType*)>>(valueGetter);
  151. DataType value = f(castObject);
  152. return RTTIPlainType<DataType>::getDynamicSize(value);
  153. }
  154. /** @copydoc RTTIPlainFieldBase::getArrayElemDynamicSize */
  155. UINT32 getArrayElemDynamicSize(void* object, int index) override
  156. {
  157. checkIsArray(true);
  158. checkType<DataType>();
  159. ObjectType* castObject = static_cast<ObjectType*>(object);
  160. std::function<DataType&(ObjectType*, UINT32)> f = any_cast<std::function<DataType&(ObjectType*, UINT32)>>(valueGetter);
  161. DataType value = f(castObject, index);
  162. return RTTIPlainType<DataType>::getDynamicSize(value);
  163. }
  164. /** Returns the size of the array managed by the field. */
  165. UINT32 getArraySize(void* object) override
  166. {
  167. checkIsArray(true);
  168. std::function<UINT32(ObjectType*)> f = any_cast<std::function<UINT32(ObjectType*)>>(arraySizeGetter);
  169. ObjectType* castObject = static_cast<ObjectType*>(object);
  170. return f(castObject);
  171. }
  172. /** Changes the size of the array managed by the field. Array must be re-populated after. */
  173. void setArraySize(void* object, UINT32 size) override
  174. {
  175. checkIsArray(true);
  176. if(arraySizeSetter.empty())
  177. {
  178. BS_EXCEPT(InternalErrorException, "Specified field (" + mName + ") has no array size setter.");
  179. }
  180. std::function<void(ObjectType*, UINT32)> f = any_cast<std::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
  181. ObjectType* castObject = static_cast<ObjectType*>(object);
  182. f(castObject, size);
  183. }
  184. /** @copydoc RTTIPlainFieldBase::toBuffer */
  185. void toBuffer(void* object, void* buffer) override
  186. {
  187. checkIsArray(false);
  188. checkType<DataType>();
  189. ObjectType* castObject = static_cast<ObjectType*>(object);
  190. std::function<DataType&(ObjectType*)> f = any_cast<std::function<DataType&(ObjectType*)>>(valueGetter);
  191. DataType value = f(castObject);
  192. RTTIPlainType<DataType>::toMemory(value, (char*)buffer);
  193. }
  194. /** @copydoc RTTIPlainFieldBase::arrayElemToBuffer */
  195. void arrayElemToBuffer(void* object, int index, void* buffer) override
  196. {
  197. checkIsArray(true);
  198. checkType<DataType>();
  199. ObjectType* castObject = static_cast<ObjectType*>(object);
  200. std::function<DataType&(ObjectType*, UINT32)> f = any_cast<std::function<DataType&(ObjectType*, UINT32)>>(valueGetter);
  201. DataType value = f(castObject, index);
  202. RTTIPlainType<DataType>::toMemory(value, (char*)buffer);
  203. }
  204. /** @copydoc RTTIPlainFieldBase::fromBuffer */
  205. void fromBuffer(void* object, void* buffer) override
  206. {
  207. checkIsArray(false);
  208. checkType<DataType>();
  209. ObjectType* castObject = static_cast<ObjectType*>(object);
  210. DataType value;
  211. RTTIPlainType<DataType>::fromMemory(value, (char*)buffer);
  212. if(valueSetter.empty())
  213. {
  214. BS_EXCEPT(InternalErrorException,
  215. "Specified field (" + mName + ") has no setter.");
  216. }
  217. std::function<void(ObjectType*, DataType&)> f = any_cast<std::function<void(ObjectType*, DataType&)>>(valueSetter);
  218. f(castObject, value);
  219. }
  220. /** @copydoc RTTIPlainFieldBase::arrayElemFromBuffer */
  221. void arrayElemFromBuffer(void* object, int index, void* buffer) override
  222. {
  223. checkIsArray(true);
  224. checkType<DataType>();
  225. ObjectType* castObject = static_cast<ObjectType*>(object);
  226. DataType value;
  227. RTTIPlainType<DataType>::fromMemory(value, (char*)buffer);
  228. if(valueSetter.empty())
  229. {
  230. BS_EXCEPT(InternalErrorException,
  231. "Specified field (" + mName + ") has no setter.");
  232. }
  233. std::function<void(ObjectType*, UINT32, DataType&)> f = any_cast<std::function<void(ObjectType*, UINT32, DataType&)>>(valueSetter);
  234. f(castObject, index, value);
  235. }
  236. };
  237. /** @} */
  238. /** @} */
  239. }