MessagePack for C++
raw.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2009 FURUHASHI Sadayuki
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_RAW_HPP
11 #define MSGPACK_V1_TYPE_RAW_HPP
12 
14 
15 #include <cstring>
16 #include <string>
17 
18 namespace msgpack {
19 
23 
24 namespace type {
25 
26 struct raw_ref {
28  raw_ref(const char* p, uint32_t s) : size(s), ptr(p) {}
29 
30  uint32_t size;
31  const char* ptr;
32 
33  std::string str() const { return std::string(ptr, size); }
34 
35  bool operator== (const raw_ref& x) const
36  {
37  return size == x.size && (size == 0 || std::memcmp(ptr, x.ptr, size) == 0);
38  }
39 
40  bool operator!= (const raw_ref& x) const
41  {
42  return !(*this == x);
43  }
44 
45  bool operator< (const raw_ref& x) const
46  {
47  if(size == x.size) { return (size == 0 ? false : std::memcmp(ptr, x.ptr, size) < 0); }
48  else { return size < x.size; }
49  }
50 
51  bool operator> (const raw_ref& x) const
52  {
53  if(size == x.size) { return (size == 0 ? false : std::memcmp(ptr, x.ptr, size) > 0); }
54  else { return size > x.size; }
55  }
56 };
57 
58 } // namespace type
59 
60 namespace adaptor {
61 
62 template <>
63 struct convert<type::raw_ref> {
65  if(o.type != msgpack::type::BIN) { throw msgpack::type_error(); }
66  v.ptr = o.via.bin.ptr;
67  v.size = o.via.bin.size;
68  return o;
69  }
70 };
71 
72 template <>
73 struct pack<type::raw_ref> {
74  template <typename Stream>
76  o.pack_bin(v.size);
77  o.pack_bin_body(v.ptr, v.size);
78  return o;
79  }
80 };
81 
82 template <>
83 struct object<type::raw_ref> {
84  void operator()(msgpack::object& o, const type::raw_ref& v) const {
86  o.via.bin.ptr = v.ptr;
87  o.via.bin.size = v.size;
88  }
89 };
90 
91 template <>
92 struct object_with_zone<type::raw_ref> {
94  static_cast<msgpack::object&>(o) << v;
95  }
96 };
97 
98 } // namespace adaptor
99 
101 } // MSGPACK_API_VERSION_NAMESPACE(v1)
103 
104 } // namespace msgpack
105 
106 #endif // MSGPACK_V1_TYPE_RAW_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1290
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1309
Definition: object_fwd.hpp:231
@ BIN
Definition: object_fwd_decl.hpp:39
Definition: adaptor_base.hpp:15
msgpack::object const & operator()(msgpack::object const &o, type::raw_ref &v) const
Definition: raw.hpp:64
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const type::raw_ref &v) const
Definition: raw.hpp:84
void operator()(msgpack::object::with_zone &o, const type::raw_ref &v) const
Definition: raw.hpp:93
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const type::raw_ref &v) const
Definition: raw.hpp:75
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
uint32_t size
Definition: object_fwd.hpp:38
const char * ptr
Definition: object_fwd.hpp:39
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
Definition: raw.hpp:26
std::string str() const
Definition: raw.hpp:33
raw_ref()
Definition: raw.hpp:27
uint32_t size
Definition: raw.hpp:30
bool operator==(const raw_ref &x) const
Definition: raw.hpp:35
bool operator>(const raw_ref &x) const
Definition: raw.hpp:51
const char * ptr
Definition: raw.hpp:31
raw_ref(const char *p, uint32_t s)
Definition: raw.hpp:28
bool operator<(const raw_ref &x) const
Definition: raw.hpp:45
bool operator!=(const raw_ref &x) const
Definition: raw.hpp:40
msgpack::object_bin bin
Definition: object_fwd.hpp:88
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66