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.

96 lines
3.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. struct EqualityComparableToNullptr
  26. : std::false_type {};
  27. template <typename T>
  28. struct EqualityComparableToNullptr<T, Void<decltype (std::declval<T>() != nullptr)>>
  29. : std::true_type {};
  30. template <typename T>
  31. constexpr bool shouldCheckAgainstNullptr = EqualityComparableToNullptr<T>::value;
  32. } // namespace detail
  33. #endif
  34. //==============================================================================
  35. /** Some helper methods for checking a callable object before invoking with
  36. the specified arguments.
  37. If the object provides a comparison operator for nullptr it will check before
  38. calling. For other objects it will just invoke the function call operator.
  39. @tags{Core}
  40. */
  41. struct NullCheckedInvocation
  42. {
  43. template <typename Callable, typename... Args,
  44. std::enable_if_t<detail::shouldCheckAgainstNullptr<Callable>, int> = 0>
  45. static void invoke (Callable&& fn, Args&&... args)
  46. {
  47. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Waddress")
  48. if (fn != nullptr)
  49. fn (std::forward<Args> (args)...);
  50. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  51. }
  52. template <typename Callable, typename... Args,
  53. std::enable_if_t<! detail::shouldCheckAgainstNullptr<Callable>, int> = 0>
  54. static void invoke (Callable&& fn, Args&&... args)
  55. {
  56. fn (std::forward<Args> (args)...);
  57. }
  58. template <typename... Args>
  59. static void invoke (std::nullptr_t, Args&&...) {}
  60. };
  61. /** Can be used to disable template constructors that would otherwise cause ambiguity with
  62. compiler-generated copy and move constructors.
  63. Adapted from https://ericniebler.com/2013/08/07/universal-references-and-the-copy-constructo/
  64. */
  65. template <typename A, typename B>
  66. using DisableIfSameOrDerived = typename std::enable_if_t<! std::is_base_of<A, std::remove_reference_t<B>>::value>;
  67. /** Copies an object, sets one of the copy's members to the specified value, and then returns the copy. */
  68. template <typename Object, typename OtherObject, typename Member>
  69. Object withMember (Object copy, Member OtherObject::* member, Member&& value)
  70. {
  71. copy.*member = std::forward<Member> (value);
  72. return copy;
  73. }
  74. } // namespace juce