BsRTTIManagedDataBlockField.h 4.6 KB

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