|
| 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" |
| 16 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 17 | +#include "msgpack/adaptor/check_container_size.hpp" |
| 18 | + |
| 19 | +#include <optional> |
| 20 | + |
| 21 | +namespace msgpack { |
| 22 | + |
| 23 | +/// @cond |
| 24 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 25 | +/// @endcond |
| 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; |
| 47 | + msgpack::adaptor::convert<T>()(o, 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 { |
| 75 | + if (v) msgpack::adaptor::object_with_zone<T>()(o, *v); |
| 76 | + else o.type = msgpack::type::NIL; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +} // namespace adaptor |
| 81 | + |
| 82 | +/// @cond |
| 83 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 84 | +/// @endcond |
| 85 | + |
| 86 | +} // namespace msgpack |
| 87 | + |
| 88 | +#endif // __cplusplus >= 201703 |
| 89 | + |
| 90 | +#endif // MSGPACK_V1_TYPE_OPTIONAL_HPP |
0 commit comments