mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-02 15:41:38 +02:00
commit
7d1be40e10
@ -22,6 +22,7 @@ OPTION (MSGPACK_32BIT "32bit compile" OFF)
|
|||||||
OPTION (MSGPACK_BOOST "Using boost libraries" OFF)
|
OPTION (MSGPACK_BOOST "Using boost libraries" OFF)
|
||||||
|
|
||||||
SET (CMAKE_CXX_FLAGS "-DMSGPACK_DISABLE_LEGACY_NIL ${CMAKE_CXX_FLAGS}")
|
SET (CMAKE_CXX_FLAGS "-DMSGPACK_DISABLE_LEGACY_NIL ${CMAKE_CXX_FLAGS}")
|
||||||
|
SET (CMAKE_CXX_FLAGS "-DMSGPACK_DISABLE_LEGACY_CONVERT ${CMAKE_CXX_FLAGS}")
|
||||||
|
|
||||||
IF (APPLE)
|
IF (APPLE)
|
||||||
SET(CMAKE_MACOSX_RPATH ON)
|
SET(CMAKE_MACOSX_RPATH ON)
|
||||||
|
@ -40,19 +40,19 @@ int main(void) {
|
|||||||
|
|
||||||
// convert it into statically typed object.
|
// convert it into statically typed object.
|
||||||
std::vector<std::string> rvec;
|
std::vector<std::string> rvec;
|
||||||
obj.convert(&rvec);
|
obj.convert(rvec);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Compile it as follows:
|
Compile it as follows:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ g++ -Ipath_to_msgpack/include -DMSGPACK_DISABLE_LEGACY_NIL hello.cc -o hello
|
$ g++ -Ipath_to_msgpack/include -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT hello.cc -o hello
|
||||||
$ ./hello
|
$ ./hello
|
||||||
["Hello", "MessagePack"]
|
["Hello", "MessagePack"]
|
||||||
```
|
```
|
||||||
|
|
||||||
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140).
|
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140) and [MSGPACK_DISABLE_LEGACY_CONVERT](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_convert-since-140).
|
||||||
|
|
||||||
## Streaming feature
|
## Streaming feature
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// results:
|
// results:
|
||||||
// $ g++ -Ipath_to_msgpack/include -DMSGPACK_DISABLE_LEGACY_NIL stream.cc -o stream
|
// $ g++ -Ipath_to_msgpack/include -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT stream.cc -o stream
|
||||||
// $ ./stream
|
// $ ./stream
|
||||||
// "Log message ... 1"
|
// "Log message ... 1"
|
||||||
// "Log message ... 2"
|
// "Log message ... 2"
|
||||||
@ -93,7 +93,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140).
|
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140) and [MSGPACK_DISABLE_LEGACY_CONVERT](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_convert-since-140).
|
||||||
|
|
||||||
### Streaming into an array or map
|
### Streaming into an array or map
|
||||||
|
|
||||||
@ -158,6 +158,6 @@ int main(void) {
|
|||||||
|
|
||||||
// you can convert object to myclass directly
|
// you can convert object to myclass directly
|
||||||
std::vector<myclass> rvec;
|
std::vector<myclass> rvec;
|
||||||
obj.convert(&rvec);
|
obj.convert(rvec);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -96,7 +96,7 @@ int main(void)
|
|||||||
// convert msgpack::object instance into the original type.
|
// convert msgpack::object instance into the original type.
|
||||||
// if the type is mismatched, it throws msgpack::type_error exception.
|
// if the type is mismatched, it throws msgpack::type_error exception.
|
||||||
msgpack::type::tuple<int, bool, std::string> dst;
|
msgpack::type::tuple<int, bool, std::string> dst;
|
||||||
deserialized.convert(&dst);
|
deserialized.convert(dst);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -112,9 +112,9 @@ Usage
|
|||||||
When you use msgpack on C++03 and C++11, you can just add
|
When you use msgpack on C++03 and C++11, you can just add
|
||||||
msgpack-c/include to your include path:
|
msgpack-c/include to your include path:
|
||||||
|
|
||||||
g++ -I msgpack-c/include -DMSGPACK_DISABLE_LEGACY_NIL your_source_file.cpp
|
g++ -I msgpack-c/include -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT your_source_file.cpp
|
||||||
|
|
||||||
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140).
|
See [MSGPACK_DISABLE_LEGACY_NIL](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_nil-since-140) and [MSGPACK_DISABLE_LEGACY_CONVERT](https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_disable_legacy_convert-since-140).
|
||||||
|
|
||||||
If you want to use C version of msgpack, you need to build it. You can
|
If you want to use C version of msgpack, you need to build it. You can
|
||||||
also install the C and C++ versions of msgpack.
|
also install the C and C++ versions of msgpack.
|
||||||
|
@ -44,7 +44,7 @@ int main(void)
|
|||||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
||||||
msgpack::object obj = result.get();
|
msgpack::object obj = result.get();
|
||||||
|
|
||||||
obj.convert(&nc);
|
obj.convert(nc);
|
||||||
|
|
||||||
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
|
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ int main(void)
|
|||||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
||||||
msgpack::object obj = result.get();
|
msgpack::object obj = result.get();
|
||||||
|
|
||||||
obj.convert(&oc);
|
obj.convert(oc);
|
||||||
|
|
||||||
std::cout << obj << " value=" << oc.value << std::endl;
|
std::cout << obj << " value=" << oc.value << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ int main(void)
|
|||||||
// convert msgpack::object instance into the original type.
|
// convert msgpack::object instance into the original type.
|
||||||
// if the type is mismatched, it throws msgpack::type_error exception.
|
// if the type is mismatched, it throws msgpack::type_error exception.
|
||||||
msgpack::type::tuple<int, bool, std::string> dst;
|
msgpack::type::tuple<int, bool, std::string> dst;
|
||||||
deserialized.convert(&dst);
|
deserialized.convert(dst);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ void test_map_pack_unpack() {
|
|||||||
std::cout << "Start converting..." << std::endl;
|
std::cout << "Start converting..." << std::endl;
|
||||||
{
|
{
|
||||||
boost::timer::cpu_timer timer;
|
boost::timer::cpu_timer timer;
|
||||||
unpacked.get().convert(&m2);
|
unpacked.get().convert(m2);
|
||||||
std::string result = timer.format();
|
std::string result = timer.format();
|
||||||
std::cout << result << std::endl;
|
std::cout << result << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ void test_array_of_array() {
|
|||||||
std::cout << "Start converting..." << std::endl;
|
std::cout << "Start converting..." << std::endl;
|
||||||
{
|
{
|
||||||
boost::timer::cpu_timer timer;
|
boost::timer::cpu_timer timer;
|
||||||
unpacked.get().convert(&v2);
|
unpacked.get().convert(v2);
|
||||||
std::string result = timer.format();
|
std::string result = timer.format();
|
||||||
std::cout << result << std::endl;
|
std::cout << result << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -523,12 +523,14 @@ inline T& object::convert(T& v) const
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T* object::convert(T* v) const
|
inline T* object::convert(T* v) const
|
||||||
{
|
{
|
||||||
convert(*v);
|
convert(*v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
#endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool object::convert_if_not_nil(T& v) const
|
inline bool object::convert_if_not_nil(T& v) const
|
||||||
|
@ -171,6 +171,8 @@ struct object {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
T& convert(T& v) const;
|
T& convert(T& v) const;
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
/// Convert the object (obsolete)
|
/// Convert the object (obsolete)
|
||||||
/**
|
/**
|
||||||
* If the object can't be converted to T, msgpack::type_error would be thrown.
|
* If the object can't be converted to T, msgpack::type_error would be thrown.
|
||||||
@ -180,6 +182,7 @@ struct object {
|
|||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* convert(T* v) const;
|
T* convert(T* v) const;
|
||||||
|
#endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
|
|
||||||
/// Convert the object if not nil
|
/// Convert the object if not nil
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
AM_CPPFLAGS = -I$(top_srcdir)/include -pthread -DMSGPACK_DISABLE_LEGACY_NIL
|
AM_CPPFLAGS = -I$(top_srcdir)/include -pthread -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT
|
||||||
AM_C_CPPFLAGS = -I$(top_srcdir)/include -pthread
|
AM_C_CPPFLAGS = -I$(top_srcdir)/include -pthread
|
||||||
AM_LDFLAGS = $(top_builddir)/src/libmsgpackc.la -lgtest_main -lgtest -lpthread
|
AM_LDFLAGS = $(top_builddir)/src/libmsgpackc.la -lgtest_main -lgtest -lpthread
|
||||||
|
|
||||||
|
@ -33,10 +33,10 @@ TEST(convert, compatibility_less)
|
|||||||
src[0] = "kumofs";
|
src[0] = "kumofs";
|
||||||
|
|
||||||
msgpack::zone z;
|
msgpack::zone z;
|
||||||
msgpack::object obj(src, &z);
|
msgpack::object obj(src, z);
|
||||||
|
|
||||||
compatibility c;
|
compatibility c;
|
||||||
EXPECT_NO_THROW( obj.convert(&c) );
|
EXPECT_NO_THROW( obj.convert(c) );
|
||||||
|
|
||||||
EXPECT_EQ("kumofs", c.str1);
|
EXPECT_EQ("kumofs", c.str1);
|
||||||
EXPECT_EQ("default", c.str2);
|
EXPECT_EQ("default", c.str2);
|
||||||
@ -50,10 +50,10 @@ TEST(convert, compatibility_more)
|
|||||||
src[2] = "cloudy";
|
src[2] = "cloudy";
|
||||||
|
|
||||||
msgpack::zone z;
|
msgpack::zone z;
|
||||||
msgpack::object obj(src, &z);
|
msgpack::object obj(src, z);
|
||||||
|
|
||||||
compatibility to;
|
compatibility to;
|
||||||
EXPECT_NO_THROW( obj.convert(&to) );
|
EXPECT_NO_THROW( obj.convert(to) );
|
||||||
|
|
||||||
EXPECT_EQ("kumofs", to.str1);
|
EXPECT_EQ("kumofs", to.str1);
|
||||||
EXPECT_EQ("mpio", to.str2);
|
EXPECT_EQ("mpio", to.str2);
|
||||||
@ -65,24 +65,14 @@ TEST(convert, enum_member)
|
|||||||
src.flag = enum_member::B;
|
src.flag = enum_member::B;
|
||||||
|
|
||||||
msgpack::zone z;
|
msgpack::zone z;
|
||||||
msgpack::object obj(src, &z);
|
msgpack::object obj(src, z);
|
||||||
|
|
||||||
enum_member to;
|
enum_member to;
|
||||||
EXPECT_NO_THROW( obj.convert(&to) );
|
EXPECT_NO_THROW( obj.convert(to) );
|
||||||
|
|
||||||
EXPECT_EQ(enum_member::B, to.flag);
|
EXPECT_EQ(enum_member::B, to.flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(convert, return_value_ptr)
|
|
||||||
{
|
|
||||||
msgpack::zone z;
|
|
||||||
msgpack::object obj(1, z);
|
|
||||||
|
|
||||||
int i;
|
|
||||||
EXPECT_EQ(obj.convert(&i), &i);
|
|
||||||
EXPECT_EQ(1, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(convert, return_value_ref)
|
TEST(convert, return_value_ref)
|
||||||
{
|
{
|
||||||
msgpack::zone z;
|
msgpack::zone z;
|
||||||
@ -94,6 +84,20 @@ TEST(convert, return_value_ref)
|
|||||||
EXPECT_EQ(i, j);
|
EXPECT_EQ(i, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
|
|
||||||
|
TEST(convert, return_value_ptr)
|
||||||
|
{
|
||||||
|
msgpack::zone z;
|
||||||
|
msgpack::object obj(1, z);
|
||||||
|
|
||||||
|
int i;
|
||||||
|
EXPECT_EQ(obj.convert(&i), &i);
|
||||||
|
EXPECT_EQ(1, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
|
||||||
|
|
||||||
TEST(convert, if_not_nil_nil)
|
TEST(convert, if_not_nil_nil)
|
||||||
{
|
{
|
||||||
msgpack::object obj;
|
msgpack::object obj;
|
||||||
|
@ -33,7 +33,7 @@ void check_convert() {
|
|||||||
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
|
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
|
||||||
|
|
||||||
T v2;
|
T v2;
|
||||||
msg.get().convert(&v2);
|
msg.get().convert(v2);
|
||||||
|
|
||||||
EXPECT_EQ(v1.get(), v2.get());
|
EXPECT_EQ(v1.get(), v2.get());
|
||||||
|
|
||||||
@ -52,4 +52,3 @@ TEST(fixint, convert)
|
|||||||
check_convert<msgpack::type::fix_uint32>();
|
check_convert<msgpack::type::fix_uint32>();
|
||||||
check_convert<msgpack::type::fix_uint64>();
|
check_convert<msgpack::type::fix_uint64>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ const unsigned int kLoop = 1000;
|
|||||||
if (it == vec.end()) goto out; \
|
if (it == vec.end()) goto out; \
|
||||||
msgpack::object obj = result.get(); \
|
msgpack::object obj = result.get(); \
|
||||||
vec_type::value_type val; \
|
vec_type::value_type val; \
|
||||||
obj.convert(&val); \
|
obj.convert(val); \
|
||||||
EXPECT_EQ(*it, val); \
|
EXPECT_EQ(*it, val); \
|
||||||
++it; \
|
++it; \
|
||||||
} \
|
} \
|
||||||
|
@ -72,7 +72,7 @@ TEST(object, convert)
|
|||||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||||
|
|
||||||
myclass m2;
|
myclass m2;
|
||||||
ret.get().convert(&m2);
|
ret.get().convert(m2);
|
||||||
|
|
||||||
EXPECT_EQ(m1, m2);
|
EXPECT_EQ(m1, m2);
|
||||||
}
|
}
|
||||||
|
@ -149,13 +149,13 @@ public:
|
|||||||
void msgpack_unpack(msgpack::object o)
|
void msgpack_unpack(msgpack::object o)
|
||||||
{
|
{
|
||||||
msgpack::type::tuple<bool, msgpack::object> tuple;
|
msgpack::type::tuple<bool, msgpack::object> tuple;
|
||||||
o.convert(&tuple);
|
o.convert(tuple);
|
||||||
|
|
||||||
is_double = tuple.get<0>();
|
is_double = tuple.get<0>();
|
||||||
if (is_double)
|
if (is_double)
|
||||||
tuple.get<1>().convert(&value.f);
|
tuple.get<1>().convert(value.f);
|
||||||
else
|
else
|
||||||
tuple.get<1>().convert(&value.i);
|
tuple.get<1>().convert(value.i);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user