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 
13 #include "msgpack/v1/adaptor/raw_decl.hpp"
14 
15 #include <cstring>
16 #include <string>
17 
18 namespace msgpack {
19 
20 /// @cond
MSGPACK_API_VERSION_NAMESPACE(v1)21 MSGPACK_API_VERSION_NAMESPACE(v1) {
22 /// @endcond
23 
24 namespace type {
25 
26 struct raw_ref {
27     raw_ref() : size(0), ptr(MSGPACK_NULLPTR) {}
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 && 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 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 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> {
64     msgpack::object const& operator()(msgpack::object const& o, type::raw_ref& v) const {
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>
75     msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::raw_ref& v) const {
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 {
85         o.type = msgpack::type::BIN;
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> {
93     void operator()(msgpack::object::with_zone& o, const type::raw_ref& v) const {
94         static_cast<msgpack::object&>(o) << v;
95     }
96 };
97 
98 } // namespace adaptor
99 
100 /// @cond
101 } // MSGPACK_API_VERSION_NAMESPACE(v1)
102 /// @endcond
103 
104 } // namespace msgpack
105 
106 #endif // MSGPACK_V1_TYPE_RAW_HPP
107