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"
14 #include "msgpack/adaptor/adaptor_base.hpp"
15 #include "msgpack/adaptor/check_container_size.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 
31 /// @cond
MSGPACK_API_VERSION_NAMESPACE(v1)32 MSGPACK_API_VERSION_NAMESPACE(v1) {
33 /// @endcond
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;
55             msgpack::adaptor::convert<T>()(o, 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 {
83         if (v) msgpack::adaptor::object_with_zone<T>()(o, *v);
84         else o.type = msgpack::type::NIL;
85     }
86 };
87 
88 } // namespace adaptor
89 
90 /// @cond
91 } // MSGPACK_API_VERSION_NAMESPACE(v1)
92 /// @endcond
93 
94 } // namespace msgpack
95 
96 #endif // MSGPACK_V1_TYPE_BOOST_OPTIONAL_HPP
97