BsCoreObjectCore.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Internal method. Sets a shared this pointer to this object. This MUST be called immediately after construction.
  26. *
  27. * @note Called automatically by the factory creation methods so user should not call this manually.
  28. */
  29. void _setThisPtr(std::shared_ptr<CoreObjectCore> ptrThis);
  30. /**
  31. * @brief Returns a shared_ptr version of "this" pointer.
  32. */
  33. SPtr<CoreObjectCore> getThisPtr() const { return mThis.lock(); }
  34. protected:
  35. friend class CoreObjectManager;
  36. friend class CoreObject;
  37. /**
  38. * @brief Copy internal dirty data to a memory buffer that will be used
  39. * for updating sim thread version of that data.
  40. *
  41. * @note This generally happens at the end of a core thread frame. Data is then passed
  42. * to the sim thread and will be available on the next sim thread frame.
  43. */
  44. virtual CoreSyncData syncFromCore(FrameAlloc* allocator) { return CoreSyncData(); }
  45. /**
  46. * @brief Update internal data from provided memory buffer that
  47. * was populated with data from the sim thread.
  48. *
  49. * @note This generally happens at the start of a core thread frame. Data used was
  50. * recorded on the previous sim thread frame.
  51. */
  52. virtual void syncToCore(const CoreSyncData& data) { }
  53. /**
  54. * @brief Marks the core data as dirty. This causes the syncFromCore()
  55. * method to trigger the next time objects are synced between core and sim threads.
  56. *
  57. * @param flags Optional flags in case you want to signal that only part of the
  58. * internal data is dirty. syncFromCore() will be called regardless
  59. * and it's up to the implementation to read the flags value if needed.
  60. */
  61. void markCoreDirty(UINT32 flags = 0xFFFFFFFF) { mCoreDirtyFlags = flags; }
  62. /**
  63. * @brief Marks the core data as clean. Normally called right after syncFromCore()
  64. * has been called.
  65. */
  66. void markCoreClean() { mCoreDirtyFlags = 0; }
  67. /**
  68. * @brief Checks is the core dirty flag set. This is used by external systems
  69. * to know when internal data has changed and sim thread potentially needs to be notified.
  70. */
  71. bool isCoreDirty() const { return mCoreDirtyFlags != 0; }
  72. UINT32 mCoreDirtyFlags;
  73. bool mIsDestroyed;
  74. std::weak_ptr<CoreObjectCore> mThis;
  75. };
  76. }