mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-20 22:31:33 +02:00
Compare commits
226 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2bf3f1856f | ||
![]() |
f145129f6e | ||
![]() |
8d365458d5 | ||
![]() |
55cfbf378e | ||
![]() |
d76093b148 | ||
![]() |
7873e41e00 | ||
![]() |
9817e9b18d | ||
![]() |
519716bbe4 | ||
![]() |
c2dd22ec10 | ||
![]() |
232aced926 | ||
![]() |
63b9a876b0 | ||
![]() |
5cf85a82d3 | ||
![]() |
b9f78821d4 | ||
![]() |
f4c5b15cc6 | ||
![]() |
4d33bd456c | ||
![]() |
3a5f7f53ff | ||
![]() |
1ed4236bcf | ||
![]() |
5ff2c6be74 | ||
![]() |
dd18402737 | ||
![]() |
686e8ca0f0 | ||
![]() |
5aa47d6677 | ||
![]() |
0d44348c7d | ||
![]() |
35929b46ae | ||
![]() |
ba3ba0367c | ||
![]() |
7ce866ad7c | ||
![]() |
0ae1965f6b | ||
![]() |
eb9e892491 | ||
![]() |
e39e1d4f60 | ||
![]() |
93a95725fc | ||
![]() |
4758d9f04b | ||
![]() |
d39c016e1d | ||
![]() |
5393a0df16 | ||
![]() |
68176e10f5 | ||
![]() |
c6a2569af8 | ||
![]() |
3424dc916c | ||
![]() |
c8ad32a39e | ||
![]() |
d0b76814b0 | ||
![]() |
bf3cb63d46 | ||
![]() |
8da7b692f6 | ||
![]() |
320c79db49 | ||
![]() |
ebf64d9892 | ||
![]() |
c12d5b8461 | ||
![]() |
7186edc45e | ||
![]() |
c94772104d | ||
![]() |
9374571056 | ||
![]() |
c232e91f83 | ||
![]() |
5b8777026a | ||
![]() |
51e435d46c | ||
![]() |
d3f9ab7dec | ||
![]() |
1ba330c473 | ||
![]() |
dba7f480ed | ||
![]() |
3a39accb0b | ||
![]() |
387eca6fbf | ||
![]() |
b2381d0513 | ||
![]() |
92952f6566 | ||
![]() |
a62a5d6c69 | ||
![]() |
0627324da6 | ||
![]() |
394331cd4e | ||
![]() |
0491768fb2 | ||
![]() |
a5705183d6 | ||
![]() |
95a6316cc7 | ||
![]() |
be32e3b1fb | ||
![]() |
b4cb5e23c0 | ||
![]() |
1375732c80 | ||
![]() |
7fbe845434 | ||
![]() |
3afa9f265e | ||
![]() |
ec8932d6a1 | ||
![]() |
f7a9805f7b | ||
![]() |
aaef612a05 | ||
![]() |
da9b75f553 | ||
![]() |
d3906bd1ab | ||
![]() |
4db90d23ac | ||
![]() |
bb455c1f71 | ||
![]() |
3275eee281 | ||
![]() |
0fe79a7752 | ||
![]() |
794adf9469 | ||
![]() |
718227bc2c | ||
![]() |
390c5ad8a0 | ||
![]() |
2da4b17d0b | ||
![]() |
d19c488929 | ||
![]() |
0db5e0439d | ||
![]() |
6083bad5ff | ||
![]() |
63a507a123 | ||
![]() |
294e3fe7ab | ||
![]() |
e5c49dae13 | ||
![]() |
900785e1aa | ||
![]() |
979efbb950 | ||
![]() |
7da1a9b3cf | ||
![]() |
661f273481 | ||
![]() |
cb5c878b77 | ||
![]() |
34ed3bc9e3 | ||
![]() |
ad7cf12128 | ||
![]() |
6fb6283463 | ||
![]() |
b45dc8db61 | ||
![]() |
3397d261d8 | ||
![]() |
eaa8be8ddd | ||
![]() |
0f9dcb8610 | ||
![]() |
3446c475d7 | ||
![]() |
ffef0a0b6f | ||
![]() |
580fbe77e9 | ||
![]() |
e92e99c4d1 | ||
![]() |
c3f793aa09 | ||
![]() |
3d9908c8e5 | ||
![]() |
7b198d5966 | ||
![]() |
601209c83c | ||
![]() |
78db826a75 | ||
![]() |
c2a63b2c54 | ||
![]() |
7c27624161 | ||
![]() |
d86b90e663 | ||
![]() |
d72bef9f9a | ||
![]() |
37972dfe3e | ||
![]() |
c6496ddf13 | ||
![]() |
a95e8c790c | ||
![]() |
03942a1b90 | ||
![]() |
d4317fdc85 | ||
![]() |
b5010c71a9 | ||
![]() |
9015bd4ecf | ||
![]() |
fe2421275d | ||
![]() |
257270c1eb | ||
![]() |
0b33a634a6 | ||
![]() |
3e396ef146 | ||
![]() |
1b07b61c04 | ||
![]() |
fa2efcdb5b | ||
![]() |
a345131aaa | ||
![]() |
ffc16736ba | ||
![]() |
3c3df3133c | ||
![]() |
16a208243b | ||
![]() |
dd407410d2 | ||
![]() |
8f52ed26c7 | ||
![]() |
479263989b | ||
![]() |
87e1adcb40 | ||
![]() |
8e166de1d1 | ||
![]() |
3aef3edbad | ||
![]() |
b2fdaac99e | ||
![]() |
9a342ab7f8 | ||
![]() |
a961f646e5 | ||
![]() |
8596d36d02 | ||
![]() |
423e5132a1 | ||
![]() |
1581acfd14 | ||
![]() |
5d4189306a | ||
![]() |
f61b282886 | ||
![]() |
3fd28d0792 | ||
![]() |
dd53b141ef | ||
![]() |
99d0a41ec6 | ||
![]() |
b8e5b918a3 | ||
![]() |
71af62c1df | ||
![]() |
7d5f04917e | ||
![]() |
46d7c65621 | ||
![]() |
87f5df1503 | ||
![]() |
9a77ab57f6 | ||
![]() |
20a7ff0be8 | ||
![]() |
075081a521 | ||
![]() |
f4387e9746 | ||
![]() |
0d14239c21 | ||
![]() |
2475187c7d | ||
![]() |
4d6e9ffaa2 | ||
![]() |
6184e17a42 | ||
![]() |
e814986b4e | ||
![]() |
3a9f74e79c | ||
![]() |
a1fb1507d4 | ||
![]() |
85ca594118 | ||
![]() |
9c9393bff9 | ||
![]() |
560bd901f8 | ||
![]() |
114ef92d42 | ||
![]() |
1840ef70ae | ||
![]() |
935db853f0 | ||
![]() |
17d2ca2d63 | ||
![]() |
c930f5367b | ||
![]() |
d8a3bc920c | ||
![]() |
711e4817a5 | ||
![]() |
8ed1d61529 | ||
![]() |
7cd359c1fd | ||
![]() |
f6cc274bbc | ||
![]() |
2598d98922 | ||
![]() |
b3846a411f | ||
![]() |
8617213149 | ||
![]() |
3628ea22d4 | ||
![]() |
8903e2dea9 | ||
![]() |
6a0729ea91 | ||
![]() |
62231983d7 | ||
![]() |
b0062a7f6f | ||
![]() |
0b3db48976 | ||
![]() |
b140b27b9a | ||
![]() |
45321baa66 | ||
![]() |
e0bd2a2911 | ||
![]() |
0b083030b1 | ||
![]() |
ea41db4cc7 | ||
![]() |
e53799d779 | ||
![]() |
5a201be844 | ||
![]() |
2aeeef3368 | ||
![]() |
28e113fd00 | ||
![]() |
c7b6bb7303 | ||
![]() |
5710b87b06 | ||
![]() |
d449b1d20d | ||
![]() |
8c62d93bca | ||
![]() |
5bd53f018f | ||
![]() |
b8cc8b72bd | ||
![]() |
840388720e | ||
![]() |
cc7379d532 | ||
![]() |
c612a177cc | ||
![]() |
163fc6f589 | ||
![]() |
44cdc5f976 | ||
![]() |
c7f193e441 | ||
![]() |
09978e500a | ||
![]() |
2fad10ca3f | ||
![]() |
e707b7a600 | ||
![]() |
e7403013e5 | ||
![]() |
c3f9696268 | ||
![]() |
cb24a0c24f | ||
![]() |
0efb8160b9 | ||
![]() |
bf13ba72b5 | ||
![]() |
6fc38d1669 | ||
![]() |
879c70f93a | ||
![]() |
6fda01111e | ||
![]() |
4f4fa39cd5 | ||
![]() |
c60b6be548 | ||
![]() |
9b4b49a6a8 | ||
![]() |
c0baf9b873 | ||
![]() |
65ef9cab66 | ||
![]() |
6ffee9e54a | ||
![]() |
43360e3786 | ||
![]() |
4d708aa4da | ||
![]() |
5d51129e65 | ||
![]() |
a0b82e39e1 | ||
![]() |
9642368835 | ||
![]() |
cbf7afc1cc |
21
Makefile.am
21
Makefile.am
@@ -1,5 +1,4 @@
|
||||
if ENABLE_CXX
|
||||
export ERB
|
||||
SUBDIRS = c cpp
|
||||
else
|
||||
SUBDIRS = c
|
||||
@@ -9,22 +8,6 @@ nobase_include_HEADERS = \
|
||||
msgpack/pack_define.h \
|
||||
msgpack/pack_template.h \
|
||||
msgpack/unpack_define.h \
|
||||
msgpack/unpack_template.h
|
||||
|
||||
EXTRA_DIST = \
|
||||
ruby/bench.rb \
|
||||
ruby/extconf.rb \
|
||||
ruby/gem/README \
|
||||
ruby/gem/Rakefile \
|
||||
ruby/gem/test/test_helper.rb \
|
||||
ruby/gengem.sh \
|
||||
ruby/msgpack.gemspec \
|
||||
ruby/pack.c \
|
||||
ruby/pack.h \
|
||||
ruby/rbinit.c \
|
||||
ruby/test_case.rb \
|
||||
ruby/test_format.rb \
|
||||
ruby/test_pack.rb \
|
||||
ruby/unpack.c \
|
||||
ruby/unpack.h
|
||||
msgpack/unpack_template.h \
|
||||
msgpack/sysdep.h
|
||||
|
||||
|
4
README
4
README
@@ -8,9 +8,7 @@ Binary-based efficient data interchange format.
|
||||
MessagePack is only tested on Linux and Mac OS X, but it may run on other
|
||||
UNIX-like platforms.
|
||||
|
||||
Following programs is required to build:
|
||||
- gcc >= 4.1 with C++ support
|
||||
- ruby >= 1.8 (ruby is used as a preprocessor)
|
||||
gcc >= 4.1 is required to build.
|
||||
|
||||
|
||||
*Installation
|
||||
|
@@ -31,11 +31,8 @@ if test x"$1" = x"--help"; then
|
||||
fi
|
||||
|
||||
|
||||
#if [ -z "$NO_NEST" ];then
|
||||
# cd c && ./bootstrap $@; cd ..
|
||||
# cd cpp && ./bootstrap $@; cd ..
|
||||
#fi
|
||||
mkdir -p ac
|
||||
(cd cpp && ./preprocess.sh $@; cd ..)
|
||||
|
||||
|
||||
ACLOCAL="aclocal"
|
||||
|
@@ -3,16 +3,26 @@ lib_LTLIBRARIES = libmsgpackc.la
|
||||
libmsgpackc_la_SOURCES = \
|
||||
unpack.c \
|
||||
object.c \
|
||||
vrefbuffer.c \
|
||||
zone.c
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack.h \
|
||||
msgpack/sbuffer.h \
|
||||
msgpack/vrefbuffer.h \
|
||||
msgpack/pack.h \
|
||||
msgpack/unpack.h \
|
||||
msgpack/object.h \
|
||||
msgpack/zone.h
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpackc_la_LDFLAGS = -version-info 1:0:0
|
||||
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
|
||||
|
||||
check_PROGRAMS = \
|
||||
msgpackc_test
|
||||
|
||||
msgpackc_test_SOURCES = test.cpp
|
||||
msgpackc_test_CXXFLAGS = -I$(top_srcdir) -I$(top_srcdir)/c
|
||||
msgpackc_test_LDFLAGS = libmsgpackc.la -lgtest_main
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
@@ -20,3 +20,4 @@
|
||||
#include "msgpack/pack.h"
|
||||
#include "msgpack/unpack.h"
|
||||
#include "msgpack/sbuffer.h"
|
||||
#include "msgpack/vrefbuffer.h"
|
||||
|
10
c/object.c
10
c/object.c
@@ -18,7 +18,17 @@
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/pack.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#ifndef PRIu64
|
||||
#define PRIu64 "I64u"
|
||||
#endif
|
||||
#ifndef PRIi64
|
||||
#define PRIi64 "I64d"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
|
||||
|
@@ -19,9 +19,7 @@
|
||||
#define MSGPACK_OBJECT_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "msgpack/sysdep.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
4
c/pack.h
4
c/pack.h
@@ -18,11 +18,9 @@
|
||||
#ifndef MSGPACK_PACK_H__
|
||||
#define MSGPACK_PACK_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "msgpack/pack_define.h"
|
||||
#include "msgpack/object.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
424
c/test.cpp
Normal file
424
c/test.cpp
Normal file
@@ -0,0 +1,424 @@
|
||||
#include "msgpack.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const unsigned int kLoop = 10000;
|
||||
const double kEPS = 1e-10;
|
||||
|
||||
#define GEN_TEST_SIGNED(test_type, func_type) \
|
||||
do { \
|
||||
vector<test_type> v; \
|
||||
v.push_back(0); \
|
||||
v.push_back(1); \
|
||||
v.push_back(-1); \
|
||||
v.push_back(numeric_limits<test_type>::min()); \
|
||||
v.push_back(numeric_limits<test_type>::max()); \
|
||||
for (unsigned int i = 0; i < kLoop; i++) \
|
||||
v.push_back(rand()); \
|
||||
for (unsigned int i = 0; i < v.size() ; i++) { \
|
||||
test_type val = v[i]; \
|
||||
msgpack_sbuffer sbuf; \
|
||||
msgpack_sbuffer_init(&sbuf); \
|
||||
msgpack_packer pk; \
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); \
|
||||
msgpack_pack_##func_type(&pk, val); \
|
||||
msgpack_zone z; \
|
||||
msgpack_zone_init(&z, 2048); \
|
||||
msgpack_object obj; \
|
||||
msgpack_unpack_return ret = \
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj); \
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret); \
|
||||
if (val < 0) { \
|
||||
EXPECT_EQ(MSGPACK_OBJECT_NEGATIVE_INTEGER, obj.type); \
|
||||
EXPECT_EQ(val, obj.via.i64); \
|
||||
} else { \
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); \
|
||||
EXPECT_EQ(val, obj.via.u64); \
|
||||
} \
|
||||
msgpack_zone_destroy(&z); \
|
||||
msgpack_sbuffer_destroy(&sbuf); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define GEN_TEST_UNSIGNED(test_type, func_type) \
|
||||
do { \
|
||||
vector<test_type> v; \
|
||||
v.push_back(0); \
|
||||
v.push_back(1); \
|
||||
v.push_back(2); \
|
||||
v.push_back(numeric_limits<test_type>::min()); \
|
||||
v.push_back(numeric_limits<test_type>::max()); \
|
||||
for (unsigned int i = 0; i < kLoop; i++) \
|
||||
v.push_back(rand()); \
|
||||
for (unsigned int i = 0; i < v.size() ; i++) { \
|
||||
test_type val = v[i]; \
|
||||
msgpack_sbuffer sbuf; \
|
||||
msgpack_sbuffer_init(&sbuf); \
|
||||
msgpack_packer pk; \
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); \
|
||||
msgpack_pack_##func_type(&pk, val); \
|
||||
msgpack_zone z; \
|
||||
msgpack_zone_init(&z, 2048); \
|
||||
msgpack_object obj; \
|
||||
msgpack_unpack_return ret = \
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj); \
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret); \
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); \
|
||||
EXPECT_EQ(val, obj.via.u64); \
|
||||
msgpack_zone_destroy(&z); \
|
||||
msgpack_sbuffer_destroy(&sbuf); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_short)
|
||||
{
|
||||
GEN_TEST_SIGNED(short, short);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_int)
|
||||
{
|
||||
GEN_TEST_SIGNED(int, int);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_long)
|
||||
{
|
||||
GEN_TEST_SIGNED(long, long);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_long_long)
|
||||
{
|
||||
GEN_TEST_SIGNED(long long, long_long);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_unsigned_short)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(unsigned short, unsigned_short);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_unsigned_int)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(unsigned int, unsigned_int);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_unsigned_long)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(unsigned long, unsigned_long);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_unsigned_long_long)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(unsigned long long, unsigned_long_long);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_uint8)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(uint8_t, uint8);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_uint16)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(uint16_t, uint16);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_uint32)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(uint32_t, uint32);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_uint64)
|
||||
{
|
||||
GEN_TEST_UNSIGNED(uint64_t, uint64);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_int8)
|
||||
{
|
||||
GEN_TEST_SIGNED(int8_t, int8);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_int16)
|
||||
{
|
||||
GEN_TEST_SIGNED(int16_t, int16);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_int32)
|
||||
{
|
||||
GEN_TEST_SIGNED(int32_t, int32);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_int64)
|
||||
{
|
||||
GEN_TEST_SIGNED(int64_t, int64);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_float)
|
||||
{
|
||||
vector<float> v;
|
||||
v.push_back(0.0);
|
||||
v.push_back(1.0);
|
||||
v.push_back(-1.0);
|
||||
v.push_back(numeric_limits<float>::min());
|
||||
v.push_back(numeric_limits<float>::max());
|
||||
v.push_back(nanf("tag"));
|
||||
v.push_back(1.0/0.0); // inf
|
||||
v.push_back(-(1.0/0.0)); // -inf
|
||||
for (unsigned int i = 0; i < kLoop; i++) {
|
||||
v.push_back(drand48());
|
||||
v.push_back(-drand48());
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < v.size() ; i++) {
|
||||
float val = v[i];
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_float(&pk, val);
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret =
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, obj.type);
|
||||
if (isnan(val))
|
||||
EXPECT_TRUE(isnan(obj.via.dec));
|
||||
else if (isinf(val))
|
||||
EXPECT_TRUE(isinf(obj.via.dec));
|
||||
else
|
||||
EXPECT_TRUE(fabs(obj.via.dec - val) <= kEPS);
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_double)
|
||||
{
|
||||
vector<double> v;
|
||||
v.push_back(0.0);
|
||||
v.push_back(-0.0);
|
||||
v.push_back(1.0);
|
||||
v.push_back(-1.0);
|
||||
v.push_back(numeric_limits<double>::min());
|
||||
v.push_back(numeric_limits<double>::max());
|
||||
v.push_back(nan("tag"));
|
||||
v.push_back(1.0/0.0); // inf
|
||||
v.push_back(-(1.0/0.0)); // -inf
|
||||
for (unsigned int i = 0; i < kLoop; i++) {
|
||||
v.push_back(drand48());
|
||||
v.push_back(-drand48());
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < v.size() ; i++) {
|
||||
double val = v[i];
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_double(&pk, val);
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret =
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, obj.type);
|
||||
if (isnan(val))
|
||||
EXPECT_TRUE(isnan(obj.via.dec));
|
||||
else if (isinf(val))
|
||||
EXPECT_TRUE(isinf(obj.via.dec));
|
||||
else
|
||||
EXPECT_TRUE(fabs(obj.via.dec - val) <= kEPS);
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_nil)
|
||||
{
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_nil(&pk);
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret =
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_NIL, obj.type);
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_true)
|
||||
{
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_true(&pk);
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret =
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, obj.type);
|
||||
EXPECT_EQ(true, obj.via.boolean);
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_false)
|
||||
{
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_false(&pk);
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret =
|
||||
msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, obj.type);
|
||||
EXPECT_EQ(false, obj.via.boolean);
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_array)
|
||||
{
|
||||
unsigned int array_size = 5;
|
||||
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_array(&pk, array_size);
|
||||
msgpack_pack_nil(&pk);
|
||||
msgpack_pack_true(&pk);
|
||||
msgpack_pack_false(&pk);
|
||||
msgpack_pack_int(&pk, 10);
|
||||
msgpack_pack_int(&pk, -10);
|
||||
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret;
|
||||
ret = msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
|
||||
EXPECT_EQ(array_size, obj.via.array.size);
|
||||
|
||||
for (unsigned int i = 0; i < obj.via.array.size; i++) {
|
||||
msgpack_object o = obj.via.array.ptr[i];
|
||||
switch (i) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_NIL, o.type);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, o.type);
|
||||
EXPECT_EQ(true, o.via.boolean);
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, o.type);
|
||||
EXPECT_EQ(false, o.via.boolean);
|
||||
break;
|
||||
case 3:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, o.type);
|
||||
EXPECT_EQ(10, o.via.u64);
|
||||
break;
|
||||
case 4:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_NEGATIVE_INTEGER, o.type);
|
||||
EXPECT_EQ(-10, o.via.i64);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_map)
|
||||
{
|
||||
unsigned int map_size = 2;
|
||||
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_map(&pk, map_size);
|
||||
msgpack_pack_true(&pk);
|
||||
msgpack_pack_false(&pk);
|
||||
msgpack_pack_int(&pk, 10);
|
||||
msgpack_pack_int(&pk, -10);
|
||||
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret;
|
||||
ret = msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type);
|
||||
EXPECT_EQ(map_size, obj.via.map.size);
|
||||
|
||||
for (unsigned int i = 0; i < map_size; i++) {
|
||||
msgpack_object key = obj.via.map.ptr[i].key;
|
||||
msgpack_object val = obj.via.map.ptr[i].val;
|
||||
switch (i) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, key.type);
|
||||
EXPECT_EQ(true, key.via.boolean);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, val.type);
|
||||
EXPECT_EQ(false, val.via.boolean);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, key.type);
|
||||
EXPECT_EQ(10, key.via.u64);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_NEGATIVE_INTEGER, val.type);
|
||||
EXPECT_EQ(-10, val.via.i64);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
||||
|
||||
TEST(MSGPACKC, simple_buffer_raw)
|
||||
{
|
||||
unsigned int raw_size = 7;
|
||||
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
msgpack_pack_raw(&pk, raw_size);
|
||||
msgpack_pack_raw_body(&pk, "fr", 2);
|
||||
msgpack_pack_raw_body(&pk, "syuki", 5);
|
||||
// invalid data
|
||||
msgpack_pack_raw_body(&pk, "", 0);
|
||||
msgpack_pack_raw_body(&pk, "kzk", 0);
|
||||
|
||||
msgpack_zone z;
|
||||
msgpack_zone_init(&z, 2048);
|
||||
msgpack_object obj;
|
||||
msgpack_unpack_return ret;
|
||||
ret = msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj);
|
||||
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_RAW, obj.type);
|
||||
EXPECT_EQ(raw_size, obj.via.raw.size);
|
||||
EXPECT_EQ(0, memcmp("frsyuki", obj.via.raw.ptr, raw_size));
|
||||
|
||||
msgpack_zone_destroy(&z);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
}
|
18
c/unpack.c
18
c/unpack.c
@@ -101,7 +101,7 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac
|
||||
{
|
||||
o->type = MSGPACK_OBJECT_ARRAY;
|
||||
o->via.array.size = 0;
|
||||
o->via.array.ptr = msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
|
||||
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
|
||||
if(o->via.array.ptr == NULL) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@@ -141,31 +141,31 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
|
||||
#define CTX_CAST(m) ((template_context*)(m))
|
||||
#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced
|
||||
|
||||
#define COUNTER_SIZE (sizeof(_msgpack_atomic_counter_t))
|
||||
|
||||
static const size_t COUNTER_SIZE = sizeof(unsigned int);
|
||||
|
||||
static inline void init_count(void* buffer)
|
||||
{
|
||||
*(volatile unsigned int*)buffer = 1;
|
||||
*(volatile _msgpack_atomic_counter_t*)buffer = 1;
|
||||
}
|
||||
|
||||
static inline void decl_count(void* buffer)
|
||||
{
|
||||
//if(--*(unsigned int*)buffer == 0) {
|
||||
if(__sync_sub_and_fetch((unsigned int*)buffer, 1) == 0) {
|
||||
// atomic if(--*(_msgpack_atomic_counter_t*)buffer == 0) { free(buffer); }
|
||||
if(_msgpack_sync_decr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer) == 0) {
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void incr_count(void* buffer)
|
||||
{
|
||||
//++*(unsigned int*)buffer;
|
||||
__sync_add_and_fetch((unsigned int*)buffer, 1);
|
||||
// atomic ++*(_msgpack_atomic_counter_t*)buffer;
|
||||
_msgpack_sync_incr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer);
|
||||
}
|
||||
|
||||
static inline unsigned int get_count(void* buffer)
|
||||
static inline _msgpack_atomic_counter_t get_count(void* buffer)
|
||||
{
|
||||
return *(volatile unsigned int*)buffer;
|
||||
return *(volatile _msgpack_atomic_counter_t*)buffer;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -15,13 +15,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef msgpack_unpacker_H__
|
||||
#define msgpack_unpacker_H__
|
||||
#ifndef MSGPACK_UNPACKER_H__
|
||||
#define MSGPACK_UNPACKER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include "msgpack/object.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
202
c/vrefbuffer.c
Normal file
202
c/vrefbuffer.c
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* MessagePack for C zero-copy buffer implementation
|
||||
*
|
||||
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "msgpack/vrefbuffer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct msgpack_vrefbuffer_chunk {
|
||||
struct msgpack_vrefbuffer_chunk* next;
|
||||
/* data ... */
|
||||
};
|
||||
|
||||
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
|
||||
size_t ref_size, size_t chunk_size)
|
||||
{
|
||||
vbuf->chunk_size = chunk_size;
|
||||
vbuf->ref_size = ref_size;
|
||||
|
||||
// glibcは72バイト以下のmallocが高速
|
||||
size_t nfirst = (sizeof(struct iovec) < 72/2) ?
|
||||
72 / sizeof(struct iovec) : 8;
|
||||
|
||||
struct iovec* array = (struct iovec*)malloc(
|
||||
sizeof(struct iovec) * nfirst);
|
||||
if(array == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vbuf->tail = array;
|
||||
vbuf->end = array + nfirst;
|
||||
vbuf->array = array;
|
||||
|
||||
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc(
|
||||
sizeof(msgpack_vrefbuffer_chunk) + chunk_size);
|
||||
if(chunk == NULL) {
|
||||
free(array);
|
||||
return false;
|
||||
}
|
||||
|
||||
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
|
||||
|
||||
ib->free = chunk_size;
|
||||
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
|
||||
ib->head = chunk;
|
||||
chunk->next = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
|
||||
{
|
||||
msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head;
|
||||
while(true) {
|
||||
msgpack_vrefbuffer_chunk* n = c->next;
|
||||
free(c);
|
||||
if(n != NULL) {
|
||||
c = n;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(vbuf->array);
|
||||
}
|
||||
|
||||
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
||||
const char* buf, unsigned int len)
|
||||
{
|
||||
if(vbuf->tail == vbuf->end) {
|
||||
const size_t nused = vbuf->tail - vbuf->array;
|
||||
const size_t nnext = nused * 2;
|
||||
|
||||
struct iovec* nvec = (struct iovec*)realloc(
|
||||
vbuf->array, sizeof(struct iovec)*nnext);
|
||||
if(nvec == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
vbuf->array = nvec;
|
||||
vbuf->end = nvec + nnext;
|
||||
vbuf->tail = nvec + nused;
|
||||
}
|
||||
|
||||
vbuf->tail->iov_base = (char*)buf;
|
||||
vbuf->tail->iov_len = len;
|
||||
++vbuf->tail;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
|
||||
const char* buf, unsigned int len)
|
||||
{
|
||||
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
|
||||
|
||||
if(ib->free < len) {
|
||||
size_t sz = vbuf->chunk_size;
|
||||
if(sz < len) {
|
||||
sz = len;
|
||||
}
|
||||
|
||||
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc(
|
||||
sizeof(msgpack_vrefbuffer_chunk) + sz);
|
||||
if(chunk == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
chunk->next = ib->head;
|
||||
ib->head = chunk;
|
||||
ib->free = sz;
|
||||
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
|
||||
}
|
||||
|
||||
char* m = ib->ptr;
|
||||
memcpy(m, buf, len);
|
||||
ib->free -= len;
|
||||
ib->ptr += len;
|
||||
|
||||
if(vbuf->tail != vbuf->array && m ==
|
||||
(const char*)((vbuf->tail-1)->iov_base) + (vbuf->tail-1)->iov_len) {
|
||||
(vbuf->tail-1)->iov_len += len;
|
||||
return 0;
|
||||
} else {
|
||||
return msgpack_vrefbuffer_append_ref(vbuf, m, len);
|
||||
}
|
||||
}
|
||||
|
||||
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
|
||||
{
|
||||
size_t sz = vbuf->chunk_size;
|
||||
|
||||
msgpack_vrefbuffer_chunk* empty = (msgpack_vrefbuffer_chunk*)malloc(
|
||||
sizeof(msgpack_vrefbuffer_chunk) + sz);
|
||||
if(empty == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
empty->next = NULL;
|
||||
|
||||
|
||||
const size_t nused = vbuf->tail - vbuf->array;
|
||||
if(to->tail + nused < vbuf->end) {
|
||||
const size_t tosize = to->tail - to->array;
|
||||
const size_t reqsize = nused + tosize;
|
||||
size_t nnext = (to->end - to->array) * 2;
|
||||
while(nnext < reqsize) {
|
||||
nnext *= 2;
|
||||
}
|
||||
|
||||
struct iovec* nvec = (struct iovec*)realloc(
|
||||
to->array, sizeof(struct iovec)*nnext);
|
||||
if(nvec == NULL) {
|
||||
free(empty);
|
||||
return -1;
|
||||
}
|
||||
|
||||
to->array = nvec;
|
||||
to->end = nvec + nnext;
|
||||
to->tail = nvec + tosize;
|
||||
}
|
||||
|
||||
memcpy(to->tail, vbuf->array, sizeof(struct iovec)*nused);
|
||||
|
||||
to->tail += nused;
|
||||
vbuf->tail = vbuf->array;
|
||||
|
||||
|
||||
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
|
||||
msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer;
|
||||
|
||||
msgpack_vrefbuffer_chunk* last = ib->head;
|
||||
while(last->next != NULL) {
|
||||
last = last->next;
|
||||
}
|
||||
last->next = toib->head;
|
||||
toib->head = ib->head;
|
||||
|
||||
if(toib->free < ib->free) {
|
||||
toib->free = ib->free;
|
||||
toib->ptr = ib->ptr;
|
||||
}
|
||||
|
||||
ib->head = empty;
|
||||
ib->free = sz;
|
||||
ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
110
c/vrefbuffer.h
Normal file
110
c/vrefbuffer.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* MessagePack for C zero-copy buffer implementation
|
||||
*
|
||||
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MSGPACK_VREFBUFFER_H__
|
||||
#define MSGPACK_VREFBUFFER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/uio.h>
|
||||
#else
|
||||
struct iovec {
|
||||
void *iov_base;
|
||||
size_t iov_len;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
|
||||
#define MSGPACK_VREFBUFFER_REF_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
|
||||
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
|
||||
#endif
|
||||
|
||||
struct msgpack_vrefbuffer_chunk;
|
||||
typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
|
||||
|
||||
typedef struct msgpack_vrefbuffer_inner_buffer {
|
||||
size_t free;
|
||||
char* ptr;
|
||||
msgpack_vrefbuffer_chunk* head;
|
||||
} msgpack_vrefbuffer_inner_buffer;
|
||||
|
||||
typedef struct msgpack_vrefbuffer {
|
||||
struct iovec* tail;
|
||||
struct iovec* end;
|
||||
struct iovec* array;
|
||||
|
||||
size_t chunk_size;
|
||||
size_t ref_size;
|
||||
|
||||
msgpack_vrefbuffer_inner_buffer inner_buffer;
|
||||
} msgpack_vrefbuffer;
|
||||
|
||||
|
||||
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
|
||||
size_t ref_size, size_t chunk_size);
|
||||
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
|
||||
|
||||
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len);
|
||||
|
||||
static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
|
||||
static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref);
|
||||
|
||||
int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
|
||||
const char* buf, unsigned int len);
|
||||
|
||||
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
||||
const char* buf, unsigned int len);
|
||||
|
||||
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
|
||||
|
||||
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
|
||||
|
||||
if(len < vbuf->ref_size) {
|
||||
return msgpack_vrefbuffer_append_copy(vbuf, buf, len);
|
||||
} else {
|
||||
return msgpack_vrefbuffer_append_ref(vbuf, buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
|
||||
{
|
||||
return vref->array;
|
||||
}
|
||||
|
||||
size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref)
|
||||
{
|
||||
return vref->tail - vref->array;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/vrefbuffer.h */
|
||||
|
153
c/zone.c
153
c/zone.c
@@ -19,83 +19,61 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline bool init_chunk_array(msgpack_zone_chunk_array* ca, size_t chunk_size)
|
||||
{
|
||||
// glibcは72バイト以下のmallocが高速
|
||||
const size_t nfirst = (sizeof(msgpack_zone_chunk) < 72/2) ?
|
||||
72 / sizeof(msgpack_zone_chunk) : 8;
|
||||
struct msgpack_zone_chunk {
|
||||
struct msgpack_zone_chunk* next;
|
||||
/* data ... */
|
||||
};
|
||||
|
||||
msgpack_zone_chunk* array = (msgpack_zone_chunk*)malloc(
|
||||
sizeof(msgpack_zone_chunk) * nfirst);
|
||||
if(!array) {
|
||||
static inline bool init_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size)
|
||||
{
|
||||
msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc(
|
||||
sizeof(msgpack_zone_chunk) + chunk_size);
|
||||
if(chunk == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t sz = chunk_size;
|
||||
|
||||
char* ptr = (char*)malloc(sz);
|
||||
if(ptr == NULL) {
|
||||
free(array);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ca->tail = array;
|
||||
ca->end = array + nfirst;
|
||||
ca->array = array;
|
||||
|
||||
array[0].free = sz;
|
||||
array[0].ptr = ptr;
|
||||
array[0].alloc = ptr;
|
||||
cl->head = chunk;
|
||||
cl->free = chunk_size;
|
||||
cl->ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
|
||||
chunk->next = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void destroy_chunk_array(msgpack_zone_chunk_array* ca)
|
||||
static inline void destroy_chunk_list(msgpack_zone_chunk_list* cl)
|
||||
{
|
||||
msgpack_zone_chunk* chunk = ca->array;
|
||||
for(; chunk != ca->tail+1; ++chunk) {
|
||||
free(chunk->alloc);
|
||||
msgpack_zone_chunk* c = cl->head;
|
||||
while(true) {
|
||||
msgpack_zone_chunk* n = c->next;
|
||||
free(c);
|
||||
if(n != NULL) {
|
||||
c = n;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(ca->array);
|
||||
}
|
||||
|
||||
static inline void clear_chunk_array(msgpack_zone_chunk_array* ca)
|
||||
static inline void clear_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size)
|
||||
{
|
||||
msgpack_zone_chunk* chunk = ca->array + 1;
|
||||
for(; chunk != ca->tail+1; ++chunk) {
|
||||
free(chunk->alloc);
|
||||
msgpack_zone_chunk* c = cl->head;
|
||||
while(true) {
|
||||
msgpack_zone_chunk* n = c->next;
|
||||
if(n != NULL) {
|
||||
free(c);
|
||||
c = n;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ca->tail = ca->array;
|
||||
|
||||
ca->array[0].free += ca->array[0].ptr - (char*)ca->array[0].alloc;
|
||||
ca->array[0].ptr = (char*)ca->array[0].alloc;
|
||||
cl->head->next = NULL;
|
||||
cl->free = chunk_size;
|
||||
cl->ptr = ((char*)cl->head) + sizeof(msgpack_zone_chunk);
|
||||
}
|
||||
|
||||
void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
|
||||
{
|
||||
msgpack_zone_chunk_array* const ca = &zone->chunk_array;
|
||||
|
||||
msgpack_zone_chunk* chunk = ++ca->tail;
|
||||
|
||||
if(chunk == ca->end) {
|
||||
// ca->arrayに空きがない
|
||||
// ca->arrayを拡張する
|
||||
|
||||
const size_t nused = ca->end - ca->array;
|
||||
const size_t nnext = (ca->end - ca->array) * 2;
|
||||
|
||||
chunk = (msgpack_zone_chunk*)realloc(ca->array,
|
||||
sizeof(msgpack_zone_chunk) * nnext);
|
||||
if(chunk == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ca->array = chunk;
|
||||
ca->end = chunk + nnext;
|
||||
chunk = ca->tail = chunk + nused;
|
||||
}
|
||||
msgpack_zone_chunk_list* const cl = &zone->chunk_list;
|
||||
|
||||
size_t sz = zone->chunk_size;
|
||||
|
||||
@@ -103,14 +81,15 @@ void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
|
||||
sz *= 2;
|
||||
}
|
||||
|
||||
char* ptr = (char*)malloc(sz);
|
||||
if(ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc(
|
||||
sizeof(msgpack_zone_chunk) + sz);
|
||||
|
||||
chunk->free = sz - size;
|
||||
chunk->ptr = ptr + size;
|
||||
chunk->alloc = ptr;
|
||||
char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
|
||||
|
||||
chunk->next = cl->head;
|
||||
cl->head = chunk;
|
||||
cl->free = sz - size;
|
||||
cl->ptr = ptr + size;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
@@ -185,19 +164,30 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
|
||||
|
||||
bool msgpack_zone_is_empty(msgpack_zone* zone)
|
||||
{
|
||||
msgpack_zone_chunk_array* const ca = &zone->chunk_array;
|
||||
msgpack_zone_chunk_list* const cl = &zone->chunk_list;
|
||||
msgpack_zone_finalizer_array* const fa = &zone->finalizer_array;
|
||||
return ca->array[0].ptr == ca->array[0].alloc &&
|
||||
ca->tail == ca->array &&
|
||||
return cl->free == zone->chunk_size && cl->head->next == NULL &&
|
||||
fa->tail == fa->array;
|
||||
}
|
||||
|
||||
|
||||
void msgpack_zone_destroy(msgpack_zone* zone)
|
||||
{
|
||||
destroy_finalizer_array(&zone->finalizer_array);
|
||||
destroy_chunk_list(&zone->chunk_list);
|
||||
}
|
||||
|
||||
void msgpack_zone_clear(msgpack_zone* zone)
|
||||
{
|
||||
clear_finalizer_array(&zone->finalizer_array);
|
||||
clear_chunk_list(&zone->chunk_list, zone->chunk_size);
|
||||
}
|
||||
|
||||
bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size)
|
||||
{
|
||||
zone->chunk_size = chunk_size;
|
||||
|
||||
if(!init_chunk_array(&zone->chunk_array, chunk_size)) {
|
||||
if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -206,30 +196,23 @@ bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size)
|
||||
return true;
|
||||
}
|
||||
|
||||
void msgpack_zone_destroy(msgpack_zone* zone)
|
||||
{
|
||||
destroy_finalizer_array(&zone->finalizer_array);
|
||||
destroy_chunk_array(&zone->chunk_array);
|
||||
}
|
||||
|
||||
void msgpack_zone_clear(msgpack_zone* zone)
|
||||
{
|
||||
clear_finalizer_array(&zone->finalizer_array);
|
||||
clear_chunk_array(&zone->chunk_array);
|
||||
}
|
||||
|
||||
msgpack_zone* msgpack_zone_new(size_t chunk_size)
|
||||
{
|
||||
msgpack_zone* zone = (msgpack_zone*)malloc(sizeof(msgpack_zone));
|
||||
msgpack_zone* zone = (msgpack_zone*)malloc(
|
||||
sizeof(msgpack_zone) + chunk_size);
|
||||
if(zone == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!msgpack_zone_init(zone, chunk_size)) {
|
||||
zone->chunk_size = chunk_size;
|
||||
|
||||
if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
|
||||
free(zone);
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
init_finalizer_array(&zone->finalizer_array);
|
||||
|
||||
return zone;
|
||||
}
|
||||
|
||||
|
46
c/zone.h
46
c/zone.h
@@ -26,37 +26,34 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct msgpack_zone_chunk {
|
||||
size_t free;
|
||||
char* ptr;
|
||||
void* alloc;
|
||||
} msgpack_zone_chunk;
|
||||
|
||||
typedef struct msgpack_zone_finalizer {
|
||||
void (*func)(void* data);
|
||||
void* data;
|
||||
} msgpack_zone_finalizer;
|
||||
|
||||
typedef struct msgpack_zone_chunk_array {
|
||||
msgpack_zone_chunk* tail;
|
||||
msgpack_zone_chunk* end;
|
||||
msgpack_zone_chunk* array;
|
||||
} msgpack_zone_chunk_array;
|
||||
|
||||
typedef struct msgpack_zone_finalizer_array {
|
||||
msgpack_zone_finalizer* tail;
|
||||
msgpack_zone_finalizer* end;
|
||||
msgpack_zone_finalizer* array;
|
||||
} msgpack_zone_finalizer_array;
|
||||
|
||||
struct msgpack_zone_chunk;
|
||||
typedef struct msgpack_zone_chunk msgpack_zone_chunk;
|
||||
|
||||
typedef struct msgpack_zone_chunk_list {
|
||||
size_t free;
|
||||
char* ptr;
|
||||
msgpack_zone_chunk* head;
|
||||
} msgpack_zone_chunk_list;
|
||||
|
||||
typedef struct msgpack_zone {
|
||||
msgpack_zone_chunk_array chunk_array;
|
||||
msgpack_zone_chunk_list chunk_list;
|
||||
msgpack_zone_finalizer_array finalizer_array;
|
||||
size_t chunk_size;
|
||||
} msgpack_zone;
|
||||
|
||||
#ifndef MSGPACK_ZONE_CHUNK_SIZE
|
||||
#define MSGPACK_ZONE_CHUNK_SIZE 2048
|
||||
#define MSGPACK_ZONE_CHUNK_SIZE 8192
|
||||
#endif
|
||||
|
||||
bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size);
|
||||
@@ -66,6 +63,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size);
|
||||
void msgpack_zone_free(msgpack_zone* zone);
|
||||
|
||||
static inline void* msgpack_zone_malloc(msgpack_zone* zone, size_t size);
|
||||
static inline void* msgpack_zone_malloc_no_align(msgpack_zone* zone, size_t size);
|
||||
|
||||
static inline bool msgpack_zone_push_finalizer(msgpack_zone* zone,
|
||||
void (*func)(void* data), void* data);
|
||||
@@ -82,23 +80,27 @@ void msgpack_zone_clear(msgpack_zone* zone);
|
||||
|
||||
void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size);
|
||||
|
||||
void* msgpack_zone_malloc(msgpack_zone* zone, size_t size)
|
||||
void* msgpack_zone_malloc_no_align(msgpack_zone* zone, size_t size)
|
||||
{
|
||||
size = ((size)+((MSGPACK_ZONE_ALIGN)-1)) & ~((MSGPACK_ZONE_ALIGN)-1);
|
||||
msgpack_zone_chunk_list* cl = &zone->chunk_list;
|
||||
|
||||
msgpack_zone_chunk* chunk = zone->chunk_array.tail;
|
||||
|
||||
if(chunk->free < size) {
|
||||
if(zone->chunk_list.free < size) {
|
||||
return msgpack_zone_malloc_expand(zone, size);
|
||||
}
|
||||
|
||||
char* ptr = chunk->ptr;
|
||||
chunk->ptr += size;
|
||||
chunk->free -= size;
|
||||
char* ptr = cl->ptr;
|
||||
cl->free -= size;
|
||||
cl->ptr += size;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* msgpack_zone_malloc(msgpack_zone* zone, size_t size)
|
||||
{
|
||||
return msgpack_zone_malloc_no_align(zone,
|
||||
((size)+((MSGPACK_ZONE_ALIGN)-1)) & ~((MSGPACK_ZONE_ALIGN)-1));
|
||||
}
|
||||
|
||||
|
||||
bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
|
||||
void (*func)(void* data), void* data);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
AC_INIT(msgpack/unpack_template.h)
|
||||
AC_CONFIG_AUX_DIR(ac)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.3.0)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.4.0)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_SUBST(CFLAGS)
|
||||
@@ -23,11 +23,6 @@ if test "$enable_cxx" != "no"; then
|
||||
if test "" = "$CXXFLAGS"; then
|
||||
CXXFLAGS="-g -O4"
|
||||
fi
|
||||
|
||||
AC_CHECK_PROG(ERB, erb, erb)
|
||||
if test "x$ERB" = x; then
|
||||
AC_MSG_ERROR([cannot find erb. Ruby is needed to build.])
|
||||
fi
|
||||
fi
|
||||
|
||||
# FIXME
|
||||
|
@@ -6,41 +6,39 @@ libmsgpack_la_SOURCES = \
|
||||
nobase_include_HEADERS = \
|
||||
msgpack.hpp \
|
||||
msgpack/sbuffer.hpp \
|
||||
msgpack/vrefbuffer.hpp \
|
||||
msgpack/pack.hpp \
|
||||
msgpack/unpack.hpp \
|
||||
msgpack/object.hpp \
|
||||
msgpack/zone.hpp \
|
||||
msgpack/type.hpp \
|
||||
msgpack/type/array.hpp \
|
||||
msgpack/type/boolean.hpp \
|
||||
msgpack/type/bool.hpp \
|
||||
msgpack/type/float.hpp \
|
||||
msgpack/type/integer.hpp \
|
||||
msgpack/type/int.hpp \
|
||||
msgpack/type/list.hpp \
|
||||
msgpack/type/deque.hpp \
|
||||
msgpack/type/map.hpp \
|
||||
msgpack/type/nil.hpp \
|
||||
msgpack/type/pair.hpp \
|
||||
msgpack/type/raw.hpp \
|
||||
msgpack/type/tuple.hpp
|
||||
|
||||
# FIXME
|
||||
object.lo: msgpack/type/tuple.hpp msgpack/zone.hpp
|
||||
|
||||
msgpack/type/tuple.hpp: msgpack/type/tuple.hpp.erb
|
||||
$(ERB) $< > $@.tmp
|
||||
mv $@.tmp $@
|
||||
|
||||
msgpack/zone.hpp: msgpack/zone.hpp.erb
|
||||
$(ERB) $< > $@.tmp
|
||||
mv $@.tmp $@
|
||||
|
||||
MOSTLYCLEANFILES = \
|
||||
msgpack/type/set.hpp \
|
||||
msgpack/type/string.hpp \
|
||||
msgpack/type/vector.hpp \
|
||||
msgpack/type/tuple.hpp \
|
||||
msgpack/zone.hpp
|
||||
|
||||
EXTRA_DIST = \
|
||||
msgpack/type/tuple.hpp.erb \
|
||||
msgpack/zone.hpp.erb
|
||||
msgpack/type/define.hpp \
|
||||
msgpack/type/tr1/unordered_map.hpp \
|
||||
msgpack/type/tr1/unordered_set.hpp
|
||||
|
||||
libmsgpack_la_LIBADD = -L../c -lmsgpackc
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpack_la_LDFLAGS = -version-info 1:0:0
|
||||
libmsgpack_la_LDFLAGS = -version-info 2:0:0
|
||||
|
||||
check_PROGRAMS = \
|
||||
msgpack_test
|
||||
|
||||
msgpack_test_SOURCES = test.cpp
|
||||
msgpack_test_CXXFLAGS = -I$(top_srcdir) -I$(top_srcdir)/c -I$(top_srcdir)/cpp
|
||||
msgpack_test_LDFLAGS = libmsgpack.la -lgtest_main
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
@@ -20,4 +20,5 @@
|
||||
#include "msgpack/pack.hpp"
|
||||
#include "msgpack/unpack.hpp"
|
||||
#include "msgpack/sbuffer.hpp"
|
||||
#include "msgpack/vrefbuffer.hpp"
|
||||
#include "msgpack.h"
|
||||
|
@@ -16,7 +16,6 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace msgpack {
|
||||
|
@@ -20,7 +20,6 @@
|
||||
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/pack.hpp"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
@@ -80,13 +79,13 @@ struct object {
|
||||
type::object_type type;
|
||||
union_type via;
|
||||
|
||||
bool is_nil() { return type == type::NIL; }
|
||||
bool is_nil() const { return type == type::NIL; }
|
||||
|
||||
template <typename T>
|
||||
T as();
|
||||
T as() const;
|
||||
|
||||
template <typename T>
|
||||
void convert(T* v);
|
||||
void convert(T* v) const;
|
||||
|
||||
object();
|
||||
object(msgpack_object obj);
|
||||
@@ -96,7 +95,7 @@ private:
|
||||
struct implicit_type;
|
||||
|
||||
public:
|
||||
implicit_type convert();
|
||||
implicit_type convert() const;
|
||||
};
|
||||
|
||||
struct object_kv {
|
||||
@@ -129,6 +128,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
// obsolete
|
||||
template <typename Type>
|
||||
class define : public Type {
|
||||
public:
|
||||
@@ -201,25 +201,25 @@ inline object::operator msgpack_object()
|
||||
}
|
||||
|
||||
|
||||
inline object::implicit_type object::convert()
|
||||
inline object::implicit_type object::convert() const
|
||||
{
|
||||
return implicit_type(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T object::as()
|
||||
inline void object::convert(T* v) const
|
||||
{
|
||||
*this >> *v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T object::as() const
|
||||
{
|
||||
T v;
|
||||
convert(&v);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void object::convert(T* v)
|
||||
{
|
||||
*this >> *v;
|
||||
}
|
||||
|
||||
|
||||
// obsolete
|
||||
template <typename T>
|
||||
|
@@ -18,10 +18,9 @@
|
||||
#ifndef MSGPACK_PACK_HPP__
|
||||
#define MSGPACK_PACK_HPP__
|
||||
|
||||
#include <arpa/inet.h> // __BYTE_ORDER
|
||||
#include "msgpack/pack_define.h"
|
||||
#include <stdexcept>
|
||||
#include <limits.h>
|
||||
#include "msgpack/pack_define.h"
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
17
cpp/preprocess.sh
Executable file
17
cpp/preprocess.sh
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
|
||||
preprocess() {
|
||||
ruby -r erb -e 'puts ERB.new(ARGF.read).result' $1.erb > $1.tmp
|
||||
if [ "$?" != 0 ]; then
|
||||
echo ""
|
||||
echo "** preprocess failed **"
|
||||
echo ""
|
||||
else
|
||||
mv $1.tmp $1
|
||||
fi
|
||||
}
|
||||
|
||||
preprocess msgpack/type/tuple.hpp
|
||||
preprocess msgpack/type/define.hpp
|
||||
preprocess msgpack/zone.hpp
|
||||
|
975
cpp/test.cpp
975
cpp/test.cpp
@@ -1,201 +1,792 @@
|
||||
#include <iostream>
|
||||
#include "msgpack.hpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <msgpack.hpp>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <list>
|
||||
|
||||
using namespace msgpack;
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class checker {
|
||||
public:
|
||||
template <typename T>
|
||||
void check(const char* d, size_t len, T should) {
|
||||
try {
|
||||
std::cout << "----" << std::endl;
|
||||
using namespace std;
|
||||
|
||||
object o;
|
||||
try {
|
||||
o = unpack(d, len, m_zone);
|
||||
} catch (std::runtime_error& e) {
|
||||
std::cout << o << std::endl;
|
||||
std::cout << "**" << e.what() << "**" << std::endl;
|
||||
return;
|
||||
}
|
||||
const unsigned int kLoop = 10000;
|
||||
const unsigned int kElements = 100;
|
||||
const double kEPS = 1e-10;
|
||||
|
||||
std::cout << o << std::endl;
|
||||
#define GEN_TEST(test_type) \
|
||||
do { \
|
||||
vector<test_type> v; \
|
||||
v.push_back(0); \
|
||||
v.push_back(1); \
|
||||
v.push_back(2); \
|
||||
v.push_back(numeric_limits<test_type>::min()); \
|
||||
v.push_back(numeric_limits<test_type>::max()); \
|
||||
for (unsigned int i = 0; i < kLoop; i++) \
|
||||
v.push_back(rand()); \
|
||||
for (unsigned int i = 0; i < v.size() ; i++) { \
|
||||
msgpack::sbuffer sbuf; \
|
||||
test_type val1 = v[i]; \
|
||||
msgpack::pack(sbuf, val1); \
|
||||
msgpack::zone z; \
|
||||
msgpack::object obj; \
|
||||
msgpack::unpack_return ret = \
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); \
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret); \
|
||||
test_type val2; \
|
||||
obj.convert(&val2); \
|
||||
EXPECT_EQ(val1, val2); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
try {
|
||||
std::stringstream s;
|
||||
pack(s, should);
|
||||
std::string str(s.str());
|
||||
object ro = unpack(str.data(), str.size(), m_zone);
|
||||
std::cout << ro << std::endl;
|
||||
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();
|
||||
}
|
||||
private:
|
||||
zone m_zone;
|
||||
};
|
||||
|
||||
int main(void)
|
||||
TEST(MSGPACK, simple_buffer_short)
|
||||
{
|
||||
checker c;
|
||||
|
||||
#if 0
|
||||
{ // SimpleValue
|
||||
const char d[] = {
|
||||
0x93, 0xc0, 0xc2, 0xc3,
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
type::make_tuple(
|
||||
type::nil(), false, true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // Fixnum
|
||||
const char d[] = {
|
||||
0x92,
|
||||
0x93, 0x00, 0x40, 0x7f,
|
||||
0x93, 0xe0, 0xf0, 0xff,
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
type::make_tuple(
|
||||
type::make_tuple(
|
||||
0, 64, 127
|
||||
),
|
||||
type::make_tuple(
|
||||
-32, -16, -1
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // FixArray
|
||||
const char d[] = {
|
||||
0x92,
|
||||
0x90,
|
||||
0x91,
|
||||
0x91, 0xc0,
|
||||
};
|
||||
std::vector<int> empty;
|
||||
c.check(d, sizeof(d),
|
||||
type::make_tuple(
|
||||
empty,
|
||||
type::make_tuple(
|
||||
type::make_tuple(
|
||||
type::nil()
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // FixRaw
|
||||
const char d[] = {
|
||||
0x94,
|
||||
0xa0,
|
||||
0xa1, 'a',
|
||||
0xa2, 'b', 'c',
|
||||
0xa3, 'd', 'e', 'f',
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
type::make_tuple(
|
||||
std::string(""),
|
||||
std::string("a"),
|
||||
std::string("bc"),
|
||||
type::raw_ref("def", 3)
|
||||
)
|
||||
);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static const unsigned TASK_ARRAY = 1000;
|
||||
static const unsigned TASK_REPEAT = 10;
|
||||
std::vector<std::string> task;
|
||||
|
||||
// create task
|
||||
{
|
||||
static char traw[64];
|
||||
memset(traw, 'a', sizeof(traw));
|
||||
|
||||
task.resize(TASK_ARRAY);
|
||||
for(unsigned i=0; i < TASK_ARRAY; ++i) {
|
||||
task[i] = std::string(traw, sizeof(traw));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::stringstream stream;
|
||||
|
||||
// send message
|
||||
{
|
||||
for(unsigned i=0; i < TASK_REPEAT; ++i) {
|
||||
pack(stream, task);
|
||||
}
|
||||
std::cout << "send " << stream.str().size() << " bytes" << std::endl;
|
||||
}
|
||||
|
||||
ssize_t total_bytes = stream.str().size();
|
||||
stream.seekg(0);
|
||||
|
||||
// reserive message
|
||||
{
|
||||
unsigned num_msg = 0;
|
||||
static const size_t RESERVE_SIZE = 32;//*1024;
|
||||
|
||||
unpacker pac;
|
||||
|
||||
while(stream.good() && total_bytes > 0) {
|
||||
|
||||
// 1. reserve buffer
|
||||
pac.reserve_buffer(RESERVE_SIZE);
|
||||
|
||||
// 2. read data to buffer() up to buffer_capacity() bytes
|
||||
size_t sz = stream.readsome(
|
||||
pac.buffer(),
|
||||
pac.buffer_capacity());
|
||||
|
||||
total_bytes -= sz;
|
||||
std::cout << "read " << sz << " bytes to capacity "
|
||||
<< pac.buffer_capacity() << " bytes"
|
||||
<< std::endl;
|
||||
|
||||
// 3. specify the number of bytes actually copied
|
||||
pac.buffer_consumed(sz);
|
||||
|
||||
// 4. repeat execute() until it returns false
|
||||
while( pac.execute() ) {
|
||||
// 5.1. take out the parsed object
|
||||
object o = pac.data();
|
||||
|
||||
// 5.2 release the zone
|
||||
std::auto_ptr<zone> olife( pac.release_zone() );
|
||||
|
||||
// 5.3 re-initialize the unpacker */
|
||||
pac.reset();
|
||||
|
||||
// do some with the o and olife
|
||||
std::cout << "message parsed: " << o << std::endl;
|
||||
++num_msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::cout << "stream finished" << std::endl;
|
||||
std::cout << num_msg << " messages reached" << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
GEN_TEST(short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_int)
|
||||
{
|
||||
GEN_TEST(int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_long)
|
||||
{
|
||||
GEN_TEST(long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_long_long)
|
||||
{
|
||||
GEN_TEST(long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_unsigned_short)
|
||||
{
|
||||
GEN_TEST(unsigned short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_unsigned_int)
|
||||
{
|
||||
GEN_TEST(unsigned int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_unsigned_long)
|
||||
{
|
||||
GEN_TEST(unsigned long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_unsigned_long_long)
|
||||
{
|
||||
GEN_TEST(unsigned long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_uint8)
|
||||
{
|
||||
GEN_TEST(uint8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_uint16)
|
||||
{
|
||||
GEN_TEST(uint16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_uint32)
|
||||
{
|
||||
GEN_TEST(uint32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_uint64)
|
||||
{
|
||||
GEN_TEST(uint64_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_int8)
|
||||
{
|
||||
GEN_TEST(int8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_int16)
|
||||
{
|
||||
GEN_TEST(int16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_int32)
|
||||
{
|
||||
GEN_TEST(int32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_int64)
|
||||
{
|
||||
GEN_TEST(int64_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_float)
|
||||
{
|
||||
vector<float> v;
|
||||
v.push_back(0.0);
|
||||
v.push_back(-0.0);
|
||||
v.push_back(1.0);
|
||||
v.push_back(-1.0);
|
||||
v.push_back(numeric_limits<float>::min());
|
||||
v.push_back(numeric_limits<float>::max());
|
||||
v.push_back(nanf("tag"));
|
||||
v.push_back(1.0/0.0); // inf
|
||||
v.push_back(-(1.0/0.0)); // -inf
|
||||
for (unsigned int i = 0; i < kLoop; i++) {
|
||||
v.push_back(drand48());
|
||||
v.push_back(-drand48());
|
||||
}
|
||||
for (unsigned int i = 0; i < v.size() ; i++) {
|
||||
msgpack::sbuffer sbuf;
|
||||
float val1 = v[i];
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
float val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
if (isnan(val1))
|
||||
EXPECT_TRUE(isnan(val2));
|
||||
else if (isinf(val1))
|
||||
EXPECT_TRUE(isinf(val2));
|
||||
else
|
||||
EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_double)
|
||||
{
|
||||
vector<double> v;
|
||||
v.push_back(0.0);
|
||||
v.push_back(-0.0);
|
||||
v.push_back(1.0);
|
||||
v.push_back(-1.0);
|
||||
v.push_back(numeric_limits<double>::min());
|
||||
v.push_back(numeric_limits<double>::max());
|
||||
v.push_back(nanf("tag"));
|
||||
v.push_back(1.0/0.0); // inf
|
||||
v.push_back(-(1.0/0.0)); // -inf
|
||||
for (unsigned int i = 0; i < kLoop; i++) {
|
||||
v.push_back(drand48());
|
||||
v.push_back(-drand48());
|
||||
}
|
||||
for (unsigned int i = 0; i < v.size() ; i++) {
|
||||
msgpack::sbuffer sbuf;
|
||||
double val1 = v[i];
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
double val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
if (isnan(val1))
|
||||
EXPECT_TRUE(isnan(val2));
|
||||
else if (isinf(val1))
|
||||
EXPECT_TRUE(isinf(val2));
|
||||
else
|
||||
EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_true)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
bool val1 = true;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
bool val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, simple_buffer_false)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
bool val1 = false;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
bool val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_string)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
string val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1 += 'a' + rand() % 26;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
string val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_vector)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
vector<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.push_back(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
vector<int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_map)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
map<int, int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1[rand()] = rand();
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
map<int, int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_deque)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
deque<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.push_back(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
deque<int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_list)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
list<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.push_back(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
list<int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_set)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
set<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.insert(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
set<int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_pair)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
pair<int, int> val1 = make_pair(rand(), rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
pair<int, int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.first, val2.first);
|
||||
EXPECT_EQ(val1.second, val2.second);
|
||||
}
|
||||
}
|
||||
|
||||
class TestClass
|
||||
{
|
||||
public:
|
||||
TestClass() : i(0), s("kzk") {}
|
||||
int i;
|
||||
string s;
|
||||
MSGPACK_DEFINE(i, s);
|
||||
};
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_class)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
TestClass val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestClass val2;
|
||||
val2.i = -1;
|
||||
val2.s = "";
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.i, val2.i);
|
||||
EXPECT_EQ(val1.s, val2.s);
|
||||
}
|
||||
}
|
||||
|
||||
class TestClass2
|
||||
{
|
||||
public:
|
||||
TestClass2() : i(0), s("kzk") {
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
v.push_back(rand());
|
||||
}
|
||||
int i;
|
||||
string s;
|
||||
vector<int> v;
|
||||
MSGPACK_DEFINE(i, s, v);
|
||||
};
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_class_old_to_new)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
TestClass val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestClass2 val2;
|
||||
val2.i = -1;
|
||||
val2.s = "";
|
||||
val2.v = vector<int>();
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.i, val2.i);
|
||||
EXPECT_EQ(val1.s, val2.s);
|
||||
EXPECT_FALSE(val2.s.empty());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_class_new_to_old)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
TestClass2 val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestClass val2;
|
||||
val2.i = -1;
|
||||
val2.s = "";
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.i, val2.i);
|
||||
EXPECT_EQ(val1.s, val2.s);
|
||||
EXPECT_FALSE(val2.s.empty());
|
||||
}
|
||||
}
|
||||
|
||||
class TestEnumMemberClass
|
||||
{
|
||||
public:
|
||||
TestEnumMemberClass()
|
||||
: t1(STATE_A), t2(STATE_B), t3(STATE_C) {}
|
||||
|
||||
enum TestEnumType {
|
||||
STATE_INVALID = 0,
|
||||
STATE_A = 1,
|
||||
STATE_B = 2,
|
||||
STATE_C = 3
|
||||
};
|
||||
TestEnumType t1;
|
||||
TestEnumType t2;
|
||||
TestEnumType t3;
|
||||
|
||||
MSGPACK_DEFINE((int&)t1, (int&)t2, (int&)t3);
|
||||
};
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_enum_member)
|
||||
{
|
||||
TestEnumMemberClass val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestEnumMemberClass val2;
|
||||
val2.t1 = TestEnumMemberClass::STATE_INVALID;
|
||||
val2.t2 = TestEnumMemberClass::STATE_INVALID;
|
||||
val2.t3 = TestEnumMemberClass::STATE_INVALID;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.t1, val2.t1);
|
||||
EXPECT_EQ(val1.t2, val2.t2);
|
||||
EXPECT_EQ(val1.t3, val2.t3);
|
||||
}
|
||||
|
||||
class TestUnionMemberClass
|
||||
{
|
||||
public:
|
||||
TestUnionMemberClass() {}
|
||||
TestUnionMemberClass(double f) {
|
||||
is_double = true;
|
||||
value.f = f;
|
||||
}
|
||||
TestUnionMemberClass(int i) {
|
||||
is_double = false;
|
||||
value.i = i;
|
||||
}
|
||||
|
||||
union {
|
||||
double f;
|
||||
int i;
|
||||
} value;
|
||||
bool is_double;
|
||||
|
||||
template <typename Packer>
|
||||
void msgpack_pack(Packer& pk) const
|
||||
{
|
||||
if (is_double)
|
||||
pk.pack(msgpack::type::tuple<bool, double>(true, value.f));
|
||||
else
|
||||
pk.pack(msgpack::type::tuple<bool, int>(false, value.i));
|
||||
}
|
||||
|
||||
void msgpack_unpack(msgpack::object o)
|
||||
{
|
||||
msgpack::type::tuple<bool, msgpack::object> tuple;
|
||||
o.convert(&tuple);
|
||||
|
||||
is_double = tuple.get<0>();
|
||||
if (is_double)
|
||||
tuple.get<1>().convert(&value.f);
|
||||
else
|
||||
tuple.get<1>().convert(&value.i);
|
||||
}
|
||||
};
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member)
|
||||
{
|
||||
{
|
||||
// double
|
||||
TestUnionMemberClass val1(1.0);
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestUnionMemberClass val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.is_double, val2.is_double);
|
||||
EXPECT_TRUE(fabs(val1.value.f - val2.value.f) < kEPS);
|
||||
}
|
||||
{
|
||||
// int
|
||||
TestUnionMemberClass val1(1);
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret);
|
||||
TestUnionMemberClass val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.is_double, val2.is_double);
|
||||
EXPECT_EQ(val1.value.i, 1);
|
||||
EXPECT_EQ(val1.value.i, val2.value.i);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define GEN_TEST_VREF(test_type) \
|
||||
do { \
|
||||
vector<test_type> v; \
|
||||
v.push_back(0); \
|
||||
for (unsigned int i = 0; i < v.size(); i++) { \
|
||||
test_type val1 = v[i]; \
|
||||
msgpack::vrefbuffer vbuf; \
|
||||
msgpack::pack(vbuf, val1); \
|
||||
msgpack::sbuffer sbuf; \
|
||||
const struct iovec* cur = vbuf.vector(); \
|
||||
const struct iovec* end = cur + vbuf.vector_size(); \
|
||||
for(; cur != end; ++cur) \
|
||||
sbuf.write((const char*)cur->iov_base, cur->iov_len); \
|
||||
msgpack::zone z; \
|
||||
msgpack::object obj; \
|
||||
msgpack::unpack_return ret = \
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); \
|
||||
EXPECT_EQ(msgpack::UNPACK_SUCCESS, ret); \
|
||||
test_type val2; \
|
||||
obj.convert(&val2); \
|
||||
EXPECT_EQ(val1, val2); \
|
||||
} \
|
||||
} while(0);
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_short)
|
||||
{
|
||||
GEN_TEST_VREF(short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_int)
|
||||
{
|
||||
GEN_TEST_VREF(int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_long)
|
||||
{
|
||||
GEN_TEST_VREF(long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_long_long)
|
||||
{
|
||||
GEN_TEST_VREF(long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_unsigned_short)
|
||||
{
|
||||
GEN_TEST_VREF(unsigned short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_unsigned_int)
|
||||
{
|
||||
GEN_TEST_VREF(unsigned int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_unsigned_long)
|
||||
{
|
||||
GEN_TEST_VREF(unsigned long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_unsigned_long_long)
|
||||
{
|
||||
GEN_TEST_VREF(unsigned long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_uint8)
|
||||
{
|
||||
GEN_TEST_VREF(uint8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_uint16)
|
||||
{
|
||||
GEN_TEST_VREF(uint16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_uint32)
|
||||
{
|
||||
GEN_TEST_VREF(uint32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_uint64)
|
||||
{
|
||||
GEN_TEST_VREF(uint64_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_int8)
|
||||
{
|
||||
GEN_TEST_VREF(int8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_int16)
|
||||
{
|
||||
GEN_TEST_VREF(int16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_int32)
|
||||
{
|
||||
GEN_TEST_VREF(int32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, vrefbuffer_int64)
|
||||
{
|
||||
GEN_TEST_VREF(int64_t);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define GEN_TEST_STREAM(test_type) \
|
||||
for (unsigned int k = 0; k < kLoop; k++) { \
|
||||
msgpack::sbuffer sbuf; \
|
||||
msgpack::packer<msgpack::sbuffer> pk(sbuf); \
|
||||
typedef std::vector<test_type> vec_type; \
|
||||
vec_type vec; \
|
||||
for(unsigned int i = 0; i < rand() % kLoop; ++i) { \
|
||||
vec_type::value_type r = rand(); \
|
||||
vec.push_back(r); \
|
||||
pk.pack(r); \
|
||||
} \
|
||||
msgpack::unpacker pac; \
|
||||
vec_type::const_iterator it = vec.begin(); \
|
||||
const char *p = sbuf.data(); \
|
||||
const char * const pend = p + sbuf.size(); \
|
||||
while (p < pend) { \
|
||||
const size_t sz = std::min<size_t>(pend - p, rand() % 128); \
|
||||
pac.reserve_buffer(sz); \
|
||||
memcpy(pac.buffer(), p, sz); \
|
||||
pac.buffer_consumed(sz); \
|
||||
while (pac.execute()) { \
|
||||
if (it == vec.end()) goto out; \
|
||||
msgpack::object obj = pac.data(); \
|
||||
msgpack::zone *life = pac.release_zone(); \
|
||||
EXPECT_TRUE(life != NULL); \
|
||||
pac.reset(); \
|
||||
vec_type::value_type val; \
|
||||
obj.convert(&val); \
|
||||
EXPECT_EQ(*it, val); \
|
||||
++it; \
|
||||
delete life; \
|
||||
} \
|
||||
p += sz; \
|
||||
} \
|
||||
out: \
|
||||
; \
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_short)
|
||||
{
|
||||
GEN_TEST_STREAM(short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_int)
|
||||
{
|
||||
GEN_TEST_STREAM(int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_long)
|
||||
{
|
||||
GEN_TEST_STREAM(long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_long_long)
|
||||
{
|
||||
GEN_TEST_STREAM(long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_unsigned_short)
|
||||
{
|
||||
GEN_TEST_STREAM(unsigned short);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_unsigned_int)
|
||||
{
|
||||
GEN_TEST_STREAM(unsigned int);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_unsigned_long)
|
||||
{
|
||||
GEN_TEST_STREAM(unsigned long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_unsigned_long_long)
|
||||
{
|
||||
GEN_TEST_STREAM(unsigned long long);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_uint8)
|
||||
{
|
||||
GEN_TEST_STREAM(uint8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_uint16)
|
||||
{
|
||||
GEN_TEST_STREAM(uint16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_uint32)
|
||||
{
|
||||
GEN_TEST_STREAM(uint32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_uint64)
|
||||
{
|
||||
GEN_TEST_STREAM(uint64_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_int8)
|
||||
{
|
||||
GEN_TEST_STREAM(int8_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_int16)
|
||||
{
|
||||
GEN_TEST_STREAM(int16_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_int32)
|
||||
{
|
||||
GEN_TEST_STREAM(int32_t);
|
||||
}
|
||||
|
||||
TEST(MSGPACK, stream_int64)
|
||||
{
|
||||
GEN_TEST_STREAM(int64_t);
|
||||
}
|
||||
|
12
cpp/type.hpp
12
cpp/type.hpp
@@ -1,9 +1,15 @@
|
||||
#include "msgpack/type/array.hpp"
|
||||
#include "msgpack/type/boolean.hpp"
|
||||
#include "msgpack/type/bool.hpp"
|
||||
#include "msgpack/type/float.hpp"
|
||||
#include "msgpack/type/integer.hpp"
|
||||
#include "msgpack/type/int.hpp"
|
||||
#include "msgpack/type/list.hpp"
|
||||
#include "msgpack/type/deque.hpp"
|
||||
#include "msgpack/type/map.hpp"
|
||||
#include "msgpack/type/nil.hpp"
|
||||
#include "msgpack/type/pair.hpp"
|
||||
#include "msgpack/type/raw.hpp"
|
||||
#include "msgpack/type/set.hpp"
|
||||
#include "msgpack/type/string.hpp"
|
||||
#include "msgpack/type/vector.hpp"
|
||||
#include "msgpack/type/tuple.hpp"
|
||||
#include "msgpack/type/define.hpp"
|
||||
|
||||
|
@@ -15,8 +15,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_BOOLEAN_HPP__
|
||||
#define MSGPACK_TYPE_BOOLEAN_HPP__
|
||||
#ifndef MSGPACK_TYPE_BOOL_HPP__
|
||||
#define MSGPACK_TYPE_BOOL_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
@@ -31,7 +31,6 @@ inline bool& operator>> (object o, bool& v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const bool& v)
|
||||
{
|
98
cpp/type/define.hpp.erb
Normal file
98
cpp/type/define.hpp.erb
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_DEFINE_HPP__
|
||||
#define MSGPACK_TYPE_DEFINE_HPP__
|
||||
|
||||
#define MSGPACK_DEFINE(...) \
|
||||
template <typename Packer> \
|
||||
void msgpack_pack(Packer& pk) const \
|
||||
{ \
|
||||
msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
|
||||
} \
|
||||
void msgpack_unpack(msgpack::object o) \
|
||||
{ \
|
||||
msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
|
||||
}
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
<% GENERATION_LIMIT = 31 %>
|
||||
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
|
||||
struct define;
|
||||
|
||||
|
||||
template <>
|
||||
struct define<> {
|
||||
typedef define<> value_type;
|
||||
typedef tuple<> tuple_type;
|
||||
template <typename Packer>
|
||||
void msgpack_pack(Packer& pk) const
|
||||
{
|
||||
pk.pack_array(1);
|
||||
}
|
||||
void msgpack_unpack(msgpack::object o)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
}
|
||||
};
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
|
||||
typedef define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
|
||||
typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> tuple_type;
|
||||
define(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
|
||||
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
|
||||
template <typename Packer>
|
||||
void msgpack_pack(Packer& pk) const
|
||||
{
|
||||
pk.pack_array(<%=i+1%>);
|
||||
<%0.upto(i) {|j|%>
|
||||
pk.pack(a<%=j%>);<%}%>
|
||||
}
|
||||
void msgpack_unpack(msgpack::object o)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
const size_t size = o.via.array.size;
|
||||
<%0.upto(i) {|j|%>
|
||||
if(size <= <%=j%>) { return; } o.via.array.ptr[<%=j%>].convert(&a<%=j%>);<%}%>
|
||||
}
|
||||
<%0.upto(i) {|j|%>
|
||||
A<%=j%>& a<%=j%>;<%}%>
|
||||
};
|
||||
<%}%>
|
||||
|
||||
inline define<> make_define()
|
||||
{
|
||||
return define<>();
|
||||
}
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
|
||||
{
|
||||
return define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
|
||||
}
|
||||
<%}%>
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
|
||||
#endif /* msgpack/type/define.hpp */
|
||||
|
56
cpp/type/deque.hpp
Normal file
56
cpp/type/deque.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_DEQUE_HPP__
|
||||
#define MSGPACK_TYPE_DEQUE_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <deque>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::deque<T>& operator>> (object o, std::deque<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
v.resize(o.via.array.size);
|
||||
object* p = o.via.array.ptr;
|
||||
object* const pend = o.via.array.ptr + o.via.array.size;
|
||||
typename std::deque<T>::iterator it = v.begin();
|
||||
for(; p < pend; ++p, ++it) {
|
||||
p->convert(&*it);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::deque<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::deque<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/deque.hpp */
|
||||
|
@@ -34,7 +34,6 @@ inline float& operator>> (object o, float& v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const float& v)
|
||||
{
|
||||
@@ -50,7 +49,6 @@ inline double& operator>> (object o, double& v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const double& v)
|
||||
{
|
||||
|
@@ -15,8 +15,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_INTEGER_HPP__
|
||||
#define MSGPACK_TYPE_INTEGER_HPP__
|
||||
#ifndef MSGPACK_TYPE_INT_HPP__
|
||||
#define MSGPACK_TYPE_INT_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <limits>
|
||||
@@ -37,7 +37,7 @@ namespace detail {
|
||||
{ throw type_error(); }
|
||||
return o.via.u64;
|
||||
} else if(o.type == type::NEGATIVE_INTEGER) {
|
||||
if(o.via.i64 < (int64_t)-std::numeric_limits<T>::max())
|
||||
if(o.via.i64 < (int64_t)std::numeric_limits<T>::min())
|
||||
{ throw type_error(); }
|
||||
return o.via.i64;
|
||||
}
|
||||
@@ -143,5 +143,5 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long long&
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/integer.hpp */
|
||||
#endif /* msgpack/type/int.hpp */
|
||||
|
56
cpp/type/list.hpp
Normal file
56
cpp/type/list.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_LIST_HPP__
|
||||
#define MSGPACK_TYPE_LIST_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <list>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::list<T>& operator>> (object o, std::list<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
v.resize(o.via.array.size);
|
||||
object* p = o.via.array.ptr;
|
||||
object* const pend = o.via.array.ptr + o.via.array.size;
|
||||
typename std::list<T>::iterator it = v.begin();
|
||||
for(; p < pend; ++p, ++it) {
|
||||
p->convert(&*it);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::list<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::list<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/list.hpp */
|
||||
|
@@ -80,13 +80,13 @@ inline std::map<K, V> operator>> (object o, std::map<K, V>& v)
|
||||
for(; p != pend; ++p) {
|
||||
K key;
|
||||
p->key.convert(&key);
|
||||
typename std::map<K,V>::iterator it(v.find(key));
|
||||
if(it != v.end()) {
|
||||
typename std::map<K,V>::iterator it(v.lower_bound(key));
|
||||
if(it != v.end() && !(key < it->first)) {
|
||||
p->val.convert(&it->second);
|
||||
} else {
|
||||
V val;
|
||||
p->val.convert(&val);
|
||||
it->insert( std::pair<K,V>(key, val) );
|
||||
} else {
|
||||
p->val.convert(&it->second);
|
||||
v.insert(it, std::pair<K,V>(key, val));
|
||||
}
|
||||
}
|
||||
return v;
|
||||
|
50
cpp/type/pair.hpp
Normal file
50
cpp/type/pair.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_PAIR_HPP__
|
||||
#define MSGPACK_TYPE_PAIR_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline std::pair<T1, T2>& operator>> (object o, std::pair<T1, T2>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
if(o.via.array.size != 2) { throw type_error(); }
|
||||
o.via.array.ptr[0].convert(&v.first);
|
||||
o.via.array.ptr[1].convert(&v.second);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T1, typename T2>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::pair<T1, T2>& v)
|
||||
{
|
||||
o.pack_array(2);
|
||||
o.pack(v.first);
|
||||
o.pack(v.second);
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/pair.hpp */
|
||||
|
@@ -69,16 +69,6 @@ inline type::raw_ref& operator>> (object o, type::raw_ref& v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
inline std::string& operator>> (object o, std::string& v)
|
||||
{
|
||||
type::raw_ref r;
|
||||
o >> r;
|
||||
v.assign(r.ptr, r.size);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::raw_ref& v)
|
||||
{
|
||||
@@ -88,15 +78,6 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::raw_ref& v)
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
|
||||
{
|
||||
o.pack_raw(v.size());
|
||||
o.pack_raw_body(v.data(), v.size());
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/raw.hpp */
|
||||
|
80
cpp/type/set.hpp
Normal file
80
cpp/type/set.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_SET_HPP__
|
||||
#define MSGPACK_TYPE_SET_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <set>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::set<T>& operator>> (object o, std::set<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
object* p = o.via.array.ptr + o.via.array.size;
|
||||
object* const pbegin = o.via.array.ptr;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
v.insert(p->as<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::set<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::set<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::multiset<T>& operator>> (object o, std::multiset<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
object* p = o.via.array.ptr + o.via.array.size;
|
||||
object* const pbegin = o.via.array.ptr;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
v.insert(p->as<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::multiset<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::multiset<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/set.hpp */
|
||||
|
46
cpp/type/string.hpp
Normal file
46
cpp/type/string.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_STRING_HPP__
|
||||
#define MSGPACK_TYPE_STRING_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
inline std::string& operator>> (object o, std::string& v)
|
||||
{
|
||||
if(o.type != type::RAW) { throw type_error(); }
|
||||
v.assign(o.via.raw.ptr, o.via.raw.size);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
|
||||
{
|
||||
o.pack_raw(v.size());
|
||||
o.pack_raw_body(v.data(), v.size());
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/string.hpp */
|
||||
|
85
cpp/type/tr1/unordered_map.hpp
Normal file
85
cpp/type/tr1/unordered_map.hpp
Normal file
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_TR1_UNORDERED_MAP_HPP__
|
||||
#define MSGPACK_TYPE_TR1_UNORDERED_MAP_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <tr1/unordered_map>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::tr1::unordered_map<K, V> operator>> (object o, std::tr1::unordered_map<K, V>& v)
|
||||
{
|
||||
if(o.type != type::MAP) { throw type_error(); }
|
||||
object_kv* p(o.via.map.ptr);
|
||||
object_kv* const pend(o.via.map.ptr + o.via.map.size);
|
||||
for(; p != pend; ++p) {
|
||||
K key;
|
||||
p->key.convert(&key);
|
||||
p->val.convert(&v[key]);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::tr1::unordered_map<K,V>& v)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename std::tr1::unordered_map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(it->first);
|
||||
o.pack(it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::tr1::unordered_multimap<K, V> operator>> (object o, std::tr1::unordered_multimap<K, V>& v)
|
||||
{
|
||||
if(o.type != type::MAP) { throw type_error(); }
|
||||
object_kv* p(o.via.map.ptr);
|
||||
object_kv* const pend(o.via.map.ptr + o.via.map.size);
|
||||
for(; p != pend; ++p) {
|
||||
std::pair<K, V> value;
|
||||
p->key.convert(&value.first);
|
||||
p->val.convert(&value.second);
|
||||
v.insert(value);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::tr1::unordered_multimap<K,V>& v)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename std::tr1::unordered_multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(it->first);
|
||||
o.pack(it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/map.hpp */
|
||||
|
80
cpp/type/tr1/unordered_set.hpp
Normal file
80
cpp/type/tr1/unordered_set.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_TR1_UNORDERED_SET_HPP__
|
||||
#define MSGPACK_TYPE_TR1_UNORDERED_SET_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <tr1/unordered_set>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::tr1::unordered_set<T>& operator>> (object o, std::tr1::unordered_set<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
object* p = o.via.array.ptr + o.via.array.size;
|
||||
object* const pbegin = o.via.array.ptr;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
v.insert(p->as<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::tr1::unordered_set<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::tr1::unordered_set<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::tr1::unordered_multiset<T>& operator>> (object o, std::tr1::unordered_multiset<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
object* p = o.via.array.ptr + o.via.array.size;
|
||||
object* const pbegin = o.via.array.ptr;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
v.insert(p->as<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::tr1::unordered_multiset<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::tr1::unordered_multiset<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/set.hpp */
|
||||
|
@@ -26,7 +26,7 @@ namespace type {
|
||||
|
||||
// FIXME operator==
|
||||
// FIXME operator!=
|
||||
<% GENERATION_LIMIT = 15 %>
|
||||
<% GENERATION_LIMIT = 31 %>
|
||||
|
||||
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
|
||||
struct tuple;
|
||||
@@ -64,7 +64,6 @@ struct tuple_type<const T&> {
|
||||
typedef const T& transparent_reference;
|
||||
};
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
<%0.upto(i) {|j|%>
|
||||
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
|
||||
@@ -78,7 +77,6 @@ private:
|
||||
<%}%>
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
<%0.upto(i) {|j|%>
|
||||
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
|
||||
@@ -91,18 +89,11 @@ private:
|
||||
<%}%>
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(typename tuple_type<A0>::transparent_reference a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference a<%=j%><%}%>)
|
||||
{
|
||||
return tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
template <>
|
||||
struct tuple<> {
|
||||
tuple() {}
|
||||
tuple(object o) { o.convert(this); }
|
||||
typedef tuple<> value_type;
|
||||
};
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
@@ -121,6 +112,18 @@ struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
|
||||
};
|
||||
<%}%>
|
||||
|
||||
inline tuple<> make_tuple()
|
||||
{
|
||||
return tuple<>();
|
||||
}
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(typename tuple_type<A0>::transparent_reference a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference a<%=j%><%}%>)
|
||||
{
|
||||
return tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
|
||||
}
|
||||
<%}%>
|
||||
|
||||
} // namespace type
|
||||
|
||||
|
||||
@@ -143,7 +146,6 @@ type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
const packer<Stream>& operator<< (
|
||||
packer<Stream>& o,
|
||||
@@ -163,7 +165,6 @@ const packer<Stream>& operator<< (
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/tuple.hpp */
|
||||
|
@@ -15,8 +15,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_ARRAY_HPP__
|
||||
#define MSGPACK_TYPE_ARRAY_HPP__
|
||||
#ifndef MSGPACK_TYPE_VECTOR_HPP__
|
||||
#define MSGPACK_TYPE_VECTOR_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
@@ -25,7 +25,7 @@ namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> operator>> (object o, std::vector<T>& v)
|
||||
inline std::vector<T>& operator>> (object o, std::vector<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
v.resize(o.via.array.size);
|
||||
@@ -38,7 +38,6 @@ inline std::vector<T> operator>> (object o, std::vector<T>& v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
|
||||
{
|
||||
@@ -53,5 +52,5 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/array.hpp */
|
||||
#endif /* msgpack/type/vector.hpp */
|
||||
|
92
cpp/vrefbuffer.hpp
Normal file
92
cpp/vrefbuffer.hpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// MessagePack for C++ zero-copy buffer implementation
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_VREFBUFFER_HPP__
|
||||
#define MSGPACK_VREFBUFFER_HPP__
|
||||
|
||||
#include "msgpack/vrefbuffer.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
class vrefbuffer : public msgpack_vrefbuffer {
|
||||
public:
|
||||
vrefbuffer(size_t ref_size = MSGPACK_VREFBUFFER_REF_SIZE,
|
||||
size_t chunk_size = MSGPACK_VREFBUFFER_CHUNK_SIZE)
|
||||
{
|
||||
msgpack_vrefbuffer_init(this, ref_size, chunk_size);
|
||||
}
|
||||
|
||||
~vrefbuffer()
|
||||
{
|
||||
msgpack_vrefbuffer_destroy(this);
|
||||
}
|
||||
|
||||
public:
|
||||
void write(const char* buf, unsigned int len)
|
||||
{
|
||||
if(len < base::ref_size) {
|
||||
append_copy(buf, len);
|
||||
} else {
|
||||
append_ref(buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
void append_ref(const char* buf, size_t len)
|
||||
{
|
||||
if(msgpack_vrefbuffer_append_ref(this, buf, len) < 0) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
void append_copy(const char* buf, size_t len)
|
||||
{
|
||||
if(msgpack_vrefbuffer_append_copy(this, buf, len) < 0) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
const struct iovec* vector() const
|
||||
{
|
||||
return msgpack_vrefbuffer_vec(this);
|
||||
}
|
||||
|
||||
size_t vector_size() const
|
||||
{
|
||||
return msgpack_vrefbuffer_veclen(this);
|
||||
}
|
||||
|
||||
void migrate(vrefbuffer* to)
|
||||
{
|
||||
if(msgpack_vrefbuffer_migrate(this, to) < 0) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef msgpack_vrefbuffer base;
|
||||
|
||||
private:
|
||||
vrefbuffer(const vrefbuffer&);
|
||||
};
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/vrefbuffer.hpp */
|
||||
|
@@ -21,6 +21,7 @@
|
||||
#include "msgpack/object.hpp"
|
||||
#include "msgpack/zone.h"
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
<% GENERATION_LIMIT = 15 %>
|
||||
@@ -34,9 +35,13 @@ public:
|
||||
|
||||
public:
|
||||
void* malloc(size_t size);
|
||||
void* malloc_no_align(size_t size);
|
||||
|
||||
void push_finalizer(void (*func)(void*), void* data);
|
||||
|
||||
template <typename T>
|
||||
void push_finalizer(std::auto_ptr<T> obj);
|
||||
|
||||
void clear();
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
@@ -77,6 +82,15 @@ inline void* zone::malloc(size_t size)
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void* zone::malloc_no_align(size_t size)
|
||||
{
|
||||
void* ptr = msgpack_zone_malloc_no_align(this, size);
|
||||
if(!ptr) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void zone::push_finalizer(void (*func)(void*), void* data)
|
||||
{
|
||||
if(!msgpack_zone_push_finalizer(this, func, data)) {
|
||||
@@ -84,6 +98,15 @@ inline void zone::push_finalizer(void (*func)(void*), void* data)
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void zone::push_finalizer(std::auto_ptr<T> obj)
|
||||
{
|
||||
if(!msgpack_zone_push_finalizer(this, &zone::object_destructor<T>, obj.get())) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
obj.release();
|
||||
}
|
||||
|
||||
inline void zone::clear()
|
||||
{
|
||||
msgpack_zone_clear(this);
|
||||
@@ -97,9 +120,8 @@ void zone::object_destructor(void* obj)
|
||||
|
||||
inline void zone::undo_malloc(size_t size)
|
||||
{
|
||||
msgpack_zone_chunk* chunk = base::chunk_array.tail;
|
||||
chunk->ptr -= size;
|
||||
chunk->free += size;
|
||||
base::chunk_list.ptr -= size;
|
||||
base::chunk_list.free += size;
|
||||
}
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
|
58
example/custom.cc
Normal file
58
example/custom.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class old_class {
|
||||
public:
|
||||
old_class() : value("default") { }
|
||||
|
||||
std::string value;
|
||||
|
||||
MSGPACK_DEFINE(value);
|
||||
};
|
||||
|
||||
class new_class {
|
||||
public:
|
||||
new_class() : value("default"), flag(-1) { }
|
||||
|
||||
std::string value;
|
||||
int flag;
|
||||
|
||||
MSGPACK_DEFINE(value, flag);
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
{
|
||||
old_class oc;
|
||||
new_class nc;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, oc);
|
||||
|
||||
msgpack::zone zone;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &zone, &obj);
|
||||
|
||||
obj.convert(&nc);
|
||||
|
||||
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
new_class nc;
|
||||
old_class oc;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, nc);
|
||||
|
||||
msgpack::zone zone;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &zone, &obj);
|
||||
|
||||
obj.convert(&oc);
|
||||
|
||||
std::cout << obj << " value=" << oc.value << std::endl;
|
||||
}
|
||||
}
|
||||
|
@@ -22,14 +22,14 @@ public:
|
||||
ssize_t count =
|
||||
read(m_sock, m_pac.buffer(), m_pac.buffer_capacity());
|
||||
|
||||
if(count < 0) {
|
||||
if(count <= 0) {
|
||||
if(count == 0) {
|
||||
throw std::runtime_error("connection closed");
|
||||
}
|
||||
if(errno == EAGAIN || errno == EINTR) {
|
||||
return;
|
||||
} else {
|
||||
throw std::runtime_error(strerror(errno));
|
||||
}
|
||||
} else if(count == 0) {
|
||||
throw std::runtime_error("connection closed");
|
||||
throw std::runtime_error(strerror(errno));
|
||||
}
|
||||
|
||||
m_pac.buffer_consumed(count);
|
||||
|
18
java/build.xml
Normal file
18
java/build.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<project name="MessagePack for Java">
|
||||
<target name="prepare">
|
||||
<mkdir dir="build" />
|
||||
<mkdir dir="dist" />
|
||||
</target>
|
||||
<target name="clean">
|
||||
<delete dir="build"/>
|
||||
<delete dir="dist"/>
|
||||
</target>
|
||||
<target name="compile" depends="prepare">
|
||||
<javac srcdir="src" destdir="build" source="1.5" target="1.5">
|
||||
<compilerarg value="-Xlint:unchecked" />
|
||||
</javac>
|
||||
</target>
|
||||
<target name="jar" depends="compile">
|
||||
<jar jarfile="dist/msgpack-0.0.0.jar" basedir="build" />
|
||||
</target>
|
||||
</project>
|
23
java/src/org/msgpack/MessageMergeable.java
Normal file
23
java/src/org/msgpack/MessageMergeable.java
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
public interface MessageMergeable {
|
||||
public void messageMerge(Object obj) throws MessageTypeException;
|
||||
}
|
||||
|
25
java/src/org/msgpack/MessagePackable.java
Normal file
25
java/src/org/msgpack/MessagePackable.java
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessagePackable {
|
||||
public void messagePack(Packer pk) throws IOException;
|
||||
}
|
||||
|
39
java/src/org/msgpack/MessageTypeException.java
Normal file
39
java/src/org/msgpack/MessageTypeException.java
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageTypeException extends IOException {
|
||||
public MessageTypeException() { }
|
||||
|
||||
public MessageTypeException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public static MessageTypeException invalidConvert(Object from, Schema to) {
|
||||
return new MessageTypeException(from.getClass().getName()+" cannot be convert to "+to.getExpression());
|
||||
}
|
||||
|
||||
/* FIXME
|
||||
public static MessageTypeException schemaMismatch(Schema to) {
|
||||
return new MessageTypeException("schema mismatch "+to.getExpression());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
409
java/src/org/msgpack/Packer.java
Normal file
409
java/src/org/msgpack/Packer.java
Normal file
@@ -0,0 +1,409 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Packer {
|
||||
protected byte[] castBytes = new byte[9];
|
||||
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
|
||||
protected OutputStream out;
|
||||
|
||||
public Packer(OutputStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
public Packer packByte(byte d) throws IOException {
|
||||
if(d < -(1<<5)) {
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBytes[1] = d;
|
||||
out.write(castBytes, 0, 2);
|
||||
} else {
|
||||
out.write(d);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packShort(short d) throws IOException {
|
||||
if(d < -(1<<5)) {
|
||||
if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, d);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
castBytes[0] = (byte)0xd0;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
}
|
||||
} else if(d < (1<<7)) {
|
||||
// fixnum
|
||||
out.write((byte)d);
|
||||
} else {
|
||||
if(d < (1<<8)) {
|
||||
// unsigned 8
|
||||
castBytes[0] = (byte)0xcc;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
} else {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, d);
|
||||
out.write(castBytes, 0, 3);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packInt(int d) throws IOException {
|
||||
if(d < -(1<<5)) {
|
||||
if(d < -(1<<15)) {
|
||||
// signed 32
|
||||
castBytes[0] = (byte)0xd2;
|
||||
castBuffer.putInt(1, d);
|
||||
out.write(castBytes, 0, 5);
|
||||
} else if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
castBytes[0] = (byte)0xd0;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
}
|
||||
} else if(d < (1<<7)) {
|
||||
// fixnum
|
||||
out.write((byte)d);
|
||||
} else {
|
||||
if(d < (1<<8)) {
|
||||
// unsigned 8
|
||||
castBytes[0] = (byte)0xcc;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
} else if(d < (1<<16)) {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
castBytes[0] = (byte)0xce;
|
||||
castBuffer.putInt(1, d);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packLong(long d) throws IOException {
|
||||
if(d < -(1L<<5)) {
|
||||
if(d < -(1L<<15)) {
|
||||
if(d < -(1L<<31)) {
|
||||
// signed 64
|
||||
castBytes[0] = (byte)0xd3;
|
||||
castBuffer.putLong(1, d);
|
||||
out.write(castBytes, 0, 9);
|
||||
} else {
|
||||
// signed 32
|
||||
castBytes[0] = (byte)0xd2;
|
||||
castBuffer.putInt(1, (int)d);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
} else {
|
||||
if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
castBytes[0] = (byte)0xd0;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
}
|
||||
}
|
||||
} else if(d < (1<<7)) {
|
||||
// fixnum
|
||||
out.write((byte)d);
|
||||
} else {
|
||||
if(d < (1L<<16)) {
|
||||
if(d < (1<<8)) {
|
||||
// unsigned 8
|
||||
castBytes[0] = (byte)0xcc;
|
||||
castBytes[1] = (byte)d;
|
||||
out.write(castBytes, 0, 2);
|
||||
} else {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
out.write(castBytes, 0, 3);
|
||||
//System.out.println("pack uint 16 "+(short)d);
|
||||
}
|
||||
} else {
|
||||
if(d < (1L<<32)) {
|
||||
// unsigned 32
|
||||
castBytes[0] = (byte)0xce;
|
||||
castBuffer.putInt(1, (int)d);
|
||||
out.write(castBytes, 0, 5);
|
||||
} else {
|
||||
// unsigned 64
|
||||
castBytes[0] = (byte)0xcf;
|
||||
castBuffer.putLong(1, d);
|
||||
out.write(castBytes, 0, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packFloat(float d) throws IOException {
|
||||
castBytes[0] = (byte)0xca;
|
||||
castBuffer.putFloat(1, d);
|
||||
out.write(castBytes, 0, 5);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packDouble(double d) throws IOException {
|
||||
castBytes[0] = (byte)0xcb;
|
||||
castBuffer.putDouble(1, d);
|
||||
out.write(castBytes, 0, 9);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packNil() throws IOException {
|
||||
out.write((byte)0xc0);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packTrue() throws IOException {
|
||||
out.write((byte)0xc3);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packFalse() throws IOException {
|
||||
out.write((byte)0xc2);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packArray(int n) throws IOException {
|
||||
if(n < 16) {
|
||||
final int d = 0x90 | n;
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xdc;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdd;
|
||||
castBuffer.putInt(1, n);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packMap(int n) throws IOException {
|
||||
if(n < 16) {
|
||||
final int d = 0x80 | n;
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xde;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdf;
|
||||
castBuffer.putInt(1, n);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packRaw(int n) throws IOException {
|
||||
if(n < 32) {
|
||||
final int d = 0xa0 | n;
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xda;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdb;
|
||||
castBuffer.putInt(1, n);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packRawBody(byte[] b) throws IOException {
|
||||
out.write(b);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packRawBody(byte[] b, int off, int length) throws IOException {
|
||||
out.write(b, off, length);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Packer packWithSchema(Object o, Schema s) throws IOException {
|
||||
s.pack(this, o);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Packer packString(String s) throws IOException {
|
||||
byte[] b = ((String)s).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
}
|
||||
|
||||
|
||||
public Packer pack(String o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packString(o);
|
||||
}
|
||||
|
||||
public Packer pack(MessagePackable o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
o.messagePack(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(byte[] o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
packRaw(o.length);
|
||||
return packRawBody(o);
|
||||
}
|
||||
|
||||
public Packer pack(List o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
packArray(o.size());
|
||||
for(Object i : o) { pack(i); }
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Packer pack(Map o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
packMap(o.size());
|
||||
for(Map.Entry e : ((Map<Object,Object>)o).entrySet()) {
|
||||
pack(e.getKey());
|
||||
pack(e.getValue());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(Boolean o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
if(o) {
|
||||
return packTrue();
|
||||
} else {
|
||||
return packFalse();
|
||||
}
|
||||
}
|
||||
|
||||
public Packer pack(Byte o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packByte(o);
|
||||
}
|
||||
|
||||
public Packer pack(Short o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packShort(o);
|
||||
}
|
||||
|
||||
public Packer pack(Integer o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packInt(o);
|
||||
}
|
||||
|
||||
public Packer pack(Long o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packLong(o);
|
||||
}
|
||||
|
||||
public Packer pack(Float o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packFloat(o);
|
||||
}
|
||||
|
||||
public Packer pack(Double o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packDouble(o);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Packer pack(Object o) throws IOException {
|
||||
if(o == null) {
|
||||
return packNil();
|
||||
} else if(o instanceof String) {
|
||||
byte[] b = ((String)o).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
} else if(o instanceof MessagePackable) {
|
||||
((MessagePackable)o).messagePack(this);
|
||||
return this;
|
||||
} else if(o instanceof byte[]) {
|
||||
byte[] b = (byte[])o;
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
} else if(o instanceof List) {
|
||||
List<Object> l = (List<Object>)o;
|
||||
packArray(l.size());
|
||||
for(Object i : l) { pack(i); }
|
||||
return this;
|
||||
} else if(o instanceof Map) {
|
||||
Map<Object,Object> m = (Map<Object,Object>)o;
|
||||
packMap(m.size());
|
||||
for(Map.Entry e : m.entrySet()) {
|
||||
pack(e.getKey());
|
||||
pack(e.getValue());
|
||||
}
|
||||
return this;
|
||||
} else if(o instanceof Boolean) {
|
||||
if((Boolean)o) {
|
||||
return packTrue();
|
||||
} else {
|
||||
return packFalse();
|
||||
}
|
||||
} else if(o instanceof Integer) {
|
||||
return packInt((Integer)o);
|
||||
} else if(o instanceof Long) {
|
||||
return packLong((Long)o);
|
||||
} else if(o instanceof Short) {
|
||||
return packShort((Short)o);
|
||||
} else if(o instanceof Byte) {
|
||||
return packByte((Byte)o);
|
||||
} else if(o instanceof Float) {
|
||||
return packFloat((Float)o);
|
||||
} else if(o instanceof Double) {
|
||||
return packDouble((Double)o);
|
||||
} else {
|
||||
throw new IOException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
133
java/src/org/msgpack/Schema.java
Normal file
133
java/src/org/msgpack/Schema.java
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.Writer;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.schema.SSchemaParser;
|
||||
import org.msgpack.schema.ClassGenerator;
|
||||
|
||||
public abstract class Schema {
|
||||
private String expression;
|
||||
private String name;
|
||||
|
||||
public Schema(String name) {
|
||||
this.expression = expression;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Schema parse(String source) {
|
||||
return SSchemaParser.parse(source);
|
||||
}
|
||||
|
||||
public static Schema load(String source) {
|
||||
return SSchemaParser.load(source);
|
||||
}
|
||||
|
||||
public void write(Writer output) throws IOException {
|
||||
ClassGenerator.write(this, output);
|
||||
}
|
||||
|
||||
public abstract void pack(Packer pk, Object obj) throws IOException;
|
||||
|
||||
public abstract Object convert(Object obj) throws MessageTypeException;
|
||||
|
||||
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
|
||||
/* FIXME
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
82
java/src/org/msgpack/UnbufferedUnpacker.java
Normal file
82
java/src/org/msgpack/UnbufferedUnpacker.java
Normal file
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.lang.Iterable;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import org.msgpack.impl.UnpackerImpl;
|
||||
|
||||
public class UnbufferedUnpacker extends UnpackerImpl {
|
||||
private int offset;
|
||||
private boolean finished;
|
||||
private Object data;
|
||||
|
||||
public UnbufferedUnpacker() {
|
||||
super();
|
||||
this.offset = 0;
|
||||
this.finished = false;
|
||||
}
|
||||
|
||||
public UnbufferedUnpacker useSchema(Schema s) {
|
||||
super.setSchema(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
super.reset();
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
void setOffset(int offset) {
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public int execute(byte[] buffer) throws UnpackException {
|
||||
return execute(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
// FIXME
|
||||
public int execute(byte[] buffer, int offset, int length) throws UnpackException
|
||||
{
|
||||
int noffset = super.execute(buffer, offset + this.offset, length);
|
||||
this.offset = noffset - offset;
|
||||
if(super.isFinished()) {
|
||||
this.data = super.getData();
|
||||
this.finished = true;
|
||||
super.reset();
|
||||
} else {
|
||||
this.finished = false;
|
||||
}
|
||||
return noffset;
|
||||
}
|
||||
}
|
||||
|
29
java/src/org/msgpack/UnpackException.java
Normal file
29
java/src/org/msgpack/UnpackException.java
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class UnpackException extends IOException {
|
||||
public UnpackException() { }
|
||||
|
||||
public UnpackException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
|
66
java/src/org/msgpack/UnpackIterator.java
Normal file
66
java/src/org/msgpack/UnpackIterator.java
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class UnpackIterator implements Iterator<Object> {
|
||||
private Unpacker pac;
|
||||
private boolean have;
|
||||
private Object data;
|
||||
|
||||
UnpackIterator(Unpacker pac) {
|
||||
this.pac = pac;
|
||||
this.have = false;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if(have) { return true; }
|
||||
try {
|
||||
while(true) {
|
||||
if(pac.execute()) {
|
||||
data = pac.getData();
|
||||
pac.reset();
|
||||
have = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(!pac.fill()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
if(!have && !hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
have = false;
|
||||
return data;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
245
java/src/org/msgpack/Unpacker.java
Normal file
245
java/src/org/msgpack/Unpacker.java
Normal file
@@ -0,0 +1,245 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.lang.Iterable;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import org.msgpack.impl.UnpackerImpl;
|
||||
|
||||
public class Unpacker extends UnpackerImpl implements Iterable<Object> {
|
||||
|
||||
public static final int DEFAULT_BUFFER_SIZE = 32*1024;
|
||||
|
||||
private int used;
|
||||
private int offset;
|
||||
private int parsed;
|
||||
private byte[] buffer;
|
||||
private int bufferReserveSize;
|
||||
private InputStream stream;
|
||||
|
||||
public Unpacker() {
|
||||
this(DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public Unpacker(int bufferReserveSize) {
|
||||
this(null, bufferReserveSize);
|
||||
}
|
||||
|
||||
public Unpacker(InputStream stream) {
|
||||
this(stream, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public Unpacker(InputStream stream, int bufferReserveSize) {
|
||||
super();
|
||||
this.used = 0;
|
||||
this.offset = 0;
|
||||
this.parsed = 0;
|
||||
this.buffer = new byte[bufferReserveSize];
|
||||
this.bufferReserveSize = bufferReserveSize/2;
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
public Unpacker useSchema(Schema s) {
|
||||
super.setSchema(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void reserveBuffer(int size) {
|
||||
if(buffer.length - used >= size) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
if(used == parsed && buffer.length >= size) {
|
||||
// rewind buffer
|
||||
used = 0;
|
||||
offset = 0;
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
int nextSize = buffer.length * 2;
|
||||
while(nextSize < size + used) {
|
||||
nextSize *= 2;
|
||||
}
|
||||
|
||||
byte[] tmp = new byte[nextSize];
|
||||
System.arraycopy(buffer, offset, tmp, 0, used - offset);
|
||||
|
||||
buffer = tmp;
|
||||
used -= offset;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public int getBufferOffset() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public int getBufferCapacity() {
|
||||
return buffer.length - used;
|
||||
}
|
||||
|
||||
public void bufferConsumed(int size) {
|
||||
used += size;
|
||||
}
|
||||
|
||||
public void feed(byte[] buffer) {
|
||||
feed(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
public void feed(byte[] buffer, int offset, int length) {
|
||||
reserveBuffer(length);
|
||||
System.arraycopy(buffer, offset, this.buffer, this.offset, length);
|
||||
bufferConsumed(length);
|
||||
}
|
||||
|
||||
public boolean fill() throws IOException {
|
||||
if(stream == null) {
|
||||
return false;
|
||||
}
|
||||
reserveBuffer(bufferReserveSize);
|
||||
int rl = stream.read(getBuffer(), getBufferOffset(), getBufferCapacity());
|
||||
if(rl <= 0) {
|
||||
return false;
|
||||
}
|
||||
bufferConsumed(rl);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Iterator<Object> iterator() {
|
||||
return new UnpackIterator(this);
|
||||
}
|
||||
|
||||
public boolean execute() throws UnpackException {
|
||||
int noffset = super.execute(buffer, offset, used);
|
||||
if(noffset <= offset) {
|
||||
return false;
|
||||
}
|
||||
parsed += noffset - offset;
|
||||
offset = noffset;
|
||||
return super.isFinished();
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return super.getData();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
super.reset();
|
||||
parsed = 0;
|
||||
}
|
||||
|
||||
public int getMessageSize() {
|
||||
return parsed - offset + used;
|
||||
}
|
||||
|
||||
public int getParsedSize() {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
public int getNonParsedSize() {
|
||||
return used - offset;
|
||||
}
|
||||
|
||||
public void skipNonparsedBuffer(int size) {
|
||||
offset += size;
|
||||
}
|
||||
|
||||
public void removeNonparsedBuffer() {
|
||||
used = offset;
|
||||
}
|
||||
|
||||
/*
|
||||
public static class Context {
|
||||
private boolean finished;
|
||||
private Object data;
|
||||
private int offset;
|
||||
private UnpackerImpl impl;
|
||||
|
||||
public Context()
|
||||
{
|
||||
this.finished = false;
|
||||
this.impl = new UnpackerImpl();
|
||||
}
|
||||
|
||||
public boolean isFinished()
|
||||
{
|
||||
return finished;
|
||||
}
|
||||
|
||||
public Object getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
int getOffset()
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
void setFinished(boolean finished)
|
||||
{
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
void setData(Object data)
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
void setOffset(int offset)
|
||||
{
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
UnpackerImpl getImpl()
|
||||
{
|
||||
return impl;
|
||||
}
|
||||
}
|
||||
|
||||
public static int unpack(Context ctx, byte[] buffer) throws UnpackException
|
||||
{
|
||||
return unpack(ctx, buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
public static int unpack(Context ctx, byte[] buffer, int offset, int length) throws UnpackException
|
||||
{
|
||||
UnpackerImpl impl = ctx.getImpl();
|
||||
int noffset = impl.execute(buffer, offset + ctx.getOffset(), length);
|
||||
ctx.setOffset(noffset - offset);
|
||||
if(impl.isFinished()) {
|
||||
ctx.setData(impl.getData());
|
||||
ctx.setFinished(false);
|
||||
impl.reset();
|
||||
} else {
|
||||
ctx.setData(null);
|
||||
ctx.setFinished(true);
|
||||
}
|
||||
int parsed = noffset - offset;
|
||||
ctx.setOffset(parsed);
|
||||
return noffset;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
500
java/src/org/msgpack/impl/UnpackerImpl.java
Normal file
500
java/src/org/msgpack/impl/UnpackerImpl.java
Normal file
@@ -0,0 +1,500 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.impl;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
//import java.math.BigInteger;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.schema.GenericSchema;
|
||||
import org.msgpack.schema.IMapSchema;
|
||||
import org.msgpack.schema.IArraySchema;
|
||||
|
||||
public class UnpackerImpl {
|
||||
static final int CS_HEADER = 0x00;
|
||||
static final int CS_FLOAT = 0x0a;
|
||||
static final int CS_DOUBLE = 0x0b;
|
||||
static final int CS_UINT_8 = 0x0c;
|
||||
static final int CS_UINT_16 = 0x0d;
|
||||
static final int CS_UINT_32 = 0x0e;
|
||||
static final int CS_UINT_64 = 0x0f;
|
||||
static final int CS_INT_8 = 0x10;
|
||||
static final int CS_INT_16 = 0x11;
|
||||
static final int CS_INT_32 = 0x12;
|
||||
static final int CS_INT_64 = 0x13;
|
||||
static final int CS_RAW_16 = 0x1a;
|
||||
static final int CS_RAW_32 = 0x1b;
|
||||
static final int CS_ARRAY_16 = 0x1c;
|
||||
static final int CS_ARRAY_32 = 0x1d;
|
||||
static final int CS_MAP_16 = 0x1e;
|
||||
static final int CS_MAP_32 = 0x1f;
|
||||
static final int ACS_RAW_VALUE = 0x20;
|
||||
static final int CT_ARRAY_ITEM = 0x00;
|
||||
static final int CT_MAP_KEY = 0x01;
|
||||
static final int CT_MAP_VALUE = 0x02;
|
||||
|
||||
static final int MAX_STACK_SIZE = 16;
|
||||
|
||||
private int cs;
|
||||
private int trail;
|
||||
private int top;
|
||||
private int[] stack_ct = new int[MAX_STACK_SIZE];
|
||||
private int[] stack_count = new int[MAX_STACK_SIZE];
|
||||
private Object[] stack_obj = new Object[MAX_STACK_SIZE];
|
||||
private Schema[] stack_schema = new Schema[MAX_STACK_SIZE];
|
||||
private int top_ct;
|
||||
private int top_count;
|
||||
private Object top_obj;
|
||||
private Schema top_schema;
|
||||
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
|
||||
private boolean finished = false;
|
||||
private Object data = null;
|
||||
|
||||
private static final Schema GENERIC_SCHEMA = new GenericSchema();
|
||||
private Schema rootSchema;
|
||||
|
||||
protected UnpackerImpl()
|
||||
{
|
||||
setSchema(GENERIC_SCHEMA);
|
||||
}
|
||||
|
||||
protected void setSchema(Schema schema)
|
||||
{
|
||||
this.rootSchema = schema;
|
||||
reset();
|
||||
}
|
||||
|
||||
protected Object getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
protected boolean isFinished()
|
||||
{
|
||||
return finished;
|
||||
}
|
||||
|
||||
protected void reset()
|
||||
{
|
||||
cs = CS_HEADER;
|
||||
top = -1;
|
||||
finished = false;
|
||||
data = null;
|
||||
top_ct = 0;
|
||||
top_count = 0;
|
||||
top_obj = null;
|
||||
top_schema = rootSchema;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected int execute(byte[] src, int off, int length) throws UnpackException
|
||||
{
|
||||
if(off >= length) { return off; }
|
||||
|
||||
int limit = length;
|
||||
int i = off;
|
||||
int count;
|
||||
|
||||
Object obj = null;
|
||||
|
||||
_out: do {
|
||||
_header_again: {
|
||||
//System.out.println("while i:"+i+" limit:"+limit);
|
||||
|
||||
int b = src[i];
|
||||
|
||||
_push: {
|
||||
_fixed_trail_again:
|
||||
if(cs == CS_HEADER) {
|
||||
|
||||
if((b & 0x80) == 0) { // Positive Fixnum
|
||||
//System.out.println("positive fixnum "+b);
|
||||
obj = top_schema.createFromByte((byte)b);
|
||||
break _push;
|
||||
}
|
||||
|
||||
if((b & 0xe0) == 0xe0) { // Negative Fixnum
|
||||
//System.out.println("negative fixnum "+b);
|
||||
obj = top_schema.createFromByte((byte)b);
|
||||
break _push;
|
||||
}
|
||||
|
||||
if((b & 0xe0) == 0xa0) { // FixRaw
|
||||
trail = b & 0x1f;
|
||||
if(trail == 0) {
|
||||
obj = top_schema.createFromRaw(new byte[0], 0, 0);
|
||||
break _push;
|
||||
}
|
||||
cs = ACS_RAW_VALUE;
|
||||
break _fixed_trail_again;
|
||||
}
|
||||
|
||||
if((b & 0xf0) == 0x90) { // FixArray
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IArraySchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
count = b & 0x0f;
|
||||
//System.out.println("fixarray count:"+count);
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
top_obj = obj;
|
||||
top_ct = CT_ARRAY_ITEM;
|
||||
top_count = count;
|
||||
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
|
||||
break _header_again;
|
||||
}
|
||||
|
||||
if((b & 0xf0) == 0x80) { // FixMap
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IMapSchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
count = b & 0x0f;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
//System.out.println("fixmap count:"+count);
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
top_obj = obj;
|
||||
top_ct = CT_MAP_KEY;
|
||||
top_count = count;
|
||||
top_schema = ((IMapSchema)top_schema).getKeySchema();
|
||||
break _header_again;
|
||||
}
|
||||
|
||||
switch(b & 0xff) { // FIXME
|
||||
case 0xc0: // nil
|
||||
obj = top_schema.createFromNil();
|
||||
break _push;
|
||||
case 0xc2: // false
|
||||
obj = top_schema.createFromBoolean(false);
|
||||
break _push;
|
||||
case 0xc3: // true
|
||||
obj = top_schema.createFromBoolean(true);
|
||||
break _push;
|
||||
case 0xca: // float
|
||||
case 0xcb: // double
|
||||
case 0xcc: // unsigned int 8
|
||||
case 0xcd: // unsigned int 16
|
||||
case 0xce: // unsigned int 32
|
||||
case 0xcf: // unsigned int 64
|
||||
case 0xd0: // signed int 8
|
||||
case 0xd1: // signed int 16
|
||||
case 0xd2: // signed int 32
|
||||
case 0xd3: // signed int 64
|
||||
trail = 1 << (b & 0x03);
|
||||
cs = b & 0x1f;
|
||||
//System.out.println("a trail "+trail+" cs:"+cs);
|
||||
break _fixed_trail_again;
|
||||
case 0xda: // raw 16
|
||||
case 0xdb: // raw 32
|
||||
case 0xdc: // array 16
|
||||
case 0xdd: // array 32
|
||||
case 0xde: // map 16
|
||||
case 0xdf: // map 32
|
||||
trail = 2 << (b & 0x01);
|
||||
cs = b & 0x1f;
|
||||
//System.out.println("b trail "+trail+" cs:"+cs);
|
||||
break _fixed_trail_again;
|
||||
default:
|
||||
//System.out.println("unknown b "+(b&0xff));
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
|
||||
} // _fixed_trail_again
|
||||
|
||||
do {
|
||||
_fixed_trail_again: {
|
||||
|
||||
if(limit - i <= trail) { break _out; }
|
||||
int n = i + 1;
|
||||
i += trail;
|
||||
|
||||
switch(cs) {
|
||||
case CS_FLOAT:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
obj = top_schema.createFromFloat( castBuffer.getFloat(0) );
|
||||
//System.out.println("float "+obj);
|
||||
break _push;
|
||||
case CS_DOUBLE:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 8);
|
||||
obj = top_schema.createFromDouble( castBuffer.getDouble(0) );
|
||||
//System.out.println("double "+obj);
|
||||
break _push;
|
||||
case CS_UINT_8:
|
||||
//System.out.println(n);
|
||||
//System.out.println(src[n]);
|
||||
//System.out.println(src[n+1]);
|
||||
//System.out.println(src[n-1]);
|
||||
obj = top_schema.createFromShort( (short)((src[n]) & 0xff) );
|
||||
//System.out.println("uint8 "+obj);
|
||||
break _push;
|
||||
case CS_UINT_16:
|
||||
//System.out.println(src[n]);
|
||||
//System.out.println(src[n+1]);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 2);
|
||||
obj = top_schema.createFromInt( ((int)castBuffer.getShort(0)) & 0xffff );
|
||||
//System.out.println("uint 16 "+obj);
|
||||
break _push;
|
||||
case CS_UINT_32:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
obj = top_schema.createFromLong( ((long)castBuffer.getInt(0)) & 0xffffffffL );
|
||||
//System.out.println("uint 32 "+obj);
|
||||
break _push;
|
||||
case CS_UINT_64:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 8);
|
||||
{
|
||||
long o = castBuffer.getLong(0);
|
||||
if(o < 0) {
|
||||
// FIXME
|
||||
//obj = GenericBigInteger.valueOf(o & 0x7fffffffL).setBit(31);
|
||||
throw new UnpackException("uint 64 bigger than 0x7fffffff is not supported");
|
||||
} else {
|
||||
obj = top_schema.createFromLong( o );
|
||||
}
|
||||
}
|
||||
break _push;
|
||||
case CS_INT_8:
|
||||
obj = top_schema.createFromByte( src[n] );
|
||||
break _push;
|
||||
case CS_INT_16:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 2);
|
||||
obj = top_schema.createFromShort( castBuffer.getShort(0) );
|
||||
break _push;
|
||||
case CS_INT_32:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
obj = top_schema.createFromInt( castBuffer.getInt(0) );
|
||||
break _push;
|
||||
case CS_INT_64:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 8);
|
||||
obj = top_schema.createFromLong( castBuffer.getLong(0) );
|
||||
break _push;
|
||||
case CS_RAW_16:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 2);
|
||||
trail = ((int)castBuffer.getShort(0)) & 0xffff;
|
||||
if(trail == 0) {
|
||||
obj = top_schema.createFromRaw(new byte[0], 0, 0);
|
||||
break _push;
|
||||
}
|
||||
cs = ACS_RAW_VALUE;
|
||||
break _fixed_trail_again;
|
||||
case CS_RAW_32:
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
// FIXME overflow check
|
||||
trail = castBuffer.getInt(0) & 0x7fffffff;
|
||||
if(trail == 0) {
|
||||
obj = top_schema.createFromRaw(new byte[0], 0, 0);
|
||||
break _push;
|
||||
}
|
||||
cs = ACS_RAW_VALUE;
|
||||
case ACS_RAW_VALUE:
|
||||
obj = top_schema.createFromRaw(src, n, trail);
|
||||
break _push;
|
||||
case CS_ARRAY_16:
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IArraySchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 2);
|
||||
count = ((int)castBuffer.getShort(0)) & 0xffff;
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
top_obj = obj;
|
||||
top_ct = CT_ARRAY_ITEM;
|
||||
top_count = count;
|
||||
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
|
||||
break _header_again;
|
||||
case CS_ARRAY_32:
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IArraySchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
// FIXME overflow check
|
||||
count = castBuffer.getInt(0) & 0x7fffffff;
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
top_obj = obj;
|
||||
top_ct = CT_ARRAY_ITEM;
|
||||
top_count = count;
|
||||
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
|
||||
break _header_again;
|
||||
case CS_MAP_16:
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IMapSchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 2);
|
||||
count = ((int)castBuffer.getShort(0)) & 0xffff;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
//System.out.println("fixmap count:"+count);
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
++top;
|
||||
top_obj = obj;
|
||||
top_ct = CT_MAP_KEY;
|
||||
top_count = count;
|
||||
top_schema = ((IMapSchema)top_schema).getKeySchema();
|
||||
break _header_again;
|
||||
case CS_MAP_32:
|
||||
if(top >= MAX_STACK_SIZE) {
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
if(!(top_schema instanceof IMapSchema)) {
|
||||
throw new RuntimeException("type error");
|
||||
}
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(src, n, 4);
|
||||
// FIXME overflow check
|
||||
count = castBuffer.getInt(0) & 0x7fffffff;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
//System.out.println("fixmap count:"+count);
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
stack_count[top] = top_count;
|
||||
stack_schema[top] = top_schema;
|
||||
top_obj = obj;
|
||||
top_ct = CT_MAP_KEY;
|
||||
top_count = count;
|
||||
top_schema = ((IMapSchema)top_schema).getKeySchema();
|
||||
break _header_again;
|
||||
default:
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
|
||||
} // _fixed_trail_again
|
||||
} while(true);
|
||||
} // _push
|
||||
|
||||
do {
|
||||
_push: {
|
||||
//System.out.println("push top:"+top);
|
||||
if(top == -1) {
|
||||
++i;
|
||||
data = obj;
|
||||
finished = true;
|
||||
break _out;
|
||||
}
|
||||
|
||||
switch(top_ct) {
|
||||
case CT_ARRAY_ITEM: {
|
||||
//System.out.println("array item "+obj);
|
||||
Object[] ar = (Object[])top_obj;
|
||||
ar[ar.length - top_count] = obj;
|
||||
if(--top_count == 0) {
|
||||
top_obj = stack_obj[top];
|
||||
top_ct = stack_ct[top];
|
||||
top_count = stack_count[top];
|
||||
top_schema = stack_schema[top];
|
||||
obj = ((IArraySchema)top_schema).createFromArray(ar);
|
||||
stack_obj[top] = null;
|
||||
stack_schema[top] = null;
|
||||
--top;
|
||||
break _push;
|
||||
} else {
|
||||
top_schema = ((IArraySchema)stack_schema[top]).getElementSchema(ar.length - top_count);
|
||||
}
|
||||
break _header_again;
|
||||
}
|
||||
case CT_MAP_KEY: {
|
||||
//System.out.println("map key:"+top+" "+obj);
|
||||
Object[] mp = (Object[])top_obj;
|
||||
mp[mp.length - top_count*2] = obj;
|
||||
top_ct = CT_MAP_VALUE;
|
||||
top_schema = ((IMapSchema)stack_schema[top]).getValueSchema();
|
||||
break _header_again;
|
||||
}
|
||||
case CT_MAP_VALUE: {
|
||||
//System.out.println("map value:"+top+" "+obj);
|
||||
Object[] mp = (Object[])top_obj;
|
||||
mp[mp.length - top_count*2 + 1] = obj;
|
||||
if(--top_count == 0) {
|
||||
top_obj = stack_obj[top];
|
||||
top_ct = stack_ct[top];
|
||||
top_count = stack_count[top];
|
||||
top_schema = stack_schema[top];
|
||||
obj = ((IMapSchema)top_schema).createFromMap(mp);
|
||||
stack_obj[top] = null;
|
||||
stack_schema[top] = null;
|
||||
--top;
|
||||
break _push;
|
||||
}
|
||||
top_ct = CT_MAP_KEY;
|
||||
break _header_again;
|
||||
}
|
||||
default:
|
||||
throw new UnpackException("parse error");
|
||||
}
|
||||
} // _push
|
||||
} while(true);
|
||||
|
||||
} // _header_again
|
||||
cs = CS_HEADER;
|
||||
++i;
|
||||
} while(i < limit); // _out
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
125
java/src/org/msgpack/schema/ArraySchema.java
Normal file
125
java/src/org/msgpack/schema/ArraySchema.java
Normal file
@@ -0,0 +1,125 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.RandomAccess;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ArraySchema extends Schema implements IArraySchema {
|
||||
private Schema elementSchema;
|
||||
|
||||
public ArraySchema(Schema elementSchema)
|
||||
{
|
||||
super("array");
|
||||
this.elementSchema = elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName()
|
||||
{
|
||||
return "List<"+elementSchema.getFullName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression()
|
||||
{
|
||||
return "(array "+elementSchema.getExpression()+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException
|
||||
{
|
||||
if(obj instanceof List) {
|
||||
ArrayList<Object> d = (ArrayList<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
elementSchema.pack(pk, d.get(i));
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
}
|
||||
|
||||
} else if(obj instanceof Set) {
|
||||
Set<Object> d = (Set<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException
|
||||
{
|
||||
if(obj instanceof List) {
|
||||
List d = (List)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
ar.add( elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else if(obj instanceof Collection) {
|
||||
Collection d = (Collection)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
for(Object e : (Collection)obj) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index)
|
||||
{
|
||||
return elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj)
|
||||
{
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
}
|
||||
|
89
java/src/org/msgpack/schema/ByteSchema.java
Normal file
89
java/src/org/msgpack/schema/ByteSchema.java
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteSchema extends Schema {
|
||||
public ByteSchema() {
|
||||
super("Byte");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packByte( ((Number)obj).byteValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Byte) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).byteValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (byte)v;
|
||||
}
|
||||
}
|
||||
|
241
java/src/org/msgpack/schema/ClassGenerator.java
Normal file
241
java/src/org/msgpack/schema/ClassGenerator.java
Normal file
@@ -0,0 +1,241 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.Writer;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ClassGenerator {
|
||||
private ClassSchema schema;
|
||||
private Writer writer;
|
||||
private int indent;
|
||||
|
||||
private ClassGenerator(Writer writer) {
|
||||
this.writer = writer;
|
||||
this.indent = 0;
|
||||
}
|
||||
|
||||
public static void write(Schema schema, Writer dest) throws IOException {
|
||||
if(!(schema instanceof ClassSchema)) {
|
||||
throw new RuntimeException("schema is not class schema");
|
||||
}
|
||||
ClassSchema cs = (ClassSchema)schema;
|
||||
new ClassGenerator(dest).run(cs);
|
||||
}
|
||||
|
||||
private void run(ClassSchema cs) throws IOException {
|
||||
List<ClassSchema> subclasses = new ArrayList<ClassSchema>();
|
||||
for(FieldSchema f : cs.getFields()) {
|
||||
findSubclassSchema(subclasses, f.getSchema());
|
||||
}
|
||||
|
||||
for(ClassSchema sub : subclasses) {
|
||||
sub.setNamespace(cs.getNamespace());
|
||||
sub.setImports(cs.getImports());
|
||||
}
|
||||
|
||||
this.schema = cs;
|
||||
|
||||
writeHeader();
|
||||
|
||||
writeClass();
|
||||
|
||||
for(ClassSchema sub : subclasses) {
|
||||
this.schema = sub;
|
||||
writeSubclass();
|
||||
}
|
||||
|
||||
writeFooter();
|
||||
|
||||
this.schema = null;
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
private void findSubclassSchema(List<ClassSchema> dst, Schema s) {
|
||||
if(s instanceof ClassSchema) {
|
||||
ClassSchema cs = (ClassSchema)s;
|
||||
if(!dst.contains(cs)) { dst.add(cs); }
|
||||
for(FieldSchema f : cs.getFields()) {
|
||||
findSubclassSchema(dst, f.getSchema());
|
||||
}
|
||||
} else if(s instanceof ArraySchema) {
|
||||
ArraySchema as = (ArraySchema)s;
|
||||
findSubclassSchema(dst, as.getElementSchema(0));
|
||||
} else if(s instanceof MapSchema) {
|
||||
MapSchema as = (MapSchema)s;
|
||||
findSubclassSchema(dst, as.getKeySchema());
|
||||
findSubclassSchema(dst, as.getValueSchema());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeHeader() throws IOException {
|
||||
if(schema.getNamespace() != null) {
|
||||
line("package "+schema.getNamespace()+";");
|
||||
line();
|
||||
}
|
||||
line("import java.util.*;");
|
||||
line("import java.io.*;");
|
||||
line("import org.msgpack.*;");
|
||||
line("import org.msgpack.schema.ClassSchema;");
|
||||
line("import org.msgpack.schema.FieldSchema;");
|
||||
}
|
||||
|
||||
private void writeFooter() throws IOException {
|
||||
line();
|
||||
}
|
||||
|
||||
private void writeClass() throws IOException {
|
||||
line();
|
||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
writeMemberVariables();
|
||||
writeMemberFunctions();
|
||||
popIndent();
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeSubclass() throws IOException {
|
||||
line();
|
||||
line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
writeMemberVariables();
|
||||
writeMemberFunctions();
|
||||
popIndent();
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeSchema() throws IOException {
|
||||
line("private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load(\""+schema.getExpression()+"\");");
|
||||
line("public static ClassSchema getSchema() { return _SCHEMA; }");
|
||||
}
|
||||
|
||||
private void writeMemberVariables() throws IOException {
|
||||
line();
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("public "+f.getSchema().getFullName()+" "+f.getName()+";");
|
||||
}
|
||||
}
|
||||
|
||||
private void writeMemberFunctions() throws IOException {
|
||||
// void messagePack(Packer pk)
|
||||
// boolean equals(Object obj)
|
||||
// int hashCode()
|
||||
// void set(int _index, Object _value)
|
||||
// Object get(int _index);
|
||||
// getXxx()
|
||||
// setXxx(Xxx xxx)
|
||||
writeConstructors();
|
||||
writeAccessors();
|
||||
writePackFunction();
|
||||
writeMergeFunction();
|
||||
writeFactoryFunction();
|
||||
}
|
||||
|
||||
private void writeConstructors() throws IOException {
|
||||
line();
|
||||
line("public "+schema.getName()+"() { }");
|
||||
}
|
||||
|
||||
private void writeAccessors() throws IOException {
|
||||
// FIXME
|
||||
//line();
|
||||
//for(FieldSchema f : schema.getFields()) {
|
||||
// line("");
|
||||
//}
|
||||
}
|
||||
|
||||
private void writePackFunction() throws IOException {
|
||||
line();
|
||||
line("@Override");
|
||||
line("public void messagePack(Packer _pk) throws IOException");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line("_pk.packArray("+schema.getFields().length+");");
|
||||
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("_fields["+i+"].getSchema().pack(_pk, "+f.getName()+");");
|
||||
++i;
|
||||
}
|
||||
popIndent();
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeMergeFunction() throws IOException {
|
||||
line();
|
||||
line("@Override");
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public void messageMerge(Object obj) throws MessageTypeException");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line("Object[] _source = ((List)obj).toArray();");
|
||||
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||
++i;
|
||||
}
|
||||
popIndent();
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeFactoryFunction() throws IOException {
|
||||
line();
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public static "+schema.getName()+" createFromMessage(Object[] _message)");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line(schema.getName()+" _self = new "+schema.getName()+"();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];");
|
||||
++i;
|
||||
}
|
||||
line("return _self;");
|
||||
popIndent();
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void line(String str) throws IOException {
|
||||
for(int i=0; i < indent; ++i) {
|
||||
writer.write("\t");
|
||||
}
|
||||
writer.write(str+"\n");
|
||||
}
|
||||
|
||||
private void line() throws IOException {
|
||||
writer.write("\n");
|
||||
}
|
||||
|
||||
private void pushIndent() {
|
||||
indent += 1;
|
||||
}
|
||||
|
||||
private void popIndent() {
|
||||
indent -= 1;
|
||||
}
|
||||
}
|
||||
|
95
java/src/org/msgpack/schema/ClassSchema.java
Normal file
95
java/src/org/msgpack/schema/ClassSchema.java
Normal file
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.msgpack.*;
|
||||
|
||||
public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
protected FieldSchema[] fields;
|
||||
protected List<String> imports;
|
||||
protected String namespace;
|
||||
protected String fqdn;
|
||||
|
||||
public ClassSchema(
|
||||
String name, String namespace,
|
||||
List<String> imports, List<FieldSchema> fields) {
|
||||
super(name);
|
||||
this.namespace = namespace;
|
||||
this.imports = imports; // FIXME clone?
|
||||
this.fields = new FieldSchema[fields.size()];
|
||||
System.arraycopy(fields.toArray(), 0, this.fields, 0, fields.size());
|
||||
if(namespace == null) {
|
||||
this.fqdn = name;
|
||||
} else {
|
||||
this.fqdn = namespace+"."+name;
|
||||
}
|
||||
}
|
||||
|
||||
public final FieldSchema[] getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
List<String> getImports() {
|
||||
return imports;
|
||||
}
|
||||
|
||||
void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
void setImports(List<String> imports) {
|
||||
this.imports = imports; // FIXME clone?
|
||||
}
|
||||
|
||||
//@Override
|
||||
//public String getFullName()
|
||||
//{
|
||||
// if(namespace == null) {
|
||||
// return getName();
|
||||
// } else {
|
||||
// return namespace+"."+getName();
|
||||
// }
|
||||
//}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
StringBuffer b = new StringBuffer();
|
||||
b.append("(class ");
|
||||
b.append(getName());
|
||||
if(namespace != null) {
|
||||
b.append(" (package "+namespace+")");
|
||||
}
|
||||
for(FieldSchema f : fields) {
|
||||
b.append(" "+f.getExpression());
|
||||
}
|
||||
b.append(")");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public boolean equals(SpecificClassSchema o) {
|
||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||
getName().equals(o.getName());
|
||||
}
|
||||
}
|
||||
|
84
java/src/org/msgpack/schema/DoubleSchema.java
Normal file
84
java/src/org/msgpack/schema/DoubleSchema.java
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleSchema extends Schema {
|
||||
public DoubleSchema() {
|
||||
super("Double");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "double";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packDouble( ((Number)obj).doubleValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).doubleValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (double)v;
|
||||
}
|
||||
}
|
||||
|
43
java/src/org/msgpack/schema/FieldSchema.java
Normal file
43
java/src/org/msgpack/schema/FieldSchema.java
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import org.msgpack.Schema;
|
||||
|
||||
public class FieldSchema {
|
||||
private String name;
|
||||
private Schema schema;
|
||||
|
||||
public FieldSchema(String name, Schema schema) {
|
||||
this.name = name;
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public final String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public final Schema getSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return "(field "+name+" "+schema.getExpression()+")";
|
||||
}
|
||||
}
|
||||
|
84
java/src/org/msgpack/schema/FloatSchema.java
Normal file
84
java/src/org/msgpack/schema/FloatSchema.java
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatSchema extends Schema {
|
||||
public FloatSchema() {
|
||||
super("Float");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "float";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packFloat( ((Number)obj).floatValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Float) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).floatValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (float)v;
|
||||
}
|
||||
}
|
||||
|
91
java/src/org/msgpack/schema/GenericClassSchema.java
Normal file
91
java/src/org/msgpack/schema/GenericClassSchema.java
Normal file
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class GenericClassSchema extends ClassSchema {
|
||||
public GenericClassSchema(
|
||||
String name, String namespace,
|
||||
List<String> imports, List<FieldSchema> fields) {
|
||||
super(name, namespace, imports, fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Map) {
|
||||
Map d = (Map)obj;
|
||||
pk.packArray(fields.length);
|
||||
for(int i=0; i < fields.length; ++i) {
|
||||
FieldSchema f = fields[i];
|
||||
f.getSchema().pack(pk, d.get(f.getName()));
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Collection) {
|
||||
// FIXME optimize
|
||||
return createFromArray( ((Collection)obj).toArray() );
|
||||
|
||||
} else if(obj instanceof Map) {
|
||||
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
||||
Map d = (Map)obj;
|
||||
for(int i=0; i < fields.length; ++i) {
|
||||
FieldSchema f = fields[i];
|
||||
String fieldName = f.getName();
|
||||
m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public Schema getElementSchema(int index) {
|
||||
// FIXME check index < fields.length
|
||||
return fields[index].getSchema();
|
||||
}
|
||||
|
||||
public Object createFromArray(Object[] obj) {
|
||||
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
||||
int i=0;
|
||||
for(; i < obj.length; ++i) {
|
||||
m.put(fields[i].getName(), obj[i]);
|
||||
}
|
||||
for(; i < fields.length; ++i) {
|
||||
m.put(fields[i].getName(), null);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
192
java/src/org/msgpack/schema/GenericSchema.java
Normal file
192
java/src/org/msgpack/schema/GenericSchema.java
Normal file
@@ -0,0 +1,192 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
//import org.msgpack.generic.*;
|
||||
|
||||
public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||
public GenericSchema() {
|
||||
super("Object");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "object";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
pk.pack(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getKeySchema() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getValueSchema() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
byte[] bytes = new byte[length];
|
||||
System.arraycopy(b, offset, bytes, 0, length);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj) {
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object createFromMap(Object[] obj) {
|
||||
HashMap m = new HashMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return new GenericBoolean(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFromByte(byte v) {
|
||||
return new GenericByte(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return new GenericShort(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return new GenericInt(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return new GenericLong(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return new GenericFloat(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return new GenericDouble(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
return new GenericRaw(b, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj) {
|
||||
// FIXME GenericArray
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromMap(Object[] obj) {
|
||||
GenericMap m = new GenericMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
26
java/src/org/msgpack/schema/IArraySchema.java
Normal file
26
java/src/org/msgpack/schema/IArraySchema.java
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import org.msgpack.Schema;
|
||||
|
||||
public interface IArraySchema {
|
||||
public Schema getElementSchema(int index);
|
||||
public Object createFromArray(Object[] obj);
|
||||
}
|
||||
|
27
java/src/org/msgpack/schema/IMapSchema.java
Normal file
27
java/src/org/msgpack/schema/IMapSchema.java
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import org.msgpack.Schema;
|
||||
|
||||
public interface IMapSchema {
|
||||
public Schema getKeySchema();
|
||||
public Schema getValueSchema();
|
||||
public Object createFromMap(Object[] obj);
|
||||
}
|
||||
|
89
java/src/org/msgpack/schema/IntSchema.java
Normal file
89
java/src/org/msgpack/schema/IntSchema.java
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntSchema extends Schema {
|
||||
public IntSchema() {
|
||||
super("Integer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "int";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packInt( ((Number)obj).intValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Integer) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).intValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (int)v;
|
||||
}
|
||||
}
|
||||
|
89
java/src/org/msgpack/schema/LongSchema.java
Normal file
89
java/src/org/msgpack/schema/LongSchema.java
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class LongSchema extends Schema {
|
||||
public LongSchema() {
|
||||
super("Long");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "long";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packLong( ((Number)obj).longValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Long) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (long)v;
|
||||
}
|
||||
}
|
||||
|
103
java/src/org/msgpack/schema/MapSchema.java
Normal file
103
java/src/org/msgpack/schema/MapSchema.java
Normal file
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class MapSchema extends Schema implements IMapSchema {
|
||||
private Schema keySchema;
|
||||
private Schema valueSchema;
|
||||
|
||||
public MapSchema(Schema keySchema, Schema valueSchema) {
|
||||
super("map");
|
||||
this.keySchema = keySchema;
|
||||
this.valueSchema = valueSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "(map "+keySchema.getExpression()+" "+valueSchema.getExpression()+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Map) {
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
pk.packMap(d.size());
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
keySchema.pack(pk, e.getKey());
|
||||
valueSchema.pack(pk, e.getValue());
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Map) {
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
Map<Object,Object> m = new HashMap<Object,Object>();
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue()));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getKeySchema() {
|
||||
return keySchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getValueSchema() {
|
||||
return valueSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object createFromMap(Object[] obj) {
|
||||
HashMap m = new HashMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
105
java/src/org/msgpack/schema/RawSchema.java
Normal file
105
java/src/org/msgpack/schema/RawSchema.java
Normal file
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class RawSchema extends Schema {
|
||||
public RawSchema() {
|
||||
super("raw");
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return "byte[]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof byte[]) {
|
||||
byte[] d = (byte[])obj;
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
if(!d.hasArray()) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
pk.packRaw(d.capacity());
|
||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
||||
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof byte[]) {
|
||||
// FIXME copy?
|
||||
//byte[] d = (byte[])obj;
|
||||
//byte[] v = new byte[d.length];
|
||||
//System.arraycopy(d, 0, v, 0, d.length);
|
||||
//return v;
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
byte[] v = new byte[d.capacity()];
|
||||
int pos = d.position();
|
||||
d.get(v);
|
||||
d.position(pos);
|
||||
return v;
|
||||
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
return ((String)obj).getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
byte[] d = new byte[length];
|
||||
System.arraycopy(b, offset, d, 0, length);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
64
java/src/org/msgpack/schema/ReflectionClassSchema.java
Normal file
64
java/src/org/msgpack/schema/ReflectionClassSchema.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.lang.reflect.*;
|
||||
import org.msgpack.*;
|
||||
|
||||
// FIXME
|
||||
public abstract class ReflectionClassSchema extends ClassSchema {
|
||||
private Constructor constructorCache;
|
||||
|
||||
public ReflectionClassSchema(String name, List<FieldSchema> fields, String namespace, List<String> imports) {
|
||||
super(name, namespace, imports, fields);
|
||||
}
|
||||
|
||||
/*
|
||||
Schema getElementSchema(int index)
|
||||
{
|
||||
// FIXME check index < fields.length
|
||||
fields[index].getSchema();
|
||||
}
|
||||
|
||||
Object createFromArray(Object[] obj)
|
||||
{
|
||||
Object o = newInstance();
|
||||
((MessageConvertable)o).messageConvert(obj);
|
||||
return o;
|
||||
}
|
||||
|
||||
Object newInstance()
|
||||
{
|
||||
if(constructorCache == null) {
|
||||
cacheConstructor();
|
||||
}
|
||||
try {
|
||||
return constructorCache.newInstance((Object[])null);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void cacheConstructor()
|
||||
{
|
||||
try {
|
||||
Class c = Class.forName(fqdn);
|
||||
int index = 0;
|
||||
for(SpecificFieldSchema f : fields) {
|
||||
f.cacheField(c, index++);
|
||||
}
|
||||
constructorCache = c.getDeclaredConstructor((Class[])null);
|
||||
constructorCache.setAccessible(true);
|
||||
} catch(ClassNotFoundException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
254
java/src/org/msgpack/schema/SSchemaParser.java
Normal file
254
java/src/org/msgpack/schema/SSchemaParser.java
Normal file
@@ -0,0 +1,254 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import org.msgpack.*;
|
||||
|
||||
// FIXME exception class
|
||||
|
||||
public class SSchemaParser {
|
||||
public static Schema parse(String source) {
|
||||
return new SSchemaParser(false).run(source);
|
||||
}
|
||||
|
||||
public static Schema load(String source) {
|
||||
return new SSchemaParser(true).run(source);
|
||||
}
|
||||
|
||||
private static abstract class SExp {
|
||||
boolean isAtom() { return false; }
|
||||
public String getAtom() { return null; }
|
||||
|
||||
boolean isTuple() { return false; }
|
||||
public SExp getTuple(int i) { return null; }
|
||||
public int size() { return 0; }
|
||||
public boolean empty() { return size() == 0; }
|
||||
Iterator<SExp> iterator(int offset) { return null; }
|
||||
}
|
||||
|
||||
private static class SAtom extends SExp {
|
||||
private String atom;
|
||||
|
||||
SAtom(String atom) { this.atom = atom; }
|
||||
|
||||
boolean isAtom() { return true; }
|
||||
public String getAtom() { return atom; }
|
||||
|
||||
public String toString() { return atom; }
|
||||
}
|
||||
|
||||
private static class STuple extends SExp {
|
||||
private List<SExp> tuple;
|
||||
|
||||
STuple() { this.tuple = new ArrayList<SExp>(); }
|
||||
|
||||
public void add(SExp e) { tuple.add(e); }
|
||||
|
||||
boolean isTuple() { return true; }
|
||||
public SExp getTuple(int i) { return tuple.get(i); }
|
||||
public int size() { return tuple.size(); }
|
||||
|
||||
Iterator<SExp> iterator(int skip) {
|
||||
Iterator<SExp> i = tuple.iterator();
|
||||
for(int s=0; s < skip; ++s) { i.next(); }
|
||||
return i;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if(tuple.isEmpty()) { return "()"; }
|
||||
Iterator<SExp> i = tuple.iterator();
|
||||
StringBuffer o = new StringBuffer();
|
||||
o.append("(").append(i.next());
|
||||
while(i.hasNext()) { o.append(" ").append(i.next()); }
|
||||
o.append(")");
|
||||
return o.toString();
|
||||
}
|
||||
}
|
||||
|
||||
boolean specificClass;
|
||||
|
||||
private SSchemaParser(boolean specificClass) {
|
||||
this.specificClass = specificClass;
|
||||
}
|
||||
|
||||
private static Pattern pattern = Pattern.compile(
|
||||
"(?:\\s+)|([\\(\\)]|[\\d\\w\\.]+)");
|
||||
|
||||
private Schema run(String source) {
|
||||
Matcher m = pattern.matcher(source);
|
||||
|
||||
Stack<STuple> stack = new Stack<STuple>();
|
||||
String token;
|
||||
|
||||
while(true) {
|
||||
while(true) {
|
||||
if(!m.find()) { throw new RuntimeException("unexpected end of file"); }
|
||||
token = m.group(1);
|
||||
if(token != null) { break; }
|
||||
}
|
||||
|
||||
if(token.equals("(")) {
|
||||
stack.push(new STuple());
|
||||
} else if(token.equals(")")) {
|
||||
STuple top = stack.pop();
|
||||
if(stack.empty()) {
|
||||
stack.push(top);
|
||||
break;
|
||||
}
|
||||
stack.peek().add(top);
|
||||
} else {
|
||||
if(stack.empty()) {
|
||||
throw new RuntimeException("unexpected token '"+token+"'");
|
||||
}
|
||||
stack.peek().add(new SAtom(token));
|
||||
}
|
||||
}
|
||||
|
||||
while(true) {
|
||||
if(!m.find()) { break; }
|
||||
token = m.group(1);
|
||||
if(token != null) { throw new RuntimeException("unexpected token '"+token+"'"); }
|
||||
}
|
||||
|
||||
return readType( stack.pop() );
|
||||
}
|
||||
|
||||
private Schema readType(SExp exp) {
|
||||
if(exp.isAtom()) {
|
||||
String type = exp.getAtom();
|
||||
if(type.equals("string")) {
|
||||
return new StringSchema();
|
||||
} else if(type.equals("raw")) {
|
||||
return new RawSchema();
|
||||
} else if(type.equals("byte")) {
|
||||
return new ByteSchema();
|
||||
} else if(type.equals("short")) {
|
||||
return new ShortSchema();
|
||||
} else if(type.equals("int")) {
|
||||
return new IntSchema();
|
||||
} else if(type.equals("long")) {
|
||||
return new LongSchema();
|
||||
} else if(type.equals("float")) {
|
||||
return new FloatSchema();
|
||||
} else if(type.equals("double")) {
|
||||
return new DoubleSchema();
|
||||
} else if(type.equals("object")) {
|
||||
return new GenericSchema();
|
||||
} else {
|
||||
throw new RuntimeException("byte, short, int, long, float, double, raw, string or object is expected but got '"+type+"': "+exp);
|
||||
}
|
||||
} else {
|
||||
String type = exp.getTuple(0).getAtom();
|
||||
if(type.equals("class")) {
|
||||
return parseClass(exp);
|
||||
} else if(type.equals("array")) {
|
||||
return parseArray(exp);
|
||||
} else if(type.equals("map")) {
|
||||
return parseMap(exp);
|
||||
} else {
|
||||
throw new RuntimeException("class, array or map is expected but got '"+type+"': "+exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ClassSchema parseClass(SExp exp) {
|
||||
if(exp.size() < 3 || !exp.getTuple(1).isAtom()) {
|
||||
throw new RuntimeException("class is (class NAME CLASS_BODY): "+exp);
|
||||
}
|
||||
|
||||
String namespace = null;
|
||||
List<String> imports = new ArrayList<String>();
|
||||
String name = exp.getTuple(1).getAtom();
|
||||
List<FieldSchema> fields = new ArrayList<FieldSchema>();
|
||||
|
||||
for(Iterator<SExp> i=exp.iterator(2); i.hasNext();) {
|
||||
SExp subexp = i.next();
|
||||
if(!subexp.isTuple() || subexp.empty() || !subexp.getTuple(0).isAtom()) {
|
||||
throw new RuntimeException("field, package or import is expected: "+subexp);
|
||||
}
|
||||
String type = subexp.getTuple(0).getAtom();
|
||||
if(type.equals("field")) {
|
||||
fields.add( parseField(subexp) );
|
||||
} else if(type.equals("package")) {
|
||||
if(namespace != null) {
|
||||
throw new RuntimeException("duplicated package definition: "+subexp);
|
||||
}
|
||||
namespace = parseNamespace(subexp);
|
||||
} else if(type.equals("import")) {
|
||||
imports.add( parseImport(subexp) );
|
||||
} else {
|
||||
throw new RuntimeException("field, package or import is expected but got '"+type+"': "+subexp);
|
||||
}
|
||||
}
|
||||
|
||||
if(specificClass) {
|
||||
return new SpecificClassSchema(name, namespace, imports, fields);
|
||||
} else {
|
||||
return new GenericClassSchema(name, namespace, imports, fields);
|
||||
}
|
||||
}
|
||||
|
||||
private ArraySchema parseArray(SExp exp) {
|
||||
if(exp.size() != 2) {
|
||||
throw new RuntimeException("array is (array ELEMENT_TYPE): "+exp);
|
||||
}
|
||||
Schema elementType = readType(exp.getTuple(1));
|
||||
return new ArraySchema(elementType);
|
||||
}
|
||||
|
||||
private MapSchema parseMap(SExp exp) {
|
||||
if(exp.size() != 3 || !exp.getTuple(1).isAtom()) {
|
||||
throw new RuntimeException("map is (map KEY_TYPE VALUE_TYPE): "+exp);
|
||||
}
|
||||
Schema keyType = readType(exp.getTuple(1));
|
||||
Schema valueType = readType(exp.getTuple(2));
|
||||
return new MapSchema(keyType, valueType);
|
||||
}
|
||||
|
||||
private String parseNamespace(SExp exp) {
|
||||
if(exp.size() != 2 || !exp.getTuple(1).isAtom()) {
|
||||
throw new RuntimeException("package is (package NAME): "+exp);
|
||||
}
|
||||
String name = exp.getTuple(1).getAtom();
|
||||
return name;
|
||||
}
|
||||
|
||||
private String parseImport(SExp exp) {
|
||||
if(exp.size() != 2 || !exp.getTuple(1).isAtom()) {
|
||||
throw new RuntimeException("import is (import NAME): "+exp);
|
||||
}
|
||||
String name = exp.getTuple(1).getAtom();
|
||||
return name;
|
||||
}
|
||||
|
||||
private FieldSchema parseField(SExp exp) {
|
||||
if(exp.size() != 3 || !exp.getTuple(1).isAtom()) {
|
||||
throw new RuntimeException("field is (field NAME TYPE): "+exp);
|
||||
}
|
||||
String name = exp.getTuple(1).getAtom();
|
||||
Schema type = readType(exp.getTuple(2));
|
||||
return new FieldSchema(name, type);
|
||||
}
|
||||
}
|
||||
|
89
java/src/org/msgpack/schema/ShortSchema.java
Normal file
89
java/src/org/msgpack/schema/ShortSchema.java
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ShortSchema extends Schema {
|
||||
public ShortSchema() {
|
||||
super("Short");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "short";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packShort( ((Number)obj).shortValue() );
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Short) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).shortValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (short)v;
|
||||
}
|
||||
}
|
||||
|
122
java/src/org/msgpack/schema/SpecificClassSchema.java
Normal file
122
java/src/org/msgpack/schema/SpecificClassSchema.java
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.lang.reflect.*;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class SpecificClassSchema extends ClassSchema {
|
||||
private Class classCache;
|
||||
private Method factoryCache;
|
||||
private Constructor constructorCache;
|
||||
|
||||
public SpecificClassSchema(
|
||||
String name, String namespace,
|
||||
List<String> imports, List<FieldSchema> fields) {
|
||||
super(name, namespace, imports, fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj == null) {
|
||||
pk.packNil();
|
||||
return;
|
||||
}
|
||||
if(classCache == null) {
|
||||
cacheFactory();
|
||||
}
|
||||
if(classCache.isInstance(obj)) {
|
||||
((MessagePackable)obj).messagePack(pk);
|
||||
} else {
|
||||
// FIXME Map<String,Object>
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Collection) {
|
||||
if(constructorCache == null) {
|
||||
cacheConstructor();
|
||||
}
|
||||
try {
|
||||
MessageMergeable o = (MessageMergeable)constructorCache.newInstance((Object[])null);
|
||||
o.messageMerge(obj);
|
||||
return o;
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public Schema getElementSchema(int index) {
|
||||
// FIXME check index < fields.length
|
||||
return fields[index].getSchema();
|
||||
}
|
||||
|
||||
public Object createFromArray(Object[] obj) {
|
||||
if(factoryCache == null) {
|
||||
cacheFactory();
|
||||
}
|
||||
try {
|
||||
return factoryCache.invoke(null, new Object[]{obj});
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getCause());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void cacheFactory() {
|
||||
try {
|
||||
classCache = Class.forName(fqdn);
|
||||
factoryCache = classCache.getDeclaredMethod("createFromMessage", new Class[]{Object[].class});
|
||||
factoryCache.setAccessible(true);
|
||||
} catch(ClassNotFoundException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void cacheConstructor() {
|
||||
try {
|
||||
classCache = Class.forName(fqdn);
|
||||
constructorCache = classCache.getDeclaredConstructor((Class[])null);
|
||||
constructorCache.setAccessible(true);
|
||||
} catch(ClassNotFoundException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
111
java/src/org/msgpack/schema/StringSchema.java
Normal file
111
java/src/org/msgpack/schema/StringSchema.java
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.schema;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class StringSchema extends Schema {
|
||||
public StringSchema() {
|
||||
super("string");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return "String";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof String) {
|
||||
try {
|
||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
||||
} else if(obj instanceof byte[]) {
|
||||
byte[] d = (byte[])obj;
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
if(!d.hasArray()) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
pk.packRaw(d.capacity());
|
||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof String) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof byte[]) {
|
||||
try {
|
||||
return new String((byte[])obj, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
try {
|
||||
if(d.hasArray()) {
|
||||
return new String(d.array(), d.position(), d.capacity(), "UTF-8");
|
||||
} else {
|
||||
byte[] v = new byte[d.capacity()];
|
||||
int pos = d.position();
|
||||
d.get(v);
|
||||
d.position(pos);
|
||||
return new String(v, "UTF-8");
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
try {
|
||||
return new String(b, offset, length, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
38
java/test/Generate.java
Normal file
38
java/test/Generate.java
Normal file
@@ -0,0 +1,38 @@
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.schema.*;
|
||||
|
||||
public class Generate {
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
String source =
|
||||
"(class MediaContent"+
|
||||
" (package serializers.msgpack)"+
|
||||
" (field image (array (class Image"+
|
||||
" (field uri string)"+
|
||||
" (field title string)"+
|
||||
" (field width int)"+
|
||||
" (field height int)"+
|
||||
" (field size int))))"+
|
||||
" (field media (class Media"+
|
||||
" (field uri string)"+
|
||||
" (field title string)"+
|
||||
" (field width int)"+
|
||||
" (field height int)"+
|
||||
" (field format string)"+
|
||||
" (field duration long)"+
|
||||
" (field size long)"+
|
||||
" (field bitrate int)"+
|
||||
" (field person (array string))"+
|
||||
" (field player int)"+
|
||||
" (field copyright string)))"+
|
||||
" )";
|
||||
|
||||
Schema schema = Schema.parse(source);
|
||||
|
||||
Writer output = new OutputStreamWriter(System.out);
|
||||
ClassGenerator.write(schema, output);
|
||||
}
|
||||
}
|
||||
|
7
java/test/README
Normal file
7
java/test/README
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
svn checkout -r114 http://thrift-protobuf-compare.googlecode.com/svn/trunk/ thrift-protobuf-compare-base
|
||||
cp -rf thrift-protobuf-compare/tpc thrift-protobuf-compare-base
|
||||
cp ../dist/msgpack.jar thrift-protobuf-compare-base/tpc/lib/
|
||||
cd thrift-protobuf-compare-base/tpc/
|
||||
ant compile
|
||||
./run-benchmark.sh
|
@@ -0,0 +1,434 @@
|
||||
package serializers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import serializers.msgpack.MessagePackSpecificSerializer;
|
||||
import serializers.msgpack.MessagePackIndirectSerializer;
|
||||
import serializers.msgpack.MessagePackDynamicSerializer;
|
||||
import serializers.msgpack.MessagePackGenericSerializer;
|
||||
import serializers.avro.AvroGenericSerializer;
|
||||
import serializers.avro.specific.AvroSpecificSerializer;
|
||||
import serializers.kryo.KryoOptimizedSerializer;
|
||||
import serializers.kryo.KryoSerializer;
|
||||
|
||||
public class BenchmarkRunner
|
||||
{
|
||||
public final static int ITERATIONS = 2000;
|
||||
public final static int TRIALS = 20;
|
||||
|
||||
/**
|
||||
* Number of milliseconds to warm up for each operation type for each serializer. Let's
|
||||
* start with 3 seconds.
|
||||
*/
|
||||
final static long WARMUP_MSECS = 3000;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Set<ObjectSerializer> _serializers = new LinkedHashSet<ObjectSerializer>();
|
||||
|
||||
public static void main(String... args) throws Exception
|
||||
{
|
||||
BenchmarkRunner runner = new BenchmarkRunner();
|
||||
|
||||
// binary codecs first
|
||||
runner.addObjectSerializer(new MessagePackSpecificSerializer());
|
||||
runner.addObjectSerializer(new MessagePackIndirectSerializer());
|
||||
runner.addObjectSerializer(new MessagePackDynamicSerializer());
|
||||
runner.addObjectSerializer(new MessagePackGenericSerializer());
|
||||
runner.addObjectSerializer(new AvroGenericSerializer());
|
||||
runner.addObjectSerializer(new AvroSpecificSerializer());
|
||||
runner.addObjectSerializer(new ActiveMQProtobufSerializer());
|
||||
runner.addObjectSerializer(new ProtobufSerializer());
|
||||
runner.addObjectSerializer(new ThriftSerializer());
|
||||
runner.addObjectSerializer(new HessianSerializer());
|
||||
runner.addObjectSerializer(new KryoSerializer());
|
||||
runner.addObjectSerializer(new KryoOptimizedSerializer());
|
||||
|
||||
// None of the other serializers use compression, so we'll leave this out.
|
||||
// runner.addObjectSerializer(new KryoCompressedSerializer());
|
||||
|
||||
// then language default serializers
|
||||
runner.addObjectSerializer(new JavaSerializer());
|
||||
|
||||
runner.addObjectSerializer(new JavaExtSerializer());
|
||||
runner.addObjectSerializer(new ScalaSerializer());
|
||||
|
||||
// then Json
|
||||
runner.addObjectSerializer(new JsonSerializer());
|
||||
runner.addObjectSerializer(new JsonDataBindingSerializer());
|
||||
runner.addObjectSerializer(new JsonMarshallerSerializer());
|
||||
runner.addObjectSerializer(new ProtostuffJsonSerializer());
|
||||
runner.addObjectSerializer(new ProtostuffNumericJsonSerializer());
|
||||
// this is pretty slow; so slow that it's almost not worth keeping but:
|
||||
runner.addObjectSerializer(new GsonSerializer());
|
||||
|
||||
// then xml via stax, textual and binary
|
||||
runner.addObjectSerializer(new StaxSerializer("stax/woodstox",
|
||||
new com.ctc.wstx.stax.WstxInputFactory(),
|
||||
new com.ctc.wstx.stax.WstxOutputFactory()));
|
||||
runner.addObjectSerializer(new StaxSerializer("stax/aalto",
|
||||
new com.fasterxml.aalto.stax.InputFactoryImpl(),
|
||||
new com.fasterxml.aalto.stax.OutputFactoryImpl()));
|
||||
|
||||
runner.addObjectSerializer(new StaxSerializer("binaryxml/FI",
|
||||
new com.sun.xml.fastinfoset.stax.factory.StAXInputFactory(),
|
||||
new com.sun.xml.fastinfoset.stax.factory.StAXOutputFactory()));
|
||||
|
||||
// No point in running all 4 variants: let's just use fastest one:
|
||||
//runner.addObjectSerializer(new XStreamSerializer("xstream (xpp)", false, null, null));
|
||||
//runner.addObjectSerializer(new XStreamSerializer("xstream (xpp with conv)", true, null, null));
|
||||
//runner.addObjectSerializer(new XStreamSerializer("xstream (stax)", false, new com.ctc.wstx.stax.WstxInputFactory(), new com.ctc.wstx.stax.WstxOutputFactory()));
|
||||
runner.addObjectSerializer(new XStreamSerializer("xstream (stax with conv)",
|
||||
true,
|
||||
new com.ctc.wstx.stax.WstxInputFactory(),
|
||||
new com.ctc.wstx.stax.WstxOutputFactory()));
|
||||
runner.addObjectSerializer(new JavolutionXMLFormatSerializer());
|
||||
|
||||
runner.addObjectSerializer(new SbinarySerializer());
|
||||
// broken? Does not correctly round-trip:
|
||||
// runner.addObjectSerializer(new YamlSerializer());
|
||||
|
||||
System.out.println("Starting");
|
||||
|
||||
runner.start();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void addObjectSerializer(ObjectSerializer serializer)
|
||||
{
|
||||
_serializers.add(serializer);
|
||||
}
|
||||
|
||||
private <T> double createObjects(ObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
serializer.create();
|
||||
}
|
||||
return iterationTime(System.nanoTime() - start, iterations);
|
||||
}
|
||||
|
||||
private double iterationTime(long delta, int iterations)
|
||||
{
|
||||
return (double) delta / (double) (iterations);
|
||||
}
|
||||
|
||||
private <T> double serializeDifferentObjects(ObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
T obj = serializer.create();
|
||||
serializer.serialize(obj);
|
||||
}
|
||||
return iterationTime(System.nanoTime()-start, iterations);
|
||||
}
|
||||
|
||||
|
||||
private <T> double serializeSameObject(ObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
// let's reuse same instance to reduce overhead
|
||||
T obj = serializer.create();
|
||||
long delta = 0;
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
serializer.serialize(obj);
|
||||
delta += System.nanoTime() - start;
|
||||
if (i % 1000 == 0)
|
||||
doGc();
|
||||
}
|
||||
return iterationTime(delta, iterations);
|
||||
}
|
||||
|
||||
private <T> double deserializeNoFieldAccess(ObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
byte[] array = serializer.serialize(serializer.create());
|
||||
long start = System.nanoTime();
|
||||
T result = null;
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
result = serializer.deserialize(array);
|
||||
}
|
||||
return iterationTime(System.nanoTime()-start, iterations);
|
||||
}
|
||||
|
||||
private <T> double deserializeAndCheckAllFields(CheckingObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
byte[] array = serializer.serialize(serializer.create());
|
||||
long delta = 0;
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
T obj = serializer.deserialize(array);
|
||||
serializer.checkAllFields(obj);
|
||||
delta += System.nanoTime() - start;
|
||||
}
|
||||
return iterationTime(delta, iterations);
|
||||
}
|
||||
|
||||
private <T> double deserializeAndCheckMediaField(CheckingObjectSerializer<T> serializer, int iterations) throws Exception
|
||||
{
|
||||
byte[] array = serializer.serialize(serializer.create());
|
||||
long delta = 0;
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
T obj = serializer.deserialize(array);
|
||||
serializer.checkMediaField(obj);
|
||||
delta += System.nanoTime() - start;
|
||||
}
|
||||
return iterationTime(delta, iterations);
|
||||
}
|
||||
|
||||
/**
|
||||
* JVM is not required to honor GC requests, but adding bit of sleep around request is
|
||||
* most likely to give it a chance to do it.
|
||||
*/
|
||||
private void doGc()
|
||||
{
|
||||
try {
|
||||
Thread.sleep(50L);
|
||||
} catch (InterruptedException ie) { }
|
||||
System.gc();
|
||||
try { // longer sleep afterwards (not needed by GC, but may help with scheduling)
|
||||
Thread.sleep(200L);
|
||||
} catch (InterruptedException ie) { }
|
||||
}
|
||||
|
||||
enum measurements
|
||||
{
|
||||
timeCreate, timeSerializeDifferentObjects, timeSerializeSameObject, timeDeserializeNoFieldAccess, timeDeserializeAndCheckMediaField, timeDeserializeAndCheckAllFields, totalTime, length
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void start() throws Exception
|
||||
{
|
||||
System.out.printf("%-24s, %15s, %15s, %15s, %15s, %15s, %15s, %15s, %10s\n",
|
||||
" ",
|
||||
"Object create",
|
||||
"Serialize",
|
||||
"/w Same Object",
|
||||
"Deserialize",
|
||||
"and Check Media",
|
||||
"and Check All",
|
||||
"Total Time",
|
||||
"Serialized Size");
|
||||
EnumMap<measurements, Map<String, Double>> values = new EnumMap<measurements, Map<String, Double>>(measurements.class);
|
||||
for (measurements m : measurements.values())
|
||||
values.put(m, new HashMap<String, Double>());
|
||||
|
||||
for (ObjectSerializer serializer : _serializers)
|
||||
{
|
||||
/*
|
||||
* Should only warm things for the serializer that we test next: HotSpot JIT will
|
||||
* otherwise spent most of its time optimizing slower ones... Use
|
||||
* -XX:CompileThreshold=1 to hint the JIT to start immediately
|
||||
*
|
||||
* Actually: 1 is often not a good value -- threshold is the number
|
||||
* of samples needed to trigger inlining, and there's no point in
|
||||
* inlining everything. Default value is in thousands, so lowering
|
||||
* it to, say, 1000 is usually better.
|
||||
*/
|
||||
warmCreation(serializer);
|
||||
doGc();
|
||||
double timeCreate = Double.MAX_VALUE;
|
||||
// do more iteration for object creation because of its short time
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeCreate = Math.min(timeCreate, createObjects(serializer, ITERATIONS * 100));
|
||||
|
||||
warmSerialization(serializer);
|
||||
|
||||
// actually: let's verify serializer actually works now:
|
||||
checkCorrectness(serializer);
|
||||
|
||||
doGc();
|
||||
double timeSerializeDifferentObjects = Double.MAX_VALUE;
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeSerializeDifferentObjects = Math.min(timeSerializeDifferentObjects, serializeDifferentObjects(serializer, ITERATIONS));
|
||||
|
||||
doGc();
|
||||
double timeSerializeSameObject = Double.MAX_VALUE;
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeSerializeSameObject = Math.min(timeSerializeSameObject, serializeSameObject(serializer, ITERATIONS));
|
||||
|
||||
warmDeserialization(serializer);
|
||||
|
||||
doGc();
|
||||
double timeDeserializeNoFieldAccess = Double.MAX_VALUE;
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeDeserializeNoFieldAccess = Math.min(timeDeserializeNoFieldAccess, deserializeNoFieldAccess(serializer, ITERATIONS));
|
||||
|
||||
double timeDeserializeAndCheckAllFields = Double.NaN;
|
||||
double timeDeserializeAndCheckMediaField = Double.NaN;
|
||||
|
||||
double totalTime = timeSerializeDifferentObjects + timeDeserializeNoFieldAccess;
|
||||
|
||||
if( serializer instanceof CheckingObjectSerializer) {
|
||||
CheckingObjectSerializer checkingSerializer = (CheckingObjectSerializer)serializer;
|
||||
|
||||
timeDeserializeAndCheckMediaField = Double.MAX_VALUE;
|
||||
doGc();
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeDeserializeAndCheckMediaField = Math.min(timeDeserializeAndCheckMediaField, deserializeAndCheckMediaField(checkingSerializer, ITERATIONS));
|
||||
|
||||
timeDeserializeAndCheckAllFields = Double.MAX_VALUE;
|
||||
doGc();
|
||||
for (int i = 0; i < TRIALS; i++)
|
||||
timeDeserializeAndCheckAllFields = Math.min(timeDeserializeAndCheckAllFields, deserializeAndCheckAllFields(checkingSerializer, ITERATIONS));
|
||||
|
||||
totalTime = timeSerializeDifferentObjects + timeDeserializeAndCheckAllFields;
|
||||
}
|
||||
|
||||
|
||||
byte[] array = serializer.serialize(serializer.create());
|
||||
System.out.printf("%-24s, %15.5f, %15.5f, %15.5f, %15.5f, %15.5f, %15.5f, %15.5f, %10d\n",
|
||||
serializer.getName(),
|
||||
timeCreate,
|
||||
timeSerializeDifferentObjects,
|
||||
timeSerializeSameObject,
|
||||
timeDeserializeNoFieldAccess,
|
||||
timeDeserializeAndCheckMediaField,
|
||||
timeDeserializeAndCheckAllFields,
|
||||
totalTime,
|
||||
array.length);
|
||||
|
||||
addValue(values, serializer.getName(), timeCreate, timeSerializeDifferentObjects, timeSerializeSameObject,
|
||||
timeDeserializeNoFieldAccess, timeDeserializeAndCheckMediaField, timeDeserializeAndCheckAllFields, totalTime, array.length);
|
||||
}
|
||||
printImages(values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that tries to validate correctness of serializer, using
|
||||
* round-trip (construct, serializer, deserialize; compare objects
|
||||
* after steps 1 and 3).
|
||||
* Currently only done for StdMediaDeserializer...
|
||||
*/
|
||||
private void checkCorrectness(ObjectSerializer serializer)
|
||||
throws Exception
|
||||
{
|
||||
Object input = serializer.create();
|
||||
byte[] array = serializer.serialize(input);
|
||||
Object output = serializer.deserialize(array);
|
||||
|
||||
if (!input.equals(output)) {
|
||||
/* Should throw an exception; but for now (that we have a few
|
||||
* failures) let's just whine...
|
||||
*/
|
||||
String msg = "serializer '"+serializer.getName()+"' failed round-trip test (ser+deser produces Object different from input), input="+input+", output="+output;
|
||||
//throw new Exception("Error: "+msg);
|
||||
System.err.println("WARN: "+msg);
|
||||
}
|
||||
}
|
||||
|
||||
private void printImages(EnumMap<measurements, Map<String, Double>> values)
|
||||
{
|
||||
for (measurements m : values.keySet()) {
|
||||
Map<String, Double> map = values.get(m);
|
||||
ArrayList<Entry> list = new ArrayList(map.entrySet());
|
||||
Collections.sort(list, new Comparator<Entry>() {
|
||||
public int compare (Entry o1, Entry o2) {
|
||||
double diff = (Double)o1.getValue() - (Double)o2.getValue();
|
||||
return diff > 0 ? 1 : (diff < 0 ? -1 : 0);
|
||||
}
|
||||
});
|
||||
LinkedHashMap<String, Double> sortedMap = new LinkedHashMap<String, Double>();
|
||||
for (Entry<String, Double> entry : list) {
|
||||
if( !entry.getValue().isNaN() ) {
|
||||
sortedMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
printImage(sortedMap, m);
|
||||
}
|
||||
}
|
||||
|
||||
private void printImage(Map<String, Double> map, measurements m)
|
||||
{
|
||||
StringBuilder valSb = new StringBuilder();
|
||||
String names = "";
|
||||
double max = Double.MIN_NORMAL;
|
||||
for (Entry<String, Double> entry : map.entrySet())
|
||||
{
|
||||
valSb.append(entry.getValue()).append(',');
|
||||
max = Math.max(max, entry.getValue());
|
||||
names = entry.getKey() + '|' + names;
|
||||
}
|
||||
|
||||
int height = Math.min(30+map.size()*20, 430);
|
||||
double scale = max * 1.1;
|
||||
System.out.println("<img src='http://chart.apis.google.com/chart?chtt="
|
||||
+ m.name()
|
||||
+ "&chf=c||lg||0||FFFFFF||1||76A4FB||0|bg||s||EFEFEF&chs=689x"+height+"&chd=t:"
|
||||
+ valSb.toString().substring(0, valSb.length() - 1)
|
||||
+ "&chds=0,"+ scale
|
||||
+ "&chxt=y"
|
||||
+ "&chxl=0:|" + names.substring(0, names.length() - 1)
|
||||
+ "&chm=N *f*,000000,0,-1,10&lklk&chdlp=t&chco=660000|660033|660066|660099|6600CC|6600FF|663300|663333|663366|663399|6633CC|6633FF|666600|666633|666666&cht=bhg&chbh=10&nonsense=aaa.png'/>");
|
||||
|
||||
}
|
||||
|
||||
private void addValue(EnumMap<measurements, Map<String, Double>> values,
|
||||
String name,
|
||||
double timeCreate,
|
||||
double timeSerializeDifferentObjects,
|
||||
double timeSerializeSameObject,
|
||||
double timeDeserializeNoFieldAccess,
|
||||
double timeDeserializeAndCheckMediaField,
|
||||
double timeDeserializeAndCheckAllFields,
|
||||
double totalTime,
|
||||
double length)
|
||||
{
|
||||
|
||||
values.get(measurements.timeCreate).put(name, timeCreate);
|
||||
values.get(measurements.timeSerializeDifferentObjects).put(name, timeSerializeDifferentObjects);
|
||||
values.get(measurements.timeSerializeSameObject).put(name, timeSerializeSameObject);
|
||||
values.get(measurements.timeDeserializeNoFieldAccess).put(name, timeDeserializeNoFieldAccess);
|
||||
values.get(measurements.timeDeserializeAndCheckMediaField).put(name, timeDeserializeAndCheckMediaField);
|
||||
values.get(measurements.timeDeserializeAndCheckAllFields).put(name, timeDeserializeAndCheckAllFields);
|
||||
values.get(measurements.totalTime).put(name, totalTime);
|
||||
values.get(measurements.length).put(name, length);
|
||||
}
|
||||
|
||||
private <T> void warmCreation(ObjectSerializer<T> serializer) throws Exception
|
||||
{
|
||||
// Instead of fixed counts, let's try to prime by running for N seconds
|
||||
long endTime = System.currentTimeMillis() + WARMUP_MSECS;
|
||||
do
|
||||
{
|
||||
createObjects(serializer, 1);
|
||||
}
|
||||
while (System.currentTimeMillis() < endTime);
|
||||
}
|
||||
|
||||
private <T> void warmSerialization(ObjectSerializer<T> serializer) throws Exception
|
||||
{
|
||||
// Instead of fixed counts, let's try to prime by running for N seconds
|
||||
long endTime = System.currentTimeMillis() + WARMUP_MSECS;
|
||||
do
|
||||
{
|
||||
serializeDifferentObjects(serializer, 1);
|
||||
}
|
||||
while (System.currentTimeMillis() < endTime);
|
||||
}
|
||||
|
||||
private <T> void warmDeserialization(ObjectSerializer<T> serializer) throws Exception
|
||||
{
|
||||
// Instead of fixed counts, let's try to prime by running for N seconds
|
||||
long endTime = System.currentTimeMillis() + WARMUP_MSECS;
|
||||
do
|
||||
{
|
||||
deserializeNoFieldAccess(serializer, 1);
|
||||
}
|
||||
while (System.currentTimeMillis() < endTime);
|
||||
}
|
||||
}
|
@@ -0,0 +1,173 @@
|
||||
package serializers.msgpack;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.schema.ClassSchema;
|
||||
import org.msgpack.schema.FieldSchema;
|
||||
|
||||
public final class MediaContent implements MessagePackable, MessageMergeable
|
||||
{
|
||||
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class MediaContent (package serializers.msgpack) (field image (array (class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int)))) (field media (class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))))");
|
||||
public static ClassSchema getSchema() { return _SCHEMA; }
|
||||
|
||||
public List<Image> image;
|
||||
public Media media;
|
||||
|
||||
public MediaContent() { }
|
||||
|
||||
@Override
|
||||
public void messagePack(Packer _pk) throws IOException
|
||||
{
|
||||
_pk.packArray(2);
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
_fields[0].getSchema().pack(_pk, image);
|
||||
_fields[1].getSchema().pack(_pk, media);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void messageMerge(Object obj) throws MessageTypeException
|
||||
{
|
||||
Object[] _source = ((List)obj).toArray();
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
if(_source.length <= 0) { return; } this.image = (List<Image>)_fields[0].getSchema().convert(_source[0]);
|
||||
if(_source.length <= 1) { return; } this.media = (Media)_fields[1].getSchema().convert(_source[1]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static MediaContent createFromMessage(Object[] _message)
|
||||
{
|
||||
MediaContent _self = new MediaContent();
|
||||
if(_message.length <= 0) { return _self; } _self.image = (List<Image>)_message[0];
|
||||
if(_message.length <= 1) { return _self; } _self.media = (Media)_message[1];
|
||||
return _self;
|
||||
}
|
||||
}
|
||||
|
||||
final class Image implements MessagePackable, MessageMergeable
|
||||
{
|
||||
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int))");
|
||||
public static ClassSchema getSchema() { return _SCHEMA; }
|
||||
|
||||
public String uri;
|
||||
public String title;
|
||||
public Integer width;
|
||||
public Integer height;
|
||||
public Integer size;
|
||||
|
||||
public Image() { }
|
||||
|
||||
@Override
|
||||
public void messagePack(Packer _pk) throws IOException
|
||||
{
|
||||
_pk.packArray(5);
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
_fields[0].getSchema().pack(_pk, uri);
|
||||
_fields[1].getSchema().pack(_pk, title);
|
||||
_fields[2].getSchema().pack(_pk, width);
|
||||
_fields[3].getSchema().pack(_pk, height);
|
||||
_fields[4].getSchema().pack(_pk, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void messageMerge(Object obj) throws MessageTypeException
|
||||
{
|
||||
Object[] _source = ((List)obj).toArray();
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
if(_source.length <= 0) { return; } this.uri = (String)_fields[0].getSchema().convert(_source[0]);
|
||||
if(_source.length <= 1) { return; } this.title = (String)_fields[1].getSchema().convert(_source[1]);
|
||||
if(_source.length <= 2) { return; } this.width = (Integer)_fields[2].getSchema().convert(_source[2]);
|
||||
if(_source.length <= 3) { return; } this.height = (Integer)_fields[3].getSchema().convert(_source[3]);
|
||||
if(_source.length <= 4) { return; } this.size = (Integer)_fields[4].getSchema().convert(_source[4]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Image createFromMessage(Object[] _message)
|
||||
{
|
||||
Image _self = new Image();
|
||||
if(_message.length <= 0) { return _self; } _self.uri = (String)_message[0];
|
||||
if(_message.length <= 1) { return _self; } _self.title = (String)_message[1];
|
||||
if(_message.length <= 2) { return _self; } _self.width = (Integer)_message[2];
|
||||
if(_message.length <= 3) { return _self; } _self.height = (Integer)_message[3];
|
||||
if(_message.length <= 4) { return _self; } _self.size = (Integer)_message[4];
|
||||
return _self;
|
||||
}
|
||||
}
|
||||
|
||||
final class Media implements MessagePackable, MessageMergeable
|
||||
{
|
||||
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))");
|
||||
public static ClassSchema getSchema() { return _SCHEMA; }
|
||||
|
||||
public String uri;
|
||||
public String title;
|
||||
public Integer width;
|
||||
public Integer height;
|
||||
public String format;
|
||||
public Long duration;
|
||||
public Long size;
|
||||
public Integer bitrate;
|
||||
public List<String> person;
|
||||
public Integer player;
|
||||
public String copyright;
|
||||
|
||||
public Media() { }
|
||||
|
||||
@Override
|
||||
public void messagePack(Packer _pk) throws IOException
|
||||
{
|
||||
_pk.packArray(11);
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
_fields[0].getSchema().pack(_pk, uri);
|
||||
_fields[1].getSchema().pack(_pk, title);
|
||||
_fields[2].getSchema().pack(_pk, width);
|
||||
_fields[3].getSchema().pack(_pk, height);
|
||||
_fields[4].getSchema().pack(_pk, format);
|
||||
_fields[5].getSchema().pack(_pk, duration);
|
||||
_fields[6].getSchema().pack(_pk, size);
|
||||
_fields[7].getSchema().pack(_pk, bitrate);
|
||||
_fields[8].getSchema().pack(_pk, person);
|
||||
_fields[9].getSchema().pack(_pk, player);
|
||||
_fields[10].getSchema().pack(_pk, copyright);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void messageMerge(Object obj) throws MessageTypeException
|
||||
{
|
||||
Object[] _source = ((List)obj).toArray();
|
||||
FieldSchema[] _fields = _SCHEMA.getFields();
|
||||
if(_source.length <= 0) { return; } this.uri = (String)_fields[0].getSchema().convert(_source[0]);
|
||||
if(_source.length <= 1) { return; } this.title = (String)_fields[1].getSchema().convert(_source[1]);
|
||||
if(_source.length <= 2) { return; } this.width = (Integer)_fields[2].getSchema().convert(_source[2]);
|
||||
if(_source.length <= 3) { return; } this.height = (Integer)_fields[3].getSchema().convert(_source[3]);
|
||||
if(_source.length <= 4) { return; } this.format = (String)_fields[4].getSchema().convert(_source[4]);
|
||||
if(_source.length <= 5) { return; } this.duration = (Long)_fields[5].getSchema().convert(_source[5]);
|
||||
if(_source.length <= 6) { return; } this.size = (Long)_fields[6].getSchema().convert(_source[6]);
|
||||
if(_source.length <= 7) { return; } this.bitrate = (Integer)_fields[7].getSchema().convert(_source[7]);
|
||||
if(_source.length <= 8) { return; } this.person = (List<String>)_fields[8].getSchema().convert(_source[8]);
|
||||
if(_source.length <= 9) { return; } this.player = (Integer)_fields[9].getSchema().convert(_source[9]);
|
||||
if(_source.length <= 10) { return; } this.copyright = (String)_fields[10].getSchema().convert(_source[10]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Media createFromMessage(Object[] _message)
|
||||
{
|
||||
Media _self = new Media();
|
||||
if(_message.length <= 0) { return _self; } _self.uri = (String)_message[0];
|
||||
if(_message.length <= 1) { return _self; } _self.title = (String)_message[1];
|
||||
if(_message.length <= 2) { return _self; } _self.width = (Integer)_message[2];
|
||||
if(_message.length <= 3) { return _self; } _self.height = (Integer)_message[3];
|
||||
if(_message.length <= 4) { return _self; } _self.format = (String)_message[4];
|
||||
if(_message.length <= 5) { return _self; } _self.duration = (Long)_message[5];
|
||||
if(_message.length <= 6) { return _self; } _self.size = (Long)_message[6];
|
||||
if(_message.length <= 7) { return _self; } _self.bitrate = (Integer)_message[7];
|
||||
if(_message.length <= 8) { return _self; } _self.person = (List<String>)_message[8];
|
||||
if(_message.length <= 9) { return _self; } _self.player = (Integer)_message[9];
|
||||
if(_message.length <= 10) { return _self; } _self.copyright = (String)_message[10];
|
||||
return _self;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,21 @@
|
||||
(class MediaContent
|
||||
(package serializers.msgpack)
|
||||
(field image (array (class Image
|
||||
(field uri string)
|
||||
(field title string)
|
||||
(field width int)
|
||||
(field height int)
|
||||
(field size int))))
|
||||
(field media (class Media
|
||||
(field uri string)
|
||||
(field title string)
|
||||
(field width int)
|
||||
(field height int)
|
||||
(field format string)
|
||||
(field duration long)
|
||||
(field size long)
|
||||
(field bitrate int)
|
||||
(field person (array string))
|
||||
(field player int)
|
||||
(field copyright string)))
|
||||
)
|
@@ -0,0 +1,68 @@
|
||||
package serializers.msgpack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import org.msgpack.*;
|
||||
import serializers.ObjectSerializer;
|
||||
|
||||
public class MessagePackDynamicSerializer implements ObjectSerializer<Object>
|
||||
{
|
||||
public String getName() {
|
||||
return "msgpack-dynamic";
|
||||
}
|
||||
|
||||
public Object create() throws Exception {
|
||||
ArrayList media = new ArrayList(11);
|
||||
media.add("http://javaone.com/keynote.mpg");
|
||||
media.add("video/mpg4");
|
||||
media.add("Javaone Keynote");
|
||||
media.add(1234567L);
|
||||
media.add(0);
|
||||
ArrayList<String> person = new ArrayList<String>(2);
|
||||
person.add("Bill Gates");
|
||||
person.add("Steve Jobs");
|
||||
media.add(person);
|
||||
media.add(0);
|
||||
media.add(0);
|
||||
media.add(0);
|
||||
media.add(123L);
|
||||
media.add("");
|
||||
|
||||
ArrayList image1 = new ArrayList(5);
|
||||
image1.add("http://javaone.com/keynote_large.jpg");
|
||||
image1.add(0);
|
||||
image1.add(0);
|
||||
image1.add(2);
|
||||
image1.add("Javaone Keynote");
|
||||
|
||||
ArrayList image2 = new ArrayList(5);
|
||||
image2.add("http://javaone.com/keynote_thumbnail.jpg");
|
||||
image2.add(0);
|
||||
image2.add(0);
|
||||
image2.add(1);
|
||||
image2.add("Javaone Keynote");
|
||||
|
||||
ArrayList content = new ArrayList(2);
|
||||
content.add(media);
|
||||
ArrayList<Object> images = new ArrayList<Object>(2);
|
||||
images.add(image1);
|
||||
images.add(image2);
|
||||
content.add(images);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public byte[] serialize(Object content) throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
Packer pk = new Packer(os);
|
||||
pk.pack(content);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
public Object deserialize(byte[] array) throws Exception {
|
||||
UnbufferedUnpacker pac = new UnbufferedUnpacker();
|
||||
pac.execute(array);
|
||||
return (Object)pac.getData();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,70 @@
|
||||
package serializers.msgpack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import org.msgpack.*;
|
||||
import serializers.ObjectSerializer;
|
||||
|
||||
public class MessagePackGenericSerializer implements ObjectSerializer<Object>
|
||||
{
|
||||
private static final Schema MEDIA_CONTENT_SCHEMA = Schema.parse("(class MediaContent (package serializers.msgpack) (field image (array (class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int)))) (field media (class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))))");
|
||||
|
||||
public String getName() {
|
||||
return "msgpack-generic";
|
||||
}
|
||||
|
||||
public Object create() throws Exception {
|
||||
HashMap<String,Object> media = new HashMap<String, Object>(11);
|
||||
media.put("uri", "http://javaone.com/keynote.mpg");
|
||||
media.put("format", "video/mpg4");
|
||||
media.put("title", "Javaone Keynote");
|
||||
media.put("duration", 1234567L);
|
||||
media.put("bitrate", 0);
|
||||
ArrayList<String> person = new ArrayList<String>(2);
|
||||
person.add("Bill Gates");
|
||||
person.add("Steve Jobs");
|
||||
media.put("person", person);
|
||||
media.put("player", 0);
|
||||
media.put("height", 0);
|
||||
media.put("width", 0);
|
||||
media.put("size", 123L);
|
||||
media.put("copyright", "");
|
||||
|
||||
HashMap<String,Object> image1 = new HashMap<String,Object>(5);
|
||||
image1.put("uri", "http://javaone.com/keynote_large.jpg");
|
||||
image1.put("width", 0);
|
||||
image1.put("height", 0);
|
||||
image1.put("size", 2);
|
||||
image1.put("title", "Javaone Keynote");
|
||||
|
||||
HashMap<String,Object> image2 = new HashMap<String,Object>(5);
|
||||
image2.put("uri", "http://javaone.com/keynote_thumbnail.jpg");
|
||||
image2.put("width", 0);
|
||||
image2.put("height", 0);
|
||||
image2.put("size", 1);
|
||||
image2.put("title", "Javaone Keynote");
|
||||
|
||||
HashMap<String,Object> content = new HashMap<String,Object>(2);
|
||||
content.put("media", media);
|
||||
ArrayList<Object> images = new ArrayList<Object>(2);
|
||||
images.add(image1);
|
||||
images.add(image2);
|
||||
content.put("image", images);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public byte[] serialize(Object content) throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
Packer pk = new Packer(os);
|
||||
pk.packWithSchema(content, MEDIA_CONTENT_SCHEMA);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
public Object deserialize(byte[] array) throws Exception {
|
||||
UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MEDIA_CONTENT_SCHEMA);
|
||||
pac.execute(array);
|
||||
return (Object)pac.getData();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,67 @@
|
||||
package serializers.msgpack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import org.msgpack.*;
|
||||
import serializers.ObjectSerializer;
|
||||
|
||||
public class MessagePackIndirectSerializer implements ObjectSerializer<MediaContent>
|
||||
{
|
||||
public String getName() {
|
||||
return "msgpack-indirect";
|
||||
}
|
||||
|
||||
public MediaContent create() throws Exception {
|
||||
Media media = new Media();
|
||||
media.uri = "http://javaone.com/keynote.mpg";
|
||||
media.format = "video/mpg4";
|
||||
media.title = "Javaone Keynote";
|
||||
media.duration = 1234567L;
|
||||
media.bitrate = 0;
|
||||
media.person = new ArrayList<String>(2);
|
||||
media.person.add("Bill Gates");
|
||||
media.person.add("Steve Jobs");
|
||||
media.player = 0;
|
||||
media.height = 0;
|
||||
media.width = 0;
|
||||
media.size = 123L;
|
||||
media.copyright = "";
|
||||
|
||||
Image image1 = new Image();
|
||||
image1.uri = "http://javaone.com/keynote_large.jpg";
|
||||
image1.width = 0;
|
||||
image1.height = 0;
|
||||
image1.size = 2;
|
||||
image1.title = "Javaone Keynote";
|
||||
|
||||
Image image2 = new Image();
|
||||
image2.uri = "http://javaone.com/keynote_thumbnail.jpg";
|
||||
image2.width = 0;
|
||||
image2.height = 0;
|
||||
image2.size = 1;
|
||||
image2.title = "Javaone Keynote";
|
||||
|
||||
MediaContent content = new MediaContent();
|
||||
content.media = media;
|
||||
content.image = new ArrayList<Image>(2);
|
||||
content.image.add(image1);
|
||||
content.image.add(image2);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public byte[] serialize(MediaContent content) throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
Packer pk = new Packer(os);
|
||||
pk.pack(content);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
public MediaContent deserialize(byte[] array) throws Exception {
|
||||
UnbufferedUnpacker pac = new UnbufferedUnpacker();
|
||||
pac.execute(array);
|
||||
Object obj = pac.getData();
|
||||
return (MediaContent)MediaContent.getSchema().convert(obj);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package serializers.msgpack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import org.msgpack.*;
|
||||
import serializers.ObjectSerializer;
|
||||
|
||||
public class MessagePackSpecificSerializer implements ObjectSerializer<MediaContent>
|
||||
{
|
||||
public String getName() {
|
||||
return "msgpack-specific";
|
||||
}
|
||||
|
||||
public MediaContent create() throws Exception {
|
||||
Media media = new Media();
|
||||
media.uri = "http://javaone.com/keynote.mpg";
|
||||
media.format = "video/mpg4";
|
||||
media.title = "Javaone Keynote";
|
||||
media.duration = 1234567L;
|
||||
media.bitrate = 0;
|
||||
media.person = new ArrayList<String>(2);
|
||||
media.person.add("Bill Gates");
|
||||
media.person.add("Steve Jobs");
|
||||
media.player = 0;
|
||||
media.height = 0;
|
||||
media.width = 0;
|
||||
media.size = 123L;
|
||||
media.copyright = "";
|
||||
|
||||
Image image1 = new Image();
|
||||
image1.uri = "http://javaone.com/keynote_large.jpg";
|
||||
image1.width = 0;
|
||||
image1.height = 0;
|
||||
image1.size = 2;
|
||||
image1.title = "Javaone Keynote";
|
||||
|
||||
Image image2 = new Image();
|
||||
image2.uri = "http://javaone.com/keynote_thumbnail.jpg";
|
||||
image2.width = 0;
|
||||
image2.height = 0;
|
||||
image2.size = 1;
|
||||
image2.title = "Javaone Keynote";
|
||||
|
||||
MediaContent content = new MediaContent();
|
||||
content.media = media;
|
||||
content.image = new ArrayList<Image>(2);
|
||||
content.image.add(image1);
|
||||
content.image.add(image2);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public byte[] serialize(MediaContent content) throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
Packer pk = new Packer(os);
|
||||
pk.pack(content);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
public MediaContent deserialize(byte[] array) throws Exception {
|
||||
UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MediaContent.getSchema());
|
||||
pac.execute(array);
|
||||
return (MediaContent)pac.getData();
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,7 @@
|
||||
#ifndef MSGPACK_PACK_DEFINE_H__
|
||||
#define MSGPACK_PACK_DEFINE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "msgpack/sysdep.h"
|
||||
#include <limits.h>
|
||||
|
||||
#endif /* msgpack/pack_define.h */
|
||||
|
@@ -16,38 +16,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define __LITTLE_ENDIAN__
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define __BIG_ENDIAN__
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
|
||||
#define STORE_BE16(d) \
|
||||
((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#define STORE_BE32(d) \
|
||||
((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#define STORE_BE64(d) \
|
||||
((char*)&d)[7], ((char*)&d)[6], ((char*)&d)[5], ((char*)&d)[4], \
|
||||
((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#define TAKE8_8(d) ((uint8_t*)&d)[0]
|
||||
#define TAKE8_16(d) ((uint8_t*)&d)[0]
|
||||
#define TAKE8_32(d) ((uint8_t*)&d)[0]
|
||||
#define TAKE8_64(d) ((uint8_t*)&d)[0]
|
||||
#elif __BIG_ENDIAN__
|
||||
|
||||
#define STORE_BE16(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1]
|
||||
|
||||
#define STORE_BE32(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3]
|
||||
|
||||
#define STORE_BE64(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3], \
|
||||
((char*)&d)[4], ((char*)&d)[5], ((char*)&d)[6], ((char*)&d)[7]
|
||||
|
||||
#define TAKE8_8(d) ((uint8_t*)&d)[0]
|
||||
#define TAKE8_16(d) ((uint8_t*)&d)[1]
|
||||
#define TAKE8_32(d) ((uint8_t*)&d)[3]
|
||||
#define TAKE8_64(d) ((uint8_t*)&d)[7]
|
||||
#endif
|
||||
|
||||
#ifndef msgpack_pack_inline_func
|
||||
@@ -71,10 +49,10 @@
|
||||
do { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); \
|
||||
} else { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_8(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} while(0)
|
||||
@@ -83,14 +61,15 @@ do { \
|
||||
do { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \
|
||||
} else if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} \
|
||||
} while(0)
|
||||
@@ -100,20 +79,22 @@ do { \
|
||||
if(d < (1<<8)) { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \
|
||||
} else { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1<<16)) { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xce; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} \
|
||||
@@ -124,24 +105,27 @@ do { \
|
||||
if(d < (1ULL<<8)) { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \
|
||||
} else { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1ULL<<16)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else if(d < (1ULL<<32)) { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xce; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else { \
|
||||
/* signed 64 */ \
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
|
||||
unsigned char buf[9]; \
|
||||
buf[0] = 0xcf; *(uint64_t*)&buf[1] = _msgpack_be64(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} \
|
||||
} \
|
||||
@@ -151,11 +135,11 @@ do { \
|
||||
do { \
|
||||
if(d < -(1<<5)) { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, d}; \
|
||||
unsigned char buf[2] = {0xd0, TAKE8_8(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
@@ -164,24 +148,26 @@ do { \
|
||||
if(d < -(1<<5)) { \
|
||||
if(d < -(1<<7)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xd1; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xd0, TAKE8_16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \
|
||||
} else { \
|
||||
if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} \
|
||||
} \
|
||||
@@ -192,32 +178,36 @@ do { \
|
||||
if(d < -(1<<5)) { \
|
||||
if(d < -(1<<15)) { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xd2; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else if(d < -(1<<7)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xd1; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xd0, TAKE8_32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \
|
||||
} else { \
|
||||
if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else if(d < (1<<16)) { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xce; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} \
|
||||
@@ -229,46 +219,52 @@ do { \
|
||||
if(d < -(1LL<<15)) { \
|
||||
if(d < -(1LL<<31)) { \
|
||||
/* signed 64 */ \
|
||||
const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; \
|
||||
unsigned char buf[9]; \
|
||||
buf[0] = 0xd3; *(uint64_t*)&buf[1] = _msgpack_be64(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} else { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xd2; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < -(1<<7)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xd1; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xd0, TAKE8_64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} \
|
||||
} else if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \
|
||||
} else { \
|
||||
if(d < (1LL<<16)) { \
|
||||
if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
unsigned char buf[3]; \
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1LL<<32)) { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
unsigned char buf[5]; \
|
||||
buf[0] = 0xce; *(uint32_t*)&buf[1] = _msgpack_be32(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else { \
|
||||
/* unsigned 64 */ \
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
|
||||
unsigned char buf[9]; \
|
||||
buf[0] = 0xcf; *(uint64_t*)&buf[1] = _msgpack_be64(d); \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} \
|
||||
} \
|
||||
@@ -280,49 +276,55 @@ do { \
|
||||
|
||||
msgpack_pack_inline_func_fastint(_uint8)(msgpack_pack_user x, uint8_t d)
|
||||
{
|
||||
const unsigned char buf[2] = {0xcc, d};
|
||||
unsigned char buf[2] = {0xcc, TAKE8_8(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_uint16)(msgpack_pack_user x, uint16_t d)
|
||||
{
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
unsigned char buf[3];
|
||||
buf[0] = 0xcd; *(uint16_t*)&buf[1] = _msgpack_be16(d);
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_uint32)(msgpack_pack_user x, uint32_t d)
|
||||
{
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xce; *(uint32_t*)&buf[1] = _msgpack_be32(d);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_uint64)(msgpack_pack_user x, uint64_t d)
|
||||
{
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)};
|
||||
unsigned char buf[9];
|
||||
buf[0] = 0xcf; *(uint64_t*)&buf[1] = _msgpack_be64(d);
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_int8)(msgpack_pack_user x, int8_t d)
|
||||
{
|
||||
const unsigned char buf[2] = {0xd0, d};
|
||||
unsigned char buf[2] = {0xd0, TAKE8_8(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_int16)(msgpack_pack_user x, int16_t d)
|
||||
{
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
|
||||
unsigned char buf[3];
|
||||
buf[0] = 0xd1; *(uint16_t*)&buf[1] = _msgpack_be16(d);
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_int32)(msgpack_pack_user x, int32_t d)
|
||||
{
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xd2; *(uint32_t*)&buf[1] = _msgpack_be32(d);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
msgpack_pack_inline_func_fastint(_int64)(msgpack_pack_user x, int64_t d)
|
||||
{
|
||||
const unsigned char buf[9] = {0xd3, STORE_BE64(d)};
|
||||
unsigned char buf[9];
|
||||
buf[0] = 0xd3; *(uint64_t*)&buf[1] = _msgpack_be64(d);
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
@@ -554,7 +556,8 @@ msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d)
|
||||
{
|
||||
union { char buf[4]; uint32_t num; } f;
|
||||
*((float*)&f.buf) = d; // FIXME
|
||||
const unsigned char buf[5] = {0xca, STORE_BE32(f.num)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xca; *(uint32_t*)&buf[1] = _msgpack_be32(f.num);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
@@ -562,7 +565,8 @@ msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d)
|
||||
{
|
||||
union { char buf[8]; uint64_t num; } f;
|
||||
*((double*)&f.buf) = d; // FIXME
|
||||
const unsigned char buf[9] = {0xcb, STORE_BE64(f.num)};
|
||||
unsigned char buf[9];
|
||||
buf[0] = 0xcb; *(uint64_t*)&buf[1] = _msgpack_be64(f.num);
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
@@ -605,12 +609,12 @@ msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n)
|
||||
unsigned char d = 0x90 | n;
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
} else if(n < 65536) {
|
||||
uint16_t d = (uint16_t)n;
|
||||
unsigned char buf[3] = {0xdc, STORE_BE16(d)};
|
||||
unsigned char buf[3];
|
||||
buf[0] = 0xdc; *(uint16_t*)&buf[1] = _msgpack_be16(n);
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)n;
|
||||
unsigned char buf[5] = {0xdd, STORE_BE32(d)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xdd; *(uint32_t*)&buf[1] = _msgpack_be32(n);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@@ -624,14 +628,14 @@ msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n)
|
||||
{
|
||||
if(n < 16) {
|
||||
unsigned char d = 0x80 | n;
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
|
||||
} else if(n < 65536) {
|
||||
uint16_t d = (uint16_t)n;
|
||||
unsigned char buf[3] = {0xde, STORE_BE16(d)};
|
||||
unsigned char buf[3];
|
||||
buf[0] = 0xde; *(uint16_t*)&buf[1] = _msgpack_be16(n);
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)n;
|
||||
unsigned char buf[5] = {0xdf, STORE_BE32(d)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xdf; *(uint32_t*)&buf[1] = _msgpack_be32(n);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@@ -645,14 +649,14 @@ msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l)
|
||||
{
|
||||
if(l < 32) {
|
||||
unsigned char d = 0xa0 | l;
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
|
||||
} else if(l < 65536) {
|
||||
uint16_t d = (uint16_t)l;
|
||||
unsigned char buf[3] = {0xda, STORE_BE16(d)};
|
||||
unsigned char buf[3];
|
||||
buf[0] = 0xda; *(uint16_t*)&buf[1] = _msgpack_be16(l);
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)l;
|
||||
unsigned char buf[5] = {0xdb, STORE_BE32(d)};
|
||||
unsigned char buf[5];
|
||||
buf[0] = 0xdb; *(uint32_t*)&buf[1] = _msgpack_be32(l);
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@@ -666,9 +670,10 @@ msgpack_pack_inline_func(_raw_body)(msgpack_pack_user x, const void* b, size_t l
|
||||
#undef msgpack_pack_user
|
||||
#undef msgpack_pack_append_buffer
|
||||
|
||||
#undef STORE_BE16
|
||||
#undef STORE_BE32
|
||||
#undef STORE_BE64
|
||||
#undef TAKE8_8
|
||||
#undef TAKE8_16
|
||||
#undef TAKE8_32
|
||||
#undef TAKE8_64
|
||||
|
||||
#undef msgpack_pack_real_uint8
|
||||
#undef msgpack_pack_real_uint16
|
||||
|
94
msgpack/sysdep.h
Normal file
94
msgpack/sysdep.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* MessagePack system dependencies
|
||||
*
|
||||
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MSGPACK_SYSDEP_H__
|
||||
#define MSGPACK_SYSDEP_H__
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int16 int16_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef long _msgpack_atomic_counter_t;
|
||||
#define _msgpack_sync_decr_and_fetch(ptr) InterlockedDecrement(ptr)
|
||||
#define _msgpack_sync_incr_and_fetch(ptr) InterlockedIncrement(ptr)
|
||||
#else
|
||||
typedef unsigned int _msgpack_atomic_counter_t;
|
||||
#define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
|
||||
#define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <arpa/inet.h> /* __BYTE_ORDER */
|
||||
#endif
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define __LITTLE_ENDIAN__
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define __BIG_ENDIAN__
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
|
||||
#define _msgpack_be16(x) ntohs(x)
|
||||
#define _msgpack_be32(x) ntohl(x)
|
||||
|
||||
#if defined(_byteswap_uint64)
|
||||
# define _msgpack_be64(x) (_byteswap_uint64(x))
|
||||
#elif defined(bswap_64)
|
||||
# define _msgpack_be64(x) bswap_64(x)
|
||||
#elif defined(__DARWIN_OSSwapInt64)
|
||||
# define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
|
||||
#else
|
||||
#define _msgpack_be64(x) \
|
||||
( ((((uint64_t)x) << 56) & 0xff00000000000000ULL ) | \
|
||||
((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
|
||||
((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
|
||||
((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
|
||||
((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
|
||||
((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
|
||||
((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
|
||||
((((uint64_t)x) >> 56) & 0x00000000000000ffULL ) )
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define _msgpack_be16(x) (x)
|
||||
#define _msgpack_be32(x) (x)
|
||||
#define _msgpack_be64(x) (x)
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* msgpack/sysdep.h */
|
||||
|
@@ -18,14 +18,10 @@
|
||||
#ifndef MSGPACK_UNPACK_DEFINE_H__
|
||||
#define MSGPACK_UNPACK_DEFINE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "msgpack/sysdep.h"
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#ifndef __WIN32__
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -37,39 +33,6 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define __LITTLE_ENDIAN__
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define __BIG_ENDIAN__
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define msgpack_betoh16(x) ntohs(x)
|
||||
#define msgpack_betoh32(x) ntohl(x)
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
#if defined(__bswap_64)
|
||||
# define msgpack_betoh64(x) __bswap_64(x)
|
||||
#elif defined(__DARWIN_OSSwapInt64)
|
||||
# define msgpack_betoh64(x) __DARWIN_OSSwapInt64(x)
|
||||
#else
|
||||
static inline uint64_t msgpack_betoh64(uint64_t x) {
|
||||
return ((x << 56) & 0xff00000000000000ULL ) |
|
||||
((x << 40) & 0x00ff000000000000ULL ) |
|
||||
((x << 24) & 0x0000ff0000000000ULL ) |
|
||||
((x << 8) & 0x000000ff00000000ULL ) |
|
||||
((x >> 8) & 0x00000000ff000000ULL ) |
|
||||
((x >> 24) & 0x0000000000ff0000ULL ) |
|
||||
((x >> 40) & 0x000000000000ff00ULL ) |
|
||||
((x >> 56) & 0x00000000000000ffULL ) ;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
#define msgpack_betoh64(x) (x)
|
||||
#endif
|
||||
|
||||
|
||||
typedef enum {
|
||||
CS_HEADER = 0x00, // nil
|
||||
|
||||
|
@@ -40,6 +40,11 @@
|
||||
#error msgpack_unpack_user type is not defined
|
||||
#endif
|
||||
|
||||
#ifndef USE_CASE_RANGE
|
||||
#if !defined(_MSC_VER)
|
||||
#define USE_CASE_RANGE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
msgpack_unpack_struct_decl(_stack) {
|
||||
msgpack_unpack_object obj;
|
||||
@@ -126,20 +131,32 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
|
||||
((unsigned int)*p & 0x1f)
|
||||
|
||||
#define PTR_CAST_8(ptr) (*(uint8_t*)ptr)
|
||||
#define PTR_CAST_16(ptr) msgpack_betoh16(*(uint16_t*)ptr)
|
||||
#define PTR_CAST_32(ptr) msgpack_betoh32(*(uint32_t*)ptr)
|
||||
#define PTR_CAST_64(ptr) msgpack_betoh64(*(uint64_t*)ptr)
|
||||
#define PTR_CAST_16(ptr) _msgpack_be16(*(uint16_t*)ptr)
|
||||
#define PTR_CAST_32(ptr) _msgpack_be32(*(uint32_t*)ptr)
|
||||
#define PTR_CAST_64(ptr) _msgpack_be64(*(uint64_t*)ptr)
|
||||
|
||||
#ifdef USE_CASE_RANGE
|
||||
#define SWITCH_RANGE_BEGIN switch(*p) {
|
||||
#define SWITCH_RANGE(FROM, TO) case FROM ... TO:
|
||||
#define SWITCH_RANGE_DEFAULT default:
|
||||
#define SWITCH_RANGE_END }
|
||||
#else
|
||||
#define SWITCH_RANGE_BEGIN { if(0) {
|
||||
#define SWITCH_RANGE(FROM, TO) } else if(FROM <= *p && *p <= TO) {
|
||||
#define SWITCH_RANGE_DEFAULT } else {
|
||||
#define SWITCH_RANGE_END } }
|
||||
#endif
|
||||
|
||||
if(p == pe) { goto _out; }
|
||||
do {
|
||||
switch(cs) {
|
||||
case CS_HEADER:
|
||||
switch(*p) {
|
||||
case 0x00 ... 0x7f: // Positive Fixnum
|
||||
SWITCH_RANGE_BEGIN
|
||||
SWITCH_RANGE(0x00, 0x7f) // Positive Fixnum
|
||||
push_fixed_value(_uint8, *(uint8_t*)p);
|
||||
case 0xe0 ... 0xff: // Negative Fixnum
|
||||
SWITCH_RANGE(0xe0, 0xff) // Negative Fixnum
|
||||
push_fixed_value(_int8, *(int8_t*)p);
|
||||
case 0xc0 ... 0xdf: // Variable
|
||||
SWITCH_RANGE(0xc0, 0xdf) // Variable
|
||||
switch(*p) {
|
||||
case 0xc0: // nil
|
||||
push_simple_value(_nil);
|
||||
@@ -182,16 +199,16 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
|
||||
default:
|
||||
goto _failed;
|
||||
}
|
||||
case 0xa0 ... 0xbf: // FixRaw
|
||||
SWITCH_RANGE(0xa0, 0xbf) // FixRaw
|
||||
again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
|
||||
case 0x90 ... 0x9f: // FixArray
|
||||
SWITCH_RANGE(0x90, 0x9f) // FixArray
|
||||
start_container(_array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
|
||||
case 0x80 ... 0x8f: // FixMap
|
||||
SWITCH_RANGE(0x80, 0x8f) // FixMap
|
||||
start_container(_map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
|
||||
|
||||
default:
|
||||
SWITCH_RANGE_DEFAULT
|
||||
goto _failed;
|
||||
}
|
||||
SWITCH_RANGE_END
|
||||
// end CS_HEADER
|
||||
|
||||
|
||||
|
13
perl/.gitignore
vendored
Normal file
13
perl/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
META.yml
|
||||
Makefile
|
||||
Makefile.old
|
||||
MessagePack.bs
|
||||
MessagePack.o
|
||||
blib/
|
||||
inc/
|
||||
msgpack/
|
||||
pack.o
|
||||
pm_to_blib
|
||||
unpack.o
|
||||
MANIFEST
|
||||
ppport.h
|
2
perl/.shipit
Normal file
2
perl/.shipit
Normal file
@@ -0,0 +1,2 @@
|
||||
steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
|
||||
git.tagpattern = perl-%v
|
42
perl/Changes
Normal file
42
perl/Changes
Normal file
@@ -0,0 +1,42 @@
|
||||
0.09
|
||||
|
||||
- support NVTYPE=="long double" or IVTYPE=="long long" environment
|
||||
(thanks to Jun Kuriyama++)
|
||||
|
||||
0.08
|
||||
|
||||
- fixed PVNV issue...
|
||||
|
||||
0.07
|
||||
|
||||
- do not use switch (SvTYPE(val)).
|
||||
|
||||
0.06
|
||||
|
||||
- use SvNOK.
|
||||
|
||||
0.05
|
||||
|
||||
- change type detection for old perl
|
||||
|
||||
0.04
|
||||
|
||||
- check SvROK first(reported by yappo++)
|
||||
- PreferInteger: faster string to integer conversion; support negative value
|
||||
(frsyuki++)
|
||||
- make PreferInteger variable magical and remove get_sv from _msgpack_pack_sv
|
||||
(frsyuki++)
|
||||
|
||||
0.03
|
||||
|
||||
- performance tuning for too long string
|
||||
- fixed memory leaks in stream unpacker
|
||||
|
||||
0.02
|
||||
|
||||
- added $Data::MessagePack::PreferInteger
|
||||
(requested by yappo++)
|
||||
|
||||
0.01
|
||||
|
||||
- initial release to CPAN
|
25
perl/MANIFEST.SKIP
Normal file
25
perl/MANIFEST.SKIP
Normal file
@@ -0,0 +1,25 @@
|
||||
\bRCS\b
|
||||
\bCVS\b
|
||||
^MANIFEST\.
|
||||
^Makefile$
|
||||
~$
|
||||
^#
|
||||
\.old$
|
||||
^blib/
|
||||
^pm_to_blib
|
||||
^MakeMaker-\d
|
||||
\.gz$
|
||||
\.cvsignore
|
||||
^t/9\d_.*\.t
|
||||
^t/perlcritic
|
||||
^tools/
|
||||
\.svn/
|
||||
^[^/]+\.yaml$
|
||||
^[^/]+\.pl$
|
||||
^\.shipit$
|
||||
^\.git/
|
||||
\.sw[pon]$
|
||||
^\.gitignore$
|
||||
\.o$
|
||||
\.bs$
|
||||
^Data-MessagePack-[0-9.]+/
|
40
perl/Makefile.PL
Normal file
40
perl/Makefile.PL
Normal file
@@ -0,0 +1,40 @@
|
||||
use inc::Module::Install;
|
||||
name 'Data-MessagePack';
|
||||
all_from 'lib/Data/MessagePack.pm';
|
||||
readme_from 'lib/Data/MessagePack.pm';
|
||||
|
||||
perl_version '5.008005';
|
||||
license 'perl';
|
||||
can_cc or die "This module requires a C compiler";
|
||||
|
||||
tests 't/*.t';
|
||||
author_tests('xt');
|
||||
use_ppport 3.19;
|
||||
|
||||
clean_files qw{
|
||||
*.stackdump
|
||||
*.gcov *.gcda *.gcno
|
||||
*.out
|
||||
nytprof
|
||||
cover_db
|
||||
};
|
||||
|
||||
if ($ENV{DEBUG}) {
|
||||
cc_append_to_ccflags '-g';
|
||||
}
|
||||
|
||||
# copy modules
|
||||
if ($Module::Install::AUTHOR && -d File::Spec->catfile('..', 'msgpack')) {
|
||||
mkdir 'msgpack' unless -d 'msgpack';
|
||||
require File::Copy;
|
||||
for my $src (<../msgpack/*.h>) {
|
||||
File::Copy::copy($src, 'msgpack/') or die "copy failed: $!";
|
||||
}
|
||||
}
|
||||
|
||||
auto_set_repository;
|
||||
build_requires 'Test::More';
|
||||
use_test_base;
|
||||
auto_include;
|
||||
WriteAll;
|
||||
|
43
perl/MessagePack.c
Normal file
43
perl/MessagePack.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include "EXTERN.h"
|
||||
#include "perl.h"
|
||||
#include "XSUB.h"
|
||||
#define NEED_newCONSTSUB
|
||||
#include "ppport.h"
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
XS(xs_pack);
|
||||
XS(xs_unpack);
|
||||
XS(xs_unpacker_new);
|
||||
XS(xs_unpacker_execute);
|
||||
XS(xs_unpacker_execute_limit);
|
||||
XS(xs_unpacker_is_finished);
|
||||
XS(xs_unpacker_data);
|
||||
XS(xs_unpacker_reset);
|
||||
XS(xs_unpacker_destroy);
|
||||
|
||||
void boot_Data__MessagePack_pack(void);
|
||||
|
||||
XS(boot_Data__MessagePack) {
|
||||
dXSARGS;
|
||||
HV * stash;
|
||||
|
||||
boot_Data__MessagePack_pack();
|
||||
|
||||
newXS("Data::MessagePack::pack", xs_pack, __FILE__);
|
||||
newXS("Data::MessagePack::unpack", xs_unpack, __FILE__);
|
||||
stash = gv_stashpvn("Data::MessagePack", strlen("Data::MessagePack"), TRUE);
|
||||
|
||||
newXS("Data::MessagePack::Unpacker::new", xs_unpacker_new, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::execute", xs_unpacker_execute, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::execute_limit", xs_unpacker_execute_limit, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::is_finished", xs_unpacker_is_finished, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::data", xs_unpacker_data, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::reset", xs_unpacker_reset, __FILE__);
|
||||
newXS("Data::MessagePack::Unpacker::DESTROY", xs_unpacker_destroy, __FILE__);
|
||||
}
|
||||
|
20
perl/benchmark/deserialize.pl
Normal file
20
perl/benchmark/deserialize.pl
Normal file
@@ -0,0 +1,20 @@
|
||||
use strict;
|
||||
use warnings;
|
||||
use Data::MessagePack;
|
||||
use JSON::XS;
|
||||
use Benchmark ':all';
|
||||
|
||||
my $a = [0..2**24];
|
||||
my $j = JSON::XS::encode_json($a);
|
||||
my $m = Data::MessagePack->pack($a);
|
||||
|
||||
print "-- deserialize\n";
|
||||
print "JSON::XS: $JSON::XS::VERSION\n";
|
||||
print "Data::MessagePack: $Data::MessagePack::VERSION\n";
|
||||
cmpthese(
|
||||
-1 => {
|
||||
json => sub { JSON::XS::decode_json($j) },
|
||||
mp => sub { Data::MessagePack->unpack($m) },
|
||||
}
|
||||
);
|
||||
|
18
perl/benchmark/serialize.pl
Normal file
18
perl/benchmark/serialize.pl
Normal file
@@ -0,0 +1,18 @@
|
||||
use strict;
|
||||
use warnings;
|
||||
use Data::MessagePack;
|
||||
use JSON::XS;
|
||||
use Benchmark ':all';
|
||||
|
||||
my $a = [0..2**24];
|
||||
|
||||
print "-- serialize\n";
|
||||
print "JSON::XS: $JSON::XS::VERSION\n";
|
||||
print "Data::MessagePack: $Data::MessagePack::VERSION\n";
|
||||
cmpthese(
|
||||
-1 => {
|
||||
json => sub { JSON::XS::encode_json($a) },
|
||||
mp => sub { Data::MessagePack->pack($a) },
|
||||
}
|
||||
);
|
||||
|
37
perl/benchmark/size.pl
Normal file
37
perl/benchmark/size.pl
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Data::MessagePack;
|
||||
use Storable;
|
||||
use Text::SimpleTable;
|
||||
|
||||
my @entries = (
|
||||
'1',
|
||||
'3.14',
|
||||
'{}',
|
||||
'[]',
|
||||
"[('a')x10]",
|
||||
"{('a')x10}",
|
||||
"+{1,+{1,+{}}}",
|
||||
"+[+[+[]]]",
|
||||
);
|
||||
|
||||
my $table = Text::SimpleTable->new([15, 'src'], [9, 'storable'], [7, 'msgpack']);
|
||||
|
||||
for my $src (@entries) {
|
||||
my $e = eval $src;
|
||||
die $@ if $@;
|
||||
|
||||
$table->row(
|
||||
$src,
|
||||
length(Storable::nfreeze(ref $e ? $e : \$e)),
|
||||
length(Data::MessagePack->pack($e)),
|
||||
);
|
||||
}
|
||||
|
||||
print "perl: $]\n";
|
||||
print "Storable: $Storable::VERSION\n";
|
||||
print "Data::MessagePack: $Data::MessagePack::VERSION\n";
|
||||
print "\n";
|
||||
print $table->draw;
|
||||
|
60
perl/lib/Data/MessagePack.pm
Normal file
60
perl/lib/Data/MessagePack.pm
Normal file
@@ -0,0 +1,60 @@
|
||||
package Data::MessagePack;
|
||||
use strict;
|
||||
use warnings;
|
||||
use XSLoader;
|
||||
use 5.008001;
|
||||
|
||||
our $VERSION = '0.09';
|
||||
our $PreferInteger = 0;
|
||||
|
||||
our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" };
|
||||
our $false = do { bless \(my $dummy = 0), "Data::MessagePack::Boolean" };
|
||||
sub true () { $true }
|
||||
sub false () { $false }
|
||||
|
||||
XSLoader::load(__PACKAGE__, $VERSION);
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Data::MessagePack - messagepack
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
my $packed = Data::MessagePack->pack($dat);
|
||||
my $unpacked = Data::MessagePack->unpack($dat);
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Data::MessagePack is a binary packer for perl.
|
||||
|
||||
=head1 Configuration Variables
|
||||
|
||||
=over 4
|
||||
|
||||
=item $Data::MessagePack::PreferInteger
|
||||
|
||||
Pack the string as int when the value looks like int(EXPERIMENTAL).
|
||||
|
||||
=back
|
||||
|
||||
=head1 AUTHORS
|
||||
|
||||
Tokuhiro Matsuno
|
||||
|
||||
=head1 THANKS TO
|
||||
|
||||
Jun Kuriyama
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This library is free software; you can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
|
||||
=head1 SEE ALSO
|
||||
|
||||
L<http://msgpack.sourceforge.jp/>
|
||||
|
52
perl/lib/Data/MessagePack/Unpacker.pod
Normal file
52
perl/lib/Data/MessagePack/Unpacker.pod
Normal file
@@ -0,0 +1,52 @@
|
||||
=head1 NAME
|
||||
|
||||
Data::MessagePack::Unpacker - messagepack streaming deserializer
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use Data::Dumper;
|
||||
my $up = Data::MessagePack::Unpacker->new;
|
||||
my $ret = $up->execute($v, 0);
|
||||
if ($ret != length($v)) {
|
||||
fail "extra bytes";
|
||||
}
|
||||
return Dumper($up->data);
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This is an streaming deserializer for messagepack.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=over 4
|
||||
|
||||
=item my $up = Data::MessagePack::Unpacker->new()
|
||||
|
||||
create new stream deserializer
|
||||
|
||||
=item $up->execute()
|
||||
|
||||
=item $up->execute_limit()
|
||||
|
||||
=item $up->is_finished()
|
||||
|
||||
is this deserializer finished?
|
||||
|
||||
=item $up->data()
|
||||
|
||||
returns deserialized object.
|
||||
|
||||
=item $up->reset()
|
||||
|
||||
reset the stream deserializer, without memory zone.
|
||||
|
||||
=back
|
||||
|
||||
=head1 AUTHORS
|
||||
|
||||
Tokuhiro Matsuno
|
||||
|
||||
=head1 SEE ALSO
|
||||
|
||||
L<Data::MessagePack>
|
||||
|
266
perl/pack.c
Normal file
266
perl/pack.c
Normal file
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* code is written by tokuhirom.
|
||||
* buffer alocation technique is taken from JSON::XS. thanks to mlehmann.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include "EXTERN.h"
|
||||
#include "perl.h"
|
||||
#include "XSUB.h"
|
||||
#include "ppport.h"
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#include "msgpack/pack_define.h"
|
||||
|
||||
#define msgpack_pack_inline_func(name) \
|
||||
static inline void msgpack_pack ## name
|
||||
|
||||
#define msgpack_pack_inline_func_cint(name) \
|
||||
static inline void msgpack_pack ## name
|
||||
|
||||
typedef struct {
|
||||
char *cur; /* SvPVX (sv) + current output position */
|
||||
char *end; /* SvEND (sv) */
|
||||
SV *sv; /* result scalar */
|
||||
} enc_t;
|
||||
static void need(enc_t *enc, STRLEN len);
|
||||
|
||||
#define msgpack_pack_user enc_t*
|
||||
|
||||
#define msgpack_pack_append_buffer(enc, buf, len) \
|
||||
need(enc, len); \
|
||||
memcpy(enc->cur, buf, len); \
|
||||
enc->cur += len;
|
||||
|
||||
#include "msgpack/pack_template.h"
|
||||
|
||||
#define INIT_SIZE 32 /* initial scalar size to be allocated */
|
||||
|
||||
#if IVSIZE == 8
|
||||
# define PACK_IV msgpack_pack_int64
|
||||
#elif IVSIZE == 4
|
||||
# define PACK_IV msgpack_pack_int32
|
||||
#elif IVSIZE == 2
|
||||
# define PACK_IV msgpack_pack_int16
|
||||
#else
|
||||
# error "msgpack only supports IVSIZE = 8,4,2 environment."
|
||||
#endif
|
||||
|
||||
|
||||
static void need(enc_t *enc, STRLEN len)
|
||||
{
|
||||
if (enc->cur + len >= enc->end) {
|
||||
STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
|
||||
SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
|
||||
enc->cur = SvPVX (enc->sv) + cur;
|
||||
enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int s_pref_int = 0;
|
||||
|
||||
static int pref_int_set(pTHX_ SV* sv, MAGIC* mg) {
|
||||
if (SvTRUE(sv)) {
|
||||
s_pref_int = 1;
|
||||
} else {
|
||||
s_pref_int = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MGVTBL pref_int_vtbl = {
|
||||
NULL,
|
||||
pref_int_set,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
#ifdef MGf_LOCAL
|
||||
NULL,
|
||||
#endif
|
||||
};
|
||||
|
||||
void boot_Data__MessagePack_pack(void) {
|
||||
SV* var = get_sv("Data::MessagePack::PreferInteger", 0);
|
||||
sv_magicext(var, NULL, PERL_MAGIC_ext, &pref_int_vtbl, NULL, 0);
|
||||
SvSETMAGIC(var);
|
||||
}
|
||||
|
||||
|
||||
static int try_int(enc_t* enc, const char *p, size_t len) {
|
||||
int negative = 0;
|
||||
const char* pe = p + len;
|
||||
uint64_t num = 0;
|
||||
|
||||
if (len == 0) { return 0; }
|
||||
|
||||
if (*p == '-') {
|
||||
/* length(-0x80000000) == 11 */
|
||||
if (len <= 1 || len > 11) { return 0; }
|
||||
negative = 1;
|
||||
++p;
|
||||
} else {
|
||||
/* length(0xFFFFFFFF) == 10 */
|
||||
if (len > 10) { return 0; }
|
||||
}
|
||||
|
||||
#if '9'=='8'+1 && '8'=='7'+1 && '7'=='6'+1 && '6'=='5'+1 && '5'=='4'+1 \
|
||||
&& '4'=='3'+1 && '3'=='2'+1 && '2'=='1'+1 && '1'=='0'+1
|
||||
do {
|
||||
unsigned int c = ((int)*(p++)) - '0';
|
||||
if (c > 9) { return 0; }
|
||||
num = num * 10 + c;
|
||||
} while(p < pe);
|
||||
#else
|
||||
do {
|
||||
switch (*(p++)) {
|
||||
case '0': num = num * 10 + 0; break;
|
||||
case '1': num = num * 10 + 1; break;
|
||||
case '2': num = num * 10 + 2; break;
|
||||
case '3': num = num * 10 + 3; break;
|
||||
case '4': num = num * 10 + 4; break;
|
||||
case '5': num = num * 10 + 5; break;
|
||||
case '6': num = num * 10 + 6; break;
|
||||
case '7': num = num * 10 + 7; break;
|
||||
case '8': num = num * 10 + 8; break;
|
||||
case '9': num = num * 10 + 9; break;
|
||||
default: return 0;
|
||||
}
|
||||
} while(p < pe);
|
||||
#endif
|
||||
|
||||
if (negative) {
|
||||
if (num > 0x80000000) { return 0; }
|
||||
msgpack_pack_int32(enc, ((int32_t)num) * -1);
|
||||
} else {
|
||||
if (num > 0xFFFFFFFF) { return 0; }
|
||||
msgpack_pack_uint32(enc, (uint32_t)num);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void _msgpack_pack_rv(enc_t *enc, SV* sv);
|
||||
|
||||
static void _msgpack_pack_sv(enc_t *enc, SV* sv) {
|
||||
SvGETMAGIC(sv);
|
||||
|
||||
if (sv==NULL) {
|
||||
msgpack_pack_nil(enc);
|
||||
} else if (SvPOKp(sv)) {
|
||||
STRLEN len;
|
||||
char * csv = SvPV(sv, len);
|
||||
|
||||
if (s_pref_int && try_int(enc, csv, len)) {
|
||||
return;
|
||||
} else {
|
||||
msgpack_pack_raw(enc, len);
|
||||
msgpack_pack_raw_body(enc, csv, len);
|
||||
}
|
||||
} else if (SvNOKp(sv)) {
|
||||
/* XXX long double is not supported yet. */
|
||||
msgpack_pack_double(enc, (double)SvNVX(sv));
|
||||
} else if (SvIOK_UV(sv)) {
|
||||
msgpack_pack_uint32(enc, SvUV(sv));
|
||||
} else if (SvIOKp(sv)) {
|
||||
PACK_IV(enc, SvIV(sv));
|
||||
} else if (SvROK(sv)) {
|
||||
_msgpack_pack_rv(enc, SvRV(sv));
|
||||
} else if (!SvOK(sv)) {
|
||||
msgpack_pack_nil(enc);
|
||||
} else if (isGV(sv)) {
|
||||
Perl_croak(aTHX_ "msgpack cannot pack the GV\n");
|
||||
} else {
|
||||
sv_dump(sv);
|
||||
Perl_croak(aTHX_ "msgpack for perl doesn't supported this type: %d\n", SvTYPE(sv));
|
||||
}
|
||||
}
|
||||
|
||||
static void _msgpack_pack_rv(enc_t *enc, SV* sv) {
|
||||
svtype svt;
|
||||
SvGETMAGIC(sv);
|
||||
svt = SvTYPE(sv);
|
||||
|
||||
if (SvOBJECT (sv)) {
|
||||
HV *stash = gv_stashpv ("Data::MessagePack::Boolean", 1); // TODO: cache?
|
||||
if (SvSTASH (sv) == stash) {
|
||||
if (SvIV(sv)) {
|
||||
msgpack_pack_true(enc);
|
||||
} else {
|
||||
msgpack_pack_false(enc);
|
||||
}
|
||||
} else {
|
||||
croak ("encountered object '%s', Data::MessagePack doesn't allow the object",
|
||||
SvPV_nolen(sv_2mortal(newRV_inc(sv))));
|
||||
}
|
||||
} else if (svt == SVt_PVHV) {
|
||||
HV* hval = (HV*)sv;
|
||||
int count = hv_iterinit(hval);
|
||||
HE* he;
|
||||
|
||||
msgpack_pack_map(enc, count);
|
||||
|
||||
while (he = hv_iternext(hval)) {
|
||||
_msgpack_pack_sv(enc, hv_iterkeysv(he));
|
||||
_msgpack_pack_sv(enc, HeVAL(he));
|
||||
}
|
||||
} else if (svt == SVt_PVAV) {
|
||||
AV* ary = (AV*)sv;
|
||||
int len = av_len(ary) + 1;
|
||||
int i;
|
||||
msgpack_pack_array(enc, len);
|
||||
for (i=0; i<len; i++) {
|
||||
SV** svp = av_fetch(ary, i, 0);
|
||||
if (svp) {
|
||||
_msgpack_pack_sv(enc, *svp);
|
||||
} else {
|
||||
msgpack_pack_nil(enc);
|
||||
}
|
||||
}
|
||||
} else if (svt < SVt_PVAV) {
|
||||
STRLEN len = 0;
|
||||
char *pv = svt ? SvPV (sv, len) : 0;
|
||||
|
||||
if (len == 1 && *pv == '1')
|
||||
msgpack_pack_true(enc);
|
||||
else if (len == 1 && *pv == '0')
|
||||
msgpack_pack_false(enc);
|
||||
else {
|
||||
sv_dump(sv);
|
||||
croak("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
|
||||
SvPV_nolen (sv_2mortal (newRV_inc (sv))));
|
||||
}
|
||||
} else {
|
||||
croak ("encountered %s, but msgpack can only represent references to arrays or hashes",
|
||||
SvPV_nolen (sv_2mortal (newRV_inc (sv))));
|
||||
}
|
||||
}
|
||||
|
||||
XS(xs_pack) {
|
||||
dXSARGS;
|
||||
if (items != 2) {
|
||||
Perl_croak(aTHX_ "Usage: Data::MessagePack->pack($dat)");
|
||||
}
|
||||
|
||||
SV* val = ST(1);
|
||||
|
||||
enc_t enc;
|
||||
enc.sv = sv_2mortal(NEWSV(0, INIT_SIZE));
|
||||
enc.cur = SvPVX(enc.sv);
|
||||
enc.end = SvEND(enc.sv);
|
||||
SvPOK_only(enc.sv);
|
||||
|
||||
_msgpack_pack_sv(&enc, val);
|
||||
|
||||
SvCUR_set(enc.sv, enc.cur - SvPVX (enc.sv));
|
||||
*SvEND (enc.sv) = 0; /* many xs functions expect a trailing 0 for text strings */
|
||||
|
||||
ST(0) = enc.sv;
|
||||
XSRETURN(1);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user