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 #if __cplusplus >= 201703
14 
15 #include "msgpack/versioning.hpp"
18 
19 #include <string_view>
20 
21 namespace msgpack {
22 
26 
27 namespace adaptor {
28 
29 template <>
30 struct convert<std::string_view> {
31  msgpack::object const& operator()(msgpack::object const& o, std::string_view& v) const {
32  switch (o.type) {
33  case msgpack::type::BIN:
34  v = std::string_view(o.via.bin.ptr, o.via.bin.size);
35  break;
36  case msgpack::type::STR:
37  v = std::string_view(o.via.str.ptr, o.via.str.size);
38  break;
39  default:
40  throw msgpack::type_error();
41  break;
42  }
43  return o;
44  }
45 };
46 
47 template <>
48 struct pack<std::string_view> {
49  template <typename Stream>
50  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::string_view& v) const {
51  uint32_t size = checked_get_container_size(v.size());
52  o.pack_str(size);
53  o.pack_str_body(v.data(), size);
54  return o;
55  }
56 };
57 
58 template <>
59 struct object<std::string_view> {
60  void operator()(msgpack::object& o, const std::string_view& v) const {
61  uint32_t size = checked_get_container_size(v.size());
63  o.via.str.ptr = v.data();
64  o.via.str.size = size;
65  }
66 };
67 
68 template <>
69 struct object_with_zone<std::string_view> {
70  void operator()(msgpack::object::with_zone& o, const std::string_view& v) const {
71  static_cast<msgpack::object&>(o) << v;
72  }
73 };
74 
75 
76 } // namespace adaptor
77 
79 } // MSGPACK_API_VERSION_NAMESPACE(v1)
81 
82 } // namespace msgpack
83 
84 #endif // __cplusplus >= 201703
85 
86 #endif // MSGPACK_V1_TYPE_STRING_VIEW_HPP
Definition: object_fwd_decl.hpp:39
const char * ptr
Definition: object_fwd.hpp:39
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
union_type via
Definition: object_fwd.hpp:93
Definition: object_fwd_decl.hpp:40
uint32_t size
Definition: object_fwd.hpp:38
Definition: adaptor_base.hpp:15
const char * ptr
Definition: object_fwd.hpp:34
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:661
Definition: object.hpp:34
packer< Stream > & pack_str_body(const char *b, uint32_t l)
Packing str body.
Definition: pack.hpp:1220
Definition: object_fwd.hpp:236
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:668
msgpack::object_str str
Definition: object_fwd.hpp:87
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
uint32_t size
Definition: object_fwd.hpp:33
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1197
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:203
msgpack::object_bin bin
Definition: object_fwd.hpp:88