1
0

variant.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef dap_variant_h
  15. #define dap_variant_h
  16. #include "any.h"
  17. namespace dap {
  18. // internal functionality
  19. namespace detail {
  20. template <typename T, typename...>
  21. struct TypeIsIn {
  22. static constexpr bool value = false;
  23. };
  24. template <typename T, typename List0, typename... ListN>
  25. struct TypeIsIn<T, List0, ListN...> {
  26. static constexpr bool value =
  27. std::is_same<T, List0>::value || TypeIsIn<T, ListN...>::value;
  28. };
  29. } // namespace detail
  30. // variant represents a type-safe union of DAP types.
  31. // variant can hold a value of any of the template argument types.
  32. // variant defaults to a default-constructed T0.
  33. template <typename T0, typename... Types>
  34. class variant {
  35. public:
  36. // constructors
  37. inline variant();
  38. template <typename T>
  39. inline variant(const T& val);
  40. // assignment
  41. template <typename T>
  42. inline variant& operator=(const T& val);
  43. // get() returns the contained value of the type T.
  44. // If the any does not contain a value of type T, then get() will assert.
  45. template <typename T>
  46. inline T& get() const;
  47. // is() returns true iff the contained value is of type T.
  48. template <typename T>
  49. inline bool is() const;
  50. // accepts() returns true iff the variant accepts values of type T.
  51. template <typename T>
  52. static constexpr bool accepts();
  53. private:
  54. friend class Serializer;
  55. friend class Deserializer;
  56. any value;
  57. };
  58. template <typename T0, typename... Types>
  59. variant<T0, Types...>::variant() : value(T0()) {}
  60. template <typename T0, typename... Types>
  61. template <typename T>
  62. variant<T0, Types...>::variant(const T& v) : value(v) {
  63. static_assert(accepts<T>(), "variant does not accept template type T");
  64. }
  65. template <typename T0, typename... Types>
  66. template <typename T>
  67. variant<T0, Types...>& variant<T0, Types...>::operator=(const T& v) {
  68. static_assert(accepts<T>(), "variant does not accept template type T");
  69. value = v;
  70. return *this;
  71. }
  72. template <typename T0, typename... Types>
  73. template <typename T>
  74. T& variant<T0, Types...>::get() const {
  75. static_assert(accepts<T>(), "variant does not accept template type T");
  76. return value.get<T>();
  77. }
  78. template <typename T0, typename... Types>
  79. template <typename T>
  80. bool variant<T0, Types...>::is() const {
  81. return value.is<T>();
  82. }
  83. template <typename T0, typename... Types>
  84. template <typename T>
  85. constexpr bool variant<T0, Types...>::accepts() {
  86. return detail::TypeIsIn<T, T0, Types...>::value;
  87. }
  88. } // namespace dap
  89. #endif // dap_variant_h