BsRTTIPlainField.h 10 KB

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