mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 15:05:37 +02:00
Added enum examples.
Added converting to zone with or without zone.
This commit is contained in:
@@ -54,12 +54,13 @@ void print(std::string const& buf) {
|
|||||||
<< (static_cast<int>(*it) & 0xff)
|
<< (static_cast<int>(*it) & 0xff)
|
||||||
<< ' ';
|
<< ' ';
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::dec << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <msgpack.hpp>
|
#include <msgpack.hpp>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
{ // pack, unpack
|
||||||
my_class my("John Smith", 42);
|
my_class my("John Smith", 42);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
msgpack::pack(ss, my);
|
msgpack::pack(ss, my);
|
||||||
@@ -69,5 +70,14 @@ int main() {
|
|||||||
msgpack::unpacked unp;
|
msgpack::unpacked unp;
|
||||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||||
msgpack::object obj = unp.get();
|
msgpack::object obj = unp.get();
|
||||||
|
std::cout << obj << std::endl;
|
||||||
assert(obj.as<my_class>() == my);
|
assert(obj.as<my_class>() == my);
|
||||||
|
}
|
||||||
|
{ // create object with zone
|
||||||
|
my_class my("John Smith", 42);
|
||||||
|
msgpack::zone z;
|
||||||
|
msgpack::object obj(my, z);
|
||||||
|
std::cout << obj << std::endl;
|
||||||
|
assert(obj.as<my_class>() == my);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -37,6 +37,8 @@ object const& operator>> (msgpack::object const& o, my_class& v);
|
|||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v);
|
packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v);
|
||||||
|
|
||||||
|
void operator<< (msgpack::object::with_zone& o, my_class const& v);
|
||||||
|
|
||||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||||
} // namespace msgpack
|
} // namespace msgpack
|
||||||
|
|
||||||
@@ -87,6 +89,14 @@ inline packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void operator<< (msgpack::object::with_zone& o, my_class const& v) {
|
||||||
|
o.type = type::ARRAY;
|
||||||
|
o.via.array.size = 2;
|
||||||
|
o.via.array.ptr = static_cast<object*>(o.zone.allocate_align(sizeof(object) * o.via.array.size));
|
||||||
|
o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone);
|
||||||
|
o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone);
|
||||||
|
}
|
||||||
|
|
||||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||||
} // namespace msgpack
|
} // namespace msgpack
|
||||||
|
|
||||||
@@ -102,10 +112,11 @@ void print(std::string const& buf) {
|
|||||||
<< (static_cast<int>(*it) & 0xff)
|
<< (static_cast<int>(*it) & 0xff)
|
||||||
<< ' ';
|
<< ' ';
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::dec << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
{ // pack, unpack
|
||||||
my_class my("John Smith", 42);
|
my_class my("John Smith", 42);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
msgpack::pack(ss, my);
|
msgpack::pack(ss, my);
|
||||||
@@ -115,5 +126,14 @@ int main() {
|
|||||||
msgpack::unpacked unp;
|
msgpack::unpacked unp;
|
||||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||||
msgpack::object obj = unp.get();
|
msgpack::object obj = unp.get();
|
||||||
|
std::cout << obj << std::endl;
|
||||||
assert(obj.as<my_class>() == my);
|
assert(obj.as<my_class>() == my);
|
||||||
|
}
|
||||||
|
{ // create object with zone
|
||||||
|
my_class my("John Smith", 42);
|
||||||
|
msgpack::zone z;
|
||||||
|
msgpack::object obj(my, z);
|
||||||
|
std::cout << obj << std::endl;
|
||||||
|
assert(obj.as<my_class>() == my);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
68
example/cpp03/enum.cpp
Normal file
68
example/cpp03/enum.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
// MessagePack for C++ example
|
||||||
|
//
|
||||||
|
// Copyright (C) 2015 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.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <msgpack_fwd.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
enum my_enum {
|
||||||
|
elem1,
|
||||||
|
elem2,
|
||||||
|
elem3
|
||||||
|
};
|
||||||
|
|
||||||
|
MSGPACK_ADD_ENUM(my_enum);
|
||||||
|
|
||||||
|
#include <msgpack.hpp>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
{ // pack, unpack
|
||||||
|
std::stringstream sbuf;
|
||||||
|
msgpack::pack(sbuf, elem1);
|
||||||
|
msgpack::pack(sbuf, elem2);
|
||||||
|
my_enum e3 = elem3;
|
||||||
|
msgpack::pack(sbuf, e3);
|
||||||
|
|
||||||
|
msgpack::unpacked result;
|
||||||
|
std::size_t off = 0;
|
||||||
|
|
||||||
|
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||||
|
std::cout << result.get().as<my_enum>() << std::endl;
|
||||||
|
assert(result.get().as<my_enum>() == elem1);
|
||||||
|
|
||||||
|
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||||
|
std::cout << result.get().as<my_enum>() << std::endl;
|
||||||
|
assert(result.get().as<my_enum>() == elem2);
|
||||||
|
|
||||||
|
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||||
|
std::cout << result.get().as<my_enum>() << std::endl;
|
||||||
|
assert(result.get().as<my_enum>() == elem3);
|
||||||
|
}
|
||||||
|
{ // create object without zone
|
||||||
|
msgpack::object obj(elem2);
|
||||||
|
std::cout << obj.as<my_enum>() << std::endl;
|
||||||
|
assert(obj.as<my_enum>() == elem2);
|
||||||
|
}
|
||||||
|
{ // create object with zone
|
||||||
|
msgpack::zone z;
|
||||||
|
msgpack::object objz(elem3, z);
|
||||||
|
std::cout << objz.as<my_enum>() << std::endl;
|
||||||
|
assert(objz.as<my_enum>() == elem3);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user