-
Notifications
You must be signed in to change notification settings - Fork 247
/
base_collections_input_vector_view.h
208 lines (168 loc) · 6 KB
/
base_collections_input_vector_view.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
namespace winrt::impl
{
template <typename T, typename Container>
struct input_vector_view :
implements<input_vector_view<T, Container>, non_agile, no_weak_ref, wfc::IVectorView<T>, wfc::IIterable<T>>,
vector_view_base<input_vector_view<T, Container>, T>
{
static_assert(std::is_same_v<Container, std::remove_reference_t<Container>>, "Must be constructed with rvalue.");
explicit input_vector_view(Container&& values) : m_values(std::forward<Container>(values))
{
}
auto& get_container() const noexcept
{
return m_values;
}
private:
Container const m_values;
};
template <typename T, typename InputIt>
struct scoped_input_vector_view :
input_scope,
implements<scoped_input_vector_view<T, InputIt>, non_agile, no_weak_ref, wfc::IVectorView<T>, wfc::IIterable<T>>,
vector_view_base<scoped_input_vector_view<T, InputIt>, T>
{
void abi_enter() const
{
check_scope();
}
scoped_input_vector_view(InputIt first, InputIt last) : m_begin(first), m_end(last)
{
}
auto get_container() const noexcept
{
return range_container<InputIt>{ m_begin, m_end };
}
#if defined(_DEBUG) && !defined(WINRT_NO_MAKE_DETECTION)
void use_make_function_to_create_this_object() final
{
}
#endif
private:
InputIt const m_begin;
InputIt const m_end;
};
template <typename T, typename InputIt>
auto make_scoped_input_vector_view(InputIt first, InputIt last)
{
using interface_type = wfc::IVectorView<T>;
std::pair<interface_type, input_scope*> result;
auto ptr = new scoped_input_vector_view<T, InputIt>(first, last);
*put_abi(result.first) = to_abi<interface_type>(ptr);
result.second = ptr;
return result;
}
}
WINRT_EXPORT namespace winrt::param
{
template <typename T>
struct vector_view
{
using value_type = T;
using interface_type = Windows::Foundation::Collections::IVectorView<value_type>;
vector_view(std::nullptr_t) noexcept
{
}
vector_view(vector_view const& values) = delete;
vector_view& operator=(vector_view const& values) = delete;
vector_view(interface_type const& values) noexcept : m_owned(false)
{
attach_abi(m_pair.first, winrt::get_abi(values));
}
template <typename Collection, std::enable_if_t<std::is_convertible_v<Collection, interface_type>, int> = 0>
vector_view(Collection const& values) noexcept
{
m_pair.first = values;
}
template <typename Allocator>
vector_view(std::vector<value_type, Allocator>&& values) : m_pair(make<impl::input_vector_view<value_type, std::vector<value_type, Allocator>>>(std::move(values)), nullptr)
{
}
template <typename Allocator>
vector_view(std::vector<value_type, Allocator> const& values) : m_pair(impl::make_scoped_input_vector_view<value_type>(values.begin(), values.end()))
{
}
vector_view(std::initializer_list<value_type> values) : m_pair(impl::make_scoped_input_vector_view<value_type>(values.begin(), values.end()))
{
}
template <typename U, std::enable_if_t<std::is_convertible_v<U, value_type>, int> = 0>
vector_view(std::initializer_list<U> values) : m_pair(impl::make_scoped_input_vector_view<value_type>(values.begin(), values.end()))
{
}
template<class InputIt>
vector_view(InputIt first, InputIt last) : m_pair(impl::make_scoped_input_vector_view<value_type>(first, last))
{
}
~vector_view() noexcept
{
if (m_pair.second)
{
m_pair.second->invalidate_scope();
}
if (!m_owned)
{
detach_abi(m_pair.first);
}
}
operator interface_type const& () const noexcept
{
return m_pair.first;
}
private:
std::pair<interface_type, impl::input_scope*> m_pair;
bool m_owned{ true };
};
template <typename T>
auto get_abi(vector_view<T> const& object) noexcept
{
return *(void**)(&object);
}
template <typename T>
struct async_vector_view
{
using value_type = T;
using interface_type = Windows::Foundation::Collections::IVectorView<value_type>;
async_vector_view(std::nullptr_t) noexcept
{
}
async_vector_view(async_vector_view const& values) = delete;
async_vector_view& operator=(async_vector_view const& values) = delete;
async_vector_view(interface_type const& values) noexcept : m_owned(false)
{
attach_abi(m_interface, winrt::get_abi(values));
}
template <typename Collection, std::enable_if_t<std::is_convertible_v<Collection, interface_type>, int> = 0>
async_vector_view(Collection const& values) noexcept
{
m_interface = values;
}
template <typename Allocator>
async_vector_view(std::vector<value_type, Allocator>&& values) :
m_interface(make<impl::input_vector_view<value_type, std::vector<value_type, Allocator>>>(std::move(values)))
{
}
async_vector_view(std::initializer_list<value_type> values) :
m_interface(make<impl::input_vector_view<value_type, std::vector<value_type>>>(values))
{
}
~async_vector_view() noexcept
{
if (!m_owned)
{
detach_abi(m_interface);
}
}
operator interface_type const& () const noexcept
{
return m_interface;
}
private:
interface_type m_interface;
bool m_owned{ true };
};
template <typename T>
auto get_abi(async_vector_view<T> const& object) noexcept
{
return *(void**)(&object);
}
}