MessagePack for C++
shared_ptr.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 
11 #ifndef MSGPACK_V1_TYPE_CPP11_SHARED_PTR_HPP
12 #define MSGPACK_V1_TYPE_CPP11_SHARED_PTR_HPP
13 
14 #include "msgpack/versioning.hpp"
17 
18 #include <memory>
19 
20 namespace msgpack {
21 
25 
26 namespace adaptor {
27 
28 template <typename T>
29 struct as<std::shared_ptr<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
30  std::shared_ptr<T> operator()(msgpack::object const& o) const {
31  if(o.is_nil()) return MSGPACK_NULLPTR;
32  return std::make_shared<T>(o.as<T>());
33  }
34 };
35 
36 template <typename T>
37 struct convert<std::shared_ptr<T>> {
38  msgpack::object const& operator()(msgpack::object const& o, std::shared_ptr<T>& v) const {
39  if(o.is_nil()) v.reset();
40  else {
41  v = std::make_shared<T>();
43  }
44  return o;
45  }
46 };
47 
48 template <typename T>
49 struct pack<std::shared_ptr<T>> {
50  template <typename Stream>
51  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::shared_ptr<T>& v) const {
52  if (v) o.pack(*v);
53  else o.pack_nil();
54  return o;
55  }
56 };
57 
58 template <typename T>
59 struct object<std::shared_ptr<T> > {
60  void operator()(msgpack::object& o, const std::shared_ptr<T>& v) const {
61  if (v) msgpack::adaptor::object<T>()(o, *v);
62  else o.type = msgpack::type::NIL;
63  }
64 };
65 
66 template <typename T>
67 struct object_with_zone<std::shared_ptr<T>> {
68  void operator()(msgpack::object::with_zone& o, const std::shared_ptr<T>& v) const {
70  else o.type = msgpack::type::NIL;
71  }
72 };
73 
74 } // namespace adaptor
75 
77 } // MSGPACK_API_VERSION_NAMESPACE(v1)
79 
80 } // namespace msgpack
81 
82 #endif // MSGPACK_V1_TYPE_CPP11_SHARED_PTR_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
std::shared_ptr< T > operator()(msgpack::object const &o) const
Definition: shared_ptr.hpp:30
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::shared_ptr< T > &v) const
Definition: shared_ptr.hpp:38
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const std::shared_ptr< T > &v) const
Definition: shared_ptr.hpp:60
void operator()(msgpack::object::with_zone &o, const std::shared_ptr< T > &v) const
Definition: shared_ptr.hpp:68
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::shared_ptr< T > &v) const
Definition: shared_ptr.hpp:51
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_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66