BsCoreObjectCore.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsAsyncOp.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup CoreThread
  9. * @{
  10. */
  11. /**
  12. * Represents counterpart of a CoreObject that is meant to be used specifically on the core thread.
  13. *
  14. * @note Core thread only.
  15. * @note Different CoreObject implementations should implement this class for their own needs.
  16. */
  17. class BS_CORE_EXPORT CoreObjectCore
  18. {
  19. protected:
  20. /** Values that represent current state of the object */
  21. enum Flags
  22. {
  23. CGCO_INITIALIZED = 0x01, /**< Object has been initialized and can be used. */
  24. CGCO_SCHEDULED_FOR_INIT = 0x02 /**< Object has been scheduled for initialization but core thread has not completed it yet. */
  25. };
  26. public:
  27. CoreObjectCore();
  28. virtual ~CoreObjectCore();
  29. /** Called on the core thread when the object is first created. */
  30. virtual void initialize();
  31. /** Returns a shared_ptr version of "this" pointer. */
  32. SPtr<CoreObjectCore> getThisPtr() const { return mThis.lock(); }
  33. public: // ***** INTERNAL ******
  34. /** @name Internal
  35. * @{
  36. */
  37. /**
  38. * Sets a shared this pointer to this object. This MUST be called immediately after construction.
  39. *
  40. * @note Internal method.
  41. * @note Called automatically by the factory creation methods so user should not call this manually.
  42. */
  43. void _setThisPtr(std::shared_ptr<CoreObjectCore> ptrThis);
  44. /** @} */
  45. protected:
  46. friend class CoreObjectManager;
  47. friend class CoreObject;
  48. /**
  49. * Update internal data from provided memory buffer that was populated with data from the sim thread.
  50. *
  51. * @note
  52. * This generally happens at the start of a core thread frame. Data used was recorded on the previous sim thread
  53. * frame.
  54. */
  55. virtual void syncToCore(const CoreSyncData& data) { }
  56. /**
  57. * Blocks the current thread until the resource is fully initialized.
  58. *
  59. * @note
  60. * If you call this without calling initialize first a deadlock will occur. You should not call this from core thread.
  61. */
  62. void synchronize();
  63. /**
  64. * Returns true if the object has been properly initialized. Methods are not allowed to be called on the object
  65. * until it is initialized.
  66. */
  67. bool isInitialized() const { return (mFlags & CGCO_INITIALIZED) != 0; }
  68. bool isScheduledToBeInitialized() const { return (mFlags & CGCO_SCHEDULED_FOR_INIT) != 0; }
  69. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGCO_INITIALIZED : mFlags & ~CGCO_INITIALIZED; }
  70. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGCO_SCHEDULED_FOR_INIT : mFlags & ~CGCO_SCHEDULED_FOR_INIT; }
  71. volatile UINT8 mFlags;
  72. std::weak_ptr<CoreObjectCore> mThis;
  73. BS_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  74. BS_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  75. };
  76. /** @} */
  77. }