10 #ifndef MSGPACK_V1_TYPE_BOOST_MSGPACK_VARIANT_HPP
11 #define MSGPACK_V1_TYPE_BOOST_MSGPACK_VARIANT_HPP
13 #if defined(MSGPACK_USE_BOOST)
31 #include <boost/variant.hpp>
32 #include <boost/operators.hpp>
43 template <
typename STR,
typename BIN,
typename EXT>
44 struct basic_variant :
52 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
54 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
59 boost::recursive_wrapper<std::vector<basic_variant<STR, BIN, EXT> > >,
60 boost::recursive_wrapper<std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >,
61 boost::recursive_wrapper<std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >
63 private boost::totally_ordered<basic_variant<STR, BIN, EXT> > {
64 typedef boost::variant<
71 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
73 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
78 boost::recursive_wrapper<std::vector<basic_variant<STR, BIN, EXT> > >,
79 boost::recursive_wrapper<std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >,
80 boost::recursive_wrapper<std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >
84 basic_variant(T
const& t):base(t) {}
86 #if defined(_MSC_VER) && _MSC_VER < 1700
89 basic_variant(basic_variant
const& other):base(static_cast<base const&>(other)) {}
90 basic_variant& operator=(basic_variant
const& other) {
91 *
static_cast<base*
>(
this) =
static_cast<base const&
>(other);
94 #endif // defined(_MSC_VER) && _MSC_VER < 1700
96 basic_variant(
char const* p):base(std::string(p)) {}
97 basic_variant(
char v) {
100 basic_variant(
signed char v) {
103 basic_variant(
unsigned char v):base(uint64_t(v)) {}
104 basic_variant(
signed int v) {
107 basic_variant(
unsigned int v):base(uint64_t(v)) {}
108 basic_variant(
signed long v) {
111 basic_variant(
unsigned long v):base(uint64_t(v)) {}
112 basic_variant(
signed long long v) {
115 basic_variant(
unsigned long long v):base(uint64_t(v)) {}
117 bool is_nil()
const {
120 bool is_bool()
const {
123 bool is_int64_t()
const {
126 bool is_uint64_t()
const {
129 bool is_double()
const {
132 bool is_string()
const {
135 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
136 bool is_boost_string_ref()
const {
139 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
140 bool is_vector_char()
const {
143 bool is_vector_char() {
146 bool is_raw_ref()
const {
149 bool is_ext()
const {
152 bool is_ext_ref()
const {
155 bool is_vector()
const {
156 return boost::get<std::vector<basic_variant<STR, BIN, EXT> > >(
this) !=
MSGPACK_NULLPTR;
158 bool is_map()
const {
159 return boost::get<std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(
this) !=
MSGPACK_NULLPTR;
161 bool is_multimap()
const {
162 return boost::get<std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(
this) !=
MSGPACK_NULLPTR;
165 bool as_bool()
const {
166 return boost::get<bool>(*
this);
168 int64_t as_int64_t()
const {
169 return boost::get<int64_t>(*
this);
171 int64_t& as_int64_t() {
172 return boost::get<int64_t>(*
this);
174 uint64_t as_uint64_t()
const {
175 return boost::get<uint64_t>(*
this);
177 uint64_t& as_uint64_t() {
178 return boost::get<uint64_t>(*
this);
180 double as_double()
const {
181 return boost::get<double>(*
this);
183 double& as_double() {
184 return boost::get<double>(*
this);
186 std::string
const& as_string()
const {
187 return boost::get<std::string>(*
this);
189 std::string& as_string() {
190 return boost::get<std::string>(*
this);
192 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
193 boost::string_ref
const& as_boost_string_ref()
const {
194 return boost::get<boost::string_ref>(*
this);
196 boost::string_ref& as_boost_string_ref() {
197 return boost::get<boost::string_ref>(*
this);
199 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
200 std::vector<char>
const& as_vector_char()
const {
201 return boost::get<std::vector<char> >(*this);
203 std::vector<char>& as_vector_char() {
204 return boost::get<std::vector<char> >(*this);
206 raw_ref
const& as_raw_ref()
const {
207 return boost::get<raw_ref>(*
this);
209 ext
const& as_ext()
const {
210 return boost::get<ext>(*
this);
213 return boost::get<ext>(*
this);
215 ext_ref
const& as_ext_ref()
const {
216 return boost::get<ext_ref>(*
this);
218 std::vector<basic_variant<STR, BIN, EXT> >
const& as_vector()
const {
219 return boost::get<std::vector<basic_variant<STR, BIN, EXT> > >(*this);
221 std::vector<basic_variant<STR, BIN, EXT> >& as_vector() {
222 return boost::get<std::vector<basic_variant<STR, BIN, EXT> > >(*this);
224 std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> >
const& as_map()
const {
225 return boost::get<std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(*this);
227 std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> >& as_map() {
228 return boost::get<std::map<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(*this);
230 std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> >
const& as_multimap()
const {
231 return boost::get<std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(*this);
233 std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> >& as_multimap() {
234 return boost::get<std::multimap<basic_variant<STR, BIN, EXT>, basic_variant<STR, BIN, EXT> > >(*this);
237 template <
typename T>
240 static_cast<base&
>(*this) = int64_t(v);
243 static_cast<base&
>(*this) = uint64_t(v);
248 template <
typename STR,
typename BIN,
typename EXT>
249 inline bool operator<(basic_variant<STR, BIN, EXT>
const& lhs, basic_variant<STR, BIN, EXT>
const& rhs) {
251 static_cast<typename basic_variant<STR, BIN, EXT>::base const&
>(lhs) <
252 static_cast<typename basic_variant<STR, BIN, EXT>::base const&
>(rhs);
255 template <
typename STR,
typename BIN,
typename EXT>
256 inline bool operator==(basic_variant<STR, BIN, EXT>
const& lhs, basic_variant<STR, BIN, EXT>
const& rhs) {
258 static_cast<typename basic_variant<STR, BIN, EXT>::base const&
>(lhs) ==
259 static_cast<typename basic_variant<STR, BIN, EXT>::base const&
>(rhs);
262 typedef basic_variant<std::string, std::vector<char>, ext> variant;
263 typedef basic_variant<
264 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
266 #else // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
268 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
269 raw_ref, ext_ref> variant_ref;
275 #if !defined (MSGPACK_USE_CPP03)
277 template <
typename STR,
typename BIN,
typename EXT>
278 struct as<type::basic_variant<STR, BIN, EXT> > {
279 type::basic_variant<STR, BIN, EXT> operator()(
msgpack::object const& o)
const {
286 return o.
as<uint64_t>();
288 return o.
as<int64_t>();
291 return o.
as<
double>();
299 return o.
as<std::vector<type::basic_variant<STR, BIN, EXT> > >();
301 return o.
as<std::multimap<type::basic_variant<STR, BIN, EXT>, type::basic_variant<STR, BIN, EXT> > >();
305 return type::basic_variant<STR, BIN, EXT>();
309 #endif // !defined (MSGPACK_USE_CPP03)
312 template <
typename STR,
typename BIN,
typename EXT>
313 struct convert<type::basic_variant<STR, BIN, EXT> > {
316 type::basic_variant<STR, BIN, EXT>& v)
const {
325 v = o.
as<uint64_t>();
344 v = o.
as<std::vector<type::basic_variant<STR, BIN, EXT> > >();
347 v = o.
as<std::multimap<type::basic_variant<STR, BIN, EXT>, type::basic_variant<STR, BIN, EXT> > >();
358 template <
typename Stream>
359 struct pack_imp : boost::static_visitor<void> {
360 template <
typename T>
361 void operator()(T
const& value)
const {
362 pack<T>()(o_, value);
364 pack_imp(packer<Stream>& o):o_(o) {}
370 template <
typename STR,
typename BIN,
typename EXT>
371 struct pack<type::basic_variant<STR, BIN, EXT> > {
372 template <
typename Stream>
374 boost::apply_visitor(detail::pack_imp<Stream>(o), v);
381 struct object_imp : boost::static_visitor<void> {
383 object<msgpack::type::nil_t>()(o_, v);
385 void operator()(
bool const& v)
const {
386 object<bool>()(o_, v);
388 void operator()(uint64_t
const& v)
const {
389 object<uint64_t>()(o_, v);
391 void operator()(int64_t
const& v)
const {
392 object<int64_t>()(o_, v);
394 void operator()(
double const& v)
const {
395 object<double>()(o_, v);
397 template <
typename T>
398 void operator()(T
const&)
const {
407 template <
typename STR,
typename BIN,
typename EXT>
408 struct object<type::basic_variant<STR, BIN, EXT> > {
410 boost::apply_visitor(detail::object_imp(o), v);
416 struct object_with_zone_imp : boost::static_visitor<void> {
417 template <
typename T>
418 void operator()(T
const& v)
const {
419 object_with_zone<T>()(o_, v);
427 template <
typename STR,
typename BIN,
typename EXT>
428 struct object_with_zone<type::basic_variant<STR, BIN, EXT> > {
430 boost::apply_visitor(detail::object_with_zone_imp(o), v);
442 #endif // MSGPACK_USE_BOOST
443 #endif // MSGPACK_V1_TYPE_BOOST_MSGPACK_VARIANT_HPP