Path: blob/master/Utilities/cmcppdap/include/dap/variant.h
3158 views
// Copyright 2019 Google LLC1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// https://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef dap_variant_h15#define dap_variant_h1617#include "any.h"1819namespace dap {2021// internal functionality22namespace detail {23template <typename T, typename...>24struct TypeIsIn {25static constexpr bool value = false;26};2728template <typename T, typename List0, typename... ListN>29struct TypeIsIn<T, List0, ListN...> {30static constexpr bool value =31std::is_same<T, List0>::value || TypeIsIn<T, ListN...>::value;32};33} // namespace detail3435// variant represents a type-safe union of DAP types.36// variant can hold a value of any of the template argument types.37// variant defaults to a default-constructed T0.38template <typename T0, typename... Types>39class variant {40public:41// constructors42inline variant();43template <typename T>44inline variant(const T& val);4546// assignment47template <typename T>48inline variant& operator=(const T& val);4950// get() returns the contained value of the type T.51// If the any does not contain a value of type T, then get() will assert.52template <typename T>53inline T& get() const;5455// is() returns true iff the contained value is of type T.56template <typename T>57inline bool is() const;5859// accepts() returns true iff the variant accepts values of type T.60template <typename T>61static constexpr bool accepts();6263private:64friend class Serializer;65friend class Deserializer;66any value;67};6869template <typename T0, typename... Types>70variant<T0, Types...>::variant() : value(T0()) {}7172template <typename T0, typename... Types>73template <typename T>74variant<T0, Types...>::variant(const T& v) : value(v) {75static_assert(accepts<T>(), "variant does not accept template type T");76}7778template <typename T0, typename... Types>79template <typename T>80variant<T0, Types...>& variant<T0, Types...>::operator=(const T& v) {81static_assert(accepts<T>(), "variant does not accept template type T");82value = v;83return *this;84}8586template <typename T0, typename... Types>87template <typename T>88T& variant<T0, Types...>::get() const {89static_assert(accepts<T>(), "variant does not accept template type T");90return value.get<T>();91}9293template <typename T0, typename... Types>94template <typename T>95bool variant<T0, Types...>::is() const {96return value.is<T>();97}9899template <typename T0, typename... Types>100template <typename T>101constexpr bool variant<T0, Types...>::accepts() {102return detail::TypeIsIn<T, T0, Types...>::value;103}104105} // namespace dap106107#endif // dap_variant_h108109110