Changeset 18610
- Timestamp:
- 09/02/08 06:15:02 (5 years ago)
- Location:
- lang/c/msgpack/trunk
- Files:
-
- 1 added
- 7 modified
-
c/pack_inline.h (modified) (1 diff)
-
cpp/Makefile (modified) (1 diff)
-
cpp/object.cpp (modified) (10 diffs)
-
cpp/object.hpp (modified) (12 diffs)
-
cpp/pack.hpp (added)
-
cpp/test.cpp (modified) (3 diffs)
-
msgpack/pack/inline_impl.h (modified) (23 diffs)
-
ruby/pack_inline.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/msgpack/trunk/c/pack_inline.h
r17708 r18610 8 8 static inline void msgpack_pack_append_buffer(msgpack_pack_t* x, const unsigned char* b, unsigned int l); 9 9 10 #include <string.h> 11 #include <arpa/inet.h> /* __BYTE_ORDER */ 10 12 #include "msgpack/pack/inline_impl.h" 11 12 13 13 14 #endif /* pack_inline.h */ -
lang/c/msgpack/trunk/cpp/Makefile
r18608 r18610 10 10 erb $< > $@ 11 11 12 test: $(NEED_PREPROCESS) unpack.o unpack_inline.o object.o zone.o test.o object.hpp unpack.hpp 12 test: $(NEED_PREPROCESS) unpack.o unpack_inline.o object.o zone.o test.o object.hpp unpack.hpp pack.hpp 13 13 $(CXX) $(LDFLAGS) unpack.o unpack_inline.o zone.o object.o test.o -o $@ 14 14 -
lang/c/msgpack/trunk/cpp/object.cpp
r18608 r18610 1 1 #include "msgpack/object.hpp" 2 #include "msgpack/pack.hpp" 2 3 3 4 namespace msgpack { … … 133 134 { s << (int16_t)v; } 134 135 136 template <typename V> 137 inline void numeric_pack(dynamic_packer& p, V v); 138 139 template <> 140 inline void numeric_pack<uint8_t>(dynamic_packer& p, uint8_t v) 141 { p.pack_unsigned_int_8(v); } 142 143 template <> 144 inline void numeric_pack<uint16_t>(dynamic_packer& p, uint16_t v) 145 { p.pack_unsigned_int_16(v); } 146 147 template <> 148 inline void numeric_pack<uint32_t>(dynamic_packer& p, uint32_t v) 149 { p.pack_unsigned_int_32(v); } 150 151 template <> 152 inline void numeric_pack<uint64_t>(dynamic_packer& p, uint64_t v) 153 { p.pack_unsigned_int_64(v); } 154 155 template <> 156 inline void numeric_pack<int8_t>(dynamic_packer& p, int8_t v) 157 { p.pack_unsigned_int_8(v); } 158 159 template <> 160 inline void numeric_pack<int16_t>(dynamic_packer& p, int16_t v) 161 { p.pack_unsigned_int_16(v); } 162 163 template <> 164 inline void numeric_pack<int32_t>(dynamic_packer& p, int32_t v) 165 { p.pack_unsigned_int_32(v); } 166 167 template <> 168 inline void numeric_pack<int64_t>(dynamic_packer& p, int64_t v) 169 { p.pack_unsigned_int_64(v); } 170 171 template <> 172 inline void numeric_pack<float>(dynamic_packer& p, float v) 173 { p.pack_float(v); } 174 175 template <> 176 inline void numeric_pack<double>(dynamic_packer& p, double v) 177 { p.pack_double(v); } 178 135 179 } // noname namespace 136 180 … … 139 183 bool object_nil::operator== (const object_class* x) const 140 184 { return typeid(*this) == typeid(*x); } 185 void object_nil::pack(dynamic_packer& p) const 186 { p.pack_nil(); } 141 187 const object_class* object_nil::inspect(std::ostream& s) const 142 188 { s << "nil"; return this; } … … 145 191 bool object_true::operator== (const object_class* x) const 146 192 { return typeid(*this) == typeid(*x); } 193 void object_true::pack(dynamic_packer& p) const 194 { p.pack_true(); } 147 195 const object_class* object_true::inspect(std::ostream& s) const 148 196 { s << "true"; return this; } … … 151 199 bool object_false::operator== (const object_class* x) const 152 200 { return typeid(*this) == typeid(*x); } 201 void object_false::pack(dynamic_packer& p) const 202 { p.pack_false(); } 153 203 const object_class* object_false::inspect(std::ostream& s) const 154 204 { s << "false"; return this; } … … 177 227 catch (negative_overflow_error&) { return true; } \ 178 228 catch (overflow_error&) { return false; } \ 229 void object_##NAME::pack(dynamic_packer& p) const \ 230 { numeric_pack(p, val); } \ 179 231 const object_class* object_##NAME::inspect(std::ostream& s) const \ 180 232 { numeric_inspect(val, s); return this; } \ … … 237 289 } \ 238 290 } } \ 239 const object_class* object_##NAME::inspect(std::ostream& s) const \ 291 void object_##NAME::pack(dynamic_packer& p) const \ 292 { numeric_pack(p, val); } \ 293 const object_class* object_##NAME::inspect(std::ostream& s) const \ 240 294 { s << val; return this; } \ 241 295 … … 261 315 if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) > 0; } \ 262 316 else { return len > xr.len; } } \ 317 void object_##NAME::pack(dynamic_packer& p) const \ 318 { p.pack_raw(ptr, len); } \ 263 319 const object_class* object_##NAME::inspect(std::ostream& s) const \ 264 320 { (s << '"').write((const char*)ptr, len) << '"'; return this; } // FIXME escape … … 304 360 return this; 305 361 } 362 void object_array::pack(dynamic_packer& p) const 363 { 364 p.pack_array(val.size()); 365 for(std::vector<object>::const_iterator it(val.begin()), it_end(val.end()); 366 it != it_end; 367 ++it) { 368 it->pack(p); 369 } 370 } 306 371 307 372 … … 335 400 return this; 336 401 } 402 void object_map::pack(dynamic_packer& p) const 403 { 404 p.pack_map(val.size()); 405 for(std::map<object, object>::const_iterator it(val.begin()), it_end(val.end()); 406 it != it_end; 407 ++it) { 408 it->first.pack(p); 409 it->second.pack(p); 410 } 411 } 337 412 338 413 -
lang/c/msgpack/trunk/cpp/object.hpp
r18608 r18610 1 1 #ifndef MSGPACK_OBJECT_HPP__ 2 2 #define MSGPACK_OBJECT_HPP__ 3 #include <iostream>4 3 5 4 #include <cstddef> … … 47 46 typedef std::map<object, object> map; 48 47 typedef std::vector<object> array; 48 49 class dynamic_packer; 49 50 50 51 … … 73 74 virtual bool operator< (const object_class* x) const { throw cast_error(); } 74 75 virtual bool operator> (const object_class* x) const { throw cast_error(); } 76 virtual void pack(dynamic_packer& p) const = 0; 75 77 operator bool() const { return xbool(); } // FIXME !isnil(); 76 78 operator uint8_t() const { return xu8(); } … … 129 131 bool operator< (object x) const { return val->operator< (x.val); } 130 132 bool operator> (object x) const { return val->operator> (x.val); } 133 void pack(dynamic_packer& p) const { val->pack(p); } 131 134 operator bool() const { return val->operator bool(); } 132 135 operator uint8_t() const { return val->operator uint8_t(); } … … 161 164 bool isnil() const; 162 165 bool operator== (const object_class* x) const; 166 void pack(dynamic_packer& p) const; 163 167 const object_class* inspect(std::ostream& s) const; 164 168 }; … … 167 171 bool xbool() const; 168 172 bool operator== (const object_class* x) const; 173 void pack(dynamic_packer& p) const; 169 174 const object_class* inspect(std::ostream& s) const; 170 175 }; … … 173 178 bool xbool() const; 174 179 bool operator== (const object_class* x) const; 180 void pack(dynamic_packer& p) const; 175 181 const object_class* inspect(std::ostream& s) const; 176 182 }; … … 192 198 bool operator< (const object_class* x) const; \ 193 199 bool operator> (const object_class* x) const; \ 200 void pack(dynamic_packer& p) const; \ 194 201 const object_class* inspect(std::ostream& s) const; \ 195 202 private: \ … … 210 217 211 218 #define FLOAT_CLASS(TYPE, NAME) \ 212 struct object_##NAME : object_class { \ 213 object_##NAME(TYPE v) : val(v) {} \ 214 uint8_t xu8 () const; \ 215 uint16_t xu16 () const; \ 216 uint32_t xu32 () const; \ 217 uint64_t xu64 () const; \ 218 int8_t xi8 () const; \ 219 int16_t xi16 () const; \ 220 int32_t xi32 () const; \ 221 int64_t xi64 () const; \ 222 float xfloat () const; \ 223 double xdouble() const; \ 224 bool operator== (const object_class* x) const; \ 225 bool operator< (const object_class* x) const; \ 226 bool operator> (const object_class* x) const; \ 227 const object_class* inspect(std::ostream& s) const; \ 228 private: \ 229 TYPE val; \ 219 struct object_##NAME : object_class { \ 220 object_##NAME(TYPE v) : val(v) {} \ 221 uint8_t xu8 () const; \ 222 uint16_t xu16 () const; \ 223 uint32_t xu32 () const; \ 224 uint64_t xu64 () const; \ 225 int8_t xi8 () const; \ 226 int16_t xi16 () const; \ 227 int32_t xi32 () const; \ 228 int64_t xi64 () const; \ 229 float xfloat () const; \ 230 double xdouble() const; \ 231 bool operator== (const object_class* x) const; \ 232 bool operator< (const object_class* x) const; \ 233 bool operator> (const object_class* x) const; \ 234 void pack(dynamic_packer& p) const; \ 235 const object_class* inspect(std::ostream& s) const; \ 236 private: \ 237 TYPE val; \ 230 238 }; 231 239 … … 243 251 bool operator< (const object_class* x) const; \ 244 252 bool operator> (const object_class* x) const; \ 253 void pack(dynamic_packer& p) const; \ 245 254 const object_class* inspect(std::ostream& s) const; \ 246 255 private: \ … … 262 271 bool operator== (const object_class* x) const; 263 272 // FIXME operator<, operator> 273 void pack(dynamic_packer& p) const; 264 274 const object_class* inspect(std::ostream& s) const; 265 275 public: … … 277 287 bool operator== (const object_class* x) const; 278 288 // FIXME operator<, operator> 289 void pack(dynamic_packer& p) const; 279 290 const object_class* inspect(std::ostream& s) const; 280 291 public: -
lang/c/msgpack/trunk/cpp/test.cpp
r18599 r18610 1 1 #include <iostream> 2 #include <string> 2 3 #include <msgpack/unpack.hpp> 4 #include <msgpack/pack.hpp> 3 5 4 6 class checker { 5 7 public: 6 8 void check(const char* d, size_t len, msgpack::object should) { 9 using msgpack::object; 7 10 try { 8 11 std::cout << "----" << std::endl; 9 msgpack::object o; 12 13 object o; 10 14 try { 11 15 o = msgpack::unpack(d, len, m_zone); … … 15 19 return; 16 20 } 21 17 22 std::cout << o << std::endl; 18 23 if(o != should) { 19 24 std::cout << "** TEST FAILED **" << std::endl; 20 25 } 26 27 try { 28 std::string s; 29 msgpack::pack(s, o); 30 object ro = msgpack::unpack(s.data(), s.size(), m_zone); 31 if(ro != o) { throw std::runtime_error("NOT MATCH"); } 32 } catch (std::runtime_error& e) { 33 std::cout << "** REUNPACK FAILED **" << std::endl; 34 std::cout << e.what() << std::endl; 35 } catch (...) { 36 std::cout << "** REUNPACK FAILED **" << std::endl; 37 std::cout << "unknown error" << std::endl; 38 } 39 21 40 } catch (...) { m_zone.clear(); throw; } 22 41 m_zone.clear(); … … 28 47 int main(void) 29 48 { 49 checker c; 30 50 31 checker c; 51 { // SimpleValue 52 msgpack::zone z; 53 const char d[] = { 54 0x93, 0xc0, 0xc2, 0xc3, 55 }; 56 c.check(d, sizeof(d), 57 z.narray( 58 z.nnil(), z.nfalse(), z.ntrue() 59 ) 60 ); 61 } 32 62 33 { // SimpleValue 34 msgpack::zone z; 35 const char d[] = { 36 0x93, 0xc0, 0xc2, 0xc3, 37 }; 38 c.check(d, sizeof(d), 39 z.narray( 40 z.nnil(), z.nfalse(), z.ntrue() 41 ) 42 ); 63 { // Fixnum 64 msgpack::zone z; 65 const char d[] = { 66 0x92, 67 0x93, 0x00, 0x40, 0x7f, 68 0x93, 0xe0, 0xf0, 0xff, 69 }; 70 c.check(d, sizeof(d), 71 z.narray( 72 z.narray( 73 z.nu8(0), 74 z.nu8(64), 75 z.nu8(127) 76 ), 77 z.narray( 78 z.ni8(-32), 79 z.ni8(-16), 80 z.ni8(-1) 81 ) 82 ) 83 ); 84 } 85 86 { // FixArray 87 msgpack::zone z; 88 const char d[] = { 89 0x92, 90 0x90, 91 0x91, 92 0x91, 0xc0, 93 }; 94 c.check(d, sizeof(d), 95 z.narray( 96 z.narray(), 97 z.narray( 98 z.narray( 99 z.nnil() 100 ) 101 ) 102 ) 103 ); 104 } 105 106 { // FixRaw 107 msgpack::zone z; 108 const char d[] = { 109 0x94, 110 0xa0, 111 0xa1, 'a', 112 0xa2, 'b', 'c', 113 0xa3, 'd', 'e', 'f', 114 }; 115 c.check(d, sizeof(d), 116 z.narray( 117 z.nraw("", 0), 118 z.nraw("a", 1), 119 z.nraw("bc", 2), 120 z.nraw("def", 3) 121 ) 122 ); 123 } 124 125 return 0; 43 126 } 44 127 45 { // Fixnum46 msgpack::zone z;47 const char d[] = {48 0x92,49 0x93, 0x00, 0x40, 0x7f,50 0x93, 0xe0, 0xf0, 0xff,51 };52 c.check(d, sizeof(d),53 z.narray(54 z.narray(55 z.nu8(0),56 z.nu8(64),57 z.nu8(127)58 ),59 z.narray(60 z.ni8(-32),61 z.ni8(-16),62 z.ni8(-1)63 )64 )65 );66 }67 68 { // FixArray69 msgpack::zone z;70 const char d[] = {71 0x92,72 0x90,73 0x91,74 0x91, 0xc0,75 };76 c.check(d, sizeof(d),77 z.narray(78 z.narray(),79 z.narray(80 z.narray(81 z.nnil()82 )83 )84 )85 );86 }87 88 { // FixRaw89 msgpack::zone z;90 const char d[] = {91 0x94,92 0xa0,93 0xa1, 'a',94 0xa2, 'b', 'c',95 0xa3, 'd', 'e', 'f',96 };97 c.check(d, sizeof(d),98 z.narray(99 z.nraw("", 0),100 z.nraw("a", 1),101 z.nraw("bc", 2),102 z.nraw("def", 3)103 )104 );105 }106 107 108 return 0;109 }110 -
lang/c/msgpack/trunk/msgpack/pack/inline_impl.h
r18599 r18610 19 19 #define MSGPACK_PACK_INLINE_IMPL_H__ 20 20 21 #include <string.h> 22 #include <arpa/inet.h> 21 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) 22 #if __BYTE_ORDER == __LITTLE_ENDIAN 23 #define __LITTLE_ENDIAN__ 24 #elif __BYTE_ORDER == __BIG_ENDIAN 25 #define __BIG_ENDIAN__ 26 #endif 27 #endif 23 28 24 29 #ifdef __LITTLE_ENDIAN__ … … 48 53 #endif 49 54 55 #ifndef msgpack_pack_inline_func(name) 56 #define msgpack_pack_inline_func(name) \ 57 inline void msgpack_pack_##name 58 #endif 50 59 51 60 /* … … 54 63 55 64 // wrapper 56 inline void msgpack_pack_int(msgpack_pack_context x, int d)65 msgpack_pack_inline_func(int)(msgpack_pack_context x, int d) 57 66 { 58 67 if(d < -32) { … … 87 96 88 97 // wrapper 89 inline void msgpack_pack_unsigned_int(msgpack_pack_context x, unsigned int d)98 msgpack_pack_inline_func(unsigned_int)(msgpack_pack_context x, unsigned int d) 90 99 { 91 100 if(d < 128) { 92 101 // fixnum 93 msgpack_pack_append_buffer(x, (u int8_t*)&d, 1);102 msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); 94 103 } else if(d < 256) { 95 104 // unsigned 8 … … 107 116 } 108 117 109 inline void msgpack_pack_unsigned_int_8(msgpack_pack_context x, uint8_t d)118 msgpack_pack_inline_func(unsigned_int_8)(msgpack_pack_context x, uint8_t d) 110 119 { 111 120 if(d < 128) { … … 117 126 } 118 127 119 inline void msgpack_pack_unsigned_int_16(msgpack_pack_context x, uint16_t d)128 msgpack_pack_inline_func(unsigned_int_16)(msgpack_pack_context x, uint16_t d) 120 129 { 121 130 const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; … … 123 132 } 124 133 125 inline void msgpack_pack_unsigned_int_32(msgpack_pack_context x, uint32_t d)134 msgpack_pack_inline_func(unsigned_int_32)(msgpack_pack_context x, uint32_t d) 126 135 { 127 136 const unsigned char buf[5] = {0xce, STORE_BE32(d)}; … … 129 138 } 130 139 131 inline void msgpack_pack_unsigned_int_64(msgpack_pack_context x, uint64_t d)140 msgpack_pack_inline_func(unsigned_int_64)(msgpack_pack_context x, uint64_t d) 132 141 { 133 142 // FIXME optimization … … 136 145 } 137 146 138 inline void msgpack_pack_signed_int_8(msgpack_pack_context x, int8_t d)147 msgpack_pack_inline_func(signed_int_8)(msgpack_pack_context x, int8_t d) 139 148 { 140 149 if(d > 0) { … … 148 157 } 149 158 150 inline void msgpack_pack_signed_int_16(msgpack_pack_context x, int16_t d)159 msgpack_pack_inline_func(signed_int_16)(msgpack_pack_context x, int16_t d) 151 160 { 152 161 const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; … … 154 163 } 155 164 156 inline void msgpack_pack_signed_int_32(msgpack_pack_context x, int32_t d)165 msgpack_pack_inline_func(signed_int_32)(msgpack_pack_context x, int32_t d) 157 166 { 158 167 const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; … … 160 169 } 161 170 162 inline void msgpack_pack_signed_int_64(msgpack_pack_context x, int64_t d)171 msgpack_pack_inline_func(signed_int_64)(msgpack_pack_context x, int64_t d) 163 172 { 164 173 // FIXME optimization … … 172 181 */ 173 182 174 inline void msgpack_pack_float(msgpack_pack_context x, float d)183 msgpack_pack_inline_func(float)(msgpack_pack_context x, float d) 175 184 { 176 185 uint32_t n = *((uint32_t*)&d); // FIXME … … 179 188 } 180 189 181 inline void msgpack_pack_double(msgpack_pack_context x, double d)190 msgpack_pack_inline_func(double)(msgpack_pack_context x, double d) 182 191 { 183 192 uint64_t n = *((uint64_t*)&d); // FIXME … … 191 200 */ 192 201 193 inline void msgpack_pack_nil(msgpack_pack_context x)202 msgpack_pack_inline_func(nil)(msgpack_pack_context x) 194 203 { 195 204 static const unsigned char d = 0xc0; … … 202 211 */ 203 212 204 inline void msgpack_pack_true(msgpack_pack_context x)213 msgpack_pack_inline_func(true)(msgpack_pack_context x) 205 214 { 206 215 static const unsigned char d = 0xc3; … … 208 217 } 209 218 210 inline void msgpack_pack_false(msgpack_pack_context x)219 msgpack_pack_inline_func(false)(msgpack_pack_context x) 211 220 { 212 221 static const unsigned char d = 0xc2; … … 219 228 */ 220 229 221 inline void msgpack_pack_array(msgpack_pack_context x, unsigned int n)230 msgpack_pack_inline_func(array)(msgpack_pack_context x, unsigned int n) 222 231 { 223 232 if(n < 16) { … … 240 249 */ 241 250 242 inline void msgpack_pack_map(msgpack_pack_context x, unsigned int n)251 msgpack_pack_inline_func(map)(msgpack_pack_context x, unsigned int n) 243 252 { 244 253 if(n < 16) { … … 261 270 */ 262 271 263 inline void msgpack_pack_string(msgpack_pack_context x, const char* b)272 msgpack_pack_inline_func(string)(msgpack_pack_context x, const char* b) 264 273 { 265 274 uint32_t l = strlen(b); … … 267 276 } 268 277 269 inline void msgpack_pack_raw(msgpack_pack_context x, const void* b, size_t l)278 msgpack_pack_inline_func(raw)(msgpack_pack_context x, const void* b, size_t l) 270 279 { 271 280 if(l < 32) { … … 281 290 msgpack_pack_append_buffer(x, buf, 5); 282 291 } 283 msgpack_pack_append_buffer(x, b, l); 284 } 285 292 msgpack_pack_append_buffer(x, (const unsigned char*)b, l); 293 } 294 295 296 #undef msgpack_pack_inline_func(name) 286 297 287 298 #undef STORE_BE16(d) … … 289 300 #undef STORE_BE64(d) 290 301 291 292 302 #endif /* msgpack/pack/inline_impl.h */ 293 303 -
lang/c/msgpack/trunk/ruby/pack_inline.h
r17708 r18610 28 28 } 29 29 30 #include <string.h> 31 #include <arpa/inet.h> /* __BYTE_ORDER */ 30 32 #include "msgpack/pack/inline_impl.h" 31 33
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)