Updated examples to support new packing/converting mechanism.

This commit is contained in:
Takatoshi Kondo
2015-04-02 18:24:10 +09:00
parent e8d3c8d6c5
commit a2c8154960
4 changed files with 41 additions and 56 deletions

View File

@@ -21,8 +21,7 @@
#include <sstream>
#include <cassert>
// msgpack.hpp is also ok
#include <msgpack_fwd.hpp>
#include <msgpack.hpp>
class my_class {
@@ -57,8 +56,6 @@ void print(std::string const& buf) {
std::cout << std::dec << std::endl;
}
#include <msgpack.hpp>
int main() {
{ // pack, unpack
my_class my("John Smith", 42);

View File

@@ -21,27 +21,6 @@
#include <sstream>
#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>
class my_class {
@@ -63,14 +42,14 @@ private:
int age_;
};
// definitions
// User defined class template specialization
namespace msgpack {
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.via.array.size != 2) throw msgpack::type_error();
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>());
return o;
}
};
template<>
struct pack<my_class> {
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.
o.pack_array(2);
o.pack(v.get_name());
o.pack(v.get_age());
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.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[1] = msgpack::object(v.get_age(), o.zone);
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack

View File

@@ -15,11 +15,12 @@
// limitations under the License.
//
#include <msgpack_fwd.hpp>
#include <sstream>
#include <iostream>
#include <cassert>
#include <msgpack.hpp>
enum my_enum {
elem1,
elem2,
@@ -28,8 +29,6 @@ enum my_enum {
MSGPACK_ADD_ENUM(my_enum);
#include <msgpack.hpp>
int main(void)
{
{ // pack, unpack

View File

@@ -20,6 +20,8 @@
#include <iostream>
#include <sstream>
// This example uses obsolete APIs
// See protocol_new.cpp
namespace myprotocol {
using namespace msgpack::type;
using msgpack::define;