-
Notifications
You must be signed in to change notification settings - Fork 612
/
variant.h
118 lines (107 loc) · 2.79 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/
#pragma once
#include <folly/Traits.h>
#include <thrift/lib/cpp2/op/Get.h>
namespace apache::thrift {
namespace detail {
template <typename Union, typename Type>
constexpr std::size_t countTypeInUnion() {
std::size_t count = 0;
op::for_each_ordinal<Union>([&](auto id) {
count += std::is_same_v<op::get_native_type<Union, decltype(id)>, Type>;
});
return count;
}
template <typename V, typename T, typename F>
void findTypeInUnion(V& variant, F f) {
using Union = std::remove_cvref_t<V>;
using Type = std::remove_cvref_t<T>;
static_assert(is_thrift_union_v<Union>);
static_assert(countTypeInUnion<Union, Type>() == 1);
op::for_each_ordinal<Union>([&](auto id) {
using Id = decltype(id);
if constexpr (std::is_same_v<op::get_native_type<Union, Id>, Type>) {
f(op::get<Id>(variant));
}
});
}
} // namespace detail
/**
* 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_types.h"
*
* MyUnion u;
* u.a_ref() = 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);
*/
template <typename T, typename V>
folly::like_t<V, T>* variant_try_get(V& variant) {
folly::like_t<V, T>* ret = nullptr;
detail::findTypeInUnion<V, T>(variant, [&](auto p) {
if (p) {
ret = &*p;
}
});
return ret;
}
/**
* 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_types.h"
*
* MyUnion u;
*
* // sets the field `a` to the value `10`.
* variant_set(u, 10);
*
* std::cout << *u.a_ref();
*/
template <typename V, typename T>
void variant_set(V& variant, T&& value) {
detail::findTypeInUnion<V, T>(
variant, [&](auto p) { p = std::forward<T>(value); });
}
} // namespace apache::thrift