Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MetainfoValueImpl.h
Go to the documentation of this file.
1 //===-- serialbox/core/MetainfoValueImpl.h ------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_METAINFOVALUEIMPL_H
16 #define SERIALBOX_CORE_METAINFOVALUEIMPL_H
17 
18 #include "serialbox/core/Array.h"
20 #include "serialbox/core/Type.h"
21 #include <boost/any.hpp>
22 #include <boost/mpl/if.hpp>
23 #include <type_traits>
24 
25 namespace serialbox {
26 
29 
34 public:
36  MetainfoValueImpl() : type_(TypeID::Invalid), any_(){};
37 
39  MetainfoValueImpl(const MetainfoValueImpl&) = default;
40 
43 
46 
49 
54  template <class ValueType, class DecayedValueType = typename std::decay<ValueType>::type,
55  class PrimitiveType = typename MakePrimitive<DecayedValueType>::type,
56  class = typename std::enable_if<
57  !std::is_same<DecayedValueType, MetainfoValueImpl>::value>::type>
58  explicit MetainfoValueImpl(ValueType&& value) {
59  static_assert(IsSupported<PrimitiveType>::value, "ValueType is not supported");
60 
62  any_ = boost::any(DecayedValueType(value));
63  }
64  explicit MetainfoValueImpl(const char* value) : MetainfoValueImpl(std::string(value)) {}
65 
75  template <class T>
76  T as() const {
77  static_assert(IsSupported<typename MakePrimitive<T>::type>::value,
78  "ValueType is not supported");
79  return T(); // Unreachable
80  }
81 
85  template <class T>
86  operator T() const {
87  return as<T>();
88  }
89 
91  void swap(MetainfoValueImpl& other) noexcept {
92  any_.swap(other.any_);
93  std::swap(type_, other.type_);
94  }
95 
97  bool operator==(const MetainfoValueImpl& right) const noexcept;
98 
100  bool operator!=(const MetainfoValueImpl& right) const noexcept { return (!(*this == right)); }
101 
103  TypeID type() const noexcept { return type_; }
104 
106  boost::any& any() noexcept { return any_; }
107  const boost::any& any() const noexcept { return any_; }
108 
110  std::string toString() const;
111 
112 private:
113  template <class T>
114  const T& convert() const noexcept {
115  return *boost::any_cast<T>(&any_);
116  }
117 
118 private:
119  TypeID type_;
120  boost::any any_;
121 };
122 
123 template <>
124 bool MetainfoValueImpl::as() const;
125 
126 template <>
127 int MetainfoValueImpl::as() const;
128 
129 template <>
130 std::int64_t MetainfoValueImpl::as() const;
131 
132 template <>
133 float MetainfoValueImpl::as() const;
134 
135 template <>
136 double MetainfoValueImpl::as() const;
137 
138 template <>
139 std::string MetainfoValueImpl::as() const;
140 
141 template <>
143 
144 template <>
146 
147 template <>
149 
150 template <>
152 
153 template <>
155 
156 template <>
158 
160 
161 } // namespace serialbox
162 
163 #endif
TypeID type() const noexcept
Get TypeID.
std::string toString() const
Convert to string.
bool operator==(const MetainfoValueImpl &right) const noexcept
Test for equality.
Represent an immutable meta information value as a type-id and type-erased data.
std::vector< T > Array
Array class used by serialbox to store meta-information.
Definition: Array.h:31
void swap(MetainfoValueImpl &other) noexcept
Swap with other.
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
bool operator!=(const MetainfoValueImpl &right) const noexcept
Test for inequality.
MetainfoValueImpl()
Default constructor.
MetainfoValueImpl & operator=(const MetainfoValueImpl &)=default
Copy assignment.
boost::any & any() noexcept
Get boost::any.
T as() const
Convert the value to type T
Convert C++ type T to TypeID.
Definition: Type.h:130
Check if the Type is recognized by serialbox i.e maps to a type-id in TypeID.
Definition: Type.h:88