mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 06:55:50 +02:00
Updated examples to support new packing/converting mechanism.
This commit is contained in:
@@ -21,8 +21,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
// msgpack.hpp is also ok
|
#include <msgpack.hpp>
|
||||||
#include <msgpack_fwd.hpp>
|
|
||||||
|
|
||||||
|
|
||||||
class my_class {
|
class my_class {
|
||||||
@@ -57,8 +56,6 @@ void print(std::string const& buf) {
|
|||||||
std::cout << std::dec << std::endl;
|
std::cout << std::dec << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <msgpack.hpp>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{ // pack, unpack
|
{ // pack, unpack
|
||||||
my_class my("John Smith", 42);
|
my_class my("John Smith", 42);
|
||||||
|
@@ -21,27 +21,6 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
// msgpack.hpp should be included at after user declarations
|
|
||||||
#include <msgpack_fwd.hpp>
|
|
||||||
|
|
||||||
|
|
||||||
// declarations
|
|
||||||
class my_class;
|
|
||||||
|
|
||||||
namespace msgpack {
|
|
||||||
|
|
||||||
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
|
|
||||||
|
|
||||||
object const& operator>> (msgpack::object const& o, my_class& v);
|
|
||||||
|
|
||||||
template <typename Stream>
|
|
||||||
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)
|
|
||||||
} // namespace msgpack
|
|
||||||
|
|
||||||
#include <msgpack.hpp>
|
#include <msgpack.hpp>
|
||||||
|
|
||||||
class my_class {
|
class my_class {
|
||||||
@@ -63,14 +42,14 @@ private:
|
|||||||
int age_;
|
int age_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// User defined class template specialization
|
||||||
// definitions
|
|
||||||
|
|
||||||
namespace msgpack {
|
namespace msgpack {
|
||||||
|
|
||||||
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
|
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
|
||||||
|
namespace adaptor {
|
||||||
|
|
||||||
inline object const& operator>> (msgpack::object const& o, my_class& v) {
|
template<>
|
||||||
|
struct convert<my_class> {
|
||||||
|
msgpack::object const& operator()(msgpack::object const& o, my_class& v) const {
|
||||||
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
|
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
|
||||||
if (o.via.array.size != 2) throw msgpack::type_error();
|
if (o.via.array.size != 2) throw msgpack::type_error();
|
||||||
v = my_class(
|
v = my_class(
|
||||||
@@ -78,25 +57,33 @@ inline object const& operator>> (msgpack::object const& o, my_class& v) {
|
|||||||
o.via.array.ptr[1].as<int>());
|
o.via.array.ptr[1].as<int>());
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct pack<my_class> {
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
inline packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v) {
|
packer<Stream>& operator()(msgpack::packer<Stream>& o, my_class const& v) const {
|
||||||
// packing member variables as an array.
|
// packing member variables as an array.
|
||||||
o.pack_array(2);
|
o.pack_array(2);
|
||||||
o.pack(v.get_name());
|
o.pack(v.get_name());
|
||||||
o.pack(v.get_age());
|
o.pack(v.get_age());
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
inline void operator<< (msgpack::object::with_zone& o, my_class const& v) {
|
template <>
|
||||||
|
struct object_with_zone<my_class> {
|
||||||
|
void operator()(msgpack::object::with_zone& o, my_class const& v) const {
|
||||||
o.type = type::ARRAY;
|
o.type = type::ARRAY;
|
||||||
o.via.array.size = 2;
|
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 = static_cast<msgpack::object*>(
|
||||||
|
o.zone.allocate_align(sizeof(msgpack::object) * o.via.array.size));
|
||||||
o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone);
|
o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone);
|
||||||
o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone);
|
o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace adaptor
|
||||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||||
} // namespace msgpack
|
} // namespace msgpack
|
||||||
|
|
||||||
|
@@ -15,11 +15,12 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <msgpack_fwd.hpp>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include <msgpack.hpp>
|
||||||
|
|
||||||
enum my_enum {
|
enum my_enum {
|
||||||
elem1,
|
elem1,
|
||||||
elem2,
|
elem2,
|
||||||
@@ -28,8 +29,6 @@ enum my_enum {
|
|||||||
|
|
||||||
MSGPACK_ADD_ENUM(my_enum);
|
MSGPACK_ADD_ENUM(my_enum);
|
||||||
|
|
||||||
#include <msgpack.hpp>
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
{ // pack, unpack
|
{ // pack, unpack
|
||||||
|
@@ -20,6 +20,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
// This example uses obsolete APIs
|
||||||
|
// See protocol_new.cpp
|
||||||
namespace myprotocol {
|
namespace myprotocol {
|
||||||
using namespace msgpack::type;
|
using namespace msgpack::type;
|
||||||
using msgpack::define;
|
using msgpack::define;
|
||||||
|
Reference in New Issue
Block a user