2
0

BsCoreObjectCore.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsAsyncOp.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Represents part of a CoreObject that is meant to be used specifically
  8. * on the core thread.
  9. *
  10. * @note Core thread only.
  11. *
  12. * Different CoreObject implementations should implement this class for their
  13. * own needs.
  14. */
  15. class BS_CORE_EXPORT CoreObjectCore
  16. {
  17. public:
  18. CoreObjectCore();
  19. virtual ~CoreObjectCore();
  20. /**
  21. * @brief Called on the core thread when the object is first created.
  22. */
  23. virtual void initialize() { }
  24. /**
  25. * @brief Called on the core thread just before the object is destroyed.
  26. */
  27. virtual void destroy();
  28. /**
  29. * @brief Internal method. Sets a shared this pointer to this object. This MUST be called immediately after construction.
  30. *
  31. * @note Called automatically by the factory creation methods so user should not call this manually.
  32. */
  33. void _setThisPtr(std::shared_ptr<CoreObjectCore> ptrThis);
  34. /**
  35. * @brief Returns a shared_ptr version of "this" pointer.
  36. */
  37. SPtr<CoreObjectCore> getThisPtr() const { return mThis.lock(); }
  38. protected:
  39. friend class CoreObjectManager;
  40. friend class CoreObject;
  41. /**
  42. * @brief Copy internal dirty data to a memory buffer that will be used
  43. * for updating sim thread version of that data.
  44. *
  45. * @note This generally happens at the end of a core thread frame. Data is then passed
  46. * to the sim thread and will be available on the next sim thread frame.
  47. */
  48. virtual CoreSyncData syncFromCore(FrameAlloc* allocator) { return CoreSyncData(); }
  49. /**
  50. * @brief Update internal data from provided memory buffer that
  51. * was populated with data from the sim thread.
  52. *
  53. * @note This generally happens at the start of a core thread frame. Data used was
  54. * recorded on the previous sim thread frame.
  55. */
  56. virtual void syncToCore(const CoreSyncData& data) { }
  57. /**
  58. * @brief Marks the core data as dirty. This causes the syncFromCore()
  59. * method to trigger the next time objects are synced between core and sim threads.
  60. *
  61. * @param flags Optional flags in case you want to signal that only part of the
  62. * internal data is dirty. syncFromCore() will be called regardless
  63. * and it's up to the implementation to read the flags value if needed.
  64. */
  65. void markCoreDirty(UINT32 flags = 0xFFFFFFFF) { mCoreDirtyFlags = flags; }
  66. /**
  67. * @brief Marks the core data as clean. Normally called right after syncFromCore()
  68. * has been called.
  69. */
  70. void markCoreClean() { mCoreDirtyFlags = 0; }
  71. /**
  72. * @brief Checks is the core dirty flag set. This is used by external systems
  73. * to know when internal data has changed and sim thread potentially needs to be notified.
  74. */
  75. bool isCoreDirty() const { return mCoreDirtyFlags != 0; }
  76. UINT32 mCoreDirtyFlags;
  77. bool mIsDestroyed;
  78. std::weak_ptr<CoreObjectCore> mThis;
  79. };
  80. }