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.

163 lines
4.5KB

  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_COPYABLE(CarlaScopedEnvVar)
  75. CARLA_PREVENT_HEAP_ALLOCATION
  76. };
  77. //=====================================================================================================================
  78. /**
  79. Helper class providing an RAII-based mechanism for temporarily setting and
  80. then re-setting a value.
  81. E.g. @code
  82. int x = 1;
  83. {
  84. CarlaScopedValueSetter setter (x, 2);
  85. // x is now 2
  86. }
  87. // x is now 1 again
  88. {
  89. CarlaScopedValueSetter setter (x, 3, 4);
  90. // x is now 3
  91. }
  92. // x is now 4
  93. @endcode
  94. */
  95. template <typename ValueType>
  96. class CarlaScopedValueSetter
  97. {
  98. public:
  99. /** Creates a CarlaScopedValueSetter that will immediately change the specified value to the
  100. given new value, and will then reset it to its original value when this object is deleted.
  101. Must be used only for 'noexcept' compatible types.
  102. */
  103. CarlaScopedValueSetter(ValueType& valueToSet, ValueType newValue) noexcept
  104. : value(valueToSet),
  105. originalValue(valueToSet)
  106. {
  107. valueToSet = newValue;
  108. }
  109. /** Creates a CarlaScopedValueSetter that will immediately change the specified value to the
  110. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  111. */
  112. CarlaScopedValueSetter(ValueType& valueToSet, ValueType newValue, ValueType valueWhenDeleted) noexcept
  113. : value(valueToSet),
  114. originalValue(valueWhenDeleted)
  115. {
  116. valueToSet = newValue;
  117. }
  118. ~CarlaScopedValueSetter() noexcept
  119. {
  120. value = originalValue;
  121. }
  122. private:
  123. //=================================================================================================================
  124. ValueType& value;
  125. const ValueType originalValue;
  126. CARLA_DECLARE_NON_COPYABLE(CarlaScopedValueSetter)
  127. CARLA_PREVENT_HEAP_ALLOCATION
  128. };
  129. // -----------------------------------------------------------------------
  130. #endif // CARLA_SCOPE_UTILS_HPP_INCLUDED