The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

132 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #ifndef DOXYGEN
  20. namespace detail
  21. {
  22. template <typename...>
  23. using Void = void;
  24. template <typename, typename = void>
  25. constexpr auto equalityComparableToNullptr = false;
  26. template <typename T>
  27. constexpr auto equalityComparableToNullptr<T, Void<decltype (std::declval<T>() != nullptr)>> = true;
  28. } // namespace detail
  29. #endif
  30. //==============================================================================
  31. /** Some helper methods for checking a callable object before invoking with
  32. the specified arguments.
  33. If the object provides a comparison operator for nullptr it will check before
  34. calling. For other objects it will just invoke the function call operator.
  35. @tags{Core}
  36. */
  37. struct NullCheckedInvocation
  38. {
  39. template <typename Callable, typename... Args>
  40. static void invoke (Callable&& fn, Args&&... args)
  41. {
  42. if constexpr (detail::equalityComparableToNullptr<Callable>)
  43. {
  44. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Waddress")
  45. if (fn != nullptr)
  46. fn (std::forward<Args> (args)...);
  47. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  48. }
  49. else
  50. {
  51. fn (std::forward<Args> (args)...);
  52. }
  53. }
  54. template <typename... Args>
  55. static void invoke (std::nullptr_t, Args&&...) {}
  56. };
  57. /** Can be used to disable template constructors that would otherwise cause ambiguity with
  58. compiler-generated copy and move constructors.
  59. Adapted from https://ericniebler.com/2013/08/07/universal-references-and-the-copy-constructo/
  60. */
  61. template <typename A, typename B>
  62. using DisableIfSameOrDerived = std::enable_if_t<! std::is_base_of_v<A, std::remove_reference_t<B>>>;
  63. /** Copies an object, sets one of the copy's members to the specified value, and then returns the copy. */
  64. template <typename Object, typename OtherObject, typename Member, typename Other>
  65. Object withMember (Object copy, Member OtherObject::* member, Other&& value)
  66. {
  67. copy.*member = std::forward<Other> (value);
  68. return copy;
  69. }
  70. /** An easy way to ensure that a function is called at the end of the current
  71. scope.
  72. Usage:
  73. @code
  74. {
  75. if (flag == true)
  76. return;
  77. // While this code executes, flag is true e.g. to prevent reentrancy
  78. flag = true;
  79. // When we exit this scope, flag must be false
  80. const ScopeGuard scope { [&] { flag = false; } };
  81. if (checkInitialCondition())
  82. return; // Scope's lambda will fire here...
  83. if (checkCriticalCondition())
  84. throw std::runtime_error{}; // ...or here...
  85. doWorkHavingEstablishedPreconditions();
  86. } // ...or here!
  87. @endcode
  88. */
  89. template <typename Fn> struct ScopeGuard : Fn { ~ScopeGuard() { Fn::operator()(); } };
  90. template <typename Fn> ScopeGuard (Fn) -> ScopeGuard<Fn>;
  91. #ifndef DOXYGEN
  92. namespace detail
  93. {
  94. template <typename Functor, typename Return, typename... Args>
  95. static constexpr auto toFnPtr (Functor functor, Return (Functor::*) (Args...) const)
  96. {
  97. return static_cast<Return (*) (Args...)> (functor);
  98. }
  99. } // namespace detail
  100. #endif
  101. /** Converts a captureless lambda to its equivalent function pointer type. */
  102. template <typename Functor>
  103. static constexpr auto toFnPtr (Functor functor) { return detail::toFnPtr (functor, &Functor::operator()); }
  104. } // namespace juce