Debian Codegraph @

Utilities/cmcppdap/include/dap/variant.h

raw
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef dap_variant_h
16 #define dap_variant_h
17
18 #include "any.h"
19
20 namespace dap {
21
22 // internal functionality
23 namespace detail {
24 template <typename T, typename...>
25 struct TypeIsIn {
26 static constexpr bool value = false;
27 };
28
29 template <typename T, typename List0, typename... ListN>
30 struct TypeIsIn<T, List0, ListN...> {
31 static constexpr bool value =
32 std::is_same<T, List0>::value || TypeIsIn<T, ListN...>::value;
33 };
34 } // namespace detail
35
36 // variant represents a type-safe union of DAP types.
37 // variant can hold a value of any of the template argument types.
38 // variant defaults to a default-constructed T0.
39 template <typename T0, typename... Types>
40 class variant {
41 public:
42 // constructors
43 inline variant();
44 template <typename T>
45 inline variant(const T& val);
46
47 // assignment
48 template <typename T>
49 inline variant& operator=(const T& val);
50
51 // get() returns the contained value of the type T.
52 // If the any does not contain a value of type T, then get() will assert.
53 template <typename T>
54 inline T& get() const;
55
56 // is() returns true iff the contained value is of type T.
57 template <typename T>
58 inline bool is() const;
59
60 // accepts() returns true iff the variant accepts values of type T.
61 template <typename T>
62 static constexpr bool accepts();
63
64 private:
65 friend class Serializer;
66 friend class Deserializer;
67 any value;
68 };
69
70 template <typename T0, typename... Types>
71 variant<T0, Types...>::variant() : value(T0()) {}
72
73 template <typename T0, typename... Types>
74 template <typename T>
75 variant<T0, Types...>::variant(const T& v) : value(v) {
76 static_assert(accepts<T>(), "variant does not accept template type T");
77 }
78
79 template <typename T0, typename... Types>
80 template <typename T>
81 variant<T0, Types...>& variant<T0, Types...>::operator=(const T& v) {
82 static_assert(accepts<T>(), "variant does not accept template type T");
83 value = v;
84 return *this;
85 }
86
87 template <typename T0, typename... Types>
88 template <typename T>
89 T& variant<T0, Types...>::get() const {
90 static_assert(accepts<T>(), "variant does not accept template type T");
91 return value.get<T>();
92 }
93
94 template <typename T0, typename... Types>
95 template <typename T>
96 bool variant<T0, Types...>::is() const {
97 return value.is<T>();
98 }
99
100 template <typename T0, typename... Types>
101 template <typename T>
102 constexpr bool variant<T0, Types...>::accepts() {
103 return detail::TypeIsIn<T, T0, Types...>::value;
104 }
105
106 } // namespace dap
107
108 #endif // dap_variant_h