Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedResourcePointer.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_SHAREDRESOURCEPOINTER_H_INCLUDED
  21. #define WATER_SHAREDRESOURCEPOINTER_H_INCLUDED
  22. #include "../water.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. A smart-pointer that automatically creates and manages the lifetime of a
  27. shared static instance of a class.
  28. The SharedObjectType template type indicates the class to use for the shared
  29. object - the only requirements on this class are that it must have a public
  30. default constructor and destructor.
  31. The SharedResourcePointer offers a pattern that differs from using a singleton or
  32. static instance of an object, because it uses reference-counting to make sure that
  33. the underlying shared object is automatically created/destroyed according to the
  34. number of SharedResourcePointer objects that exist. When the last one is deleted,
  35. the underlying object is also immediately destroyed. This allows you to use scoping
  36. to manage the lifetime of a shared resource.
  37. Note: the construction/deletion of the shared object must not involve any
  38. code that makes recursive calls to a SharedResourcePointer, or you'll cause
  39. a deadlock.
  40. Example:
  41. @code
  42. // An example of a class that contains the shared data you want to use.
  43. struct MySharedData
  44. {
  45. // There's no need to ever create an instance of this class directly yourself,
  46. // but it does need a public constructor that does the initialisation.
  47. MySharedData()
  48. {
  49. sharedStuff = generateHeavyweightStuff();
  50. }
  51. Array<SomeKindOfData> sharedStuff;
  52. };
  53. struct DataUserClass
  54. {
  55. DataUserClass()
  56. {
  57. // Multiple instances of the DataUserClass will all have the same
  58. // shared common instance of MySharedData referenced by their sharedData
  59. // member variables.
  60. useSharedStuff (sharedData->sharedStuff);
  61. }
  62. // By keeping this pointer as a member variable, the shared resource
  63. // is guaranteed to be available for as long as the DataUserClass object.
  64. SharedResourcePointer<MySharedData> sharedData;
  65. };
  66. @endcode
  67. */
  68. template <typename SharedObjectType>
  69. class SharedResourcePointer
  70. {
  71. public:
  72. /** Creates an instance of the shared object.
  73. If other SharedResourcePointer objects for this type already exist, then
  74. this one will simply point to the same shared object that they are already
  75. using. Otherwise, if this is the first SharedResourcePointer to be created,
  76. then a shared object will be created automatically.
  77. */
  78. SharedResourcePointer()
  79. {
  80. initialise();
  81. }
  82. SharedResourcePointer (const SharedResourcePointer&)
  83. {
  84. initialise();
  85. }
  86. /** Destructor.
  87. If no other SharedResourcePointer objects exist, this will also delete
  88. the shared object to which it refers.
  89. */
  90. ~SharedResourcePointer()
  91. {
  92. SharedObjectHolder& holder = getSharedObjectHolder();
  93. const SpinLock::ScopedLockType sl (holder.lock);
  94. if (--(holder.refCount) == 0)
  95. holder.sharedInstance = nullptr;
  96. }
  97. /** Returns the shared object. */
  98. operator SharedObjectType*() const noexcept { return sharedObject; }
  99. /** Returns the shared object. */
  100. SharedObjectType& get() const noexcept { return *sharedObject; }
  101. /** Returns the object that this pointer references.
  102. The pointer returned may be a nullptr, of course.
  103. */
  104. SharedObjectType& getObject() const noexcept { return *sharedObject; }
  105. SharedObjectType* operator->() const noexcept { return sharedObject; }
  106. private:
  107. struct SharedObjectHolder : public ReferenceCountedObject
  108. {
  109. SpinLock lock;
  110. ScopedPointer<SharedObjectType> sharedInstance;
  111. int refCount;
  112. };
  113. static SharedObjectHolder& getSharedObjectHolder() noexcept
  114. {
  115. static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { 0 };
  116. return *reinterpret_cast<SharedObjectHolder*> (holder);
  117. }
  118. SharedObjectType* sharedObject;
  119. void initialise()
  120. {
  121. SharedObjectHolder& holder = getSharedObjectHolder();
  122. const SpinLock::ScopedLockType sl (holder.lock);
  123. if (++(holder.refCount) == 1)
  124. holder.sharedInstance = new SharedObjectType();
  125. sharedObject = holder.sharedInstance;
  126. }
  127. // There's no need to assign to a SharedResourcePointer because every
  128. // instance of the class is exactly the same!
  129. SharedResourcePointer& operator= (const SharedResourcePointer&) WATER_DELETED_FUNCTION;
  130. };
  131. }
  132. #endif // WATER_SHAREDRESOURCEPOINTER_H_INCLUDED