Skip to content

Commit 9e7564c

Browse files
committed
Merge pull request #304 from redboltz/add_old_raw_pack_support
Added v4(old) raw packing support.
2 parents e0a2c2a + 34a4241 commit 9e7564c

File tree

11 files changed

+759
-2
lines changed

11 files changed

+759
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ IF (MSGPACK_ENABLE_CXX)
130130
include/msgpack/adaptor/nil.hpp
131131
include/msgpack/adaptor/pair.hpp
132132
include/msgpack/adaptor/raw.hpp
133+
include/msgpack/adaptor/v4raw.hpp
133134
include/msgpack/adaptor/set.hpp
134135
include/msgpack/adaptor/string.hpp
135136
include/msgpack/adaptor/tr1/unordered_map.hpp

include/msgpack/adaptor/v4raw.hpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#ifndef MSGPACK_TYPE_V4RAW_HPP
19+
#define MSGPACK_TYPE_V4RAW_HPP
20+
21+
#include "msgpack/versioning.hpp"
22+
#include "msgpack/adaptor/adaptor_base.hpp"
23+
#include <cstring>
24+
#include <string>
25+
26+
namespace msgpack {
27+
28+
/// @cond
29+
MSGPACK_API_VERSION_NAMESPACE(v1) {
30+
/// @endcond
31+
32+
namespace type {
33+
34+
struct v4raw_ref {
35+
v4raw_ref() : size(0), ptr(nullptr) {}
36+
v4raw_ref(const char* p, uint32_t s) : size(s), ptr(p) {}
37+
38+
uint32_t size;
39+
const char* ptr;
40+
41+
std::string str() const { return std::string(ptr, size); }
42+
43+
bool operator== (const v4raw_ref& x) const
44+
{
45+
return size == x.size && std::memcmp(ptr, x.ptr, size) == 0;
46+
}
47+
48+
bool operator!= (const v4raw_ref& x) const
49+
{
50+
return !(*this == x);
51+
}
52+
53+
bool operator< (const v4raw_ref& x) const
54+
{
55+
if(size == x.size) { return std::memcmp(ptr, x.ptr, size) < 0; }
56+
else { return size < x.size; }
57+
}
58+
59+
bool operator> (const v4raw_ref& x) const
60+
{
61+
if(size == x.size) { return std::memcmp(ptr, x.ptr, size) > 0; }
62+
else { return size > x.size; }
63+
}
64+
};
65+
66+
} // namespace type
67+
68+
namespace adaptor {
69+
70+
template <>
71+
struct convert<msgpack::type::v4raw_ref> {
72+
msgpack::object const& operator()(msgpack::object const& o, msgpack::type::v4raw_ref& v) const {
73+
if(o.type != msgpack::type::STR) { throw msgpack::type_error(); }
74+
v.ptr = o.via.str.ptr;
75+
v.size = o.via.str.size;
76+
return o;
77+
}
78+
};
79+
80+
template <>
81+
struct pack<msgpack::type::v4raw_ref> {
82+
template <typename Stream>
83+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::v4raw_ref& v) const {
84+
o.pack_v4raw(v.size);
85+
o.pack_v4raw_body(v.ptr, v.size);
86+
return o;
87+
}
88+
};
89+
90+
template <>
91+
struct object<msgpack::type::v4raw_ref> {
92+
void operator()(msgpack::object& o, const msgpack::type::v4raw_ref& v) const {
93+
o.type = msgpack::type::STR;
94+
o.via.str.ptr = v.ptr;
95+
o.via.str.size = v.size;
96+
}
97+
};
98+
99+
template <>
100+
struct object_with_zone<msgpack::type::v4raw_ref> {
101+
void operator()(msgpack::object::with_zone& o, const msgpack::type::v4raw_ref& v) const {
102+
static_cast<msgpack::object&>(o) << v;
103+
}
104+
};
105+
106+
} // namespace adaptor
107+
108+
/// @cond
109+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
110+
/// @endcond
111+
112+
} // namespace msgpack
113+
114+
#endif // MSGPACK_TYPE_V4RAW_HPP

include/msgpack/pack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ static int msgpack_pack_map(msgpack_packer* pk, size_t n);
9797
static int msgpack_pack_str(msgpack_packer* pk, size_t l);
9898
static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l);
9999

100+
static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l);
101+
static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l);
102+
100103
static int msgpack_pack_bin(msgpack_packer* pk, size_t l);
101104
static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l);
102105

@@ -150,4 +153,3 @@ inline void msgpack_packer_free(msgpack_packer* pk)
150153
#endif
151154

152155
#endif /* msgpack/pack.h */
153-

include/msgpack/pack.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class packer {
8888
packer<Stream>& pack_str(uint32_t l);
8989
packer<Stream>& pack_str_body(const char* b, uint32_t l);
9090

91+
// v4
92+
packer<Stream>& pack_v4raw(uint32_t l);
93+
packer<Stream>& pack_v4raw_body(const char* b, uint32_t l);
94+
9195
packer<Stream>& pack_bin(uint32_t l);
9296
packer<Stream>& pack_bin_body(const char* b, uint32_t l);
9397

@@ -713,6 +717,34 @@ inline packer<Stream>& packer<Stream>::pack_str_body(const char* b, uint32_t l)
713717
return *this;
714718
}
715719

720+
// Raw (V4)
721+
722+
template <typename Stream>
723+
inline packer<Stream>& packer<Stream>::pack_v4raw(uint32_t l)
724+
{
725+
if(l < 32) {
726+
unsigned char d = 0xa0u | static_cast<uint8_t>(l);
727+
char buf = take8_8(d);
728+
append_buffer(&buf, 1);
729+
} else if(l < 65536) {
730+
char buf[3];
731+
buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
732+
append_buffer(buf, 3);
733+
} else {
734+
char buf[5];
735+
buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
736+
append_buffer(buf, 5);
737+
}
738+
return *this;
739+
}
740+
741+
template <typename Stream>
742+
inline packer<Stream>& packer<Stream>::pack_v4raw_body(const char* b, uint32_t l)
743+
{
744+
append_buffer(b, l);
745+
return *this;
746+
}
747+
716748
template <typename Stream>
717749
inline packer<Stream>& packer<Stream>::pack_bin(uint32_t l)
718750
{

include/msgpack/pack_template.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,31 @@ msgpack_pack_inline_func(_str_body)(msgpack_pack_user x, const void* b, size_t l
779779
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
780780
}
781781

782+
/*
783+
* Raw (V4)
784+
*/
785+
786+
msgpack_pack_inline_func(_v4raw)(msgpack_pack_user x, size_t l)
787+
{
788+
if(l < 32) {
789+
unsigned char d = 0xa0 | (uint8_t)l;
790+
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
791+
} else if(l < 65536) {
792+
unsigned char buf[3];
793+
buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l);
794+
msgpack_pack_append_buffer(x, buf, 3);
795+
} else {
796+
unsigned char buf[5];
797+
buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l);
798+
msgpack_pack_append_buffer(x, buf, 5);
799+
}
800+
}
801+
802+
msgpack_pack_inline_func(_v4raw_body)(msgpack_pack_user x, const void* b, size_t l)
803+
{
804+
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
805+
}
806+
782807
/*
783808
* Bin
784809
*/
@@ -888,4 +913,3 @@ msgpack_pack_inline_func(_ext_body)(msgpack_pack_user x, const void* b, size_t l
888913
#undef msgpack_pack_real_int16
889914
#undef msgpack_pack_real_int32
890915
#undef msgpack_pack_real_int64
891-

include/msgpack/type.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "adaptor/nil.hpp"
1111
#include "adaptor/pair.hpp"
1212
#include "adaptor/raw.hpp"
13+
#include "adaptor/v4raw.hpp"
1314
#include "adaptor/set.hpp"
1415
#include "adaptor/string.hpp"
1516
#include "adaptor/vector.hpp"

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ nobase_include_HEADERS += \
8181
../include/msgpack/adaptor/nil.hpp \
8282
../include/msgpack/adaptor/pair.hpp \
8383
../include/msgpack/adaptor/raw.hpp \
84+
../include/msgpack/adaptor/v4raw.hpp \
8485
../include/msgpack/adaptor/set.hpp \
8586
../include/msgpack/adaptor/string.hpp \
8687
../include/msgpack/adaptor/tr1/unordered_map.hpp \

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LIST (APPEND check_PROGRAMS
3030
reference.cpp
3131
limit.cpp
3232
json.cpp
33+
raw.cpp
3334
)
3435

3536
IF (MSGPACK_BOOST)

test/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ check_PROGRAMS = \
2727
reference \
2828
limit \
2929
json \
30+
raw \
3031
iterator_cpp11 \
3132
boost_optional \
3233
boost_string_ref
@@ -82,6 +83,8 @@ limit_SOURCES = limit.cpp
8283

8384
json_SOURCES = json.cpp
8485

86+
raw_SOURCES = raw.cpp
87+
8588
iterator_cpp11_SOURCES = iterator_cpp11.cpp
8689

8790
boost_optional_SOURCES = boost_optional.cpp

0 commit comments

Comments
 (0)