BsCoreObjectCore.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsAsyncOp.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Helper class for syncing dirty data from sim CoreObject to
  8. * core CoreObject and other way around.
  9. */
  10. class CoreSyncData
  11. {
  12. public:
  13. CoreSyncData()
  14. :data(nullptr), size(0)
  15. { }
  16. CoreSyncData(UINT8* data, UINT32 size)
  17. :data(data), size(size)
  18. { }
  19. /**
  20. * @brief Gets the internal data and checks the data is of
  21. * valid size.
  22. */
  23. template<class T>
  24. const T& getData()
  25. {
  26. assert(sizeof(T) == size);
  27. return *(T*)data;
  28. }
  29. private:
  30. UINT8* data;
  31. UINT32 size;
  32. };
  33. /**
  34. * @brief Represents part of a CoreObject that is meant to be used specifically
  35. * on the core thread.
  36. *
  37. * @note Core thread only.
  38. *
  39. * Different CoreObject implementations should implement this class for their
  40. * own needs.
  41. */
  42. class BS_CORE_EXPORT CoreObjectCore
  43. {
  44. public:
  45. CoreObjectCore();
  46. protected:
  47. friend class CoreObjectManager;
  48. friend class CoreObject;
  49. /**
  50. * @brief Called on the core thread when the object is first created.
  51. */
  52. virtual void initialize() { }
  53. /**
  54. * @brief Called on the core thread just before the object is destroyed.
  55. */
  56. virtual void destroy() { }
  57. /**
  58. * @brief Copy internal dirty data to a memory buffer that will be used
  59. * for updating sim thread version of that data.
  60. */
  61. virtual CoreSyncData syncFromCore(FrameAlloc* allocator) { return CoreSyncData(); }
  62. /**
  63. * @brief Update internal data from provided memory buffer that
  64. * was populated with data from the sim thread.
  65. */
  66. virtual void syncToCore(const CoreSyncData& data) { }
  67. /**
  68. * @brief Marks the core data as dirty. This causes the syncFromCore()
  69. * method to trigger the next time objects are synced between core and sim threads.
  70. *
  71. * @param flags Optional flags in case you want to signal that only part of the
  72. * internal data is dirty. syncFromCore() will be called regardless
  73. * and it's up to the implementation to read the flags value if needed.
  74. */
  75. void markCoreDirty(UINT32 flags = 0xFFFFFFFF) { mCoreDirtyFlags = flags; }
  76. /**
  77. * @brief Marks the core data as clean. Normally called right after syncFromCore()
  78. * has been called.
  79. */
  80. void markCoreClean() { mCoreDirtyFlags = 0; }
  81. /**
  82. * @brief Checks is the core dirty flag set. This is used by external systems
  83. * to know when internal data has changed and sim thread potentially needs to be notified.
  84. */
  85. bool isCoreDirty() const { return mCoreDirtyFlags != 0; }
  86. UINT32 mCoreDirtyFlags;
  87. };
  88. }