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.

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