BsRTTIManagedDataBlockField.h 4.6 KB

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