BsRTTIManagedDataBlockField.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsRTTIField.h"
  4. #include "BsManagedDataBlock.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Base class containing common functionality for a managed data block class field.
  9. *
  10. * @note Managed data blocks are just blocks of memory that may, or may not be released
  11. * automatically when they are no longer referenced. They are useful when wanting to
  12. * return some temporary data only for serialization purposes.
  13. */
  14. struct RTTIManagedDataBlockFieldBase : public RTTIField
  15. {
  16. Any mCustomAllocator;
  17. /**
  18. * @brief Retrieves a managed data block from the specified instance.
  19. */
  20. virtual ManagedDataBlock getValue(void* object) = 0;
  21. /**
  22. * @brief Sets a managed data block on the specified instance.
  23. */
  24. virtual void setValue(void* object, ManagedDataBlock value) = 0;
  25. /**
  26. * @brief Allocate memory for the managed data block. Used primarily
  27. * to allocate memory before sending it to "setValue" method.
  28. */
  29. virtual UINT8* allocate(void* object, UINT32 bytes) = 0;
  30. };
  31. /**
  32. * @brief Class containing a managed data block field containing a specific type.
  33. */
  34. template <class DataType, class ObjectType>
  35. struct RTTIManagedDataBlockField : public RTTIManagedDataBlockFieldBase
  36. {
  37. /**
  38. * @brief Initializes a field that returns a block of bytes. Can be used for serializing pretty much anything.
  39. *
  40. * @param name Name of the field.
  41. * @param uniqueId Unique identifier for this field. Although name is also a unique
  42. * identifier we want a small data type that can be used for efficiently
  43. * serializing data to disk and similar. It is primarily used for compatibility
  44. * between different versions of serialized data.
  45. * @param getter The getter method for the field. Must be a specific signature: SerializableDataBlock(ObjectType*)
  46. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, SerializableDataBlock)
  47. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  48. * @param customAllocator (optional) Custom allocator that will be used when de-serializing DataBlock memory.
  49. */
  50. void initSingle(const String& name, UINT16 uniqueId, Any getter, Any setter, UINT64 flags, Any customAllocator = Any())
  51. {
  52. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_DataBlock, flags);
  53. mCustomAllocator = customAllocator;
  54. }
  55. /**
  56. * @copydoc RTTIField::getTypeSize
  57. */
  58. virtual UINT32 getTypeSize() override
  59. {
  60. return 0; // Data block types don't store size the conventional way
  61. }
  62. /**
  63. * @copydoc RTTIField::hasDynamicSize
  64. */
  65. virtual bool hasDynamicSize() override
  66. {
  67. return true;
  68. }
  69. /**
  70. * @copydoc RTTIField::getArraySize
  71. */
  72. virtual UINT32 getArraySize(void* object) override
  73. {
  74. BS_EXCEPT(InternalErrorException,
  75. "Data block types don't support arrays.");
  76. }
  77. /**
  78. * @copydoc RTTIField::setArraySize
  79. */
  80. virtual void setArraySize(void* object, UINT32 size) override
  81. {
  82. BS_EXCEPT(InternalErrorException,
  83. "Data block types don't support arrays.");
  84. }
  85. /**
  86. * @copydoc RTTIManagedDataBlockFieldBase::getValue
  87. */
  88. virtual ManagedDataBlock getValue(void* object) override
  89. {
  90. ObjectType* castObj = static_cast<ObjectType*>(object);
  91. std::function<ManagedDataBlock(ObjectType*)> f = any_cast<std::function<ManagedDataBlock(ObjectType*)>>(valueGetter);
  92. return f(castObj);
  93. }
  94. /**
  95. * @copydoc RTTIManagedDataBlockFieldBase::setValue
  96. */
  97. virtual void setValue(void* object, ManagedDataBlock value) override
  98. {
  99. ObjectType* castObj = static_cast<ObjectType*>(object);
  100. std::function<void(ObjectType*, ManagedDataBlock)> f = any_cast<std::function<void(ObjectType*, ManagedDataBlock)>>(valueSetter);
  101. f(castObj, value);
  102. }
  103. /**
  104. * @copydoc RTTIManagedDataBlockFieldBase::allocate
  105. */
  106. virtual UINT8* allocate(void* object, UINT32 bytes) override
  107. {
  108. if(mCustomAllocator.empty())
  109. return (UINT8*)bs_alloc(bytes);
  110. else
  111. {
  112. ObjectType* castObj = static_cast<ObjectType*>(object);
  113. std::function<UINT8*(ObjectType*, UINT32)> f = any_cast<std::function<UINT8*(ObjectType*, UINT32)>>(mCustomAllocator);
  114. return f(castObj, bytes);
  115. }
  116. }
  117. };
  118. }