From 2103c12e053d5d1700ccbd4b308e3fbf9837ac55 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Mon, 1 Sep 2014 22:42:08 +0900 Subject: [PATCH] Added char* packing support. char* is packed to STR similar as std::string. See https://github.com/msgpack/msgpack-c/issues/110 --- CMakeLists.txt | 1 + include/msgpack/adaptor/char_ptr.hpp | 56 ++++++++++++++++++++++++++++ include/msgpack/type.hpp | 1 + src/Makefile.am | 1 + test/msgpack_test.cpp | 17 +++++++++ test/object_with_zone.cpp | 23 +++++++++++- 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 include/msgpack/adaptor/char_ptr.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dcee3f1e..89da4c2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ IF (MSGPACK_ENABLE_CXX) LIST (APPEND msgpack_HEADERS include/msgpack.hpp include/msgpack/adaptor/bool.hpp + include/msgpack/adaptor/char_ptr.hpp include/msgpack/adaptor/cpp11/array.hpp include/msgpack/adaptor/cpp11/array_char.hpp include/msgpack/adaptor/cpp11/forward_list.hpp diff --git a/include/msgpack/adaptor/char_ptr.hpp b/include/msgpack/adaptor/char_ptr.hpp new file mode 100644 index 00000000..2ef6c98b --- /dev/null +++ b/include/msgpack/adaptor/char_ptr.hpp @@ -0,0 +1,56 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2014 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_CHAR_PTR_HPP +#define MSGPACK_TYPE_CHAR_PTR_HPP + +#include "msgpack/object.hpp" +#include + +namespace msgpack { + +template +inline packer& operator<< (packer& o, const char* v) +{ + std::size_t size = std::strlen(v); + o.pack_str(size); + o.pack_str_body(v, size); + return o; +} + +inline void operator<< (object::with_zone& o, const char* v) +{ + std::size_t size = std::strlen(v); + o.type = type::STR; + char* ptr = static_cast(o.zone.allocate_align(size)); + o.via.str.ptr = ptr; + o.via.str.size = static_cast(size); + memcpy(ptr, v, size); +} + +inline void operator<< (object& o, const char* v) +{ + std::size_t size = std::strlen(v); + o.type = type::STR; + o.via.str.ptr = v; + o.via.str.size = static_cast(size); +} + + +} // namespace msgpack + +#endif // MSGPACK_TYPE_CHAR_PTR_HPP diff --git a/include/msgpack/type.hpp b/include/msgpack/type.hpp index e1ef05a8..ab10d9ed 100644 --- a/include/msgpack/type.hpp +++ b/include/msgpack/type.hpp @@ -1,5 +1,6 @@ #include "cpp_config.hpp" #include "adaptor/bool.hpp" +#include "adaptor/char_ptr.hpp" #include "adaptor/deque.hpp" #include "adaptor/fixint.hpp" #include "adaptor/float.hpp" diff --git a/src/Makefile.am b/src/Makefile.am index 37c5f776..1528d20e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -53,6 +53,7 @@ if ENABLE_CXX nobase_include_HEADERS += \ ../include/msgpack.hpp \ ../include/msgpack/adaptor/bool.hpp \ + ../include/msgpack/adaptor/char_ptr.hpp \ ../include/msgpack/adaptor/cpp11/array.hpp \ ../include/msgpack/adaptor/cpp11/array_char.hpp \ ../include/msgpack/adaptor/cpp11/forward_list.hpp \ diff --git a/test/msgpack_test.cpp b/test/msgpack_test.cpp index bef5f93a..dcbb883f 100644 --- a/test/msgpack_test.cpp +++ b/test/msgpack_test.cpp @@ -459,6 +459,23 @@ TEST(MSGPACK_STL, simple_buffer_string) } } +TEST(MSGPACK_STL, simple_buffer_cstring) +{ + for (unsigned int k = 0; k < kLoop; k++) { + string val1; + for (unsigned int i = 0; i < kElements; i++) + val1 += 'a' + rand() % 26; + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1.c_str()); + msgpack::unpacked ret; + msgpack::unpack(ret, sbuf.data(), sbuf.size()); + EXPECT_EQ(ret.get().type, msgpack::type::STR); + string val2 = ret.get().as(); + EXPECT_EQ(val1.size(), val2.size()); + EXPECT_EQ(val1, val2); + } +} + TEST(MSGPACK_STL, simple_buffer_vector) { for (unsigned int k = 0; k < kLoop; k++) { diff --git a/test/object_with_zone.cpp b/test/object_with_zone.cpp index 123e570c..cb49b8b3 100644 --- a/test/object_with_zone.cpp +++ b/test/object_with_zone.cpp @@ -259,6 +259,28 @@ TEST(object_without_zone, string) EXPECT_EQ(obj.as()[0], 'd'); } +// char* +TEST(object_with_zone, char_ptr) +{ + char v[] = "abc"; + msgpack::zone z; + msgpack::object obj(v, z); + EXPECT_EQ(obj.as(), std::string(v)); + v[0] = 'd'; + EXPECT_EQ(obj.as()[0], 'a'); +} + +TEST(object_without_zone, char_ptr) +{ + char v[] = "abc"; + msgpack::zone z; + msgpack::object obj(v); + EXPECT_EQ(obj.as(), std::string(v)); + v[0] = 'd'; + EXPECT_EQ(obj.as()[0], 'd'); +} + + // raw_ref TEST(object_with_zone, raw_ref) { @@ -659,4 +681,3 @@ TEST(object_with_zone, tuple) } #endif -