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.

104 lines
4.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. //==============================================================================
  18. /**
  19. Macro to enable bitwise operations for scoped enums (enum struct/class).
  20. To use this, add the line JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS (MyEnum)
  21. after your enum declaration at file scope level.
  22. e.g. @code
  23. enum class MyEnum
  24. {
  25. one = 1 << 0,
  26. two = 1 << 1,
  27. three = 1 << 2
  28. };
  29. JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS (MyEnum)
  30. MyEnum e = MyEnum::one | MyEnum::two;
  31. bool hasTwo = (e & MyEnum::two) != MyEnum{}; // true
  32. bool hasTwo = hasBitValueSet (e, MyEnum::two); // true
  33. e = withBitValueCleared (e, MyEnum::two);
  34. bool hasTwo = hasBitValueSet (e, MyEnum::two); // false
  35. @endcode
  36. */
  37. #define JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS(EnumType) \
  38. static_assert (std::is_enum_v<EnumType>, \
  39. "JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS " \
  40. "should only be used with enum types"); \
  41. constexpr auto operator& (EnumType a, EnumType b) \
  42. { \
  43. using base_type = std::underlying_type<EnumType>::type; \
  44. return static_cast<EnumType> (base_type (a) & base_type (b)); \
  45. } \
  46. constexpr auto operator| (EnumType a, EnumType b) \
  47. { \
  48. using base_type = std::underlying_type<EnumType>::type; \
  49. return static_cast<EnumType> (base_type (a) | base_type (b)); \
  50. } \
  51. constexpr auto operator~ (EnumType a) \
  52. { \
  53. using base_type = std::underlying_type<EnumType>::type; \
  54. return static_cast<EnumType> (~base_type (a)); \
  55. } \
  56. constexpr auto& operator|= (EnumType& a, EnumType b) \
  57. { \
  58. a = (a | b); \
  59. return a; \
  60. } \
  61. constexpr auto& operator&= (EnumType& a, EnumType b) \
  62. { \
  63. a = (a & b); \
  64. return a; \
  65. }
  66. namespace juce
  67. {
  68. template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int> = 0>
  69. constexpr bool hasBitValueSet (EnumType enumValue, EnumType valueToLookFor) noexcept
  70. {
  71. return (enumValue & valueToLookFor) != EnumType{};
  72. }
  73. template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int> = 0>
  74. constexpr EnumType withBitValueSet (EnumType enumValue, EnumType valueToAdd) noexcept
  75. {
  76. return enumValue | valueToAdd;
  77. }
  78. template <typename EnumType, std::enable_if_t<std::is_enum_v<EnumType>, int> = 0>
  79. constexpr EnumType withBitValueCleared (EnumType enumValue, EnumType valueToRemove) noexcept
  80. {
  81. return enumValue & ~valueToRemove;
  82. }
  83. }