diff options
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/Makefile | 2 | ||||
-rw-r--r-- | cpp/object.cpp | 77 | ||||
-rw-r--r-- | cpp/object.hpp | 49 | ||||
-rw-r--r-- | cpp/pack.hpp | 192 | ||||
-rw-r--r-- | cpp/test.cpp | 155 |
5 files changed, 385 insertions, 90 deletions
diff --git a/cpp/Makefile b/cpp/Makefile index 04b421c..5ef7ecd 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -9,7 +9,7 @@ all: test %.hpp: %.hpp.erb erb $< > $@ -test: $(NEED_PREPROCESS) unpack.o unpack_inline.o object.o zone.o test.o object.hpp unpack.hpp +test: $(NEED_PREPROCESS) unpack.o unpack_inline.o object.o zone.o test.o object.hpp unpack.hpp pack.hpp $(CXX) $(LDFLAGS) unpack.o unpack_inline.o zone.o object.o test.o -o $@ .PHONY: clean diff --git a/cpp/object.cpp b/cpp/object.cpp index 099b541..02e0438 100644 --- a/cpp/object.cpp +++ b/cpp/object.cpp @@ -1,4 +1,5 @@ #include "msgpack/object.hpp" +#include "msgpack/pack.hpp" namespace msgpack { @@ -132,24 +133,73 @@ template <> inline void numeric_inspect<int8_t>(int8_t v, std::ostream& s) { s << (int16_t)v; } +template <typename V> +inline void numeric_pack(dynamic_packer& p, V v); + +template <> +inline void numeric_pack<uint8_t>(dynamic_packer& p, uint8_t v) + { p.pack_unsigned_int_8(v); } + +template <> +inline void numeric_pack<uint16_t>(dynamic_packer& p, uint16_t v) + { p.pack_unsigned_int_16(v); } + +template <> +inline void numeric_pack<uint32_t>(dynamic_packer& p, uint32_t v) + { p.pack_unsigned_int_32(v); } + +template <> +inline void numeric_pack<uint64_t>(dynamic_packer& p, uint64_t v) + { p.pack_unsigned_int_64(v); } + +template <> +inline void numeric_pack<int8_t>(dynamic_packer& p, int8_t v) + { p.pack_unsigned_int_8(v); } + +template <> +inline void numeric_pack<int16_t>(dynamic_packer& p, int16_t v) + { p.pack_unsigned_int_16(v); } + +template <> +inline void numeric_pack<int32_t>(dynamic_packer& p, int32_t v) + { p.pack_unsigned_int_32(v); } + +template <> +inline void numeric_pack<int64_t>(dynamic_packer& p, int64_t v) + { p.pack_unsigned_int_64(v); } + +template <> +inline void numeric_pack<float>(dynamic_packer& p, float v) + { p.pack_float(v); } + +template <> +inline void numeric_pack<double>(dynamic_packer& p, double v) + { p.pack_double(v); } + } // noname namespace bool object_nil::isnil() const { return true; } bool object_nil::operator== (const object_class* x) const { return typeid(*this) == typeid(*x); } +void object_nil::pack(dynamic_packer& p) const + { p.pack_nil(); } const object_class* object_nil::inspect(std::ostream& s) const { s << "nil"; return this; } bool object_true::xbool() const { return true; } bool object_true::operator== (const object_class* x) const { return typeid(*this) == typeid(*x); } +void object_true::pack(dynamic_packer& p) const + { p.pack_true(); } const object_class* object_true::inspect(std::ostream& s) const { s << "true"; return this; } bool object_false::xbool() const { return false; } bool object_false::operator== (const object_class* x) const { return typeid(*this) == typeid(*x); } +void object_false::pack(dynamic_packer& p) const + { p.pack_false(); } const object_class* object_false::inspect(std::ostream& s) const { s << "false"; return this; } @@ -176,6 +226,8 @@ bool object_##NAME::operator> (const object_class* x) const \ try { return val > x->x##NAME(); } \ catch (negative_overflow_error&) { return true; } \ catch (overflow_error&) { return false; } \ +void object_##NAME::pack(dynamic_packer& p) const \ + { numeric_pack(p, val); } \ const object_class* object_##NAME::inspect(std::ostream& s) const \ { numeric_inspect(val, s); return this; } \ @@ -236,7 +288,9 @@ bool object_##NAME::operator> (const object_class* x) const { \ catch (type_error&) { return true; } \ } \ } } \ -const object_class* object_##NAME::inspect(std::ostream& s) const \ +void object_##NAME::pack(dynamic_packer& p) const \ + { numeric_pack(p, val); } \ +const object_class* object_##NAME::inspect(std::ostream& s) const \ { s << val; return this; } \ FLOAT_OBJECT(float) @@ -260,6 +314,8 @@ bool object_##NAME::operator> (const object_class* x) const { \ const_raw xr(x->xraw()); \ if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) > 0; } \ else { return len > xr.len; } } \ +void object_##NAME::pack(dynamic_packer& p) const \ + { p.pack_raw(ptr, len); } \ const object_class* object_##NAME::inspect(std::ostream& s) const \ { (s << '"').write((const char*)ptr, len) << '"'; return this; } // FIXME escape @@ -303,6 +359,15 @@ const object_class* object_array::inspect(std::ostream& s) const s << ']'; return this; } +void object_array::pack(dynamic_packer& p) const +{ + p.pack_array(val.size()); + for(std::vector<object>::const_iterator it(val.begin()), it_end(val.end()); + it != it_end; + ++it) { + it->pack(p); + } +} map& object_map::xmap() { return val; } @@ -334,6 +399,16 @@ const object_class* object_map::inspect(std::ostream& s) const s << '}'; return this; } +void object_map::pack(dynamic_packer& p) const +{ + p.pack_map(val.size()); + for(std::map<object, object>::const_iterator it(val.begin()), it_end(val.end()); + it != it_end; + ++it) { + it->first.pack(p); + it->second.pack(p); + } +} } // namespace msgpack diff --git a/cpp/object.hpp b/cpp/object.hpp index 992ac1e..3f22dfc 100644 --- a/cpp/object.hpp +++ b/cpp/object.hpp @@ -1,6 +1,5 @@ #ifndef MSGPACK_OBJECT_HPP__ #define MSGPACK_OBJECT_HPP__ -#include <iostream> #include <cstddef> #include <stdexcept> @@ -47,6 +46,8 @@ struct object; typedef std::map<object, object> map; typedef std::vector<object> array; +class dynamic_packer; + struct object_class { virtual ~object_class() {} @@ -72,6 +73,7 @@ struct object_class { bool operator!= (const object_class* x) const { return !(this->operator==(x)); } virtual bool operator< (const object_class* x) const { throw cast_error(); } virtual bool operator> (const object_class* x) const { throw cast_error(); } + virtual void pack(dynamic_packer& p) const = 0; operator bool() const { return xbool(); } // FIXME !isnil(); operator uint8_t() const { return xu8(); } operator uint16_t() const { return xu16(); } @@ -128,6 +130,7 @@ struct object { bool operator!= (object x) const { return val->operator!= (x.val); } bool operator< (object x) const { return val->operator< (x.val); } bool operator> (object x) const { return val->operator> (x.val); } + void pack(dynamic_packer& p) const { val->pack(p); } operator bool() const { return val->operator bool(); } operator uint8_t() const { return val->operator uint8_t(); } operator uint16_t() const { return val->operator uint16_t(); } @@ -160,18 +163,21 @@ inline std::ostream& operator<< (std::ostream& s, const object& o) struct object_nil : object_class { bool isnil() const; bool operator== (const object_class* x) const; + void pack(dynamic_packer& p) const; const object_class* inspect(std::ostream& s) const; }; struct object_true : object_class { bool xbool() const; bool operator== (const object_class* x) const; + void pack(dynamic_packer& p) const; const object_class* inspect(std::ostream& s) const; }; struct object_false : object_class { bool xbool() const; bool operator== (const object_class* x) const; + void pack(dynamic_packer& p) const; const object_class* inspect(std::ostream& s) const; }; @@ -191,6 +197,7 @@ struct object_##NAME : object_class { \ bool operator== (const object_class* x) const; \ bool operator< (const object_class* x) const; \ bool operator> (const object_class* x) const; \ + void pack(dynamic_packer& p) const; \ const object_class* inspect(std::ostream& s) const; \ private: \ TYPE val; \ @@ -209,24 +216,25 @@ INTEGER_CLASS(int64_t, i64) #define FLOAT_CLASS(TYPE, NAME) \ -struct object_##NAME : object_class { \ - object_##NAME(TYPE v) : val(v) {} \ - uint8_t xu8 () const; \ - uint16_t xu16 () const; \ - uint32_t xu32 () const; \ - uint64_t xu64 () const; \ - int8_t xi8 () const; \ - int16_t xi16 () const; \ - int32_t xi32 () const; \ - int64_t xi64 () const; \ - float xfloat () const; \ - double xdouble() const; \ - bool operator== (const object_class* x) const; \ - bool operator< (const object_class* x) const; \ - bool operator> (const object_class* x) const; \ - const object_class* inspect(std::ostream& s) const; \ -private: \ - TYPE val; \ +struct object_##NAME : object_class { \ + object_##NAME(TYPE v) : val(v) {} \ + uint8_t xu8 () const; \ + uint16_t xu16 () const; \ + uint32_t xu32 () const; \ + uint64_t xu64 () const; \ + int8_t xi8 () const; \ + int16_t xi16 () const; \ + int32_t xi32 () const; \ + int64_t xi64 () const; \ + float xfloat () const; \ + double xdouble() const; \ + bool operator== (const object_class* x) const; \ + bool operator< (const object_class* x) const; \ + bool operator> (const object_class* x) const; \ + void pack(dynamic_packer& p) const; \ + const object_class* inspect(std::ostream& s) const; \ +private: \ + TYPE val; \ }; FLOAT_CLASS(float, float) @@ -242,6 +250,7 @@ struct object_##NAME : object_class { \ bool operator== (const object_class* x) const; \ bool operator< (const object_class* x) const; \ bool operator> (const object_class* x) const; \ + void pack(dynamic_packer& p) const; \ const object_class* inspect(std::ostream& s) const; \ private: \ TYPE ptr; \ @@ -261,6 +270,7 @@ struct object_array : object_class, object_container_mixin { const array& xarray() const; bool operator== (const object_class* x) const; // FIXME operator<, operator> + void pack(dynamic_packer& p) const; const object_class* inspect(std::ostream& s) const; public: void push_back(object o) { val.push_back(o); } @@ -276,6 +286,7 @@ struct object_map : object_class, object_container_mixin { const map& xmap() const; bool operator== (const object_class* x) const; // FIXME operator<, operator> + void pack(dynamic_packer& p) const; const object_class* inspect(std::ostream& s) const; public: void store(object k, object v) { val[k] = v; } diff --git a/cpp/pack.hpp b/cpp/pack.hpp new file mode 100644 index 0000000..89f8cee --- /dev/null +++ b/cpp/pack.hpp @@ -0,0 +1,192 @@ +#ifndef MSGPACK_PACK_HPP__ +#define MSGPACK_PACK_HPP__ + +#include "msgpack/object.hpp" +#include "msgpack/zone.hpp" +#include <arpa/inet.h> // __BYTE_ORDER +#include <stdexcept> + +namespace msgpack { + + +template <typename Stream> +class packer { +public: + packer(Stream& s); + +public: + void pack_int(int d) { pack_int_impl(m_stream, d); } + void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } + void pack_unsigned_int_8(uint8_t d) { pack_unsigned_int_8_impl(m_stream, d); } + void pack_unsigned_int_16(uint16_t d) { pack_unsigned_int_16_impl(m_stream, d); } + void pack_unsigned_int_32(uint32_t d) { pack_unsigned_int_32_impl(m_stream, d); } + void pack_unsigned_int_64(uint64_t d) { pack_unsigned_int_64_impl(m_stream, d); } + void pack_signed_int_8(uint8_t d) { pack_signed_int_8_impl(m_stream, d); } + void pack_signed_int_16(uint16_t d) { pack_signed_int_16_impl(m_stream, d); } + void pack_signed_int_32(uint32_t d) { pack_signed_int_32_impl(m_stream, d); } + void pack_signed_int_64(uint64_t d) { pack_signed_int_64_impl(m_stream, d); } + void pack_float(float d) { pack_float_impl(m_stream, d); } + void pack_double(double d) { pack_double_impl(m_stream, d); } + void pack_nil() { pack_nil(m_stream); } + void pack_true() { pack_true(m_stream); } + void pack_false() { pack_false(m_stream); } + void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } + void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } + void pack_string(const char* b) { pack_string_impl(m_stream, b); } + void pack_raw(const void* b, size_t l) { pack_raw_impl(m_stream, b, l); } + +private: + static void pack_int_impl(Stream& x, int d); + static void pack_unsigned_int_impl(Stream& x, unsigned int d); + static void pack_unsigned_int_8_impl(Stream& x, uint8_t d); + static void pack_unsigned_int_16_impl(Stream& x, uint16_t d); + static void pack_unsigned_int_32_impl(Stream& x, uint32_t d); + static void pack_unsigned_int_64_impl(Stream& x, uint64_t d); + static void pack_signed_int_8_impl(Stream& x, int8_t d); + static void pack_signed_int_16_impl(Stream& x, int16_t d); + static void pack_signed_int_32_impl(Stream& x, int32_t d); + static void pack_signed_int_64_impl(Stream& x, int64_t d); + static void pack_float_impl(Stream& x, float d); + static void pack_double_impl(Stream& x, double d); + static void pack_nil_impl(Stream& x); + static void pack_true_impl(Stream& x); + static void pack_false_impl(Stream& x); + static void pack_array_impl(Stream& x, unsigned int n); + static void pack_map_impl(Stream& x, unsigned int n); + static void pack_string_impl(Stream& x, const char* b); + static void pack_raw_impl(Stream& x, const void* b, size_t l); + static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len) + { x.append((const char*)buf, len); } + +private: + Stream& m_stream; + +private: + packer(); +}; + +#define msgpack_pack_inline_func(name) \ + template <typename Stream> \ + inline void packer<Stream>::pack_ ## name ## _impl +#define msgpack_pack_context Stream& +#define msgpack_pack_append_buffer append_buffer +#include "msgpack/pack/inline_impl.h" +#undef msgpack_pack_context +#undef msgpack_pack_append_buffer + +template <typename Stream> +packer<Stream>::packer(Stream& s) : m_stream(s) { } + + +class dynamic_stream { +public: + template <typename Stream> + dynamic_stream(Stream& s); +public: + dynamic_stream& append(const char* buf, size_t len) + { (*m_function)(m_object, buf, len); return *this; } +private: + void* m_object; + void (*m_function)(void* object, const char* buf, size_t len); +private: + template <typename Stream, Stream& (Stream::*MemFun)(const char*, size_t)> + static void append_trampoline(void* object, const char* buf, size_t len); +}; + + +class dynamic_packer { +public: + template <typename Stream> + dynamic_packer(Stream& s); + +public: + void pack_int(int d) { pack_int_impl(m_stream, d); } + void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } + void pack_unsigned_int_8(uint8_t d) { pack_unsigned_int_8_impl(m_stream, d); } + void pack_unsigned_int_16(uint16_t d) { pack_unsigned_int_16_impl(m_stream, d); } + void pack_unsigned_int_32(uint32_t d) { pack_unsigned_int_32_impl(m_stream, d); } + void pack_unsigned_int_64(uint64_t d) { pack_unsigned_int_64_impl(m_stream, d); } + void pack_signed_int_8(uint8_t d) { pack_signed_int_8_impl(m_stream, d); } + void pack_signed_int_16(uint16_t d) { pack_signed_int_16_impl(m_stream, d); } + void pack_signed_int_32(uint32_t d) { pack_signed_int_32_impl(m_stream, d); } + void pack_signed_int_64(uint64_t d) { pack_signed_int_64_impl(m_stream, d); } + void pack_float(float d) { pack_float_impl(m_stream, d); } + void pack_double(double d) { pack_double_impl(m_stream, d); } + void pack_nil() { pack_nil_impl(m_stream); } + void pack_true() { pack_true_impl(m_stream); } + void pack_false() { pack_false_impl(m_stream); } + void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } + void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } + void pack_string(const char* b) { pack_string_impl(m_stream, b); } + void pack_raw(const void* b, size_t l) { pack_raw_impl(m_stream, b, l); } + +public: + +private: + static void pack_int_impl(dynamic_stream& x, int d); + static void pack_unsigned_int_impl(dynamic_stream& x, unsigned int d); + static void pack_unsigned_int_8_impl(dynamic_stream& x, uint8_t d); + static void pack_unsigned_int_16_impl(dynamic_stream& x, uint16_t d); + static void pack_unsigned_int_32_impl(dynamic_stream& x, uint32_t d); + static void pack_unsigned_int_64_impl(dynamic_stream& x, uint64_t d); + static void pack_signed_int_8_impl(dynamic_stream& x, int8_t d); + static void pack_signed_int_16_impl(dynamic_stream& x, int16_t d); + static void pack_signed_int_32_impl(dynamic_stream& x, int32_t d); + static void pack_signed_int_64_impl(dynamic_stream& x, int64_t d); + static void pack_float_impl(dynamic_stream& x, float d); + static void pack_double_impl(dynamic_stream& x, double d); + static void pack_nil_impl(dynamic_stream& x); + static void pack_true_impl(dynamic_stream& x); + static void pack_false_impl(dynamic_stream& x); + static void pack_array_impl(dynamic_stream& x, unsigned int n); + static void pack_map_impl(dynamic_stream& x, unsigned int n); + static void pack_string_impl(dynamic_stream& x, const char* b); + static void pack_raw_impl(dynamic_stream& x, const void* b, size_t l); + static void append_buffer(dynamic_stream& x, const unsigned char* buf, unsigned int len) + { x.append((const char*)buf, len); } + +private: + dynamic_stream m_stream; + +private: + dynamic_packer(); +}; + +#undef MSGPACK_PACK_INLINE_IMPL_H__ +#define msgpack_pack_inline_func(name) \ + inline void dynamic_packer::pack_ ## name ## _impl +#define msgpack_pack_context dynamic_stream& +#define msgpack_pack_append_buffer append_buffer +#include "msgpack/pack/inline_impl.h" +#undef msgpack_pack_context +#undef msgpack_pack_append_buffer + +template <typename Stream> +dynamic_packer::dynamic_packer(Stream& s) : m_stream(s) { } + +template <typename Stream> +dynamic_stream::dynamic_stream(Stream& s) +{ + m_object = reinterpret_cast<void*>(&s); + m_function = &dynamic_stream::append_trampoline<Stream, &Stream::append>; +} + +template <typename Stream, Stream& (Stream::*MemFun)(const char*, size_t)> +void dynamic_stream::append_trampoline(void* object, const char* buf, size_t len) +{ + (reinterpret_cast<Stream*>(object)->*MemFun)(buf, len); +} + + +template <typename Stream> +inline void pack(Stream& s, object o) +{ + dynamic_packer pk(s); + o.pack(pk); +} + + +} // namespace msgpack + +#endif /* msgpack/pack.hpp */ + diff --git a/cpp/test.cpp b/cpp/test.cpp index 423a6bd..12e9150 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -1,12 +1,16 @@ #include <iostream> +#include <string> #include <msgpack/unpack.hpp> +#include <msgpack/pack.hpp> class checker { public: void check(const char* d, size_t len, msgpack::object should) { + using msgpack::object; try { std::cout << "----" << std::endl; - msgpack::object o; + + object o; try { o = msgpack::unpack(d, len, m_zone); } catch (std::runtime_error& e) { @@ -14,10 +18,25 @@ public: std::cout << "**" << e.what() << "**" << std::endl; return; } + std::cout << o << std::endl; if(o != should) { std::cout << "** TEST FAILED **" << std::endl; } + + try { + std::string s; + msgpack::pack(s, o); + object ro = msgpack::unpack(s.data(), s.size(), m_zone); + if(ro != o) { throw std::runtime_error("NOT MATCH"); } + } catch (std::runtime_error& e) { + std::cout << "** REUNPACK FAILED **" << std::endl; + std::cout << e.what() << std::endl; + } catch (...) { + std::cout << "** REUNPACK FAILED **" << std::endl; + std::cout << "unknown error" << std::endl; + } + } catch (...) { m_zone.clear(); throw; } m_zone.clear(); } @@ -27,84 +46,82 @@ private: int main(void) { + checker c; -checker c; - -{ // SimpleValue - msgpack::zone z; - const char d[] = { - 0x93, 0xc0, 0xc2, 0xc3, - }; - c.check(d, sizeof(d), - z.narray( - z.nnil(), z.nfalse(), z.ntrue() - ) - ); -} - -{ // Fixnum - msgpack::zone z; - const char d[] = { - 0x92, - 0x93, 0x00, 0x40, 0x7f, - 0x93, 0xe0, 0xf0, 0xff, - }; - c.check(d, sizeof(d), - z.narray( + { // SimpleValue + msgpack::zone z; + const char d[] = { + 0x93, 0xc0, 0xc2, 0xc3, + }; + c.check(d, sizeof(d), z.narray( - z.nu8(0), - z.nu8(64), - z.nu8(127) - ), - z.narray( - z.ni8(-32), - z.ni8(-16), - z.ni8(-1) + z.nnil(), z.nfalse(), z.ntrue() ) - ) - ); -} + ); + } -{ // FixArray - msgpack::zone z; - const char d[] = { - 0x92, - 0x90, - 0x91, - 0x91, 0xc0, - }; - c.check(d, sizeof(d), - z.narray( - z.narray(), + { // Fixnum + msgpack::zone z; + const char d[] = { + 0x92, + 0x93, 0x00, 0x40, 0x7f, + 0x93, 0xe0, 0xf0, 0xff, + }; + c.check(d, sizeof(d), z.narray( z.narray( - z.nnil() + z.nu8(0), + z.nu8(64), + z.nu8(127) + ), + z.narray( + z.ni8(-32), + z.ni8(-16), + z.ni8(-1) ) ) - ) - ); -} + ); + } -{ // FixRaw - msgpack::zone z; - const char d[] = { - 0x94, - 0xa0, - 0xa1, 'a', - 0xa2, 'b', 'c', - 0xa3, 'd', 'e', 'f', - }; - c.check(d, sizeof(d), - z.narray( - z.nraw("", 0), - z.nraw("a", 1), - z.nraw("bc", 2), - z.nraw("def", 3) - ) - ); -} + { // FixArray + msgpack::zone z; + const char d[] = { + 0x92, + 0x90, + 0x91, + 0x91, 0xc0, + }; + c.check(d, sizeof(d), + z.narray( + z.narray(), + z.narray( + z.narray( + z.nnil() + ) + ) + ) + ); + } + { // FixRaw + msgpack::zone z; + const char d[] = { + 0x94, + 0xa0, + 0xa1, 'a', + 0xa2, 'b', 'c', + 0xa3, 'd', 'e', 'f', + }; + c.check(d, sizeof(d), + z.narray( + z.nraw("", 0), + z.nraw("a", 1), + z.nraw("bc", 2), + z.nraw("def", 3) + ) + ); + } -return 0; + return 0; } |