mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 15:05:37 +02:00
Added char* packing support. char* is packed to STR similar as std::string.
See https://github.com/msgpack/msgpack-c/issues/110
This commit is contained in:
@@ -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
|
||||
|
56
include/msgpack/adaptor/char_ptr.hpp
Normal file
56
include/msgpack/adaptor/char_ptr.hpp
Normal file
@@ -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 <cstring>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& 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<char*>(o.zone.allocate_align(size));
|
||||
o.via.str.ptr = ptr;
|
||||
o.via.str.size = static_cast<uint32_t>(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<uint32_t>(size);
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // MSGPACK_TYPE_CHAR_PTR_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"
|
||||
|
@@ -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 \
|
||||
|
@@ -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<string>();
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_vector)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
|
@@ -259,6 +259,28 @@ TEST(object_without_zone, string)
|
||||
EXPECT_EQ(obj.as<string>()[0], 'd');
|
||||
}
|
||||
|
||||
// char*
|
||||
TEST(object_with_zone, char_ptr)
|
||||
{
|
||||
char v[] = "abc";
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(v, z);
|
||||
EXPECT_EQ(obj.as<string>(), std::string(v));
|
||||
v[0] = 'd';
|
||||
EXPECT_EQ(obj.as<string>()[0], 'a');
|
||||
}
|
||||
|
||||
TEST(object_without_zone, char_ptr)
|
||||
{
|
||||
char v[] = "abc";
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(v);
|
||||
EXPECT_EQ(obj.as<string>(), std::string(v));
|
||||
v[0] = 'd';
|
||||
EXPECT_EQ(obj.as<string>()[0], 'd');
|
||||
}
|
||||
|
||||
|
||||
// raw_ref
|
||||
TEST(object_with_zone, raw_ref)
|
||||
{
|
||||
@@ -659,4 +681,3 @@ TEST(object_with_zone, tuple)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user