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.

105 lines
3.5KB

  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. [[nodiscard]] Object withMember (Object copy, Member OtherObject::* member, Other&& value)
  66. {
  67. copy.*member = std::forward<Other> (value);
  68. return copy;
  69. }
  70. #ifndef DOXYGEN
  71. namespace detail
  72. {
  73. template <typename Functor, typename Return, typename... Args>
  74. static constexpr auto toFnPtr (Functor functor, Return (Functor::*) (Args...) const)
  75. {
  76. return static_cast<Return (*) (Args...)> (functor);
  77. }
  78. } // namespace detail
  79. #endif
  80. /** Converts a captureless lambda to its equivalent function pointer type. */
  81. template <typename Functor>
  82. static constexpr auto toFnPtr (Functor functor) { return detail::toFnPtr (functor, &Functor::operator()); }
  83. } // namespace juce