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.

395 lines
14KB

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