Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
210 lines (188 loc) · 6.22 KB

File metadata and controls

210 lines (188 loc) · 6.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
#pragma once
#include "TypeList.hpp"
namespace vmp {
//! Variadic binding and rebinding of data
//!
//! DataBind can gather and forward data as follows:
//! \code{.cpp}
//! DataBind<char, int, double> bind('a', 2, 3.4);
//! bool value = false;
//! auto call = [&value] (char, int, double) { value = true; };
//! bind(call);
//! \endcode
template<typename... _Data_>
class DataBind:
public typelist<_Data_...>
{
template<typename... __Data_>
friend class DataBind;
template<typename _Head, typename... _Body_>
struct HeadBody {
using Head = _Head;
using Body = DataBind<_Body_...>;
};
using Head = typename HeadBody<_Data_...>::Head;
using Body = typename HeadBody<_Data_...>::Body;
Head head;
Body body;
// QUESTION: If there is a friend relation, why is the private call recursion not allowed?
template<typename Call, typename _Head, typename... _Body_>
void recursed(Call&& call) {
body([call, this] (_Body_&... _body_) {
// NOTE: Must call public method since no friend relationship exists
call(head, _body_...);
});
}
template<bool t_condition, typename t_type>
struct define_if {};
template<typename t_type>
struct define_if<true, t_type> {
using type = t_type;
};
public:
//! Default Constructor
//!
//! This constructor assumes as all _Data_ types have default constructors
DataBind() = default;
//! Copy construction
//!
//! Construction assumes all argument __Data_ types can be cast to _Data types
//! Requires that sizeof...(_Data) == sizeof...(__Data)
template<typename... __Data_>
DataBind(const DataBind<__Data_...>& source):
head(source.head),
body(source.body)
{}
//! Move contruction
//!
//! Construction assumes all argument __Data types can be converted to _Data types
//! Requires that sizeof...(_Data) == sizeof...(__Data)
template<typename... __Data_>
DataBind(DataBind<__Data_...>&& src):
head(static_cast<Head&&>(src.head)),
body(static_cast<Body&&>(src.body))
{}
// TODO: Variadic concatenation constructors
//! Copy assignment
//!
//! Assignment assumes all argument __Data types can be cast to _Data types
//! Base case enforces requirement that sizeof...(_Data) == sizeof...(__Data)
template<typename... __Data>
DataBind& operator = (const DataBind<__Data...>& rhs) {
head = rhs.head;
body = rhs.body;
return *this;
}
//! Move assignment
//!
//! Assignment assumes all argument __Data types can be moved to _Data types
//! Requires that sizeof...(_Data) == sizeof...(__Data)
template<typename... __Data>
DataBind& operator = (DataBind<__Data...>&& rhs) {
head = static_cast<Head&&>(rhs.head);
body = static_cast<Body&&>(rhs.body);
return *this;
}
// QUESTION: Is specialization inference from a variadic constructor possible?
// NOTE: As of C++17 the template specialization can be inferred
// from constructor arguments, so a template template can also
// be used here via auto as return type.
// NOTE: This would require using _Data_ in the constructor, which could conflict
// with the default constructor.
// QUESTION: Is it possible to handle mixed copy & move arguments?
// QUESTION: If partial arguments are provided,
// will the remaining arguments be defaulted?
// QUESTION: If too many arguments are provided, what happens?
//! Copy constructor using constituent copy construtors
//!
//! Copy constructor accepts arguments only of the types specified as _Data_ variadic.
//! Consequently, class template deduction is possible from constructor.
//!
//! Head separation prevents conflict with default constructor.
//! The argument count must be `<= sizeof...(_Data_)`
//! A partial list of P arguments will initialize only the first P members, with the remaining
//! being default initialized.
template<typename... _Body_>
DataBind(const Head& _head, const _Body_&... _body):
head(_head),
body(_body...)
{}
//! Move constructor using constituent move construtors
//!
//! Move constructor accepts arguments only of the types specified as Data variadic.
//! Consequently, class template deduction is possible from constructor.
//!
//! Head separation prevents conflict with default constructor.
//! The argument count must be `<= sizeof...(_Data_)`
//! A partial list of P arguments will initialize only the first P members, with the remaining
//! being default initialized.
template<typename... _Rest>
DataBind(Head&& _head, _Rest&&... _body):
head(static_cast<Head&&>(_head)),
body(static_cast<_Rest&&>(_body)...)
{}
//! Rebinds the DataBind elements as arguments of call
//!
//! It is expected that the argument is an in-line (r-value) lambda.
//! This makes it possible to handle arbitrary return types through
//! external binding.
template<typename Call>
void operator () (Call&& call) {
recursed<Call, _Data_...>(static_cast<Call&&>(call));
}
// QUESTION: Is it necesary to redefine typelist<_Data_...> if it is inherited?
//! Returns the element of DataBind identified by index
//!
//! It is expected that the return type is assigned to auto& value,
//! since the return type is determined by the index template argument.
template<sizetype index>
typename define_if<
0 != index,
Body
>::type::template part<index - 1>&
data() {
// NOTE: part<index> must be outside of declare_if arguments, otherwise
// recursion will not terminate when index == 0
return body.template data<index - 1>();
}
template<sizetype index>
typename define_if<
0 == index,
Head
>::type&
data() {
return head;
}
template<sizetype index>
const typename define_if<
0 != index,
Body
>::type::template part<index - 1>&
data() const {
return body.template data<index - 1>();
}
template<sizetype index>
const typename define_if<
0 == index,
Head
>::type&
data() const {
return head;
}
};
template<>
class DataBind<>:
public typelist<>
{
public:
DataBind() = default;
DataBind(const DataBind&) = default;
DataBind(DataBind&&) = default;
DataBind& operator = (const DataBind&) = default;
DataBind& operator = (DataBind&&) = default;
template<typename t_Call>
void operator () (const t_Call& call) {
call();
}
};
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.