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 #if defined(__GNUC__)
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wconversion"
20 #endif // defined(__GNUC__)
21 
22 // To suppress warning on Boost.1.58.0
23 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wunused-parameter"
26 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
27 
28 #include <boost/optional.hpp>
29 
30 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
31 #pragma GCC diagnostic pop
32 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
33 
34 #if defined(__GNUC__)
35 #pragma GCC diagnostic pop
36 #endif // defined(__GNUC__)
37 
38 namespace msgpack {
39 
43 
44 namespace adaptor {
45 
46 #if !defined (MSGPACK_USE_CPP03)
47 
48 template <typename T>
49 struct as<boost::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
50  boost::optional<T> operator()(msgpack::object const& o) const {
51  if(o.is_nil()) return boost::none;
52  return o.as<T>();
53  }
54 };
55 
56 #endif // !defined (MSGPACK_USE_CPP03)
57 
58 template <typename T>
59 struct convert<boost::optional<T> > {
60  msgpack::object const& operator()(msgpack::object const& o, boost::optional<T>& v) const {
61  if(o.is_nil()) v = boost::none;
62  else {
63  T t;
65  v = t;
66  }
67  return o;
68  }
69 };
70 
71 template <typename T>
72 struct pack<boost::optional<T> > {
73  template <typename Stream>
74  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const boost::optional<T>& v) const {
75  if (v) o.pack(*v);
76  else o.pack_nil();
77  return o;
78  }
79 };
80 
81 template <typename T>
82 struct object<boost::optional<T> > {
83  void operator()(msgpack::object& o, const boost::optional<T>& v) const {
84  if (v) msgpack::adaptor::object<T>()(o, *v);
85  else o.type = msgpack::type::NIL;
86  }
87 };
88 
89 template <typename T>
90 struct object_with_zone<boost::optional<T> > {
91  void operator()(msgpack::object::with_zone& o, const boost::optional<T>& v) const {
93  else o.type = msgpack::type::NIL;
94  }
95 };
96 
97 } // namespace adaptor
98 
100 } // MSGPACK_API_VERSION_NAMESPACE(v1)
102 
103 } // namespace msgpack
104 
105 #endif // MSGPACK_V1_TYPE_BOOST_OPTIONAL_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_nil()
Packing nil.
Definition: pack.hpp:1170
packer< Stream > & pack(const T &v)
Packing function template.
@ NIL
Definition: object_fwd_decl.hpp:28
Definition: adaptor_base.hpp:15
boost::optional< T > operator()(msgpack::object const &o) const
Definition: optional.hpp:50
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, boost::optional< T > &v) const
Definition: optional.hpp:60
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const boost::optional< T > &v) const
Definition: optional.hpp:83
void operator()(msgpack::object::with_zone &o, const boost::optional< T > &v) const
Definition: optional.hpp:91
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const boost::optional< T > &v) const
Definition: optional.hpp:74
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:1121
msgpack::type::object_type type
Definition: object_fwd.hpp:92
bool is_nil() const
Cheking nil.
Definition: object_fwd.hpp:99
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66