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
20namespace dap {
21
22// internal functionality
23namespace detail {
24template <typename T, typename...>
25struct TypeIsIn {
26 static constexpr bool value = false;
27};
28
29template <typename T, typename List0, typename... ListN>
30struct 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.
39template <typename T0, typename... Types>
40class 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
70template <typename T0, typename... Types>
71variant<T0, Types...>::variant() : value(T0()) {}
72
73template <typename T0, typename... Types>
74template <typename T>
75variant<T0, Types...>::variant(const T& v) : value(v) {
76 static_assert(accepts<T>(), "variant does not accept template type T");
77}
78
79template <typename T0, typename... Types>
80template <typename T>
81variant<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
87template <typename T0, typename... Types>
88template <typename T>
89T& variant<T0, Types...>::get() const {
90 static_assert(accepts<T>(), "variant does not accept template type T");
91 return value.get<T>();
92}
93
94template <typename T0, typename... Types>
95template <typename T>
96bool variant<T0, Types...>::is() const {
97 return value.is<T>();
98}
99
100template <typename T0, typename... Types>
101template <typename T>
102constexpr bool variant<T0, Types...>::accepts() {
103 return detail::TypeIsIn<T, T0, Types...>::value;
104}
105
106} // namespace dap
107
108#endif // dap_variant_h
109