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.

189 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2019 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 "ReferenceCountedObject.h"
  23. #include "../threads/SpinLock.h"
  24. #include "CarlaScopeUtils.hpp"
  25. namespace water {
  26. //==============================================================================
  27. /**
  28. A smart-pointer that automatically creates and manages the lifetime of a
  29. shared static instance of a class.
  30. The SharedObjectType template type indicates the class to use for the shared
  31. object - the only requirements on this class are that it must have a public
  32. default constructor and destructor.
  33. The SharedResourcePointer offers a pattern that differs from using a singleton or
  34. static instance of an object, because it uses reference-counting to make sure that
  35. the underlying shared object is automatically created/destroyed according to the
  36. number of SharedResourcePointer objects that exist. When the last one is deleted,
  37. the underlying object is also immediately destroyed. This allows you to use scoping
  38. to manage the lifetime of a shared resource.
  39. Note: the construction/deletion of the shared object must not involve any
  40. code that makes recursive calls to a SharedResourcePointer, or you'll cause
  41. a deadlock.
  42. Example:
  43. @code
  44. // An example of a class that contains the shared data you want to use.
  45. struct MySharedData
  46. {
  47. // There's no need to ever create an instance of this class directly yourself,
  48. // but it does need a public constructor that does the initialisation.
  49. MySharedData()
  50. {
  51. sharedStuff = generateHeavyweightStuff();
  52. }
  53. Array<SomeKindOfData> sharedStuff;
  54. };
  55. struct DataUserClass
  56. {
  57. DataUserClass()
  58. {
  59. // Multiple instances of the DataUserClass will all have the same
  60. // shared common instance of MySharedData referenced by their sharedData
  61. // member variables.
  62. useSharedStuff (sharedData->sharedStuff);
  63. }
  64. // By keeping this pointer as a member variable, the shared resource
  65. // is guaranteed to be available for as long as the DataUserClass object.
  66. SharedResourcePointer<MySharedData> sharedData;
  67. };
  68. @endcode
  69. */
  70. template <typename SharedObjectType>
  71. class SharedResourcePointer
  72. {
  73. public:
  74. /** Creates an instance of the shared object.
  75. If other SharedResourcePointer objects for this type already exist, then
  76. this one will simply point to the same shared object that they are already
  77. using. Otherwise, if this is the first SharedResourcePointer to be created,
  78. then a shared object will be created automatically.
  79. */
  80. SharedResourcePointer()
  81. : sharedObject(nullptr)
  82. {
  83. initialise();
  84. }
  85. SharedResourcePointer(const char* const v1, const char* const v2)
  86. : sharedObject(nullptr)
  87. {
  88. initialise_v2(v1, v2);
  89. }
  90. SharedResourcePointer (const SharedResourcePointer&)
  91. : sharedObject(nullptr)
  92. {
  93. initialise();
  94. }
  95. /** Destructor.
  96. If no other SharedResourcePointer objects exist, this will also delete
  97. the shared object to which it refers.
  98. */
  99. ~SharedResourcePointer()
  100. {
  101. SharedObjectHolder& holder = getSharedObjectHolder();
  102. const SpinLock::ScopedLockType sl (holder.lock);
  103. if (--(holder.refCount) == 0)
  104. holder.sharedInstance = nullptr;
  105. }
  106. /** Returns the shared object. */
  107. operator SharedObjectType*() const noexcept { return sharedObject; }
  108. /** Returns the shared object. */
  109. SharedObjectType& get() const noexcept { return *sharedObject; }
  110. /** Returns the object that this pointer references.
  111. The pointer returned may be a nullptr, of course.
  112. */
  113. SharedObjectType& getObject() const noexcept { return *sharedObject; }
  114. SharedObjectType* getPointer() const noexcept { return sharedObject; }
  115. SharedObjectType* operator->() const noexcept { return sharedObject; }
  116. private:
  117. struct SharedObjectHolder : public ReferenceCountedObject
  118. {
  119. SpinLock lock;
  120. CarlaScopedPointer<SharedObjectType> sharedInstance;
  121. int refCount;
  122. };
  123. static SharedObjectHolder& getSharedObjectHolder() noexcept
  124. {
  125. static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { nullptr };
  126. return *reinterpret_cast<SharedObjectHolder*> (holder);
  127. }
  128. SharedObjectType* sharedObject;
  129. void initialise()
  130. {
  131. SharedObjectHolder& holder = getSharedObjectHolder();
  132. const SpinLock::ScopedLockType sl (holder.lock);
  133. if (++(holder.refCount) == 1)
  134. holder.sharedInstance = new SharedObjectType();
  135. sharedObject = holder.sharedInstance;
  136. }
  137. void initialise_v2(const char* const v1, const char* const v2)
  138. {
  139. SharedObjectHolder& holder = getSharedObjectHolder();
  140. const SpinLock::ScopedLockType sl (holder.lock);
  141. if (++(holder.refCount) == 1)
  142. holder.sharedInstance = new SharedObjectType(v1, v2);
  143. sharedObject = holder.sharedInstance;
  144. }
  145. // There's no need to assign to a SharedResourcePointer because every
  146. // instance of the class is exactly the same!
  147. SharedResourcePointer& operator= (const SharedResourcePointer&) WATER_DELETED_FUNCTION;
  148. };
  149. }
  150. #endif // WATER_SHAREDRESOURCEPOINTER_H_INCLUDED