MessagePack for C++
string_view.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_BOOST_STRING_VIEW_HPP
11 #define MSGPACK_V1_TYPE_BOOST_STRING_VIEW_HPP
12 
13 #include <boost/version.hpp>
14 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 61
15 
16 #include "msgpack/versioning.hpp"
19 
20 #include <boost/utility/string_view.hpp>
21 
22 namespace msgpack {
23 
27 
28 namespace adaptor {
29 
30 template <>
31 struct convert<boost::string_view> {
32  msgpack::object const& operator()(msgpack::object const& o, boost::string_view& v) const {
33  switch (o.type) {
34  case msgpack::type::BIN:
35  v = boost::string_view(o.via.bin.ptr, o.via.bin.size);
36  break;
37  case msgpack::type::STR:
38  v = boost::string_view(o.via.str.ptr, o.via.str.size);
39  break;
40  default:
41  throw msgpack::type_error();
42  break;
43  }
44  return o;
45  }
46 };
47 
48 template <>
49 struct pack<boost::string_view> {
50  template <typename Stream>
51  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const boost::string_view& v) const {
52  uint32_t size = checked_get_container_size(v.size());
53  o.pack_str(size);
54  o.pack_str_body(v.data(), size);
55  return o;
56  }
57 };
58 
59 template <>
60 struct object<boost::string_view> {
61  void operator()(msgpack::object& o, const boost::string_view& v) const {
62  uint32_t size = checked_get_container_size(v.size());
64  o.via.str.ptr = v.data();
65  o.via.str.size = size;
66  }
67 };
68 
69 template <>
70 struct object_with_zone<boost::string_view> {
71  void operator()(msgpack::object::with_zone& o, const boost::string_view& v) const {
72  static_cast<msgpack::object&>(o) << v;
73  }
74 };
75 
76 
77 } // namespace adaptor
78 
80 } // MSGPACK_API_VERSION_NAMESPACE(v1)
82 
83 } // namespace msgpack
84 
85 #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
86 
87 #endif // MSGPACK_V1_TYPE_BOOST_STRING_VIEW_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_str_body(const char *b, uint32_t l)
Packing str body.
Definition: pack.hpp:1255
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1232
Definition: object_fwd.hpp:231
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
@ STR
Definition: object_fwd_decl.hpp:38
@ BIN
Definition: object_fwd_decl.hpp:39
Definition: adaptor_base.hpp:15
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1180
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1173
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:641
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:657
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
uint32_t size
Definition: object_fwd.hpp:38
const char * ptr
Definition: object_fwd.hpp:39
const char * ptr
Definition: object_fwd.hpp:34
uint32_t size
Definition: object_fwd.hpp:33
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
union_type via
Definition: object_fwd.hpp:93
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::object_bin bin
Definition: object_fwd.hpp:88
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66