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.

168 lines
6.1KB

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