Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added v4(old) raw packing support. #304

Merged
merged 1 commit into from
Jul 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/nil.hpp
include/msgpack/adaptor/pair.hpp
include/msgpack/adaptor/raw.hpp
include/msgpack/adaptor/v4raw.hpp
include/msgpack/adaptor/set.hpp
include/msgpack/adaptor/string.hpp
include/msgpack/adaptor/tr1/unordered_map.hpp
Expand Down
114 changes: 114 additions & 0 deletions include/msgpack/adaptor/v4raw.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef MSGPACK_TYPE_V4RAW_HPP
#define MSGPACK_TYPE_V4RAW_HPP

#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include <cstring>
#include <string>

namespace msgpack {

/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond

namespace type {

struct v4raw_ref {
v4raw_ref() : size(0), ptr(nullptr) {}
v4raw_ref(const char* p, uint32_t s) : size(s), ptr(p) {}

uint32_t size;
const char* ptr;

std::string str() const { return std::string(ptr, size); }

bool operator== (const v4raw_ref& x) const
{
return size == x.size && std::memcmp(ptr, x.ptr, size) == 0;
}

bool operator!= (const v4raw_ref& x) const
{
return !(*this == x);
}

bool operator< (const v4raw_ref& x) const
{
if(size == x.size) { return std::memcmp(ptr, x.ptr, size) < 0; }
else { return size < x.size; }
}

bool operator> (const v4raw_ref& x) const
{
if(size == x.size) { return std::memcmp(ptr, x.ptr, size) > 0; }
else { return size > x.size; }
}
};

} // namespace type

namespace adaptor {

template <>
struct convert<msgpack::type::v4raw_ref> {
msgpack::object const& operator()(msgpack::object const& o, msgpack::type::v4raw_ref& v) const {
if(o.type != msgpack::type::STR) { throw msgpack::type_error(); }
v.ptr = o.via.str.ptr;
v.size = o.via.str.size;
return o;
}
};

template <>
struct pack<msgpack::type::v4raw_ref> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::v4raw_ref& v) const {
o.pack_v4raw(v.size);
o.pack_v4raw_body(v.ptr, v.size);
return o;
}
};

template <>
struct object<msgpack::type::v4raw_ref> {
void operator()(msgpack::object& o, const msgpack::type::v4raw_ref& v) const {
o.type = msgpack::type::STR;
o.via.str.ptr = v.ptr;
o.via.str.size = v.size;
}
};

template <>
struct object_with_zone<msgpack::type::v4raw_ref> {
void operator()(msgpack::object::with_zone& o, const msgpack::type::v4raw_ref& v) const {
static_cast<msgpack::object&>(o) << v;
}
};

} // namespace adaptor

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond

} // namespace msgpack

#endif // MSGPACK_TYPE_V4RAW_HPP
4 changes: 3 additions & 1 deletion include/msgpack/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static int msgpack_pack_map(msgpack_packer* pk, size_t n);
static int msgpack_pack_str(msgpack_packer* pk, size_t l);
static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l);

static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l);
static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l);

static int msgpack_pack_bin(msgpack_packer* pk, size_t l);
static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l);

Expand Down Expand Up @@ -150,4 +153,3 @@ inline void msgpack_packer_free(msgpack_packer* pk)
#endif

#endif /* msgpack/pack.h */

32 changes: 32 additions & 0 deletions include/msgpack/pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class packer {
packer<Stream>& pack_str(uint32_t l);
packer<Stream>& pack_str_body(const char* b, uint32_t l);

// v4
packer<Stream>& pack_v4raw(uint32_t l);
packer<Stream>& pack_v4raw_body(const char* b, uint32_t l);

packer<Stream>& pack_bin(uint32_t l);
packer<Stream>& pack_bin_body(const char* b, uint32_t l);

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

// Raw (V4)

template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_v4raw(uint32_t l)
{
if(l < 32) {
unsigned char d = 0xa0u | static_cast<uint8_t>(l);
char buf = take8_8(d);
append_buffer(&buf, 1);
} else if(l < 65536) {
char buf[3];
buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
append_buffer(buf, 3);
} else {
char buf[5];
buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
append_buffer(buf, 5);
}
return *this;
}

template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_v4raw_body(const char* b, uint32_t l)
{
append_buffer(b, l);
return *this;
}

template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_bin(uint32_t l)
{
Expand Down
26 changes: 25 additions & 1 deletion include/msgpack/pack_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,31 @@ msgpack_pack_inline_func(_str_body)(msgpack_pack_user x, const void* b, size_t l
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
}

/*
* Raw (V4)
*/

msgpack_pack_inline_func(_v4raw)(msgpack_pack_user x, size_t l)
{
if(l < 32) {
unsigned char d = 0xa0 | (uint8_t)l;
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
} else if(l < 65536) {
unsigned char buf[3];
buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l);
msgpack_pack_append_buffer(x, buf, 3);
} else {
unsigned char buf[5];
buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l);
msgpack_pack_append_buffer(x, buf, 5);
}
}

msgpack_pack_inline_func(_v4raw_body)(msgpack_pack_user x, const void* b, size_t l)
{
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
}

/*
* Bin
*/
Expand Down Expand Up @@ -888,4 +913,3 @@ msgpack_pack_inline_func(_ext_body)(msgpack_pack_user x, const void* b, size_t l
#undef msgpack_pack_real_int16
#undef msgpack_pack_real_int32
#undef msgpack_pack_real_int64

1 change: 1 addition & 0 deletions include/msgpack/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "adaptor/nil.hpp"
#include "adaptor/pair.hpp"
#include "adaptor/raw.hpp"
#include "adaptor/v4raw.hpp"
#include "adaptor/set.hpp"
#include "adaptor/string.hpp"
#include "adaptor/vector.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ nobase_include_HEADERS += \
../include/msgpack/adaptor/nil.hpp \
../include/msgpack/adaptor/pair.hpp \
../include/msgpack/adaptor/raw.hpp \
../include/msgpack/adaptor/v4raw.hpp \
../include/msgpack/adaptor/set.hpp \
../include/msgpack/adaptor/string.hpp \
../include/msgpack/adaptor/tr1/unordered_map.hpp \
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LIST (APPEND check_PROGRAMS
reference.cpp
limit.cpp
json.cpp
raw.cpp
)

IF (MSGPACK_BOOST)
Expand Down
3 changes: 3 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ check_PROGRAMS = \
reference \
limit \
json \
raw \
iterator_cpp11 \
boost_optional \
boost_string_ref
Expand Down Expand Up @@ -82,6 +83,8 @@ limit_SOURCES = limit.cpp

json_SOURCES = json.cpp

raw_SOURCES = raw.cpp

iterator_cpp11_SOURCES = iterator_cpp11.cpp

boost_optional_SOURCES = boost_optional.cpp
Expand Down