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.

149 lines
4.6KB

  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. //==============================================================================
  20. inline constexpr auto dynamicExtent = std::numeric_limits<size_t>::max();
  21. namespace detail
  22. {
  23. //==============================================================================
  24. template <typename, typename = void>
  25. constexpr auto hasToAddress = false;
  26. template <typename T>
  27. constexpr auto hasToAddress<T, Void<decltype (std::pointer_traits<T>::to_address (std::declval<T>()))>> = true;
  28. template <typename, typename = void>
  29. constexpr auto hasDataAndSize = false;
  30. template <typename T>
  31. constexpr auto hasDataAndSize<T,
  32. Void<decltype (std::data (std::declval<T>())),
  33. decltype (std::size (std::declval<T>()))>> = true;
  34. template <size_t Extent>
  35. struct NumBase
  36. {
  37. constexpr NumBase() = default;
  38. constexpr explicit NumBase (size_t) {}
  39. constexpr size_t size() const { return Extent; }
  40. };
  41. template <>
  42. struct NumBase<dynamicExtent>
  43. {
  44. constexpr NumBase() = default;
  45. constexpr explicit NumBase (size_t arg)
  46. : num (arg) {}
  47. constexpr size_t size() const { return num; }
  48. size_t num{};
  49. };
  50. template <typename T>
  51. constexpr T* toAddress (T* p)
  52. {
  53. return p;
  54. }
  55. template <typename It>
  56. constexpr auto toAddress (const It& it)
  57. {
  58. if constexpr (detail::hasToAddress<It>)
  59. return std::pointer_traits<It>::to_address (it);
  60. else
  61. return toAddress (it.operator->());
  62. }
  63. }
  64. //==============================================================================
  65. /**
  66. A non-owning view over contiguous objects stored in an Array or vector
  67. or other similar container.
  68. This is a bit like std::span from C++20, but with a more limited interface.
  69. */
  70. template <typename Value, size_t Extent = dynamicExtent>
  71. class Span : private detail::NumBase<Extent> // for empty-base optimisation
  72. {
  73. using Base = detail::NumBase<Extent>;
  74. public:
  75. static constexpr auto extent = Extent;
  76. template <size_t e = extent, std::enable_if_t<e == 0 || e == dynamicExtent, int> = 0>
  77. constexpr Span() {}
  78. template <typename It>
  79. constexpr Span (It it, size_t end)
  80. : Base (end), ptr (detail::toAddress (it)) {}
  81. template <typename Range, std::enable_if_t<detail::hasDataAndSize<Range>, int> = 0>
  82. constexpr Span (Range&& range)
  83. : Base (std::size (range)), ptr (std::data (range)) {}
  84. constexpr Span (const Span&) = default;
  85. constexpr Span& operator= (const Span&) = default;
  86. using Base::size;
  87. constexpr Value* begin() const { return ptr; }
  88. constexpr Value* end() const { return ptr + size(); }
  89. constexpr auto& front() const { return ptr[0]; }
  90. constexpr auto& back() const { return ptr[size() - 1]; }
  91. constexpr auto& operator[] (size_t index) const { return ptr[index]; }
  92. constexpr Value* data() const { return ptr; }
  93. constexpr bool empty() const { return size() == 0; }
  94. private:
  95. Value* ptr = nullptr;
  96. };
  97. template <typename T, typename End>
  98. Span (T, End) -> Span<std::remove_pointer_t<decltype (detail::toAddress (std::declval<T>()))>>;
  99. template <typename T, size_t N>
  100. Span (T (&) [N]) -> Span<T, N>;
  101. template <typename T, size_t N>
  102. Span (std::array<T, N>&) -> Span<T, N>;
  103. template <typename T, size_t N>
  104. Span (const std::array<T, N>&) -> Span<const T, N>;
  105. template <typename Range>
  106. Span (Range&& r) -> Span<std::remove_pointer_t<decltype (std::data (r))>>;
  107. } // namespace juce