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.

169 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED
  24. #define JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED
  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* operator->() const noexcept { return sharedObject; }
  107. private:
  108. struct SharedObjectHolder : public ReferenceCountedObject
  109. {
  110. SpinLock lock;
  111. ScopedPointer<SharedObjectType> sharedInstance;
  112. int refCount;
  113. };
  114. static SharedObjectHolder& getSharedObjectHolder() noexcept
  115. {
  116. static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { 0 };
  117. return *reinterpret_cast<SharedObjectHolder*> (holder);
  118. }
  119. SharedObjectType* sharedObject;
  120. void initialise()
  121. {
  122. SharedObjectHolder& holder = getSharedObjectHolder();
  123. const SpinLock::ScopedLockType sl (holder.lock);
  124. if (++(holder.refCount) == 1)
  125. holder.sharedInstance = new SharedObjectType();
  126. sharedObject = holder.sharedInstance;
  127. }
  128. // There's no need to assign to a SharedResourcePointer because every
  129. // instance of the class is exactly the same!
  130. SharedResourcePointer& operator= (const SharedResourcePointer&) JUCE_DELETED_FUNCTION;
  131. JUCE_LEAK_DETECTOR (SharedResourcePointer)
  132. };
  133. #endif // JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED