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 #if __cplusplus >= 201703
14 
15 #include "msgpack/versioning.hpp"
18 
19 #include <optional>
20 
21 namespace msgpack {
22 
26 
27 namespace adaptor {
28 
29 #if !defined (MSGPACK_USE_CPP03)
30 
31 template <typename T>
32 struct as<std::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
33  std::optional<T> operator()(msgpack::object const& o) const {
34  if(o.is_nil()) return std::nullopt;
35  return o.as<T>();
36  }
37 };
38 
39 #endif // !defined (MSGPACK_USE_CPP03)
40 
41 template <typename T>
42 struct convert<std::optional<T> > {
43  msgpack::object const& operator()(msgpack::object const& o, std::optional<T>& v) const {
44  if(o.is_nil()) v = std::nullopt;
45  else {
46  T t;
48  v = t;
49  }
50  return o;
51  }
52 };
53 
54 template <typename T>
55 struct pack<std::optional<T> > {
56  template <typename Stream>
57  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::optional<T>& v) const {
58  if (v) o.pack(*v);
59  else o.pack_nil();
60  return o;
61  }
62 };
63 
64 template <typename T>
65 struct object<std::optional<T> > {
66  void operator()(msgpack::object& o, const std::optional<T>& v) const {
67  if (v) msgpack::adaptor::object<T>()(o, *v);
68  else o.type = msgpack::type::NIL;
69  }
70 };
71 
72 template <typename T>
73 struct object_with_zone<std::optional<T> > {
74  void operator()(msgpack::object::with_zone& o, const std::optional<T>& v) const {
76  else o.type = msgpack::type::NIL;
77  }
78 };
79 
80 } // namespace adaptor
81 
83 } // MSGPACK_API_VERSION_NAMESPACE(v1)
85 
86 } // namespace msgpack
87 
88 #endif // __cplusplus >= 201703
89 
90 #endif // MSGPACK_V1_TYPE_OPTIONAL_HPP
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:588
Definition: adaptor_base.hpp:15
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:661
Definition: object.hpp:34
packer< Stream > & pack(const T &v)
Packing function template.
bool is_nil() const
Cheking nil.
Definition: object_fwd.hpp:99
Definition: object_fwd_decl.hpp:29
Definition: adaptor_base.hpp:43
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:668
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::type::object_type type
Definition: object_fwd.hpp:92
#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
packer< Stream > & pack_nil()
Packing nil.
Definition: pack.hpp:1135
Definition: adaptor_base.hpp:27