MessagePack for C++
optional.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2017 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_OPTIONAL_HPP
11 #define MSGPACK_V1_TYPE_OPTIONAL_HPP
12 
13 #include "msgpack/cpp_version.hpp"
14 
15 #if MSGPACK_CPP_VERSION >= 201703
16 
17 #include "msgpack/versioning.hpp"
20 
21 #include <optional>
22 
23 namespace msgpack {
24 
28 
29 namespace adaptor {
30 
31 #if !defined (MSGPACK_USE_CPP03)
32 
33 template <typename T>
34 struct as<std::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
35  std::optional<T> operator()(msgpack::object const& o) const {
36  if(o.is_nil()) return std::nullopt;
37  return o.as<T>();
38  }
39 };
40 
41 #endif // !defined (MSGPACK_USE_CPP03)
42 
43 template <typename T>
44 struct convert<std::optional<T> > {
45  msgpack::object const& operator()(msgpack::object const& o, std::optional<T>& v) const {
46  if(o.is_nil()) v = std::nullopt;
47  else {
48  T t;
50  v = t;
51  }
52  return o;
53  }
54 };
55 
56 template <typename T>
57 struct pack<std::optional<T> > {
58  template <typename Stream>
59  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::optional<T>& v) const {
60  if (v) o.pack(*v);
61  else o.pack_nil();
62  return o;
63  }
64 };
65 
66 template <typename T>
67 struct object<std::optional<T> > {
68  void operator()(msgpack::object& o, const std::optional<T>& v) const {
69  if (v) msgpack::adaptor::object<T>()(o, *v);
70  else o.type = msgpack::type::NIL;
71  }
72 };
73 
74 template <typename T>
75 struct object_with_zone<std::optional<T> > {
76  void operator()(msgpack::object::with_zone& o, const std::optional<T>& v) const {
78  else o.type = msgpack::type::NIL;
79  }
80 };
81 
82 } // namespace adaptor
83 
85 } // MSGPACK_API_VERSION_NAMESPACE(v1)
87 
88 } // namespace msgpack
89 
90 #endif // MSGPACK_CPP_VERSION >= 201703
91 
92 #endif // MSGPACK_V1_TYPE_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
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1180
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1173
Definition: adaptor_base.hpp:27
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:641
Definition: adaptor_base.hpp:43
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:657
Definition: adaptor_base.hpp:38
void operator()(msgpack::object &o, T const &v) const
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:650
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