diff --git a/example/cpp03/class_intrusive.cpp b/example/cpp03/class_intrusive.cpp index 64a3956e..dc27f0af 100644 --- a/example/cpp03/class_intrusive.cpp +++ b/example/cpp03/class_intrusive.cpp @@ -21,8 +21,7 @@ #include #include -// msgpack.hpp is also ok -#include +#include class my_class { @@ -57,8 +56,6 @@ void print(std::string const& buf) { std::cout << std::dec << std::endl; } -#include - int main() { { // pack, unpack my_class my("John Smith", 42); diff --git a/example/cpp03/class_non_intrusive.cpp b/example/cpp03/class_non_intrusive.cpp index 1168b484..5a60ca36 100644 --- a/example/cpp03/class_non_intrusive.cpp +++ b/example/cpp03/class_non_intrusive.cpp @@ -21,27 +21,6 @@ #include #include -// msgpack.hpp should be included at after user declarations -#include - - -// 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 -packer& operator<< (msgpack::packer& 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 class my_class { @@ -63,40 +42,48 @@ 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) { - if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); - if (o.via.array.size != 2) throw msgpack::type_error(); - v = my_class( - o.via.array.ptr[0].as(), - o.via.array.ptr[1].as()); - return o; -} +template<> +struct convert { + 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( + o.via.array.ptr[0].as(), + o.via.array.ptr[1].as()); + return o; + } +}; +template<> +struct pack { + template + packer& operator()(msgpack::packer& 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; + } +}; -template -inline packer& operator<< (msgpack::packer& o, my_class const& v) { - // 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) { - o.type = type::ARRAY; - o.via.array.size = 2; - o.via.array.ptr = static_cast(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); -} +template <> +struct object_with_zone { + 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( + 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 diff --git a/example/cpp03/enum.cpp b/example/cpp03/enum.cpp index c87dd516..2f8f505f 100644 --- a/example/cpp03/enum.cpp +++ b/example/cpp03/enum.cpp @@ -15,11 +15,12 @@ // limitations under the License. // -#include #include #include #include +#include + enum my_enum { elem1, elem2, @@ -28,8 +29,6 @@ enum my_enum { MSGPACK_ADD_ENUM(my_enum); -#include - int main(void) { { // pack, unpack diff --git a/example/cpp03/protocol.cpp b/example/cpp03/protocol.cpp index b4462f5b..5b8a06e3 100644 --- a/example/cpp03/protocol.cpp +++ b/example/cpp03/protocol.cpp @@ -20,6 +20,8 @@ #include #include +// This example uses obsolete APIs +// See protocol_new.cpp namespace myprotocol { using namespace msgpack::type; using msgpack::define;