CmRTTIManagedDataBlockField.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmRTTIField.h"
  4. #include "CmManagedDataBlock.h"
  5. namespace CamelotEngine
  6. {
  7. struct RTTIManagedDataBlockFieldBase : public RTTIField
  8. {
  9. virtual ManagedDataBlock getValue(void* object) = 0;
  10. virtual void setValue(void* object, ManagedDataBlock value) = 0;
  11. };
  12. template <class DataType, class ObjectType>
  13. struct RTTIManagedDataBlockField : public RTTIManagedDataBlockFieldBase
  14. {
  15. /**
  16. * @brief Initializes a field that returns a block of bytes. Can be used for serializing pretty much anything.
  17. *
  18. * @param name Name of the field.
  19. * @param uniqueId Unique identifier for this field. Although name is also a unique
  20. * identifier we want a small data type that can be used for efficiently
  21. * serializing data to disk and similar. It is primarily used for compatibility
  22. * between different versions of serialized data.
  23. * @param getter The getter method for the field. Cannot be null. Must be a specific signature: SerializableDataBlock(ObjectType*)
  24. * @param setter The setter method for the field. Can be null. Must be a specific signature: void(ObjectType*, SerializableDataBlock)
  25. */
  26. void initSingle(const std::string& name, UINT16 uniqueId, boost::any getter, boost::any setter)
  27. {
  28. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_DataBlock);
  29. }
  30. virtual UINT32 getTypeSize()
  31. {
  32. return 0; // Data block types don't store size the conventional way
  33. }
  34. virtual bool hasDynamicSize()
  35. {
  36. return true;
  37. }
  38. virtual UINT32 getArraySize(void* object)
  39. {
  40. CM_EXCEPT(InternalErrorException,
  41. "Data block types don't support arrays.");
  42. }
  43. virtual void setArraySize(void* object, UINT32 size)
  44. {
  45. CM_EXCEPT(InternalErrorException,
  46. "Data block types don't support arrays.");
  47. }
  48. virtual ManagedDataBlock getValue(void* object)
  49. {
  50. ObjectType* castObj = static_cast<ObjectType*>(object);
  51. boost::function<ManagedDataBlock(ObjectType*)> f = boost::any_cast<boost::function<ManagedDataBlock(ObjectType*)>>(valueGetter);
  52. return f(castObj);
  53. }
  54. virtual void setValue(void* object, ManagedDataBlock value)
  55. {
  56. ObjectType* castObj = static_cast<ObjectType*>(object);
  57. boost::function<void(ObjectType*, ManagedDataBlock)> f = boost::any_cast<boost::function<void(ObjectType*, ManagedDataBlock)>>(valueSetter);
  58. f(castObj, value);
  59. }
  60. };
  61. }