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_STRING_VIEW_HPP
11 #define MSGPACK_V1_TYPE_STRING_VIEW_HPP
12 
13 #include "msgpack/cpp_version.hpp"
14 
15 #if MSGPACK_CPP_VERSION >= 201703
16 
17 #include "msgpack/versioning.hpp"
19 #include "msgpack/object.hpp"
21 
22 #include <string_view>
23 
24 namespace msgpack {
25 
29 
30 namespace adaptor {
31 
32 template <>
33 struct convert<std::string_view> {
34  msgpack::object const& operator()(msgpack::object const& o, std::string_view& v) const {
35  switch (o.type) {
36  case msgpack::type::BIN:
37  v = std::string_view(o.via.bin.ptr, o.via.bin.size);
38  break;
39  case msgpack::type::STR:
40  v = std::string_view(o.via.str.ptr, o.via.str.size);
41  break;
42  default:
43  throw msgpack::type_error();
44  break;
45  }
46  return o;
47  }
48 };
49 
50 template <>
51 struct pack<std::string_view> {
52  template <typename Stream>
53  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::string_view& v) const {
54  uint32_t size = checked_get_container_size(v.size());
55  o.pack_str(size);
56  o.pack_str_body(v.data(), size);
57  return o;
58  }
59 };
60 
61 template <>
62 struct object<std::string_view> {
63  void operator()(msgpack::object& o, const std::string_view& v) const {
64  uint32_t size = checked_get_container_size(v.size());
66  o.via.str.ptr = v.data();
67  o.via.str.size = size;
68  }
69 };
70 
71 template <>
72 struct object_with_zone<std::string_view> {
73  void operator()(msgpack::object::with_zone& o, const std::string_view& v) const {
74  static_cast<msgpack::object&>(o) << v;
75  }
76 };
77 
78 
79 } // namespace adaptor
80 
82 } // MSGPACK_API_VERSION_NAMESPACE(v1)
84 
85 } // namespace msgpack
86 
87 #endif // MSGPACK_CPP_VERSION >= 201703
88 
89 #endif // MSGPACK_V1_TYPE_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:1277
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1254
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:1185
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:1178
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:646
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:662
void operator()(msgpack::object &o, T const &v) const
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:655
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