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.

CarlaScopeUtils.hpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Carla Scope-related classes and tools (pointer and setter taken from JUCE v4)
  3. * Copyright (C) 2013 Raw Material Software Ltd.
  4. * Copyright (c) 2016 ROLI Ltd.
  5. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  18. */
  19. #ifndef CARLA_SCOPE_UTILS_HPP_INCLUDED
  20. #define CARLA_SCOPE_UTILS_HPP_INCLUDED
  21. #include "CarlaUtils.hpp"
  22. #include <algorithm>
  23. #include <clocale>
  24. // -----------------------------------------------------------------------
  25. // CarlaScopedEnvVar class
  26. class CarlaScopedEnvVar {
  27. public:
  28. CarlaScopedEnvVar(const char* const envVar, const char* const valueOrNull) noexcept
  29. : key(nullptr),
  30. origValue(nullptr)
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(envVar != nullptr && envVar[0] != '\0',);
  33. key = carla_strdup_safe(envVar);
  34. CARLA_SAFE_ASSERT_RETURN(key != nullptr,);
  35. if (const char* const envVarValue = std::getenv(key))
  36. {
  37. origValue = carla_strdup_safe(envVarValue);
  38. CARLA_SAFE_ASSERT_RETURN(origValue != nullptr,);
  39. }
  40. // change env var if requested
  41. if (valueOrNull != nullptr)
  42. carla_setenv(key, valueOrNull);
  43. // if null, unset. but only if there is in an active env var value
  44. else if (origValue != nullptr)
  45. carla_unsetenv(key);
  46. }
  47. ~CarlaScopedEnvVar() noexcept
  48. {
  49. bool hasOrigValue = false;
  50. if (origValue != nullptr)
  51. {
  52. hasOrigValue = true;
  53. carla_setenv(key, origValue);
  54. delete[] origValue;
  55. origValue = nullptr;
  56. }
  57. if (key != nullptr)
  58. {
  59. if (! hasOrigValue)
  60. carla_unsetenv(key);
  61. delete[] key;
  62. key = nullptr;
  63. }
  64. }
  65. private:
  66. const char* key;
  67. const char* origValue;
  68. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopedEnvVar)
  69. CARLA_PREVENT_HEAP_ALLOCATION
  70. };
  71. // -----------------------------------------------------------------------
  72. // CarlaScopedLocale class
  73. class CarlaScopedLocale {
  74. public:
  75. CarlaScopedLocale() noexcept
  76. : locale(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr)))
  77. {
  78. ::setlocale(LC_NUMERIC, "C");
  79. }
  80. ~CarlaScopedLocale() noexcept
  81. {
  82. if (locale != nullptr)
  83. {
  84. ::setlocale(LC_NUMERIC, locale);
  85. delete[] locale;
  86. }
  87. }
  88. private:
  89. const char* const locale;
  90. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopedLocale)
  91. CARLA_PREVENT_HEAP_ALLOCATION
  92. };
  93. //=====================================================================================================================
  94. /**
  95. This class holds a pointer which is automatically deleted when this object goes
  96. out of scope.
  97. Once a pointer has been passed to a CarlaScopedPointer, it will make sure that the pointer
  98. gets deleted when the CarlaScopedPointer is deleted. Using the CarlaScopedPointer on the stack or
  99. as member variables is a good way to use RAII to avoid accidentally leaking dynamically
  100. created objects.
  101. A CarlaScopedPointer can be used in pretty much the same way that you'd use a normal pointer
  102. to an object. If you use the assignment operator to assign a different object to a
  103. CarlaScopedPointer, the old one will be automatically deleted.
  104. A const CarlaScopedPointer is guaranteed not to lose ownership of its object or change the
  105. object to which it points during its lifetime. This means that making a copy of a const
  106. CarlaScopedPointer is impossible, as that would involve the new copy taking ownership from the
  107. old one.
  108. If you need to get a pointer out of a CarlaScopedPointer without it being deleted, you
  109. can use the release() method.
  110. Something to note is the main difference between this class and the std::auto_ptr class,
  111. which is that CarlaScopedPointer provides a cast-to-object operator, whereas std::auto_ptr
  112. requires that you always call get() to retrieve the pointer. The advantages of providing
  113. the cast is that you don't need to call get(), so can use the CarlaScopedPointer in pretty much
  114. exactly the same way as a raw pointer. The disadvantage is that the compiler is free to
  115. use the cast in unexpected and sometimes dangerous ways - in particular, it becomes difficult
  116. to return a CarlaScopedPointer as the result of a function. To avoid this causing errors,
  117. CarlaScopedPointer contains an overloaded constructor that should cause a syntax error in these
  118. circumstances, but it does mean that instead of returning a CarlaScopedPointer from a function,
  119. you'd need to return a raw pointer (or use a std::auto_ptr instead).
  120. */
  121. template<class ObjectType>
  122. class CarlaScopedPointer
  123. {
  124. public:
  125. //=================================================================================================================
  126. /** Creates a CarlaScopedPointer containing a null pointer. */
  127. CarlaScopedPointer() noexcept
  128. : object(nullptr) {}
  129. /** Creates a CarlaScopedPointer that owns the specified object. */
  130. CarlaScopedPointer(ObjectType* const objectToTakePossessionOf) noexcept
  131. : object(objectToTakePossessionOf) {}
  132. /** Creates a CarlaScopedPointer that takes its pointer from another CarlaScopedPointer.
  133. Because a pointer can only belong to one CarlaScopedPointer, this transfers
  134. the pointer from the other object to this one, and the other object is reset to
  135. be a null pointer.
  136. */
  137. CarlaScopedPointer(CarlaScopedPointer& objectToTransferFrom) noexcept
  138. : object(objectToTransferFrom.object)
  139. {
  140. objectToTransferFrom.object = nullptr;
  141. }
  142. /** Destructor.
  143. This will delete the object that this CarlaScopedPointer currently refers to.
  144. */
  145. ~CarlaScopedPointer()
  146. {
  147. delete object;
  148. }
  149. /** Changes this CarlaScopedPointer to point to a new object.
  150. Because a pointer can only belong to one CarlaScopedPointer, this transfers
  151. the pointer from the other object to this one, and the other object is reset to
  152. be a null pointer.
  153. If this CarlaScopedPointer already points to an object, that object
  154. will first be deleted.
  155. */
  156. CarlaScopedPointer& operator=(CarlaScopedPointer& objectToTransferFrom)
  157. {
  158. if (this != objectToTransferFrom.getAddress())
  159. {
  160. // Two CarlaScopedPointers should never be able to refer to the same object - if
  161. // this happens, you must have done something dodgy!
  162. CARLA_SAFE_ASSERT_RETURN(object == nullptr || object != objectToTransferFrom.object, *this);
  163. ObjectType* const oldObject = object;
  164. object = objectToTransferFrom.object;
  165. objectToTransferFrom.object = nullptr;
  166. delete oldObject;
  167. }
  168. return *this;
  169. }
  170. /** Changes this CarlaScopedPointer to point to a new object.
  171. If this CarlaScopedPointer already points to an object, that object
  172. will first be deleted.
  173. The pointer that you pass in may be a nullptr.
  174. */
  175. CarlaScopedPointer& operator=(ObjectType* const newObjectToTakePossessionOf)
  176. {
  177. if (object != newObjectToTakePossessionOf)
  178. {
  179. ObjectType* const oldObject = object;
  180. object = newObjectToTakePossessionOf;
  181. delete oldObject;
  182. }
  183. return *this;
  184. }
  185. //=================================================================================================================
  186. /** Returns the object that this CarlaScopedPointer refers to. */
  187. operator ObjectType*() const noexcept { return object; }
  188. /** Returns the object that this CarlaScopedPointer refers to. */
  189. ObjectType* get() const noexcept { return object; }
  190. /** Returns the object that this CarlaScopedPointer refers to. */
  191. ObjectType& operator*() const noexcept { return *object; }
  192. /** Lets you access methods and properties of the object that this CarlaScopedPointer refers to. */
  193. ObjectType* operator->() const noexcept { return object; }
  194. //=================================================================================================================
  195. /** Removes the current object from this CarlaScopedPointer without deleting it.
  196. This will return the current object, and set the CarlaScopedPointer to a null pointer.
  197. */
  198. ObjectType* release() noexcept { ObjectType* const o = object; object = nullptr; return o; }
  199. //=================================================================================================================
  200. /** Swaps this object with that of another CarlaScopedPointer.
  201. The two objects simply exchange their pointers.
  202. */
  203. void swapWith(CarlaScopedPointer<ObjectType>& other) noexcept
  204. {
  205. // Two CarlaScopedPointers should never be able to refer to the same object - if
  206. // this happens, you must have done something dodgy!
  207. CARLA_SAFE_ASSERT_RETURN(object != other.object || this == other.getAddress() || object == nullptr,);
  208. std::swap(object, other.object);
  209. }
  210. private:
  211. //=================================================================================================================
  212. ObjectType* object;
  213. // (Required as an alternative to the overloaded & operator).
  214. const CarlaScopedPointer* getAddress() const noexcept { return this; }
  215. #ifdef CARLA_PROPER_CPP11_SUPPORT
  216. CarlaScopedPointer(const CarlaScopedPointer&) = delete;
  217. CarlaScopedPointer& operator=(const CarlaScopedPointer&) = delete;
  218. #else
  219. CarlaScopedPointer(const CarlaScopedPointer&);
  220. CarlaScopedPointer& operator=(const CarlaScopedPointer&);
  221. #endif
  222. };
  223. //=====================================================================================================================
  224. /** Compares a CarlaScopedPointer with another pointer.
  225. This can be handy for checking whether this is a null pointer.
  226. */
  227. template<class ObjectType>
  228. bool operator==(const CarlaScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  229. {
  230. return static_cast<ObjectType*>(pointer1) == pointer2;
  231. }
  232. /** Compares a CarlaScopedPointer with another pointer.
  233. This can be handy for checking whether this is a null pointer.
  234. */
  235. template<class ObjectType>
  236. bool operator!=(const CarlaScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
  237. {
  238. return static_cast<ObjectType*>(pointer1) != pointer2;
  239. }
  240. //=====================================================================================================================
  241. /**
  242. Helper class providing an RAII-based mechanism for temporarily setting and
  243. then re-setting a value.
  244. E.g. @code
  245. int x = 1;
  246. {
  247. CarlaScopedValueSetter setter (x, 2);
  248. // x is now 2
  249. }
  250. // x is now 1 again
  251. {
  252. CarlaScopedValueSetter setter (x, 3, 4);
  253. // x is now 3
  254. }
  255. // x is now 4
  256. @endcode
  257. */
  258. template <typename ValueType>
  259. class CarlaScopedValueSetter
  260. {
  261. public:
  262. /** Creates a CarlaScopedValueSetter that will immediately change the specified value to the
  263. given new value, and will then reset it to its original value when this object is deleted.
  264. Must be used only for 'noexcept' compatible types.
  265. */
  266. CarlaScopedValueSetter(ValueType& valueToSet, ValueType newValue) noexcept
  267. : value(valueToSet),
  268. originalValue(valueToSet)
  269. {
  270. valueToSet = newValue;
  271. }
  272. /** Creates a CarlaScopedValueSetter that will immediately change the specified value to the
  273. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  274. */
  275. CarlaScopedValueSetter(ValueType& valueToSet, ValueType newValue, ValueType valueWhenDeleted) noexcept
  276. : value(valueToSet),
  277. originalValue(valueWhenDeleted)
  278. {
  279. valueToSet = newValue;
  280. }
  281. ~CarlaScopedValueSetter() noexcept
  282. {
  283. value = originalValue;
  284. }
  285. private:
  286. //=================================================================================================================
  287. ValueType& value;
  288. const ValueType originalValue;
  289. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopedValueSetter)
  290. CARLA_PREVENT_HEAP_ALLOCATION
  291. };
  292. // -----------------------------------------------------------------------
  293. #endif // CARLA_SCOPE_UTILS_HPP_INCLUDED