MessagePack for C++
optional.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2015 KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_V1_TYPE_BOOST_OPTIONAL_HPP
11 #define MSGPACK_V1_TYPE_BOOST_OPTIONAL_HPP
12 
13 #include "msgpack/versioning.hpp"
16 
17 // To suppress warning on Boost.1.58.0
18 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wunused-parameter"
21 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
22 
23 #include <boost/optional.hpp>
24 
25 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
26 #pragma GCC diagnostic pop
27 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
28 
29 namespace msgpack {
30 
34 
35 namespace adaptor {
36 
37 #if !defined (MSGPACK_USE_CPP03)
38 
39 template <typename T>
40 struct as<boost::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
41  boost::optional<T> operator()(msgpack::object const& o) const {
42  if(o.is_nil()) return boost::none;
43  return o.as<T>();
44  }
45 };
46 
47 #endif // !defined (MSGPACK_USE_CPP03)
48 
49 template <typename T>
50 struct convert<boost::optional<T> > {
51  msgpack::object const& operator()(msgpack::object const& o, boost::optional<T>& v) const {
52  if(o.is_nil()) v = boost::none;
53  else {
54  T t;
56  v = t;
57  }
58  return o;
59  }
60 };
61 
62 template <typename T>
63 struct pack<boost::optional<T> > {
64  template <typename Stream>
65  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const boost::optional<T>& v) const {
66  if (v) o.pack(*v);
67  else o.pack_nil();
68  return o;
69  }
70 };
71 
72 template <typename T>
73 struct object<boost::optional<T> > {
74  void operator()(msgpack::object& o, const boost::optional<T>& v) const {
75  if (v) msgpack::adaptor::object<T>()(o, *v);
76  else o.type = msgpack::type::NIL;
77  }
78 };
79 
80 template <typename T>
81 struct object_with_zone<boost::optional<T> > {
82  void operator()(msgpack::object::with_zone& o, const boost::optional<T>& v) const {
84  else o.type = msgpack::type::NIL;
85  }
86 };
87 
88 } // namespace adaptor
89 
91 } // MSGPACK_API_VERSION_NAMESPACE(v1)
93 
94 } // namespace msgpack
95 
96 #endif // MSGPACK_V1_TYPE_BOOST_OPTIONAL_HPP
bool is_nil() const
Cheking nil.
Definition: object_fwd.hpp:98
boost::optional< T > operator()(msgpack::object const &o) const
Definition: optional.hpp:41
void operator()(msgpack::object::with_zone &o, const boost::optional< T > &v) const
Definition: optional.hpp:82
Definition: object_fwd_decl.hpp:60
Definition: adaptor_base.hpp:15
Definition: object.hpp:34
packer< Stream > & pack(const T &v)
Packing function template.
Definition: object_fwd_decl.hpp:29
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:584
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:32
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const boost::optional< T > &v) const
Definition: optional.hpp:65
msgpack::type::object_type type
Definition: object_fwd.hpp:91
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: adaptor_base.hpp:38
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::object const & operator()(msgpack::object const &o, boost::optional< T > &v) const
Definition: optional.hpp:51
packer< Stream > & pack_nil()
Packing nil.
Definition: pack.hpp:1135
void operator()(msgpack::object &o, const boost::optional< T > &v) const
Definition: optional.hpp:74
Definition: adaptor_base.hpp:27