JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Span.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
27inline constexpr auto dynamicExtent = std::numeric_limits<size_t>::max();
28
29namespace detail
30{
31 //==============================================================================
32 template <typename, typename = void>
33 constexpr auto hasToAddress = false;
34
35 template <typename T>
36 constexpr auto hasToAddress<T, Void<decltype (std::pointer_traits<T>::to_address (std::declval<T>()))>> = true;
37
38 template <typename, typename = void>
39 constexpr auto hasDataAndSize = false;
40
41 template <typename T>
42 constexpr auto hasDataAndSize<T,
44 decltype (std::size (std::declval<T>()))>> = true;
45
46 template <size_t Extent>
47 struct NumBase
48 {
49 constexpr NumBase() = default;
50
51 constexpr explicit NumBase (size_t) {}
52
53 constexpr size_t size() const { return Extent; }
54 };
55
56 template <>
57 struct NumBase<dynamicExtent>
58 {
59 constexpr NumBase() = default;
60
61 constexpr explicit NumBase (size_t arg)
62 : num (arg) {}
63
64 constexpr size_t size() const { return num; }
65
66 size_t num{};
67 };
68
69 template <typename T>
70 constexpr T* toAddress (T* p)
71 {
72 return p;
73 }
74
75 template <typename It>
76 constexpr auto toAddress (const It& it)
77 {
78 if constexpr (detail::hasToAddress<It>)
80 else
81 return toAddress (it.operator->());
82 }
83}
84
85//==============================================================================
94template <typename Value, size_t Extent = dynamicExtent>
95class Span : private detail::NumBase<Extent> // for empty-base optimisation
96{
98
99public:
100 static constexpr auto extent = Extent;
101
102 template <size_t e = extent, std::enable_if_t<e == 0 || e == dynamicExtent, int> = 0>
103 constexpr Span() {}
104
105 template <typename It>
106 constexpr Span (It it, size_t end)
107 : Base (end), ptr (detail::toAddress (it)) {}
108
109 template <typename Range, std::enable_if_t<detail::hasDataAndSize<Range>, int> = 0>
110 constexpr Span (Range&& range)
111 : Base (std::size (range)), ptr (std::data (range)) {}
112
113 constexpr Span (const Span&) = default;
114
115 constexpr Span& operator= (const Span&) = default;
116
117 constexpr Span (Span&&) noexcept = default;
118
119 constexpr Span& operator= (Span&&) noexcept = default;
120
121 using Base::size;
122
123 constexpr Value* begin() const { return ptr; }
124 constexpr Value* end() const { return ptr + size(); }
125
126 constexpr auto& front() const { return ptr[0]; }
127 constexpr auto& back() const { return ptr[size() - 1]; }
128
129 constexpr auto& operator[] (size_t index) const { return ptr[index]; }
130 constexpr Value* data() const { return ptr; }
131
132 constexpr bool empty() const { return size() == 0; }
133
134private:
135 Value* ptr = nullptr;
136};
137
138template <typename T, typename End>
140
141template <typename T, size_t N>
142Span (T (&) [N]) -> Span<T, N>;
143
144template <typename T, size_t N>
146
147template <typename T, size_t N>
149
150template <typename Range>
151Span (Range&& r) -> Span<std::remove_pointer_t<decltype (std::data (r))>>;
152
153
154} // namespace juce
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
A non-owning view over contiguous objects stored in an Array or vector or other similar container.
Definition juce_Span.h:96
Represents a shared variant value.
Definition juce_Value.h:51
T data(T... args)
T max(T... args)
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T size(T... args)
T to_address(T... args)