CmRTTIManagedDataBlockField.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * @param flags Various flags you can use to specialize how systems handle this field
  26. */
  27. void initSingle(const std::string& name, UINT16 uniqueId, boost::any getter, boost::any setter, UINT64 flags)
  28. {
  29. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_DataBlock, flags);
  30. }
  31. virtual UINT32 getTypeSize()
  32. {
  33. return 0; // Data block types don't store size the conventional way
  34. }
  35. virtual bool hasDynamicSize()
  36. {
  37. return true;
  38. }
  39. virtual UINT32 getArraySize(void* object)
  40. {
  41. CM_EXCEPT(InternalErrorException,
  42. "Data block types don't support arrays.");
  43. }
  44. virtual void setArraySize(void* object, UINT32 size)
  45. {
  46. CM_EXCEPT(InternalErrorException,
  47. "Data block types don't support arrays.");
  48. }
  49. virtual ManagedDataBlock getValue(void* object)
  50. {
  51. ObjectType* castObj = static_cast<ObjectType*>(object);
  52. boost::function<ManagedDataBlock(ObjectType*)> f = boost::any_cast<boost::function<ManagedDataBlock(ObjectType*)>>(valueGetter);
  53. return f(castObj);
  54. }
  55. virtual void setValue(void* object, ManagedDataBlock value)
  56. {
  57. ObjectType* castObj = static_cast<ObjectType*>(object);
  58. boost::function<void(ObjectType*, ManagedDataBlock)> f = boost::any_cast<boost::function<void(ObjectType*, ManagedDataBlock)>>(valueSetter);
  59. f(castObj, value);
  60. }
  61. };
  62. }