forked from facebook/fbthrift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
variant.h
255 lines (238 loc) · 6.8 KB
/
variant.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THRIFT_FATAL_VARIANT_H_
#define THRIFT_FATAL_VARIANT_H_ 1
#include <cassert>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <fatal/type/search.h>
#include <fatal/type/transform.h>
#include <folly/Traits.h>
#include <thrift/lib/cpp2/reflection/reflection.h>
namespace apache {
namespace thrift {
namespace detail {
template <typename T, typename V>
using variant_helper =
typename reflect_variant<folly::remove_cvref_t<V>>::traits;
} // namespace detail
/**
* READ ME FIRST: this header enhances Thrift unions with variant-style
* functionality.
*
* Please refer to the top of `thrift/lib/cpp2/reflection/reflection.h` on how
* to enable compile-time reflection for unions. The present header relies on it
* for its functionality.
*
* TROUBLESHOOTING:
* - make sure you've followed the instructions on `reflection.h` to enable
* generation of compile-time reflection;
* - make sure you've included the union metadata for your Thrift union, as
* specified in `reflection.h`;
* - make sure exactly one of the Thrift union's field map to a specific type.
*
* @author: Marcelo Juchem <[email protected]>
*/
/**
* Gets a reference to the Thrift union's member for the given type.
*
* NOTE: this is an unchecked version of the variant getter. If the desired
* field is not the one currently set in the union, the behavior of this call
* is undefined. For a safer alternative, use either `variant_checked_get` or
* `variant_try_get`.
*
* Example:
*
* // Foo.thrift
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // foo.cpp
* #include "project_dir/gen-cpp2/Foo_fatal_union.h"
* #include <thrift/lib/cpp2/reflection/variant.h>
*
* MyUnion u;
* u.set_a(10);
*
* // yields a reference to field `a`
* std::cout << variant_get<std::int32_t>(u);
*
* // undefined behavior
* std::cout << variant_get<double>(u);
*
* @author: Marcelo Juchem <[email protected]>
*/
template <typename T, typename V>
auto variant_get(V&& variant)
-> decltype(detail::variant_helper<T, V>::by_type ::template get<T>(
std::forward<V>(variant))) {
using traits = detail::variant_helper<T, V>;
assert(traits::get_id(variant) == traits::by_type::template id<T>::value);
return traits::by_type::template get<T>(std::forward<V>(variant));
}
/**
* Gets a reference to the Thrift union's member for the given type.
*
* If the desired field is not the one currently set in the union, throws an
* `std::invalid_argument` exception.
*
* Example:
*
* // Foo.thrift
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // foo.cpp
* #include "project_dir/gen-cpp2/Foo_fatal_union.h"
* #include <thrift/lib/cpp2/reflection/variant.h>
*
* MyUnion u;
* u.set_a(10);
*
* // yields a reference to field `a`
* std::cout << variant_checked_get<std::int32_t>(u);
*
* // throws `std::invalid_argument`
* std::cout << variant_checked_get<double>(u);
*
* @author: Marcelo Juchem <[email protected]>
*/
template <typename T, typename V>
auto variant_checked_get(V&& variant)
-> decltype(detail::variant_helper<T, V>::by_type ::template get<T>(
std::forward<V>(variant))) {
using traits = detail::variant_helper<T, V>;
if (traits::get_id(variant) != traits::by_type::template id<T>::value) {
throw std::invalid_argument(
"type requested to variant_checked_get() is not the one stored in the"
" variant");
}
return traits::by_type::template get<T>(std::forward<V>(variant));
}
/**
* Gets a pointer to the Thrift union's member for the given type.
*
* If the desired field is not the one currently set in the union, returns
* `nullptr`.
*
* Example:
*
* // Foo.thrift
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // foo.cpp
* #include "project_dir/gen-cpp2/Foo_fatal_union.h"
* #include <thrift/lib/cpp2/reflection/variant.h>
*
* MyUnion u;
* u.set_a(10);
*
* // yields a pointer to field `a`
* std::cout << variant_try_get<std::int32_t>(u);
*
* // yields `nullptr`
* std::cout << variant_try_get<double>(u);
*
* @author: Marcelo Juchem <[email protected]>
*/
template <typename T, typename V>
auto variant_try_get(V& variant) -> decltype(std::addressof(
detail::variant_helper<T, V>::by_type::template get<T>(variant))) {
using traits = detail::variant_helper<T, V>;
if (traits::get_id(variant) != traits::by_type::template id<T>::value) {
return nullptr;
}
return std::addressof(traits::by_type::template get<T>(variant));
}
/**
* Sets the Thrift union's member for the given type with the provided value.
*
* Example:
*
* // Foo.thrift
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // foo.cpp
* #include "project_dir/gen-cpp2/Foo_fatal_union.h"
* #include <thrift/lib/cpp2/reflection/variant.h>
*
* MyUnion u;
*
* // sets the field `a` to the value `10`.
* variant_set(u, 10);
*
* std::cout << u.get_a();
*
* @author: Marcelo Juchem <[email protected]>
*/
template <typename V, typename T>
folly::remove_cvref_t<T>& variant_set(V& variant, T&& value) {
using type = folly::remove_cvref_t<T>;
using by_type = typename detail::variant_helper<type, V>::by_type;
by_type::template set<type>(variant, std::forward<T>(value));
return by_type::template get<type>(variant);
}
/**
* Sets the Thrift union's member for the given type by perfectly forwarding the
* given arguments to its constructor.
*
* Example:
*
* // Foo.thrift
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // foo.cpp
* #include "project_dir/gen-cpp2/Foo_fatal_union.h"
* #include <thrift/lib/cpp2/reflection/variant.h>
*
* MyUnion u;
*
* // sets the field `b` to the string "hello, world".
* variant_emplace<std::string>(u, "hello, world");
*
* std::cout << u.get_b();
*
* @author: Marcelo Juchem <[email protected]>
*/
template <typename T, typename V, typename... Args>
T& variant_emplace(V& variant, Args&&... args) {
using by_type = typename detail::variant_helper<T, V>::by_type;
by_type::template set<T>(variant, std::forward<Args>(args)...);
return by_type::template get<T>(variant);
}
} // namespace thrift
} // namespace apache
#endif // THRIFT_FATAL_VARIANT_H_