CmRTTIPlainField.h 10 KB

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