CmRTTIManagedDataBlockField.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmRTTIField.h"
  4. #include "CmManagedDataBlock.h"
  5. namespace CamelotFramework
  6. {
  7. struct RTTIManagedDataBlockFieldBase : public RTTIField
  8. {
  9. boost::any mCustomAllocator;
  10. virtual ManagedDataBlock getValue(void* object) = 0;
  11. virtual void setValue(void* object, ManagedDataBlock value) = 0;
  12. virtual UINT8* allocate(void* object, UINT32 bytes) = 0;
  13. };
  14. template <class DataType, class ObjectType>
  15. struct RTTIManagedDataBlockField : public RTTIManagedDataBlockFieldBase
  16. {
  17. /**
  18. * @brief Initializes a field that returns a block of bytes. Can be used for serializing pretty much anything.
  19. *
  20. * @param name Name of the field.
  21. * @param uniqueId Unique identifier for this field. Although name is also a unique
  22. * identifier we want a small data type that can be used for efficiently
  23. * serializing data to disk and similar. It is primarily used for compatibility
  24. * between different versions of serialized data.
  25. * @param getter The getter method for the field. Cannot be null. Must be a specific signature: SerializableDataBlock(ObjectType*)
  26. * @param setter The setter method for the field. Can be null. Must be a specific signature: void(ObjectType*, SerializableDataBlock)
  27. * @param flags Various flags you can use to specialize how systems handle this field
  28. * @param customAllocator (optional) Custom allocator that will be used when de-serializing DataBlock memory.
  29. */
  30. void initSingle(const String& name, UINT16 uniqueId, boost::any getter, boost::any setter, UINT64 flags, boost::any customAllocator = boost::any())
  31. {
  32. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_DataBlock, flags);
  33. mCustomAllocator = customAllocator;
  34. }
  35. virtual UINT32 getTypeSize()
  36. {
  37. return 0; // Data block types don't store size the conventional way
  38. }
  39. virtual bool hasDynamicSize()
  40. {
  41. return true;
  42. }
  43. virtual UINT32 getArraySize(void* object)
  44. {
  45. CM_EXCEPT(InternalErrorException,
  46. "Data block types don't support arrays.");
  47. }
  48. virtual void setArraySize(void* object, UINT32 size)
  49. {
  50. CM_EXCEPT(InternalErrorException,
  51. "Data block types don't support arrays.");
  52. }
  53. virtual ManagedDataBlock getValue(void* object)
  54. {
  55. ObjectType* castObj = static_cast<ObjectType*>(object);
  56. boost::function<ManagedDataBlock(ObjectType*)> f = boost::any_cast<boost::function<ManagedDataBlock(ObjectType*)>>(valueGetter);
  57. return f(castObj);
  58. }
  59. virtual void setValue(void* object, ManagedDataBlock value)
  60. {
  61. ObjectType* castObj = static_cast<ObjectType*>(object);
  62. boost::function<void(ObjectType*, ManagedDataBlock)> f = boost::any_cast<boost::function<void(ObjectType*, ManagedDataBlock)>>(valueSetter);
  63. f(castObj, value);
  64. }
  65. virtual UINT8* allocate(void* object, UINT32 bytes)
  66. {
  67. if(mCustomAllocator.empty())
  68. return (UINT8*)cm_alloc<ScratchAlloc>(bytes);
  69. else
  70. {
  71. ObjectType* castObj = static_cast<ObjectType*>(object);
  72. boost::function<UINT8*(ObjectType*, UINT32)> f = boost::any_cast<boost::function<UINT8*(ObjectType*, UINT32)>>(mCustomAllocator);
  73. return f(castObj, bytes);
  74. }
  75. }
  76. };
  77. }