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.

89 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. } // namespace detail
  31. #endif
  32. //==============================================================================
  33. /** Some helper methods for checking a callable object before invoking with
  34. the specified arguments.
  35. If the object provides a comparison operator for nullptr it will check before
  36. calling. For other objects it will just invoke the function call operator.
  37. @tags{Core}
  38. */
  39. struct NullCheckedInvocation
  40. {
  41. template <typename Callable, typename... Args,
  42. std::enable_if_t<detail::EqualityComparableToNullptr<Callable>::value, int> = 0>
  43. static void invoke (Callable&& fn, Args&&... args)
  44. {
  45. if (fn != nullptr)
  46. fn (std::forward<Args> (args)...);
  47. }
  48. template <typename Callable, typename... Args,
  49. std::enable_if_t<! detail::EqualityComparableToNullptr<Callable>::value, int> = 0>
  50. static void invoke (Callable&& fn, Args&&... args)
  51. {
  52. fn (std::forward<Args> (args)...);
  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 = typename std::enable_if_t<! std::is_base_of<A, std::remove_reference_t<B>>::value>;
  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>
  65. Object withMember (Object copy, Member OtherObject::* member, Member&& value)
  66. {
  67. copy.*member = std::forward<Member> (value);
  68. return copy;
  69. }
  70. } // namespace juce