mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-20 22:31:33 +02:00
Compare commits
325 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e57084f6df | ||
![]() |
8783cf8ec3 | ||
![]() |
2af7a7c6ac | ||
![]() |
91a1f8d9e1 | ||
![]() |
fc5e8ddca1 | ||
![]() |
6352472c5f | ||
![]() |
833ee6484c | ||
![]() |
dda3d24bca | ||
![]() |
f77d76a320 | ||
![]() |
c51fabf6ed | ||
![]() |
b10cb658ca | ||
![]() |
68f60568ac | ||
![]() |
9fbca83ac0 | ||
![]() |
d19bfaa2cb | ||
![]() |
53d5ddb345 | ||
![]() |
0a5c2e7ab9 | ||
![]() |
35802ba949 | ||
![]() |
72160aac9a | ||
![]() |
9df6916029 | ||
![]() |
9bfa2354ff | ||
![]() |
7d945d3c8e | ||
![]() |
05e28752f1 | ||
![]() |
01b6673528 | ||
![]() |
4e85ebbf98 | ||
![]() |
120e8bffd7 | ||
![]() |
8335823748 | ||
![]() |
c9fcf4020f | ||
![]() |
b10a736744 | ||
![]() |
60fbaf7612 | ||
![]() |
d24193630e | ||
![]() |
354af69f62 | ||
![]() |
bccac610a4 | ||
![]() |
5aa6209664 | ||
![]() |
d693d92702 | ||
![]() |
f2622e54e3 | ||
![]() |
20fe9b6dde | ||
![]() |
11f7aa4212 | ||
![]() |
f53c351fd2 | ||
![]() |
fb96617377 | ||
![]() |
05b8c00ee7 | ||
![]() |
58854fdae9 | ||
![]() |
fd31ff772f | ||
![]() |
ab8e0c9e31 | ||
![]() |
abeed3be84 | ||
![]() |
a65438c6fe | ||
![]() |
c3f43fb0cf | ||
![]() |
2807504a81 | ||
![]() |
08b716c96d | ||
![]() |
b4b1f0a2c9 | ||
![]() |
228f742b2f | ||
![]() |
a55affe4d5 | ||
![]() |
a6ec726ed7 | ||
![]() |
87835a4e60 | ||
![]() |
1f18af4395 | ||
![]() |
e79747a600 | ||
![]() |
bd36ac2c0c | ||
![]() |
c6186f2c01 | ||
![]() |
d639f57470 | ||
![]() |
99a2d28592 | ||
![]() |
254ee80c16 | ||
![]() |
e43f57fe1a | ||
![]() |
f88c029a4c | ||
![]() |
93c3cbeaef | ||
![]() |
88c77b793f | ||
![]() |
11286524a5 | ||
![]() |
7c863c341e | ||
![]() |
3416cf984e | ||
![]() |
1784746e7e | ||
![]() |
6c6df1adaf | ||
![]() |
f91e1c17c0 | ||
![]() |
58201b95f2 | ||
![]() |
fcce8f6d51 | ||
![]() |
f7bdda8828 | ||
![]() |
df5a60fd5b | ||
![]() |
5782ab7ccc | ||
![]() |
72e3f98213 | ||
![]() |
1b1433a664 | ||
![]() |
568704ed22 | ||
![]() |
05a7e4eb64 | ||
![]() |
7a0acf2838 | ||
![]() |
27787505e0 | ||
![]() |
2c8048115d | ||
![]() |
7df1780853 | ||
![]() |
d5609f3207 | ||
![]() |
cd10fbc1fe | ||
![]() |
ca0b085d15 | ||
![]() |
9a034234d8 | ||
![]() |
66ef6c9e4c | ||
![]() |
7df60b259b | ||
![]() |
34b3bbc883 | ||
![]() |
e02d20dd74 | ||
![]() |
1a11608f1f | ||
![]() |
9e2c9d7812 | ||
![]() |
404a393315 | ||
![]() |
c69092e110 | ||
![]() |
ebc0eeac79 | ||
![]() |
2b72f35c32 | ||
![]() |
d8212ad620 | ||
![]() |
1066bb38a8 | ||
![]() |
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 |
30
Makefile.am
30
Makefile.am
@@ -1,30 +0,0 @@
|
||||
if ENABLE_CXX
|
||||
export ERB
|
||||
SUBDIRS = c cpp
|
||||
else
|
||||
SUBDIRS = c
|
||||
endif
|
||||
|
||||
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
|
||||
|
72
README
72
README
@@ -1,72 +0,0 @@
|
||||
MessagePack
|
||||
-----------
|
||||
Binary-based efficient data interchange format.
|
||||
|
||||
|
||||
*Requirements
|
||||
|
||||
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)
|
||||
|
||||
|
||||
*Installation
|
||||
|
||||
Simply run ./configure && make && make install to install C and C++ binding.
|
||||
|
||||
$ ./configure
|
||||
$ make
|
||||
$ sudo make install
|
||||
|
||||
To install Ruby binding, run ./gengem.sh script on ruby/ directory and install
|
||||
generated gem package.
|
||||
|
||||
$ cd ruby
|
||||
$ ./gengem.sh
|
||||
$ gem install gem/pkg/msgpack-*.gem
|
||||
|
||||
|
||||
*Usage
|
||||
|
||||
C++:
|
||||
include msgpack.hpp header and link libmsgpack library.
|
||||
see example/simple.cc for example.
|
||||
|
||||
g++ simple.cc -lmsgpack
|
||||
g++ stream.cc -lmsgpack -lpthread
|
||||
|
||||
|
||||
C:
|
||||
include msgpack.h header and link libmsgpackc library.
|
||||
see example/simple.c for example.
|
||||
|
||||
gcc simple.c -lmsgpackc
|
||||
|
||||
|
||||
Ruby:
|
||||
require msgpack library.
|
||||
see example/simple.rb for example.
|
||||
|
||||
ruby -rubygems simple.rb
|
||||
|
||||
|
||||
API Document is available at http://msgpack.sourceforge.jp/.
|
||||
|
||||
|
||||
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.
|
||||
|
37
README.md
Normal file
37
README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
MessagePack
|
||||
===========
|
||||
Extremely efficient object serialization library. It's like JSON, but very fast and small.
|
||||
|
||||
|
||||
## What's MessagePack?
|
||||
|
||||
MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.
|
||||
|
||||
Typical small integer (like flags or error code) is saved only in 1 byte, and typical short string only needs 1 byte except the length of the string itself. \[1,2,3\] (3 elements array) is serialized in 4 bytes using MessagePack as follows:
|
||||
|
||||
require 'msgpack'
|
||||
msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
|
||||
MessagePack.unpack(msg) #=> [1,2,3]
|
||||
|
||||
|
||||
## Performance
|
||||
|
||||

|
||||
|
||||
In this test, it measured the elapsed time of serializing and deserializing 200,000 target objects. The target object consists of the three integers and 512 bytes string.
|
||||
The source code of this test is available from [frsyuki' serializer-speed-test repository.](http://github.com/frsyuki/serializer-speed-test)
|
||||
|
||||
|
||||
## Getting Started
|
||||
|
||||
Usage and other documents about implementations in each language are found at [the web site.](http://msgpack.sourceforge.net/)
|
||||
|
||||
|
||||
## Learn More
|
||||
|
||||
- [Project Web Site](http://msgpack.sourceforge.net/)
|
||||
- [MessagePack format specification](http://msgpack.sourceforge.net/spec)
|
||||
- [Repository at github](http://github.com/msgpack/msgpack)
|
||||
- [Wiki](http://msgpack.sourceforge.net/start)
|
||||
- [MessagePack-RPC](http://github.com/msgpack/msgpack-rpc)
|
||||
|
@@ -1,18 +0,0 @@
|
||||
lib_LTLIBRARIES = libmsgpackc.la
|
||||
|
||||
libmsgpackc_la_SOURCES = \
|
||||
unpack.c \
|
||||
object.c \
|
||||
zone.c
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack.h \
|
||||
msgpack/sbuffer.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
|
||||
|
323
c/bench.c
323
c/bench.c
@@ -1,323 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <msgpack/pack.h>
|
||||
#include <msgpack/unpack.h>
|
||||
#include <yajl/yajl_parse.h>
|
||||
#include <yajl/yajl_gen.h>
|
||||
|
||||
|
||||
static struct timeval g_timer;
|
||||
|
||||
void reset_timer()
|
||||
{
|
||||
gettimeofday(&g_timer, NULL);
|
||||
}
|
||||
|
||||
void show_timer(size_t bufsz)
|
||||
{
|
||||
struct timeval endtime;
|
||||
gettimeofday(&endtime, NULL);
|
||||
double sec = (endtime.tv_sec - g_timer.tv_sec)
|
||||
+ (double)(endtime.tv_usec - g_timer.tv_usec) / 1000 / 1000;
|
||||
printf("%f sec\n", sec);
|
||||
printf("%f MB\n", ((double)bufsz)/1024/1024);
|
||||
printf("%f Mbps\n", ((double)bufsz)*8/sec/1000/1000);
|
||||
}
|
||||
|
||||
|
||||
static int reformat_null(void * ctx) { return 1; }
|
||||
static int reformat_boolean(void * ctx, int boolean) { return 1; }
|
||||
static int reformat_number(void * ctx, const char * s, unsigned int l) { return 1; }
|
||||
static int reformat_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; }
|
||||
static int reformat_map_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; }
|
||||
static int reformat_start_map(void * ctx) { return 1; }
|
||||
static int reformat_end_map(void * ctx) { return 1; }
|
||||
static int reformat_start_array(void * ctx) { return 1; }
|
||||
static int reformat_end_array(void * ctx) { return 1; }
|
||||
|
||||
|
||||
static void* unpack_uint8(void* data, uint8_t d) { return NULL; }
|
||||
static void* unpack_uint16(void* data, uint16_t d) { return NULL; }
|
||||
static void* unpack_uint32(void* data, uint32_t d) { return NULL; }
|
||||
static void* unpack_uint64(void* data, uint64_t d) { return NULL; }
|
||||
static void* unpack_int8(void* data, int8_t d) { return NULL; }
|
||||
static void* unpack_int16(void* data, int16_t d) { return NULL; }
|
||||
static void* unpack_int32(void* data, int32_t d) { return NULL; }
|
||||
static void* unpack_int64(void* data, int64_t d) { return NULL; }
|
||||
static void* unpack_float(void* data, float d) { return NULL; }
|
||||
static void* unpack_double(void* data, double d) { return NULL; }
|
||||
static void* unpack_nil(void* data) { return NULL; }
|
||||
static void* unpack_true(void* data) { return NULL; }
|
||||
static void* unpack_false(void* data) { return NULL; }
|
||||
static void* unpack_array(void* data, unsigned int n) { return NULL; }
|
||||
static void unpack_array_item(void* data, void* c, void* o) { }
|
||||
static void* unpack_map(void* data, unsigned int n) { return NULL; }
|
||||
static void unpack_map_item(void* data, void* c, void* k, void* v) { }
|
||||
static void* unpack_raw(void* data, const char* b, const char* p, unsigned int l) { /*printf("unpack raw %p %lu\n",p,l);*/ return NULL; }
|
||||
|
||||
|
||||
typedef struct {
|
||||
size_t allocated;
|
||||
size_t length;
|
||||
char* buffer;
|
||||
} pack_buffer;
|
||||
|
||||
static const size_t PACK_INITIAL_BUFFER_SIZE = 32*1024;
|
||||
|
||||
static void pack_buffer_init(pack_buffer* data)
|
||||
{
|
||||
data->buffer = malloc(PACK_INITIAL_BUFFER_SIZE);
|
||||
data->length = 0;
|
||||
data->allocated = PACK_INITIAL_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
static void pack_buffer_reset(pack_buffer* data)
|
||||
{
|
||||
data->buffer = realloc(data->buffer, PACK_INITIAL_BUFFER_SIZE);
|
||||
data->allocated = PACK_INITIAL_BUFFER_SIZE;
|
||||
data->length = 0;
|
||||
}
|
||||
|
||||
static void pack_buffer_free(pack_buffer* data)
|
||||
{
|
||||
free(data->buffer);
|
||||
}
|
||||
|
||||
static void pack_append_buffer(void* user, const char* b, unsigned int l)
|
||||
{
|
||||
pack_buffer* data = (pack_buffer*)user;
|
||||
if(data->allocated - data->length < l) {
|
||||
data->buffer = realloc(data->buffer, data->allocated*2);
|
||||
data->allocated *= 2;
|
||||
}
|
||||
memcpy(data->buffer + data->length, b, l);
|
||||
data->length += l;
|
||||
}
|
||||
|
||||
|
||||
static const unsigned int TASK_INT_NUM = 1<<24;
|
||||
static const unsigned int TASK_STR_LEN = 1<<15;
|
||||
//static const unsigned int TASK_INT_NUM = 1<<20;
|
||||
//static const unsigned int TASK_STR_LEN = 1<<12;
|
||||
static const char* TASK_STR_PTR;
|
||||
|
||||
|
||||
void bench_json(void)
|
||||
{
|
||||
puts("== JSON ==");
|
||||
|
||||
|
||||
yajl_gen_config gcfg = {0, NULL};
|
||||
yajl_gen g = yajl_gen_alloc(&gcfg);
|
||||
|
||||
yajl_parser_config hcfg = { 0, 0 };
|
||||
yajl_callbacks callbacks = {
|
||||
reformat_null,
|
||||
reformat_boolean,
|
||||
NULL,
|
||||
NULL,
|
||||
reformat_number,
|
||||
reformat_string,
|
||||
reformat_start_map,
|
||||
reformat_map_key,
|
||||
reformat_end_map,
|
||||
reformat_start_array,
|
||||
reformat_end_array
|
||||
};
|
||||
yajl_handle h = yajl_alloc(&callbacks, &hcfg, NULL);
|
||||
|
||||
|
||||
const unsigned char * buf;
|
||||
unsigned int len;
|
||||
|
||||
|
||||
puts("generate integer");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
yajl_gen_array_open(g);
|
||||
for(i=0; i < TASK_INT_NUM; ++i) {
|
||||
yajl_gen_integer(g, i);
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
show_timer(len);
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
|
||||
puts("----");
|
||||
puts("parse integer");
|
||||
reset_timer();
|
||||
{
|
||||
yajl_status stat = yajl_parse(h, buf, len);
|
||||
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
||||
unsigned char * str = yajl_get_error(h, 1, buf, len);
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
show_timer(len);
|
||||
|
||||
|
||||
//yajl_gen_clear(g);
|
||||
yajl_gen_free(g);
|
||||
g = yajl_gen_alloc(&gcfg);
|
||||
yajl_free(h);
|
||||
h = yajl_alloc(&callbacks, &hcfg, NULL);
|
||||
|
||||
|
||||
puts("----");
|
||||
puts("generate string");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
yajl_gen_array_open(g);
|
||||
for(i=0; i < TASK_STR_LEN; ++i) {
|
||||
yajl_gen_string(g, (const unsigned char*)TASK_STR_PTR, i);
|
||||
}
|
||||
yajl_gen_array_close(g);
|
||||
}
|
||||
show_timer(len);
|
||||
|
||||
yajl_gen_get_buf(g, &buf, &len);
|
||||
|
||||
puts("----");
|
||||
puts("parse string");
|
||||
reset_timer();
|
||||
{
|
||||
yajl_status stat = yajl_parse(h, buf, len);
|
||||
if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
|
||||
unsigned char * str = yajl_get_error(h, 1, buf, len);
|
||||
fprintf(stderr, (const char *) str);
|
||||
}
|
||||
}
|
||||
show_timer(len);
|
||||
|
||||
|
||||
yajl_gen_free(g);
|
||||
yajl_free(h);
|
||||
}
|
||||
|
||||
|
||||
void bench_msgpack(void)
|
||||
{
|
||||
puts("== MessagePack ==");
|
||||
|
||||
|
||||
pack_buffer mpkbuf;
|
||||
pack_buffer_init(&mpkbuf);
|
||||
|
||||
msgpack_pack_t* mpk = msgpack_pack_new(
|
||||
&mpkbuf, pack_append_buffer);
|
||||
|
||||
msgpack_unpack_callback cb = {
|
||||
unpack_uint8,
|
||||
unpack_uint16,
|
||||
unpack_uint32,
|
||||
unpack_uint64,
|
||||
unpack_int8,
|
||||
unpack_int16,
|
||||
unpack_int32,
|
||||
unpack_int64,
|
||||
unpack_float,
|
||||
unpack_double,
|
||||
unpack_nil,
|
||||
unpack_true,
|
||||
unpack_false,
|
||||
unpack_array,
|
||||
unpack_array_item,
|
||||
unpack_map,
|
||||
unpack_map_item,
|
||||
unpack_raw,
|
||||
};
|
||||
msgpack_unpack_t* mupk = msgpack_unpack_new(NULL, &cb);
|
||||
|
||||
|
||||
size_t len;
|
||||
const char* buf;
|
||||
|
||||
|
||||
puts("pack integer");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
msgpack_pack_array(mpk, TASK_INT_NUM);
|
||||
for(i=0; i < TASK_INT_NUM; ++i) {
|
||||
msgpack_pack_unsigned_int(mpk, i);
|
||||
}
|
||||
}
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
|
||||
puts("----");
|
||||
puts("unpack integer");
|
||||
reset_timer();
|
||||
{
|
||||
size_t off = 0;
|
||||
int ret = msgpack_unpack_execute(mupk, buf, len, &off);
|
||||
if(ret < 0) {
|
||||
fprintf(stderr, "Parse error.\n");
|
||||
} else if(ret == 0) {
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
|
||||
pack_buffer_reset(&mpkbuf);
|
||||
msgpack_unpack_reset(mupk);
|
||||
|
||||
|
||||
puts("----");
|
||||
puts("pack string");
|
||||
reset_timer();
|
||||
{
|
||||
unsigned int i;
|
||||
msgpack_pack_array(mpk, TASK_STR_LEN);
|
||||
for(i=0; i < TASK_STR_LEN; ++i) {
|
||||
msgpack_pack_raw(mpk, i);
|
||||
msgpack_pack_raw_body(mpk, TASK_STR_PTR, i);
|
||||
}
|
||||
}
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
len = mpkbuf.length;
|
||||
buf = mpkbuf.buffer;
|
||||
|
||||
puts("----");
|
||||
puts("unpack string");
|
||||
reset_timer();
|
||||
{
|
||||
size_t off = 0;
|
||||
int ret = msgpack_unpack_execute(mupk, buf, len, &off);
|
||||
if(ret < 0) {
|
||||
fprintf(stderr, "Parse error.\n");
|
||||
} else if(ret == 0) {
|
||||
fprintf(stderr, "Not finished.\n");
|
||||
}
|
||||
}
|
||||
show_timer(mpkbuf.length);
|
||||
|
||||
|
||||
msgpack_unpack_free(mupk);
|
||||
msgpack_pack_free(mpk);
|
||||
pack_buffer_free(&mpkbuf);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
char* str = malloc(TASK_STR_LEN);
|
||||
memset(str, 'a', TASK_STR_LEN);
|
||||
TASK_STR_PTR = str;
|
||||
|
||||
bench_msgpack();
|
||||
bench_json();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,9 +0,0 @@
|
||||
|
||||
CFLAGS += -Wall -g -I. -I.. -O4
|
||||
LDFLAGS += -lyajl
|
||||
|
||||
all: bench
|
||||
|
||||
bench: bench.o pack.o unpack.o pack.h unpack.h
|
||||
$(CC) bench.o pack.o unpack.o $(CFLAGS) $(LDFLAGS) -o $@
|
||||
|
44
configure.in
44
configure.in
@@ -1,44 +0,0 @@
|
||||
AC_INIT(msgpack/unpack_template.h)
|
||||
AC_CONFIG_AUX_DIR(ac)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.3.0)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_SUBST(CFLAGS)
|
||||
if test "" = "$CFLAGS"; then
|
||||
CFLAGS="-g -O4"
|
||||
fi
|
||||
|
||||
AC_PROG_CC
|
||||
|
||||
CFLAGS="-O4 -Wall $CFLAGS -I.."
|
||||
|
||||
|
||||
AC_MSG_CHECKING([if c++ api is enabled])
|
||||
AC_ARG_ENABLE(cxx,
|
||||
AS_HELP_STRING([--disable-cxx],
|
||||
[don't build c++ api.]) )
|
||||
AC_MSG_RESULT($enable_cxx)
|
||||
if test "$enable_cxx" != "no"; then
|
||||
AC_SUBST(CXXFLAGS)
|
||||
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
|
||||
AC_PROG_CXX
|
||||
|
||||
CXXFLAGS="-O4 -Wall $CXXFLAGS -I.. -I../c"
|
||||
|
||||
|
||||
AM_CONDITIONAL(ENABLE_CXX, test "$enable_cxx" != "no")
|
||||
|
||||
AC_PROG_LIBTOOL
|
||||
|
||||
AC_OUTPUT([Makefile c/Makefile cpp/Makefile])
|
||||
|
@@ -1,4 +1,4 @@
|
||||
Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
Copyright (C) 2008-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.
|
10
cpp/ChangeLog
Normal file
10
cpp/ChangeLog
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
2010-04-29 version 0.5.0:
|
||||
|
||||
* msgpack_object_type is changed. MSGPACK_OBJECT_NIL is now 0x00.
|
||||
* New safe streaming deserializer API.
|
||||
* Add object::object(const T&) and object::operator=(const T&)
|
||||
* Add operator==(object, const T&)
|
||||
* MSGPACK_DEFINE macro defines msgpack_object(object* obj, zone* z)
|
||||
* C++ programs doesn't need to link "msgpackc" library.
|
||||
|
@@ -1,46 +1,93 @@
|
||||
|
||||
lib_LTLIBRARIES = libmsgpack.la
|
||||
|
||||
libmsgpack_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
vrefbuffer.c \
|
||||
zone.c \
|
||||
object.cpp
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpack_la_LDFLAGS = -version-info 3:0:0
|
||||
|
||||
|
||||
# backward compatibility
|
||||
lib_LTLIBRARIES += libmsgpackc.la
|
||||
|
||||
libmsgpackc_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
vrefbuffer.c \
|
||||
zone.c
|
||||
|
||||
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
|
||||
|
||||
# work around for duplicated file name
|
||||
kumo_manager_CFLAGS = $(AM_CFLAGS)
|
||||
kumo_manager_CXXFLAGS = $(AM_CXXFLAGS)
|
||||
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack/pack_define.h \
|
||||
msgpack/pack_template.h \
|
||||
msgpack/unpack_define.h \
|
||||
msgpack/unpack_template.h \
|
||||
msgpack/sysdep.h \
|
||||
msgpack.h \
|
||||
msgpack/sbuffer.h \
|
||||
msgpack/vrefbuffer.h \
|
||||
msgpack/zbuffer.h \
|
||||
msgpack/pack.h \
|
||||
msgpack/unpack.h \
|
||||
msgpack/object.h \
|
||||
msgpack/zone.h \
|
||||
msgpack.hpp \
|
||||
msgpack/sbuffer.hpp \
|
||||
msgpack/vrefbuffer.hpp \
|
||||
msgpack/zbuffer.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
|
||||
msgpack/type/define.hpp \
|
||||
msgpack/type/tr1/unordered_map.hpp \
|
||||
msgpack/type/tr1/unordered_set.hpp
|
||||
|
||||
|
||||
EXTRA_DIST = \
|
||||
msgpack/type/tuple.hpp.erb \
|
||||
msgpack/zone.hpp.erb
|
||||
README.md \
|
||||
LICENSE \
|
||||
NOTICE \
|
||||
msgpack_vc8.vcproj \
|
||||
msgpack_vc8.sln \
|
||||
msgpack_vc8.postbuild.bat
|
||||
|
||||
libmsgpack_la_LIBADD = -L../c -lmsgpackc
|
||||
SUBDIRS = test
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpack_la_LDFLAGS = -version-info 1:0:0
|
||||
check_PROGRAMS = \
|
||||
msgpackc_test \
|
||||
msgpack_test
|
||||
|
||||
msgpackc_test_SOURCES = msgpackc_test.cpp
|
||||
msgpackc_test_LDADD = libmsgpack.la -lgtest_main
|
||||
|
||||
msgpack_test_SOURCES = msgpack_test.cpp
|
||||
msgpack_test_LDADD = libmsgpack.la -lgtest_main
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
||||
|
78
cpp/README.md
Normal file
78
cpp/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
MessagePack for C/C++
|
||||
=====================
|
||||
Binary-based efficient object serialization library.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Download latest package from [releases of MessagePack](http://sourceforge.net/projects/msgpack/files/) and extract it.
|
||||
|
||||
On UNIX-like platform, run ./configure && make && sudo make install:
|
||||
|
||||
$ ./configure
|
||||
$ make
|
||||
$ sudo make install
|
||||
|
||||
On Windows, open msgpack_vc8.vcproj file and build it using batch build. DLLs are built on lib folder, and the headers are built on include folder.
|
||||
|
||||
To use the library in your program, include msgpack.hpp header and link msgpack and msgpackc library.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
#include <msgpack.hpp>
|
||||
#include <vector>
|
||||
|
||||
int main(void) {
|
||||
// This is target object.
|
||||
std::vector<std::string> target;
|
||||
target.push_back("Hello,");
|
||||
target.push_back("World!");
|
||||
|
||||
// Serialize it.
|
||||
msgpack::sbuffer buffer; // simple buffer
|
||||
msgpack::pack(&buffer, target);
|
||||
|
||||
// Deserialize the serialized data.
|
||||
msgpack::zone mempool; // this manages the life of deserialized object
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(buffer.data, buffer.size, NULL, &mempool, &obj);
|
||||
|
||||
if(ret != msgapck::UNPACK_SUCCESS) {
|
||||
// error check
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Print the deserialized object to stdout.
|
||||
std::cout << obj << std::endl; // ["Hello," "World!"]
|
||||
|
||||
// Convert the deserialized object to staticaly typed object.
|
||||
std::vector<std::string> result;
|
||||
obj.convert(&result);
|
||||
|
||||
// If the type is mismatched, it throws msgpack::type_error.
|
||||
obj.as<int>(); // type is mismatched, msgpack::type_error is thrown
|
||||
}
|
||||
|
||||
API document and other example codes are available at the [wiki.](http://msgpack.sourceforge.net/start)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
Copyright (C) 2008-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.
|
||||
|
||||
See also NOTICE file.
|
||||
|
188
cpp/bench.cpp
188
cpp/bench.cpp
@@ -1,188 +0,0 @@
|
||||
#include <msgpack/unpack.hpp>
|
||||
#include <msgpack/pack.hpp>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
static const unsigned int TASK_INT_NUM = 1<<24;
|
||||
static const unsigned int TASK_STR_LEN = 1<<15;
|
||||
//static const unsigned int TASK_INT_NUM = 1<<22;
|
||||
//static const unsigned int TASK_STR_LEN = 1<<13;
|
||||
static const char* TASK_STR_PTR;
|
||||
|
||||
|
||||
class simple_timer {
|
||||
public:
|
||||
void reset() { gettimeofday(&m_timeval, NULL); }
|
||||
void show_stat(size_t bufsz)
|
||||
{
|
||||
struct timeval endtime;
|
||||
gettimeofday(&endtime, NULL);
|
||||
double sec = (endtime.tv_sec - m_timeval.tv_sec)
|
||||
+ (double)(endtime.tv_usec - m_timeval.tv_usec) / 1000 / 1000;
|
||||
std::cout << sec << " sec" << std::endl;
|
||||
std::cout << (double(bufsz)/1024/1024) << " MB" << std::endl;
|
||||
std::cout << (bufsz/sec/1000/1000*8) << " Mbps" << std::endl;
|
||||
}
|
||||
private:
|
||||
timeval m_timeval;
|
||||
};
|
||||
|
||||
|
||||
class simple_buffer {
|
||||
public:
|
||||
static const size_t DEFAULT_INITIAL_SIZE = 32*1024;//512*1024*1024*2;
|
||||
|
||||
simple_buffer(size_t initial_size = DEFAULT_INITIAL_SIZE) :
|
||||
m_storage((char*)malloc(initial_size)),
|
||||
m_allocated(initial_size),
|
||||
m_used(0)
|
||||
{
|
||||
if(!m_storage) { throw std::bad_alloc(); }
|
||||
}
|
||||
|
||||
~simple_buffer()
|
||||
{
|
||||
free(m_storage);
|
||||
}
|
||||
|
||||
public:
|
||||
inline void write(const char* buf, size_t len)
|
||||
{
|
||||
if(m_allocated - m_used < len) {
|
||||
expand_buffer(len);
|
||||
}
|
||||
memcpy(m_storage + m_used, buf, len);
|
||||
m_used += len;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
m_used = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
void expand_buffer(size_t req)
|
||||
{
|
||||
size_t nsize = m_allocated * 2;
|
||||
size_t at_least = m_used + req;
|
||||
while(nsize < at_least) { nsize *= 2; }
|
||||
char* tmp = (char*)realloc(m_storage, nsize);
|
||||
if(!tmp) { throw std::bad_alloc(); }
|
||||
m_storage = tmp;
|
||||
m_allocated = nsize;
|
||||
}
|
||||
|
||||
public:
|
||||
size_t size() const { return m_used; }
|
||||
const char* data() const { return m_storage; }
|
||||
|
||||
private:
|
||||
char* m_storage;
|
||||
size_t m_allocated;
|
||||
size_t m_used;
|
||||
};
|
||||
|
||||
|
||||
void bench_msgpack_int()
|
||||
{
|
||||
simple_buffer buf;
|
||||
simple_timer timer;
|
||||
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "pack integer" << std::endl;
|
||||
|
||||
timer.reset();
|
||||
{
|
||||
msgpack::packer<simple_buffer> pk(buf);
|
||||
pk.pack_array(TASK_INT_NUM);
|
||||
for(unsigned int i=0; i < TASK_INT_NUM; ++i) {
|
||||
pk.pack_unsigned_int(i);
|
||||
}
|
||||
}
|
||||
timer.show_stat(buf.size());
|
||||
|
||||
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "unpack integer" << std::endl;
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
|
||||
timer.reset();
|
||||
{
|
||||
obj = msgpack::unpack(buf.data(), buf.size(), z);
|
||||
}
|
||||
timer.show_stat(buf.size());
|
||||
|
||||
/*
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "dynamic pack integer" << std::endl;
|
||||
|
||||
buf.clear();
|
||||
|
||||
timer.reset();
|
||||
msgpack::pack(buf, obj);
|
||||
timer.show_stat(buf.size());
|
||||
*/
|
||||
}
|
||||
|
||||
void bench_msgpack_str()
|
||||
{
|
||||
simple_buffer buf;
|
||||
simple_timer timer;
|
||||
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "pack string" << std::endl;
|
||||
|
||||
timer.reset();
|
||||
{
|
||||
msgpack::packer<simple_buffer> pk(buf);
|
||||
pk.pack_array(TASK_STR_LEN);
|
||||
for(unsigned int i=0; i < TASK_STR_LEN; ++i) {
|
||||
pk.pack_raw(i);
|
||||
pk.pack_raw_body(TASK_STR_PTR, i);
|
||||
}
|
||||
}
|
||||
timer.show_stat(buf.size());
|
||||
|
||||
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "unpack string" << std::endl;
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
|
||||
timer.reset();
|
||||
{
|
||||
obj = msgpack::unpack(buf.data(), buf.size(), z);
|
||||
}
|
||||
timer.show_stat(buf.size());
|
||||
|
||||
|
||||
/*
|
||||
std::cout << "----" << std::endl;
|
||||
std::cout << "dynamic pack string" << std::endl;
|
||||
|
||||
buf.clear();
|
||||
|
||||
timer.reset();
|
||||
msgpack::pack(buf, obj);
|
||||
timer.show_stat(buf.size());
|
||||
*/
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char* str = (char*)malloc(TASK_STR_LEN);
|
||||
memset(str, 'a', TASK_STR_LEN);
|
||||
TASK_STR_PTR = str;
|
||||
|
||||
bench_msgpack_int();
|
||||
bench_msgpack_str();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,9 +0,0 @@
|
||||
|
||||
CXXFLAGS += -Wall -g -I. -I.. -O4
|
||||
LDFLAGS +=
|
||||
|
||||
all: bench
|
||||
|
||||
bench: bench.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp
|
||||
$(CXX) bench.o unpack.o zone.o object.o $(CXXFLAGS) $(LDFLAGS) -o $@
|
||||
|
@@ -31,11 +31,17 @@ 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
|
||||
test -f AUTHORS || touch AUTHORS
|
||||
test -f COPYING || touch COPYING
|
||||
test -f ChangeLog || touch ChangeLog
|
||||
test -f NEWS || touch NEWS
|
||||
test -f README || cp -f README.md README
|
||||
|
||||
if ! ./preprocess; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
|
||||
ACLOCAL="aclocal"
|
43
cpp/configure.in
Normal file
43
cpp/configure.in
Normal file
@@ -0,0 +1,43 @@
|
||||
AC_INIT(object.cpp)
|
||||
AC_CONFIG_AUX_DIR(ac)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.5.0)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_SUBST(CFLAGS)
|
||||
CFLAGS="-O4 -Wall $CFLAGS"
|
||||
|
||||
AC_SUBST(CXXFLAGS)
|
||||
CXXFLAGS="-O4 -Wall $CXXFLAGS"
|
||||
|
||||
AC_PROG_CC
|
||||
AC_PROG_CXX
|
||||
|
||||
AC_PROG_LIBTOOL
|
||||
AM_PROG_AS
|
||||
AM_PROG_CC_C_O
|
||||
|
||||
AC_LANG_PUSH([C++])
|
||||
AC_CHECK_HEADERS(tr1/unordered_map)
|
||||
AC_CHECK_HEADERS(tr1/unordered_set)
|
||||
AC_LANG_POP([C++])
|
||||
|
||||
AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [
|
||||
AC_TRY_LINK([
|
||||
int atomic_sub(int i) { return __sync_sub_and_fetch(&i, 1); }
|
||||
int atomic_add(int i) { return __sync_add_and_fetch(&i, 1); }
|
||||
], [], msgpack_cv_atomic_ops="yes")
|
||||
])
|
||||
if test "$msgpack_cv_atomic_ops" != "yes"; then
|
||||
AC_MSG_ERROR([__sync_* atomic operations are not supported.
|
||||
|
||||
Note that gcc < 4.1 is not supported.
|
||||
|
||||
If you are using gcc >= 4.1 and the default target CPU architecture is "i386", try to
|
||||
add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as follows:
|
||||
|
||||
$ ./configure CFLAGS="-march=i686" CXXFLAGS="-march=i686"
|
||||
])
|
||||
fi
|
||||
|
||||
AC_OUTPUT([Makefile test/Makefile])
|
||||
|
@@ -1 +0,0 @@
|
||||
.
|
@@ -20,3 +20,4 @@
|
||||
#include "msgpack/pack.h"
|
||||
#include "msgpack/unpack.h"
|
||||
#include "msgpack/sbuffer.h"
|
||||
#include "msgpack/vrefbuffer.h"
|
@@ -20,4 +20,5 @@
|
||||
#include "msgpack/pack.hpp"
|
||||
#include "msgpack/unpack.hpp"
|
||||
#include "msgpack/sbuffer.hpp"
|
||||
#include "msgpack/vrefbuffer.hpp"
|
||||
#include "msgpack.h"
|
||||
|
@@ -19,9 +19,6 @@
|
||||
#define MSGPACK_OBJECT_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -30,14 +27,14 @@ extern "C" {
|
||||
|
||||
|
||||
typedef enum {
|
||||
MSGPACK_OBJECT_NIL = 0x01,
|
||||
MSGPACK_OBJECT_BOOLEAN = 0x02,
|
||||
MSGPACK_OBJECT_POSITIVE_INTEGER = 0x03,
|
||||
MSGPACK_OBJECT_NEGATIVE_INTEGER = 0x04,
|
||||
MSGPACK_OBJECT_DOUBLE = 0x05,
|
||||
MSGPACK_OBJECT_RAW = 0x06,
|
||||
MSGPACK_OBJECT_ARRAY = 0x07,
|
||||
MSGPACK_OBJECT_MAP = 0x08,
|
||||
MSGPACK_OBJECT_NIL = 0x00,
|
||||
MSGPACK_OBJECT_BOOLEAN = 0x01,
|
||||
MSGPACK_OBJECT_POSITIVE_INTEGER = 0x02,
|
||||
MSGPACK_OBJECT_NEGATIVE_INTEGER = 0x03,
|
||||
MSGPACK_OBJECT_DOUBLE = 0x04,
|
||||
MSGPACK_OBJECT_RAW = 0x05,
|
||||
MSGPACK_OBJECT_ARRAY = 0x06,
|
||||
MSGPACK_OBJECT_MAP = 0x07,
|
||||
} msgpack_object_type;
|
||||
|
||||
|
||||
@@ -82,6 +79,8 @@ typedef struct msgpack_object_kv {
|
||||
|
||||
void msgpack_object_print(FILE* out, msgpack_object o);
|
||||
|
||||
bool msgpack_object_equal(const msgpack_object x, const msgpack_object y);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
@@ -20,7 +20,7 @@
|
||||
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/pack.hpp"
|
||||
#include <stdint.h>
|
||||
#include "msgpack/zone.hpp"
|
||||
#include <string.h>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
@@ -35,14 +35,14 @@ class type_error : public std::bad_cast { };
|
||||
|
||||
namespace type {
|
||||
enum object_type {
|
||||
NIL = 0x01,
|
||||
BOOLEAN = 0x02,
|
||||
POSITIVE_INTEGER = 0x03,
|
||||
NEGATIVE_INTEGER = 0x04,
|
||||
DOUBLE = 0x05,
|
||||
RAW = 0x06,
|
||||
ARRAY = 0x07,
|
||||
MAP = 0x08,
|
||||
NIL = MSGPACK_OBJECT_NIL,
|
||||
BOOLEAN = MSGPACK_OBJECT_BOOLEAN,
|
||||
POSITIVE_INTEGER = MSGPACK_OBJECT_POSITIVE_INTEGER,
|
||||
NEGATIVE_INTEGER = MSGPACK_OBJECT_NEGATIVE_INTEGER,
|
||||
DOUBLE = MSGPACK_OBJECT_DOUBLE,
|
||||
RAW = MSGPACK_OBJECT_RAW,
|
||||
ARRAY = MSGPACK_OBJECT_ARRAY,
|
||||
MAP = MSGPACK_OBJECT_MAP,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -80,23 +80,36 @@ 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);
|
||||
operator msgpack_object();
|
||||
|
||||
object(msgpack_object o);
|
||||
|
||||
template <typename T>
|
||||
explicit object(const T& v);
|
||||
|
||||
template <typename T>
|
||||
object(const T& v, zone* z);
|
||||
|
||||
template <typename T>
|
||||
object& operator=(const T& v);
|
||||
|
||||
operator msgpack_object() const;
|
||||
|
||||
struct with_zone;
|
||||
|
||||
private:
|
||||
struct implicit_type;
|
||||
|
||||
public:
|
||||
implicit_type convert();
|
||||
implicit_type convert() const;
|
||||
};
|
||||
|
||||
struct object_kv {
|
||||
@@ -104,18 +117,44 @@ struct object_kv {
|
||||
object val;
|
||||
};
|
||||
|
||||
struct object::with_zone : object {
|
||||
with_zone(msgpack::zone* zone) : zone(zone) { }
|
||||
msgpack::zone* zone;
|
||||
private:
|
||||
with_zone();
|
||||
};
|
||||
|
||||
|
||||
bool operator==(const object x, const object y);
|
||||
bool operator!=(const object x, const object y);
|
||||
|
||||
template <typename T>
|
||||
bool operator==(const object x, const T& y);
|
||||
|
||||
template <typename T>
|
||||
bool operator==(const T& y, const object x);
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(const object x, const T& y);
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(const T& y, const object x);
|
||||
|
||||
std::ostream& operator<< (std::ostream& s, const object o);
|
||||
|
||||
|
||||
// serialize operator
|
||||
template <typename Stream, typename T>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, const T& v);
|
||||
|
||||
// convert operator
|
||||
template <typename T>
|
||||
T& operator>> (object o, T& v);
|
||||
|
||||
// deconvert operator
|
||||
template <typename T>
|
||||
void operator<< (object::with_zone& o, const T& v);
|
||||
|
||||
|
||||
struct object::implicit_type {
|
||||
implicit_type(object o) : obj(o) { }
|
||||
@@ -129,6 +168,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
// obsolete
|
||||
template <typename Type>
|
||||
class define : public Type {
|
||||
public:
|
||||
@@ -179,45 +219,108 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void operator<< (object::with_zone& o, const T& v)
|
||||
{
|
||||
v.msgpack_object(static_cast<object*>(&o), o.zone);
|
||||
}
|
||||
|
||||
|
||||
inline bool operator==(const object x, const object y)
|
||||
{
|
||||
return msgpack_object_equal(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const object x, const T& y)
|
||||
try {
|
||||
return x == object(y);
|
||||
} catch (msgpack::type_error&) {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool operator!=(const object x, const object y)
|
||||
{ return !(x == y); }
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const T& y, const object x)
|
||||
{ return x == y; }
|
||||
|
||||
inline object::object() { }
|
||||
template <typename T>
|
||||
inline bool operator!=(const object x, const T& y)
|
||||
{ return !(x == y); }
|
||||
|
||||
inline object::object(msgpack_object obj)
|
||||
{
|
||||
// FIXME beter way?
|
||||
::memcpy(this, &obj, sizeof(obj));
|
||||
}
|
||||
|
||||
inline object::operator msgpack_object()
|
||||
{
|
||||
// FIXME beter way?
|
||||
msgpack_object obj;
|
||||
::memcpy(&obj, this, sizeof(obj));
|
||||
return obj;
|
||||
}
|
||||
template <typename T>
|
||||
inline bool operator!=(const T& y, const object x)
|
||||
{ return x != y; }
|
||||
|
||||
|
||||
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)
|
||||
|
||||
inline object::object()
|
||||
{
|
||||
*this >> *v;
|
||||
type = type::NIL;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline object::object(const T& v)
|
||||
{
|
||||
*this << v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline object& object::operator=(const T& v)
|
||||
{
|
||||
*this = object(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
object::object(const T& v, zone* z)
|
||||
{
|
||||
with_zone oz(z);
|
||||
oz << v;
|
||||
type = oz.type;
|
||||
via = oz.via;
|
||||
}
|
||||
|
||||
|
||||
inline object::object(msgpack_object o)
|
||||
{
|
||||
// FIXME beter way?
|
||||
::memcpy(this, &o, sizeof(o));
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, msgpack_object v)
|
||||
{
|
||||
// FIXME beter way?
|
||||
::memcpy(&o, &v, sizeof(v));
|
||||
}
|
||||
|
||||
inline object::operator msgpack_object() const
|
||||
{
|
||||
// FIXME beter way?
|
||||
msgpack_object obj;
|
||||
::memcpy(&obj, this, sizeof(obj));
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
@@ -260,35 +363,15 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
|
||||
return o;
|
||||
|
||||
case type::POSITIVE_INTEGER:
|
||||
if(v.via.u64 <= (uint64_t)std::numeric_limits<uint16_t>::max()) {
|
||||
if(v.via.u64 <= (uint16_t)std::numeric_limits<uint8_t>::max()) {
|
||||
o.pack_uint8(v.via.u64);
|
||||
} else {
|
||||
o.pack_uint16(v.via.u64);
|
||||
}
|
||||
} else {
|
||||
if(v.via.u64 <= (uint64_t)std::numeric_limits<uint32_t>::max()) {
|
||||
o.pack_uint32(v.via.u64);
|
||||
} else {
|
||||
o.pack_uint64(v.via.u64);
|
||||
}
|
||||
}
|
||||
o.pack_uint64(v.via.u64);
|
||||
return o;
|
||||
|
||||
case type::NEGATIVE_INTEGER:
|
||||
if(v.via.i64 >= (int64_t)std::numeric_limits<int16_t>::min()) {
|
||||
if(v.via.i64 >= (int64_t)std::numeric_limits<int8_t>::min()) {
|
||||
o.pack_int8(v.via.i64);
|
||||
} else {
|
||||
o.pack_int16(v.via.i64);
|
||||
}
|
||||
} else {
|
||||
if(v.via.i64 >= (int64_t)std::numeric_limits<int32_t>::min()) {
|
||||
o.pack_int64(v.via.i64);
|
||||
} else {
|
||||
o.pack_int64(v.via.i64);
|
||||
}
|
||||
}
|
||||
o.pack_int64(v.via.i64);
|
||||
return o;
|
||||
|
||||
case type::DOUBLE:
|
||||
o.pack_double(v.via.dec);
|
||||
return o;
|
||||
|
||||
case type::RAW:
|
@@ -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" {
|
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for C++ serializing routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2008-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.
|
||||
@@ -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 {
|
||||
|
||||
@@ -29,6 +28,7 @@ namespace msgpack {
|
||||
template <typename Stream>
|
||||
class packer {
|
||||
public:
|
||||
packer(Stream* s);
|
||||
packer(Stream& s);
|
||||
~packer();
|
||||
|
||||
@@ -40,12 +40,12 @@ public:
|
||||
packer<Stream>& pack_uint16(uint16_t d);
|
||||
packer<Stream>& pack_uint32(uint32_t d);
|
||||
packer<Stream>& pack_uint64(uint64_t d);
|
||||
packer<Stream>& pack_int8(uint8_t d);
|
||||
packer<Stream>& pack_int16(uint16_t d);
|
||||
packer<Stream>& pack_int32(uint32_t d);
|
||||
packer<Stream>& pack_int64(uint64_t d);
|
||||
packer<Stream>& pack_int8(int8_t d);
|
||||
packer<Stream>& pack_int16(int16_t d);
|
||||
packer<Stream>& pack_int32(int32_t d);
|
||||
packer<Stream>& pack_int64(int64_t d);
|
||||
|
||||
packer<Stream>& pack_short(int d);
|
||||
packer<Stream>& pack_short(short d);
|
||||
packer<Stream>& pack_int(int d);
|
||||
packer<Stream>& pack_long(long d);
|
||||
packer<Stream>& pack_long_long(long long d);
|
||||
@@ -112,6 +112,12 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline void pack(Stream* s, const T& v)
|
||||
{
|
||||
packer<Stream>(s).pack(v);
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline void pack(Stream& s, const T& v)
|
||||
{
|
||||
@@ -134,6 +140,9 @@ inline void pack(Stream& s, const T& v)
|
||||
#include "msgpack/pack_template.h"
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
packer<Stream>::packer(Stream* s) : m_stream(*s) { }
|
||||
|
||||
template <typename Stream>
|
||||
packer<Stream>::packer(Stream& s) : m_stream(s) { }
|
||||
|
||||
@@ -157,24 +166,24 @@ inline packer<Stream>& packer<Stream>::pack_uint64(uint64_t d)
|
||||
{ _pack_uint64(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_int8(uint8_t d)
|
||||
inline packer<Stream>& packer<Stream>::pack_int8(int8_t d)
|
||||
{ _pack_int8(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_int16(uint16_t d)
|
||||
inline packer<Stream>& packer<Stream>::pack_int16(int16_t d)
|
||||
{ _pack_int16(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_int32(uint32_t d)
|
||||
inline packer<Stream>& packer<Stream>::pack_int32(int32_t d)
|
||||
{ _pack_int32(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_int64(uint64_t d)
|
||||
inline packer<Stream>& packer<Stream>::pack_int64(int64_t d)
|
||||
{ _pack_int64(m_stream, d); return *this;}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_short(int d)
|
||||
inline packer<Stream>& packer<Stream>::pack_short(short d)
|
||||
{ _pack_short(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
@@ -22,13 +22,14 @@
|
||||
#include <string.h>
|
||||
|
||||
#ifndef MSGPACK_SBUFFER_INIT_SIZE
|
||||
#define MSGPACK_SBUFFER_INIT_SIZE 2048
|
||||
#define MSGPACK_SBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct msgpack_sbuffer {
|
||||
size_t size;
|
||||
char* data;
|
15
cpp/msgpack/type.hpp
Normal file
15
cpp/msgpack/type.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "msgpack/type/bool.hpp"
|
||||
#include "msgpack/type/float.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)
|
||||
{
|
||||
@@ -40,6 +39,15 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const bool& v)
|
||||
return o;
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, bool v)
|
||||
{
|
||||
o.type = type::BOOLEAN;
|
||||
o.via.boolean = v;
|
||||
}
|
||||
|
||||
inline void operator<< (object::with_zone& o, bool v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
117
cpp/msgpack/type/define.hpp.erb
Normal file
117
cpp/msgpack/type/define.hpp.erb
Normal file
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// 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); \
|
||||
}\
|
||||
template <typename MSGPACK_OBJECT> \
|
||||
void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone* z) const \
|
||||
{ \
|
||||
msgpack::type::make_define(__VA_ARGS__).msgpack_object(o, z); \
|
||||
}
|
||||
|
||||
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(); }
|
||||
}
|
||||
void msgpack_object(msgpack::object* o, msgpack::zone* z) const
|
||||
{
|
||||
o->type = type::ARRAY;
|
||||
o->via.array.ptr = NULL;
|
||||
o->via.array.size = 0;
|
||||
}
|
||||
};
|
||||
<%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%>);<%}%>
|
||||
}
|
||||
void msgpack_object(msgpack::object* o, msgpack::zone* z) const
|
||||
{
|
||||
o->type = type::ARRAY;
|
||||
o->via.array.ptr = (object*)z->malloc(sizeof(object)*<%=i+1%>);
|
||||
o->via.array.size = <%=i+1%>;
|
||||
<%0.upto(i) {|j|%>
|
||||
o->via.array.ptr[<%=j%>] = object(a<%=j%>, z);<%}%>
|
||||
}
|
||||
<%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 */
|
||||
|
77
cpp/msgpack/type/deque.hpp
Normal file
77
cpp/msgpack/type/deque.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::deque<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::deque<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // 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)
|
||||
{
|
||||
@@ -59,6 +57,25 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const double& v)
|
||||
}
|
||||
|
||||
|
||||
inline void operator<< (object& o, float v)
|
||||
{
|
||||
o.type = type::DOUBLE;
|
||||
o.via.dec = v;
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, double v)
|
||||
{
|
||||
o.type = type::DOUBLE;
|
||||
o.via.dec = v;
|
||||
}
|
||||
|
||||
inline void operator<< (object::with_zone& o, float v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, double v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/float.hpp */
|
@@ -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;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ namespace detail {
|
||||
throw type_error();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
static inline T convert_integer(object o)
|
||||
{
|
||||
@@ -141,7 +141,71 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long long&
|
||||
{ o.pack_unsigned_long_long(v); return o; }
|
||||
|
||||
|
||||
inline void operator<< (object& o, signed char v)
|
||||
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, signed short v)
|
||||
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, signed int v)
|
||||
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, signed long v)
|
||||
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, signed long long v)
|
||||
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
|
||||
inline void operator<< (object& o, unsigned char v)
|
||||
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, unsigned short v)
|
||||
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, unsigned int v)
|
||||
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, unsigned long v)
|
||||
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
inline void operator<< (object& o, unsigned long long v)
|
||||
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
|
||||
|
||||
|
||||
inline void operator<< (object::with_zone& o, signed char v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, signed short v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, signed int v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, signed long v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, signed long long v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
inline void operator<< (object::with_zone& o, unsigned char v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, unsigned short v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, unsigned int v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, unsigned long v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
inline void operator<< (object::with_zone& o, unsigned long long v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/integer.hpp */
|
||||
#endif /* msgpack/type/int.hpp */
|
||||
|
@@ -15,43 +15,63 @@
|
||||
// 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_LIST_HPP__
|
||||
#define MSGPACK_TYPE_LIST_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> operator>> (object o, std::vector<T>& v)
|
||||
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;
|
||||
T* it = &v.front();
|
||||
typename std::list<T>::iterator it = v.begin();
|
||||
for(; p < pend; ++p, ++it) {
|
||||
p->convert(it);
|
||||
p->convert(&*it);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::list<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
for(typename std::list<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::list<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::list<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/array.hpp */
|
||||
#endif /* msgpack/type/list.hpp */
|
||||
|
@@ -70,6 +70,28 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::assoc_vector<K
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const type::assoc_vector<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = NULL;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename type::assoc_vector<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::map<K, V> operator>> (object o, std::map<K, V>& v)
|
||||
@@ -80,13 +102,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;
|
||||
@@ -104,6 +126,28 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::map<K,V>& v)
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const std::map<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = NULL;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::map<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::multimap<K, V> operator>> (object o, std::multimap<K, V>& v)
|
||||
@@ -132,6 +176,28 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::multimap<K,V>&
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const std::multimap<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = NULL;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::multimap<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
@@ -42,6 +42,14 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::nil& v)
|
||||
return o;
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, type::nil v)
|
||||
{
|
||||
o.type = type::NIL;
|
||||
}
|
||||
|
||||
inline void operator<< (object::with_zone& o, type::nil v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
61
cpp/msgpack/type/pair.hpp
Normal file
61
cpp/msgpack/type/pair.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline void operator<< (object::with_zone& o, const std::pair<T1, T2>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*2);
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = 2;
|
||||
p[0] = object(v.first, o.zone);
|
||||
p[1] = object(v.second, o.zone);
|
||||
}
|
||||
|
||||
|
||||
} // 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)
|
||||
{
|
||||
@@ -87,15 +77,16 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::raw_ref& v)
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
|
||||
inline void operator<< (object& o, const type::raw_ref& v)
|
||||
{
|
||||
o.pack_raw(v.size());
|
||||
o.pack_raw_body(v.data(), v.size());
|
||||
return o;
|
||||
o.type = type::RAW;
|
||||
o.via.raw.ptr = v.ptr;
|
||||
o.via.raw.size = v.size;
|
||||
}
|
||||
|
||||
inline void operator<< (object::with_zone& o, const type::raw_ref& v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
122
cpp/msgpack/type/set.hpp
Normal file
122
cpp/msgpack/type/set.hpp
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// 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 void operator<< (object::with_zone& o, const std::set<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::set<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::multiset<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::multiset<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/set.hpp */
|
||||
|
62
cpp/msgpack/type/string.hpp
Normal file
62
cpp/msgpack/type/string.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
inline void operator<< (object::with_zone& o, const std::string& v)
|
||||
{
|
||||
o.type = type::RAW;
|
||||
char* ptr = (char*)o.zone->malloc(v.size());
|
||||
o.via.raw.ptr = ptr;
|
||||
o.via.raw.size = v.size();
|
||||
memcpy(ptr, v.data(), v.size());
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, const std::string& v)
|
||||
{
|
||||
o.type = type::RAW;
|
||||
o.via.raw.ptr = v.data();
|
||||
o.via.raw.size = v.size();
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/string.hpp */
|
||||
|
129
cpp/msgpack/type/tr1/unordered_map.hpp
Normal file
129
cpp/msgpack/type/tr1/unordered_map.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
//
|
||||
// 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 void operator<< (object::with_zone& o, const std::tr1::unordered_map<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = NULL;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::tr1::unordered_map<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const std::tr1::unordered_multimap<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = NULL;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::tr1::unordered_multimap<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/map.hpp */
|
||||
|
122
cpp/msgpack/type/tr1/unordered_set.hpp
Normal file
122
cpp/msgpack/type/tr1/unordered_set.hpp
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// 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 void operator<< (object::with_zone& o, const std::tr1::unordered_set<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::tr1::unordered_set<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::tr1::unordered_multiset<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::tr1::unordered_multiset<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // 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,6 +165,25 @@ const packer<Stream>& operator<< (
|
||||
}
|
||||
<%}%>
|
||||
|
||||
inline void operator<< (
|
||||
object::with_zone& o,
|
||||
const type::tuple<>& v) {
|
||||
o.type = type::ARRAY;
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
}
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
inline void operator<< (
|
||||
object::with_zone& o,
|
||||
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
|
||||
o.type = type::ARRAY;
|
||||
o.via.array.ptr = (object*)o.zone->malloc(sizeof(object)*<%=i+1%>);
|
||||
o.via.array.size = <%=i+1%>;
|
||||
<%0.upto(i) {|j|%>
|
||||
o.via.array.ptr[<%=j%>] = object(v.template get<<%=j%>>(), o.zone);<%}%>
|
||||
}
|
||||
<%}%>
|
||||
|
||||
} // namespace msgpack
|
||||
|
81
cpp/msgpack/type/vector.hpp
Normal file
81
cpp/msgpack/type/vector.hpp
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// 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_VECTOR_HPP__
|
||||
#define MSGPACK_TYPE_VECTOR_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
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);
|
||||
if(o.via.array.size > 0) {
|
||||
object* p = o.via.array.ptr;
|
||||
object* const pend = o.via.array.ptr + o.via.array.size;
|
||||
T* it = &v[0];
|
||||
do {
|
||||
p->convert(it);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::vector<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = NULL;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::vector<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/vector.hpp */
|
||||
|
@@ -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" {
|
@@ -37,6 +37,31 @@ struct unpack_error : public std::runtime_error {
|
||||
};
|
||||
|
||||
|
||||
class unpacked {
|
||||
public:
|
||||
unpacked() { }
|
||||
|
||||
unpacked(object obj, std::auto_ptr<msgpack::zone> z) :
|
||||
m_obj(obj), m_zone(z) { }
|
||||
|
||||
object& get()
|
||||
{ return m_obj; }
|
||||
|
||||
const object& get() const
|
||||
{ return m_obj; }
|
||||
|
||||
std::auto_ptr<msgpack::zone>& zone()
|
||||
{ return m_zone; }
|
||||
|
||||
const std::auto_ptr<msgpack::zone>& zone() const
|
||||
{ return m_zone; }
|
||||
|
||||
private:
|
||||
object m_obj;
|
||||
std::auto_ptr<msgpack::zone> m_zone;
|
||||
};
|
||||
|
||||
|
||||
class unpacker : public msgpack_unpacker {
|
||||
public:
|
||||
unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE);
|
||||
@@ -53,39 +78,22 @@ public:
|
||||
/*! 3. specify the number of bytes actually copied */
|
||||
void buffer_consumed(size_t size);
|
||||
|
||||
/*! 4. repeat execute() until it retunrs false */
|
||||
bool execute();
|
||||
/*! 4. repeat next() until it retunrs false */
|
||||
bool next(unpacked* result);
|
||||
|
||||
/*! 5.1. if execute() returns true, take out the parsed object */
|
||||
object data();
|
||||
|
||||
/*! 5.2. the object is valid until the zone is deleted */
|
||||
// Note that once release_zone() from unpacker, you must delete it
|
||||
// otherwise the memrory will leak.
|
||||
zone* release_zone();
|
||||
|
||||
/*! 5.2. this method is equivalence to `delete release_zone()` */
|
||||
void reset_zone();
|
||||
|
||||
/*! 5.3. after release_zone(), re-initialize unpacker */
|
||||
void reset();
|
||||
|
||||
/*! 6. check if the size of message doesn't exceed assumption. */
|
||||
/*! 5. check if the size of message doesn't exceed assumption. */
|
||||
size_t message_size() const;
|
||||
|
||||
|
||||
// Basic usage of the unpacker is as following:
|
||||
//
|
||||
// msgpack::unpacker pac;
|
||||
//
|
||||
// while( /* readable */ ) {
|
||||
// while( /* input is readable */ ) {
|
||||
//
|
||||
// // 1.
|
||||
// pac.reserve(1024);
|
||||
// pac.reserve_buffer(32*1024);
|
||||
//
|
||||
// // 2.
|
||||
// ssize_t bytes =
|
||||
// read(the_source, pac.buffer(), pac.buffer_capacity());
|
||||
// size_t bytes = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
//
|
||||
// // error handling ...
|
||||
//
|
||||
@@ -93,25 +101,40 @@ public:
|
||||
// pac.buffer_consumed(bytes);
|
||||
//
|
||||
// // 4.
|
||||
// while(pac.execute()) {
|
||||
// // 5.1
|
||||
// object o = pac.data();
|
||||
// msgpack::unpacked result;
|
||||
// while(pac.next(&result)) {
|
||||
// // do some with the object with the zone.
|
||||
// msgpack::object obj = result.get();
|
||||
// std::auto_ptr<msgpack:zone> z = result.zone();
|
||||
// on_message(obj, z);
|
||||
//
|
||||
// // 5.2
|
||||
// std::auto_ptr<msgpack::zone> olife( pac.release_zone() );
|
||||
// //// boost::shared_ptr is also usable:
|
||||
// // boost::shared_ptr<msgpack::zone> life(z.release());
|
||||
// // on_message(result.get(), life);
|
||||
// }
|
||||
//
|
||||
// // boost::shared_ptr is also usable:
|
||||
// // boost::shared_ptr<msgpack::zone> olife( pac.release_zone() );
|
||||
//
|
||||
// // 5.3
|
||||
// pac.reset();
|
||||
//
|
||||
// // do some with the object with the old zone.
|
||||
// do_something(o, olife);
|
||||
// // 5.
|
||||
// if(pac.message_size() > 10*1024*1024) {
|
||||
// throw std::runtime_error("message is too large");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
/*! for backward compatibility */
|
||||
bool execute();
|
||||
|
||||
/*! for backward compatibility */
|
||||
object data();
|
||||
|
||||
/*! for backward compatibility */
|
||||
zone* release_zone();
|
||||
|
||||
/*! for backward compatibility */
|
||||
void reset_zone();
|
||||
|
||||
/*! for backward compatibility */
|
||||
void reset();
|
||||
|
||||
public:
|
||||
// These functions are usable when non-MessagePack message follows after
|
||||
// MessagePack message.
|
||||
@@ -137,6 +160,11 @@ private:
|
||||
};
|
||||
|
||||
|
||||
static bool unpack(unpacked* result,
|
||||
const char* data, size_t len, size_t* offset = NULL);
|
||||
|
||||
|
||||
// obsolete
|
||||
typedef enum {
|
||||
UNPACK_SUCCESS = 2,
|
||||
UNPACK_EXTRA_BYTES = 1,
|
||||
@@ -144,6 +172,7 @@ typedef enum {
|
||||
UNPACK_PARSE_ERROR = -1,
|
||||
} unpack_return;
|
||||
|
||||
// obsolete
|
||||
static unpack_return unpack(const char* data, size_t len, size_t* off,
|
||||
zone* z, object* result);
|
||||
|
||||
@@ -187,6 +216,27 @@ inline void unpacker::buffer_consumed(size_t size)
|
||||
return msgpack_unpacker_buffer_consumed(this, size);
|
||||
}
|
||||
|
||||
inline bool unpacker::next(unpacked* result)
|
||||
{
|
||||
int ret = msgpack_unpacker_execute(this);
|
||||
|
||||
if(ret < 0) {
|
||||
throw unpack_error("parse error");
|
||||
}
|
||||
|
||||
if(ret == 0) {
|
||||
result->zone().reset();
|
||||
result->get() = object();
|
||||
return false;
|
||||
|
||||
} else {
|
||||
result->zone().reset( release_zone() );
|
||||
result->get() = data();
|
||||
reset();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline bool unpacker::execute()
|
||||
{
|
||||
@@ -230,12 +280,12 @@ inline void unpacker::reset()
|
||||
msgpack_unpacker_reset(this);
|
||||
}
|
||||
|
||||
|
||||
inline size_t unpacker::message_size() const
|
||||
{
|
||||
return msgpack_unpacker_message_size(this);
|
||||
}
|
||||
|
||||
|
||||
inline size_t unpacker::parsed_size() const
|
||||
{
|
||||
return msgpack_unpacker_parsed_size(this);
|
||||
@@ -262,6 +312,38 @@ inline void unpacker::remove_nonparsed_buffer()
|
||||
}
|
||||
|
||||
|
||||
inline bool unpack(unpacked* result,
|
||||
const char* data, size_t len, size_t* offset)
|
||||
{
|
||||
msgpack::object obj;
|
||||
std::auto_ptr<msgpack::zone> z(new zone());
|
||||
|
||||
unpack_return ret = (unpack_return)msgpack_unpack(
|
||||
data, len, offset, z.get(),
|
||||
reinterpret_cast<msgpack_object*>(&obj));
|
||||
|
||||
switch(ret) {
|
||||
case UNPACK_SUCCESS:
|
||||
result->get() = obj;
|
||||
result->zone() = z;
|
||||
return false;
|
||||
|
||||
case UNPACK_EXTRA_BYTES:
|
||||
result->get() = obj;
|
||||
result->zone() = z;
|
||||
return true;
|
||||
|
||||
case UNPACK_CONTINUE:
|
||||
throw unpack_error("insufficient bytes");
|
||||
|
||||
case UNPACK_PARSE_ERROR:
|
||||
default:
|
||||
throw unpack_error("parse error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// obsolete
|
||||
inline unpack_return unpack(const char* data, size_t len, size_t* off,
|
||||
zone* z, object* result)
|
||||
{
|
110
cpp/msgpack/vrefbuffer.h
Normal file
110
cpp/msgpack/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
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
|
||||
#define MSGPACK_VREFBUFFER_REF_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
|
||||
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#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 */
|
||||
|
92
cpp/msgpack/vrefbuffer.hpp
Normal file
92
cpp/msgpack/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 */
|
||||
|
180
cpp/msgpack/zbuffer.h
Normal file
180
cpp/msgpack/zbuffer.h
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* MessagePack for C deflate buffer implementation
|
||||
*
|
||||
* Copyright (C) 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.
|
||||
*/
|
||||
#ifndef MSGPACK_ZBUFFER_H__
|
||||
#define MSGPACK_ZBUFFER_H__
|
||||
|
||||
#include "msgpack/sysdep.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_INIT_SIZE
|
||||
#define MSGPACK_ZBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
|
||||
#define MSGPACK_ZBUFFER_RESERVE_SIZE 512
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct msgpack_zbuffer {
|
||||
z_stream stream;
|
||||
char* data;
|
||||
size_t init_size;
|
||||
} msgpack_zbuffer;
|
||||
|
||||
|
||||
static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf,
|
||||
int level, size_t init_size);
|
||||
static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf);
|
||||
static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline bool msgpack_zbuffer_reset(msgpack_zbuffer* zbuf);
|
||||
static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf);
|
||||
static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf);
|
||||
|
||||
|
||||
static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len);
|
||||
|
||||
static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf);
|
||||
|
||||
|
||||
bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf,
|
||||
int level, size_t init_size)
|
||||
{
|
||||
memset(zbuf, 0, sizeof(msgpack_zbuffer));
|
||||
zbuf->init_size = init_size;
|
||||
if(deflateInit(&zbuf->stream, level) != Z_OK) {
|
||||
free(zbuf->data);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
deflateEnd(&zbuf->stream);
|
||||
free(zbuf->data);
|
||||
}
|
||||
|
||||
bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
size_t used = (char*)zbuf->stream.next_out - zbuf->data;
|
||||
size_t csize = used + zbuf->stream.avail_out;
|
||||
size_t nsize = (csize == 0) ? zbuf->init_size : csize * 2;
|
||||
|
||||
char* tmp = (char*)realloc(zbuf->data, nsize);
|
||||
if(tmp == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
zbuf->data = tmp;
|
||||
zbuf->stream.next_out = (Bytef*)(tmp + used);
|
||||
zbuf->stream.avail_out = nsize - used;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
msgpack_zbuffer* zbuf = (msgpack_zbuffer*)data;
|
||||
|
||||
zbuf->stream.next_in = (Bytef*)buf;
|
||||
zbuf->stream.avail_in = len;
|
||||
|
||||
do {
|
||||
if(zbuf->stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
|
||||
if(!msgpack_zbuffer_expand(zbuf)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if(deflate(&zbuf->stream, Z_NO_FLUSH) != Z_OK) {
|
||||
return -1;
|
||||
}
|
||||
} while(zbuf->stream.avail_in > 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
while(true) {
|
||||
switch(deflate(&zbuf->stream, Z_FINISH)) {
|
||||
case Z_STREAM_END:
|
||||
return zbuf->data;
|
||||
case Z_OK:
|
||||
if(!msgpack_zbuffer_expand(zbuf)) {
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf)
|
||||
{
|
||||
return zbuf->data;
|
||||
}
|
||||
|
||||
size_t msgpack_zbuffer_size(const msgpack_zbuffer* zbuf)
|
||||
{
|
||||
return (char*)zbuf->stream.next_out - zbuf->data;
|
||||
}
|
||||
|
||||
void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
zbuf->stream.avail_out += (char*)zbuf->stream.next_out - zbuf->data;
|
||||
zbuf->stream.next_out = (Bytef*)zbuf->data;
|
||||
}
|
||||
|
||||
bool msgpack_zbuffer_reset(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
if(deflateReset(&zbuf->stream) != Z_OK) {
|
||||
return false;
|
||||
}
|
||||
msgpack_zbuffer_reset_buffer(zbuf);
|
||||
return true;
|
||||
}
|
||||
|
||||
char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
char* tmp = zbuf->data;
|
||||
zbuf->data = NULL;
|
||||
zbuf->stream.next_out = NULL;
|
||||
zbuf->stream.avail_out = 0;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/zbuffer.h */
|
||||
|
100
cpp/msgpack/zbuffer.hpp
Normal file
100
cpp/msgpack/zbuffer.hpp
Normal file
@@ -0,0 +1,100 @@
|
||||
//
|
||||
// MessagePack for C++ deflate buffer implementation
|
||||
//
|
||||
// Copyright (C) 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.
|
||||
//
|
||||
#ifndef MSGPACK_ZBUFFER_HPP__
|
||||
#define MSGPACK_ZBUFFER_HPP__
|
||||
|
||||
#include "msgpack/zbuffer.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
class zbuffer : public msgpack_zbuffer {
|
||||
public:
|
||||
zbuffer(int level = Z_DEFAULT_COMPRESSION,
|
||||
size_t init_size = MSGPACK_ZBUFFER_INIT_SIZE)
|
||||
{
|
||||
msgpack_zbuffer_init(this, level, init_size);
|
||||
}
|
||||
|
||||
~zbuffer()
|
||||
{
|
||||
msgpack_zbuffer_destroy(this);
|
||||
}
|
||||
|
||||
public:
|
||||
void write(const char* buf, unsigned int len)
|
||||
{
|
||||
if(msgpack_zbuffer_write(this, buf, len) < 0) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
char* flush()
|
||||
{
|
||||
char* buf = msgpack_zbuffer_flush(this);
|
||||
if(!buf) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* data()
|
||||
{
|
||||
return base::data;
|
||||
}
|
||||
|
||||
const char* data() const
|
||||
{
|
||||
return base::data;
|
||||
}
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return msgpack_zbuffer_size(this);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
if(!msgpack_zbuffer_reset(this)) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
void reset_buffer()
|
||||
{
|
||||
msgpack_zbuffer_reset_buffer(this);
|
||||
}
|
||||
|
||||
char* release_buffer()
|
||||
{
|
||||
return msgpack_zbuffer_release_buffer(this);
|
||||
}
|
||||
|
||||
private:
|
||||
typedef msgpack_zbuffer base;
|
||||
|
||||
private:
|
||||
zbuffer(const zbuffer&);
|
||||
};
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/zbuffer.hpp */
|
||||
|
@@ -18,45 +18,41 @@
|
||||
#ifndef MSGPACK_ZONE_H__
|
||||
#define MSGPACK_ZONE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "msgpack/sysdep.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
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 +62,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 +79,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);
|
@@ -18,9 +18,9 @@
|
||||
#ifndef MSGPACK_ZONE_HPP__
|
||||
#define MSGPACK_ZONE_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include "msgpack/zone.h"
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
<% GENERATION_LIMIT = 15 %>
|
||||
@@ -34,9 +34,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 +81,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 +97,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 +119,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|%>
|
982
cpp/msgpack_test.cpp
Normal file
982
cpp/msgpack_test.cpp
Normal file
@@ -0,0 +1,982 @@
|
||||
#include "msgpack.hpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <list>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
const unsigned int kLoop = 10000;
|
||||
const unsigned int kElements = 100;
|
||||
const double kEPS = 1e-10;
|
||||
|
||||
#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)
|
||||
|
||||
TEST(MSGPACK, simple_buffer_short)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// STL
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_multimap)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
multimap<int, int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++) {
|
||||
int i1 = rand();
|
||||
val1.insert(make_pair(i1, rand()));
|
||||
val1.insert(make_pair(i1, 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);
|
||||
multimap<int, int> val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
vector<pair<int, int> > v1, v2;
|
||||
multimap<int, int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it)
|
||||
v1.push_back(make_pair(it->first, it->second));
|
||||
for (it = val2.begin(); it != val2.end(); ++it)
|
||||
v2.push_back(make_pair(it->first, it->second));
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(v1.size(), v2.size());
|
||||
sort(v1.begin(), v1.end());
|
||||
sort(v2.begin(), v2.end());
|
||||
EXPECT_TRUE(v1 == v2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_multiset)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
multiset<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);
|
||||
multiset<int> val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
vector<int> v1, v2;
|
||||
multiset<int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it)
|
||||
v1.push_back(*it);
|
||||
for (it = val2.begin(); it != val2.end(); ++it)
|
||||
v2.push_back(*it);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(v1.size(), v2.size());
|
||||
sort(v1.begin(), v1.end());
|
||||
sort(v2.begin(), v2.end());
|
||||
EXPECT_TRUE(v1 == v2);
|
||||
}
|
||||
}
|
||||
|
||||
// TR1
|
||||
|
||||
#ifdef HAVE_TR1_UNORDERED_MAP
|
||||
#include <tr1/unordered_map>
|
||||
#include "msgpack/type/tr1/unordered_map.hpp"
|
||||
TEST(MSGPACK_TR1, simple_buffer_unordered_map)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
tr1::unordered_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);
|
||||
tr1::unordered_map<int, int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
tr1::unordered_map<int, int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it) {
|
||||
EXPECT_TRUE(val2.find(it->first) != val2.end());
|
||||
EXPECT_EQ(it->second, val2.find(it->first)->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_TR1, simple_buffer_unordered_multimap)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
tr1::unordered_multimap<int, int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++) {
|
||||
int i1 = rand();
|
||||
val1.insert(make_pair(i1, rand()));
|
||||
val1.insert(make_pair(i1, 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);
|
||||
tr1::unordered_multimap<int, int> val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
vector<pair<int, int> > v1, v2;
|
||||
tr1::unordered_multimap<int, int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it)
|
||||
v1.push_back(make_pair(it->first, it->second));
|
||||
for (it = val2.begin(); it != val2.end(); ++it)
|
||||
v2.push_back(make_pair(it->first, it->second));
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(v1.size(), v2.size());
|
||||
sort(v1.begin(), v1.end());
|
||||
sort(v2.begin(), v2.end());
|
||||
EXPECT_TRUE(v1 == v2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_TR1_UNORDERED_SET
|
||||
#include <tr1/unordered_set>
|
||||
#include "msgpack/type/tr1/unordered_set.hpp"
|
||||
TEST(MSGPACK_TR1, simple_buffer_unordered_set)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
tr1::unordered_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);
|
||||
tr1::unordered_set<int> val2;
|
||||
obj.convert(&val2);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
tr1::unordered_set<int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it)
|
||||
EXPECT_TRUE(val2.find(*it) != val2.end());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_TR1, simple_buffer_unordered_multiset)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
tr1::unordered_multiset<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);
|
||||
tr1::unordered_multiset<int> val2;
|
||||
obj.convert(&val2);
|
||||
|
||||
vector<int> v1, v2;
|
||||
tr1::unordered_multiset<int>::const_iterator it;
|
||||
for (it = val1.begin(); it != val1.end(); ++it)
|
||||
v1.push_back(*it);
|
||||
for (it = val2.begin(); it != val2.end(); ++it)
|
||||
v2.push_back(*it);
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_EQ(v1.size(), v2.size());
|
||||
sort(v1.begin(), v1.end());
|
||||
sort(v2.begin(), v2.end());
|
||||
EXPECT_TRUE(v1 == v2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// User-Defined Structures
|
||||
|
||||
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);
|
||||
}
|
43
cpp/msgpack_vc8.postbuild.bat
Normal file
43
cpp/msgpack_vc8.postbuild.bat
Normal file
@@ -0,0 +1,43 @@
|
||||
IF NOT EXIST include MKDIR include
|
||||
IF NOT EXIST include\msgpack MKDIR include\msgpack
|
||||
IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type
|
||||
IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1
|
||||
copy msgpack\pack_define.h include\msgpack\
|
||||
copy msgpack\pack_template.h include\msgpack\
|
||||
copy msgpack\unpack_define.h include\msgpack\
|
||||
copy msgpack\unpack_template.h include\msgpack\
|
||||
copy msgpack\sysdep.h include\msgpack\
|
||||
copy msgpack.h include\
|
||||
copy msgpack\sbuffer.h include\msgpack\
|
||||
copy msgpack\vrefbuffer.h include\msgpack\
|
||||
copy msgpack\zbuffer.h include\msgpack\
|
||||
copy msgpack\pack.h include\msgpack\
|
||||
copy msgpack\unpack.h include\msgpack\
|
||||
copy msgpack\object.h include\msgpack\
|
||||
copy msgpack\zone.h include\msgpack\
|
||||
copy msgpack.hpp include\
|
||||
copy msgpack\sbuffer.hpp include\msgpack\
|
||||
copy msgpack\vrefbuffer.hpp include\msgpack\
|
||||
copy msgpack\zbuffer.hpp include\msgpack\
|
||||
copy msgpack\pack.hpp include\msgpack\
|
||||
copy msgpack\unpack.hpp include\msgpack\
|
||||
copy msgpack\object.hpp include\msgpack\
|
||||
copy msgpack\zone.hpp include\msgpack\
|
||||
copy msgpack\type.hpp include\msgpack\type\
|
||||
copy msgpack\type\bool.hpp include\msgpack\type\
|
||||
copy msgpack\type\float.hpp include\msgpack\type\
|
||||
copy msgpack\type\int.hpp include\msgpack\type\
|
||||
copy msgpack\type\list.hpp include\msgpack\type\
|
||||
copy msgpack\type\deque.hpp include\msgpack\type\
|
||||
copy msgpack\type\map.hpp include\msgpack\type\
|
||||
copy msgpack\type\nil.hpp include\msgpack\type\
|
||||
copy msgpack\type\pair.hpp include\msgpack\type\
|
||||
copy msgpack\type\raw.hpp include\msgpack\type\
|
||||
copy msgpack\type\set.hpp include\msgpack\type\
|
||||
copy msgpack\type\string.hpp include\msgpack\type\
|
||||
copy msgpack\type\vector.hpp include\msgpack\type\
|
||||
copy msgpack\type\tuple.hpp include\msgpack\type\
|
||||
copy msgpack\type\define.hpp include\msgpack\type\
|
||||
copy msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
|
||||
copy msgpack\type\tr1\unordered_set.hpp include\msgpack\type\
|
||||
|
20
cpp/msgpack_vc8.sln
Normal file
20
cpp/msgpack_vc8.sln
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 9.00
|
||||
# Visual C++ Express 2008
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MessagePack", "msgpack_vc8.vcproj", "{122A2EA4-B283-4241-9655-786DE78283B2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{122A2EA4-B283-4241-9655-786DE78283B2}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{122A2EA4-B283-4241-9655-786DE78283B2}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{122A2EA4-B283-4241-9655-786DE78283B2}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{122A2EA4-B283-4241-9655-786DE78283B2}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
279
cpp/msgpack_vc8.vcproj
Normal file
279
cpp/msgpack_vc8.vcproj
Normal file
@@ -0,0 +1,279 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8.00"
|
||||
Name="MessagePack"
|
||||
ProjectGUID="{122A2EA4-B283-4241-9655-786DE78283B2}"
|
||||
RootNamespace="MessagePack"
|
||||
Keyword="Win32Proj"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description="Gathering header files"
|
||||
CommandLine="msgpack_vc8.postbuild.bat"
|
||||
Outputs="include"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="."
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="1"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
DebugInformationFormat="4"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="lib\msgpackd.lib"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description="Gathering header files"
|
||||
CommandLine="msgpack_vc8.postbuild.bat"
|
||||
Outputs="include"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
AdditionalIncludeDirectories="."
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
|
||||
RuntimeLibrary="2"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="lib\msgpack.lib"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\objectc.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\unpack.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\vrefbuffer.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\zone.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\object.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\msgpack\pack_define.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\pack_template.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\sysdep.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\unpack_define.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\unpack_template.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
424
cpp/msgpackc_test.cpp
Normal file
424
cpp/msgpackc_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);
|
||||
}
|
@@ -17,8 +17,6 @@
|
||||
//
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
@@ -62,7 +60,6 @@ std::ostream& operator<< (std::ostream& s, const object o)
|
||||
}
|
||||
s << "]";
|
||||
break;
|
||||
// FIXME loop optimiziation
|
||||
|
||||
case type::MAP:
|
||||
s << "{";
|
||||
@@ -77,7 +74,6 @@ std::ostream& operator<< (std::ostream& s, const object o)
|
||||
}
|
||||
s << "}";
|
||||
break;
|
||||
// FIXME loop optimiziation
|
||||
|
||||
default:
|
||||
// FIXME
|
||||
@@ -87,53 +83,5 @@ std::ostream& operator<< (std::ostream& s, const object o)
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const object x, const object y)
|
||||
{
|
||||
if(x.type != y.type) { return false; }
|
||||
|
||||
switch(x.type) {
|
||||
case type::NIL:
|
||||
return true;
|
||||
|
||||
case type::BOOLEAN:
|
||||
return x.via.boolean == y.via.boolean;
|
||||
|
||||
case type::POSITIVE_INTEGER:
|
||||
return x.via.u64 == y.via.u64;
|
||||
|
||||
case type::NEGATIVE_INTEGER:
|
||||
return x.via.i64 == y.via.i64;
|
||||
|
||||
case type::RAW:
|
||||
return x.via.raw.size == y.via.raw.size &&
|
||||
memcmp(x.via.raw.ptr, y.via.raw.ptr, x.via.raw.size) == 0;
|
||||
|
||||
case type::ARRAY:
|
||||
if(x.via.array.size != y.via.array.size) { return false; }
|
||||
for(object* px(x.via.array.ptr),
|
||||
* const pxend(x.via.array.ptr + x.via.array.size),
|
||||
* py(y.via.array.ptr);
|
||||
px < pxend; ++px, ++py) {
|
||||
if(*px != *py) { return false; }
|
||||
}
|
||||
return true;
|
||||
// FIXME loop optimiziation
|
||||
|
||||
case type::MAP:
|
||||
if(x.via.map.size != y.via.map.size) { return false; }
|
||||
for(object_kv* px(x.via.map.ptr),
|
||||
* const pxend(x.via.map.ptr + x.via.map.size),
|
||||
* py(y.via.map.ptr);
|
||||
px < pxend; ++px, ++py) {
|
||||
if(px->key != py->key || px->val != py->val) { return false; }
|
||||
}
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
|
@@ -18,7 +18,18 @@
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/pack.h"
|
||||
#include <stdio.h>
|
||||
#include <string.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)
|
||||
@@ -131,7 +142,6 @@ void msgpack_object_print(FILE* out, msgpack_object o)
|
||||
}
|
||||
fprintf(out, "]");
|
||||
break;
|
||||
// FIXME loop optimiziation
|
||||
|
||||
case MSGPACK_OBJECT_MAP:
|
||||
fprintf(out, "{");
|
||||
@@ -151,7 +161,6 @@ void msgpack_object_print(FILE* out, msgpack_object o)
|
||||
}
|
||||
fprintf(out, "}");
|
||||
break;
|
||||
// FIXME loop optimiziation
|
||||
|
||||
default:
|
||||
// FIXME
|
||||
@@ -159,3 +168,70 @@ void msgpack_object_print(FILE* out, msgpack_object o)
|
||||
}
|
||||
}
|
||||
|
||||
bool msgpack_object_equal(const msgpack_object x, const msgpack_object y)
|
||||
{
|
||||
if(x.type != y.type) { return false; }
|
||||
|
||||
switch(x.type) {
|
||||
case MSGPACK_OBJECT_NIL:
|
||||
return true;
|
||||
|
||||
case MSGPACK_OBJECT_BOOLEAN:
|
||||
return x.via.boolean == y.via.boolean;
|
||||
|
||||
case MSGPACK_OBJECT_POSITIVE_INTEGER:
|
||||
return x.via.u64 == y.via.u64;
|
||||
|
||||
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
|
||||
return x.via.i64 == y.via.i64;
|
||||
|
||||
case MSGPACK_OBJECT_DOUBLE:
|
||||
return x.via.dec == y.via.dec;
|
||||
|
||||
case MSGPACK_OBJECT_RAW:
|
||||
return x.via.raw.size == y.via.raw.size &&
|
||||
memcmp(x.via.raw.ptr, y.via.raw.ptr, x.via.raw.size) == 0;
|
||||
|
||||
case MSGPACK_OBJECT_ARRAY:
|
||||
if(x.via.array.size != y.via.array.size) {
|
||||
return false;
|
||||
} else if(x.via.array.size == 0) {
|
||||
return true;
|
||||
} else {
|
||||
msgpack_object* px = x.via.array.ptr;
|
||||
msgpack_object* const pxend = x.via.array.ptr + x.via.array.size;
|
||||
msgpack_object* py = y.via.array.ptr;
|
||||
do {
|
||||
if(!msgpack_object_equal(*px, *py)) {
|
||||
return false;
|
||||
}
|
||||
++px;
|
||||
++py;
|
||||
} while(px < pxend);
|
||||
return true;
|
||||
}
|
||||
|
||||
case MSGPACK_OBJECT_MAP:
|
||||
if(x.via.map.size != y.via.map.size) {
|
||||
return false;
|
||||
} else if(x.via.map.size == 0) {
|
||||
return true;
|
||||
} else {
|
||||
msgpack_object_kv* px = x.via.map.ptr;
|
||||
msgpack_object_kv* const pxend = x.via.map.ptr + x.via.map.size;
|
||||
msgpack_object_kv* py = y.via.map.ptr;
|
||||
do {
|
||||
if(!msgpack_object_equal(px->key, py->key) || !msgpack_object_equal(px->val, py->val)) {
|
||||
return false;
|
||||
}
|
||||
++px;
|
||||
++py;
|
||||
} while(px < pxend);
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
22
cpp/preprocess
Executable file
22
cpp/preprocess
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/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
|
||||
cp -f ../msgpack/sysdep.h msgpack/
|
||||
cp -f ../msgpack/pack_define.h msgpack/
|
||||
cp -f ../msgpack/pack_template.h msgpack/
|
||||
cp -f ../msgpack/unpack_define.h msgpack/
|
||||
cp -f ../msgpack/unpack_template.h msgpack/
|
||||
|
201
cpp/test.cpp
201
cpp/test.cpp
@@ -1,201 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <msgpack.hpp>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
|
||||
using namespace msgpack;
|
||||
|
||||
class checker {
|
||||
public:
|
||||
template <typename T>
|
||||
void check(const char* d, size_t len, T should) {
|
||||
try {
|
||||
std::cout << "----" << std::endl;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::cout << o << std::endl;
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
28
cpp/test/Makefile.am
Normal file
28
cpp/test/Makefile.am
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
AM_CPPFLAGS = -I..
|
||||
AM_C_CPPFLAGS = -I..
|
||||
AM_LDFLAGS = ../libmsgpack.la -lgtest_main
|
||||
|
||||
check_PROGRAMS = \
|
||||
zone \
|
||||
pack_unpack \
|
||||
streaming \
|
||||
object \
|
||||
convert \
|
||||
buffer
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
||||
zone_SOURCES = zone.cc
|
||||
|
||||
pack_unpack_SOURCES = pack_unpack.cc
|
||||
|
||||
streaming_SOURCES = streaming.cc
|
||||
|
||||
object_SOURCES = object.cc
|
||||
|
||||
convert_SOURCES = convert.cc
|
||||
|
||||
buffer_SOURCES = buffer.cc
|
||||
buffer_LDADD = -lz
|
||||
|
50
cpp/test/buffer.cc
Normal file
50
cpp/test/buffer.cc
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <msgpack/zbuffer.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string.h>
|
||||
|
||||
TEST(buffer, sbuffer)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
sbuf.write("a", 1);
|
||||
sbuf.write("a", 1);
|
||||
sbuf.write("a", 1);
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
}
|
||||
|
||||
|
||||
TEST(buffer, vrefbuffer)
|
||||
{
|
||||
msgpack::vrefbuffer vbuf;
|
||||
vbuf.write("a", 1);
|
||||
vbuf.write("a", 1);
|
||||
vbuf.write("a", 1);
|
||||
|
||||
const struct iovec* vec = vbuf.vector();
|
||||
size_t veclen = vbuf.vector_size();
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
for(size_t i=0; i < veclen; ++i) {
|
||||
sbuf.write((const char*)vec[i].iov_base, vec[i].iov_len);
|
||||
}
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
}
|
||||
|
||||
|
||||
TEST(buffer, zbuffer)
|
||||
{
|
||||
msgpack::zbuffer zbuf;
|
||||
zbuf.write("a", 1);
|
||||
zbuf.write("a", 1);
|
||||
zbuf.write("a", 1);
|
||||
|
||||
zbuf.flush();
|
||||
|
||||
char* data = zbuf.data();
|
||||
size_t size = zbuf.size();
|
||||
}
|
||||
|
74
cpp/test/convert.cc
Normal file
74
cpp/test/convert.cc
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class compatibility {
|
||||
public:
|
||||
compatibility() : str1("default"), str2("default") { }
|
||||
|
||||
std::string str1;
|
||||
std::string str2;
|
||||
|
||||
MSGPACK_DEFINE(str1, str2);
|
||||
};
|
||||
|
||||
TEST(convert, compatibility_less)
|
||||
{
|
||||
std::vector<std::string> src(1);
|
||||
src[0] = "kumofs";
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(src, &z);
|
||||
|
||||
compatibility c;
|
||||
EXPECT_NO_THROW( obj.convert(&c) );
|
||||
|
||||
EXPECT_EQ("kumofs", c.str1);
|
||||
EXPECT_EQ("default", c.str2);
|
||||
}
|
||||
|
||||
TEST(convert, compatibility_more)
|
||||
{
|
||||
std::vector<std::string> src(3);
|
||||
src[0] = "kumofs";
|
||||
src[1] = "mpio";
|
||||
src[2] = "cloudy";
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(src, &z);
|
||||
|
||||
compatibility to;
|
||||
EXPECT_NO_THROW( obj.convert(&to) );
|
||||
|
||||
EXPECT_EQ("kumofs", to.str1);
|
||||
EXPECT_EQ("mpio", to.str2);
|
||||
}
|
||||
|
||||
|
||||
class enum_member {
|
||||
public:
|
||||
enum_member() : flag(A) { }
|
||||
|
||||
enum flags_t {
|
||||
A = 0,
|
||||
B = 1,
|
||||
};
|
||||
|
||||
flags_t flag;
|
||||
|
||||
MSGPACK_DEFINE((int&)flag);
|
||||
};
|
||||
|
||||
TEST(convert, enum_member)
|
||||
{
|
||||
enum_member src;
|
||||
src.flag = enum_member::B;
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(src, &z);
|
||||
|
||||
enum_member to;
|
||||
EXPECT_NO_THROW( obj.convert(&to) );
|
||||
|
||||
EXPECT_EQ(enum_member::B, to.flag);
|
||||
}
|
||||
|
134
cpp/test/object.cc
Normal file
134
cpp/test/object.cc
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
struct myclass {
|
||||
myclass() : num(0), str("default") { }
|
||||
|
||||
myclass(int num, const std::string& str) :
|
||||
num(0), str("default") { }
|
||||
|
||||
~myclass() { }
|
||||
|
||||
int num;
|
||||
std::string str;
|
||||
|
||||
MSGPACK_DEFINE(num, str);
|
||||
|
||||
bool operator==(const myclass& o) const
|
||||
{
|
||||
return num == o.num && str == o.str;
|
||||
}
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& o, const myclass& m)
|
||||
{
|
||||
return o << "myclass("<<m.num<<",\""<<m.str<<"\")";
|
||||
}
|
||||
|
||||
|
||||
TEST(object, convert)
|
||||
{
|
||||
myclass m1;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, m1);
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS);
|
||||
|
||||
myclass m2;
|
||||
obj.convert(&m2);
|
||||
|
||||
EXPECT_EQ(m1, m2);
|
||||
}
|
||||
|
||||
|
||||
TEST(object, as)
|
||||
{
|
||||
myclass m1;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, m1);
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS);
|
||||
|
||||
EXPECT_EQ(m1, obj.as<myclass>());
|
||||
}
|
||||
|
||||
|
||||
TEST(object, print)
|
||||
{
|
||||
msgpack::object obj;
|
||||
std::cout << obj << std::endl;
|
||||
}
|
||||
|
||||
|
||||
TEST(object, is_nil)
|
||||
{
|
||||
msgpack::object obj;
|
||||
EXPECT_TRUE(obj.is_nil());
|
||||
}
|
||||
|
||||
|
||||
TEST(object, type_error)
|
||||
{
|
||||
msgpack::object obj(1);
|
||||
EXPECT_THROW(obj.as<std::string>(), msgpack::type_error);
|
||||
EXPECT_THROW(obj.as<std::vector<int> >(), msgpack::type_error);
|
||||
EXPECT_EQ(1, obj.as<int>());
|
||||
EXPECT_EQ(1, obj.as<short>());
|
||||
EXPECT_EQ(1u, obj.as<unsigned int>());
|
||||
EXPECT_EQ(1u, obj.as<unsigned long>());
|
||||
}
|
||||
|
||||
|
||||
TEST(object, equal_primitive)
|
||||
{
|
||||
msgpack::object obj_nil;
|
||||
EXPECT_EQ(obj_nil, msgpack::object());
|
||||
|
||||
msgpack::object obj_int(1);
|
||||
EXPECT_EQ(obj_int, msgpack::object(1));
|
||||
EXPECT_EQ(obj_int, 1);
|
||||
|
||||
msgpack::object obj_double(1.2);
|
||||
EXPECT_EQ(obj_double, msgpack::object(1.2));
|
||||
EXPECT_EQ(obj_double, 1.2);
|
||||
|
||||
msgpack::object obj_bool(true);
|
||||
EXPECT_EQ(obj_bool, msgpack::object(true));
|
||||
EXPECT_EQ(obj_bool, true);
|
||||
}
|
||||
|
||||
|
||||
TEST(object, construct_primitive)
|
||||
{
|
||||
msgpack::object obj_nil;
|
||||
EXPECT_EQ(msgpack::type::NIL, obj_nil.type);
|
||||
|
||||
msgpack::object obj_uint(1);
|
||||
EXPECT_EQ(msgpack::type::POSITIVE_INTEGER, obj_uint.type);
|
||||
EXPECT_EQ(1u, obj_uint.via.u64);
|
||||
|
||||
msgpack::object obj_int(-1);
|
||||
EXPECT_EQ(msgpack::type::NEGATIVE_INTEGER, obj_int.type);
|
||||
EXPECT_EQ(-1, obj_int.via.i64);
|
||||
|
||||
msgpack::object obj_double(1.2);
|
||||
EXPECT_EQ(msgpack::type::DOUBLE, obj_double.type);
|
||||
EXPECT_EQ(1.2, obj_double.via.dec);
|
||||
|
||||
msgpack::object obj_bool(true);
|
||||
EXPECT_EQ(msgpack::type::BOOLEAN, obj_bool.type);
|
||||
EXPECT_EQ(true, obj_bool.via.boolean);
|
||||
}
|
||||
|
127
cpp/test/pack_unpack.cc
Normal file
127
cpp/test/pack_unpack.cc
Normal file
@@ -0,0 +1,127 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
TEST(pack, num)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, 1);
|
||||
}
|
||||
|
||||
|
||||
TEST(pack, vector)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
std::vector<int> vec;
|
||||
vec.push_back(1);
|
||||
vec.push_back(2);
|
||||
vec.push_back(3);
|
||||
msgpack::pack(sbuf, vec);
|
||||
}
|
||||
|
||||
|
||||
TEST(pack, to_ostream)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
msgpack::pack(stream, 1);
|
||||
}
|
||||
|
||||
|
||||
struct myclass {
|
||||
myclass() : num(0), str("default") { }
|
||||
|
||||
myclass(int num, const std::string& str) :
|
||||
num(0), str("default") { }
|
||||
|
||||
~myclass() { }
|
||||
|
||||
int num;
|
||||
std::string str;
|
||||
|
||||
MSGPACK_DEFINE(num, str);
|
||||
};
|
||||
|
||||
|
||||
TEST(pack, myclass)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
myclass m(1, "msgpack");
|
||||
msgpack::pack(sbuf, m);
|
||||
}
|
||||
|
||||
|
||||
TEST(unpack, myclass)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
myclass m1(1, "phraser");
|
||||
msgpack::pack(sbuf, m1);
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
|
||||
msgpack::unpack_return ret =
|
||||
msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
|
||||
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS);
|
||||
|
||||
myclass m2 = obj.as<myclass>();
|
||||
EXPECT_EQ(m1.num, m2.num);
|
||||
EXPECT_EQ(m1.str, m2.str);
|
||||
}
|
||||
|
||||
|
||||
TEST(unpack, sequence)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, 1);
|
||||
msgpack::pack(sbuf, 2);
|
||||
msgpack::pack(sbuf, 3);
|
||||
|
||||
bool cont;
|
||||
size_t offset = 0;
|
||||
|
||||
msgpack::unpacked msg;
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_TRUE(cont);
|
||||
EXPECT_EQ(1, msg.get().as<int>());
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_TRUE(cont);
|
||||
EXPECT_EQ(2, msg.get().as<int>());
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_FALSE(cont);
|
||||
EXPECT_EQ(3, msg.get().as<int>());
|
||||
}
|
||||
|
||||
|
||||
TEST(unpack, sequence_compat)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, 1);
|
||||
msgpack::pack(sbuf, 2);
|
||||
msgpack::pack(sbuf, 3);
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
msgpack::zone z;
|
||||
msgpack::object obj;
|
||||
msgpack::unpack_return ret;
|
||||
|
||||
ret = msgpack::unpack(sbuf.data(), sbuf.size(), &offset, &z, &obj);
|
||||
EXPECT_TRUE(ret >= 0);
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_EXTRA_BYTES);
|
||||
EXPECT_EQ(1, obj.as<int>());
|
||||
|
||||
ret = msgpack::unpack(sbuf.data(), sbuf.size(), &offset, &z, &obj);
|
||||
EXPECT_TRUE(ret >= 0);
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_EXTRA_BYTES);
|
||||
EXPECT_EQ(2, obj.as<int>());
|
||||
|
||||
ret = msgpack::unpack(sbuf.data(), sbuf.size(), &offset, &z, &obj);
|
||||
EXPECT_TRUE(ret >= 0);
|
||||
EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS);
|
||||
EXPECT_EQ(3, obj.as<int>());
|
||||
}
|
||||
|
213
cpp/test/streaming.cc
Normal file
213
cpp/test/streaming.cc
Normal file
@@ -0,0 +1,213 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
TEST(streaming, basic)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
|
||||
pk.pack(1);
|
||||
pk.pack(2);
|
||||
pk.pack(3);
|
||||
|
||||
std::istringstream input(stream.str());
|
||||
|
||||
msgpack::unpacker pac;
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
msgpack::object obj = result.get();
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(1, obj.as<int>());
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(2, obj.as<int>());
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(3, obj.as<int>());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class event_handler {
|
||||
public:
|
||||
event_handler(std::istream& input) : input(input) { }
|
||||
~event_handler() { }
|
||||
|
||||
void on_read()
|
||||
{
|
||||
while(true) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
|
||||
if(len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
on_message(result.get(), result.zone());
|
||||
}
|
||||
|
||||
if(pac.message_size() > 10*1024*1024) {
|
||||
throw std::runtime_error("message is too large");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(msgpack::object obj, std::auto_ptr<msgpack::zone> z)
|
||||
{
|
||||
EXPECT_EQ(expect, obj.as<int>());
|
||||
}
|
||||
|
||||
int expect;
|
||||
|
||||
private:
|
||||
std::istream& input;
|
||||
msgpack::unpacker pac;
|
||||
};
|
||||
|
||||
TEST(streaming, event)
|
||||
{
|
||||
std::stringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
|
||||
event_handler handler(stream);
|
||||
|
||||
pk.pack(1);
|
||||
handler.expect = 1;
|
||||
handler.on_read();
|
||||
|
||||
pk.pack(2);
|
||||
handler.expect = 2;
|
||||
handler.on_read();
|
||||
|
||||
pk.pack(3);
|
||||
handler.expect = 3;
|
||||
handler.on_read();
|
||||
}
|
||||
|
||||
|
||||
// backward compatibility
|
||||
TEST(streaming, basic_compat)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
|
||||
pk.pack(1);
|
||||
pk.pack(2);
|
||||
pk.pack(3);
|
||||
|
||||
std::istringstream input(stream.str());
|
||||
|
||||
msgpack::unpacker pac;
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
while(pac.execute()) {
|
||||
std::auto_ptr<msgpack::zone> z(pac.release_zone());
|
||||
msgpack::object obj = pac.data();
|
||||
pac.reset();
|
||||
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(1, obj.as<int>());
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(2, obj.as<int>());
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(3, obj.as<int>());
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// backward compatibility
|
||||
class event_handler_compat {
|
||||
public:
|
||||
event_handler_compat(std::istream& input) : input(input) { }
|
||||
~event_handler_compat() { }
|
||||
|
||||
void on_read()
|
||||
{
|
||||
while(true) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
|
||||
if(len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
while(pac.execute()) {
|
||||
std::auto_ptr<msgpack::zone> z(pac.release_zone());
|
||||
msgpack::object obj = pac.data();
|
||||
pac.reset();
|
||||
on_message(obj, z);
|
||||
}
|
||||
|
||||
if(pac.message_size() > 10*1024*1024) {
|
||||
throw std::runtime_error("message is too large");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(msgpack::object obj, std::auto_ptr<msgpack::zone> z)
|
||||
{
|
||||
EXPECT_EQ(expect, obj.as<int>());
|
||||
}
|
||||
|
||||
int expect;
|
||||
|
||||
private:
|
||||
std::istream& input;
|
||||
msgpack::unpacker pac;
|
||||
};
|
||||
|
||||
TEST(streaming, event_compat)
|
||||
{
|
||||
std::stringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
|
||||
event_handler_compat handler(stream);
|
||||
|
||||
pk.pack(1);
|
||||
handler.expect = 1;
|
||||
handler.on_read();
|
||||
|
||||
pk.pack(2);
|
||||
handler.expect = 2;
|
||||
handler.on_read();
|
||||
|
||||
pk.pack(3);
|
||||
handler.expect = 3;
|
||||
handler.on_read();
|
||||
}
|
||||
|
78
cpp/test/zone.cc
Normal file
78
cpp/test/zone.cc
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(zone, malloc)
|
||||
{
|
||||
msgpack::zone z;
|
||||
char* buf1 = (char*)z.malloc(4);
|
||||
memcpy(buf1, "test", 4);
|
||||
char* buf2 = (char*)z.malloc(4);
|
||||
memcpy(buf2, "test", 4);
|
||||
}
|
||||
|
||||
|
||||
class myclass {
|
||||
public:
|
||||
myclass() : num(0), str("default") { }
|
||||
|
||||
myclass(int num, const std::string& str) :
|
||||
num(num), str(str) { }
|
||||
|
||||
~myclass() { }
|
||||
|
||||
int num;
|
||||
std::string str;
|
||||
|
||||
private:
|
||||
myclass(const myclass&);
|
||||
};
|
||||
|
||||
|
||||
TEST(zone, allocate)
|
||||
{
|
||||
msgpack::zone z;
|
||||
myclass* m = z.allocate<myclass>();
|
||||
EXPECT_EQ(m->num, 0);
|
||||
EXPECT_EQ(m->str, "default");
|
||||
}
|
||||
|
||||
|
||||
TEST(zone, allocate_constructor)
|
||||
{
|
||||
msgpack::zone z;
|
||||
myclass* m = z.allocate<myclass>(7, "msgpack");
|
||||
EXPECT_EQ(m->num, 7);
|
||||
EXPECT_EQ(m->str, "msgpack");
|
||||
}
|
||||
|
||||
|
||||
static void custom_finalizer_func(void* user)
|
||||
{
|
||||
myclass* m = (myclass*)user;
|
||||
delete m;
|
||||
}
|
||||
|
||||
TEST(zone, push_finalizer)
|
||||
{
|
||||
msgpack::zone z;
|
||||
myclass* m = new myclass();
|
||||
z.push_finalizer(custom_finalizer_func, (void*)m);
|
||||
}
|
||||
|
||||
|
||||
TEST(zone, push_finalizer_auto_ptr)
|
||||
{
|
||||
msgpack::zone z;
|
||||
std::auto_ptr<myclass> am(new myclass());
|
||||
z.push_finalizer(am);
|
||||
}
|
||||
|
||||
|
||||
TEST(zone, malloc_no_align)
|
||||
{
|
||||
msgpack::zone z;
|
||||
char* buf1 = (char*)z.malloc_no_align(4);
|
||||
char* buf2 = (char*)z.malloc_no_align(4);
|
||||
EXPECT_EQ(buf1+4, buf2);
|
||||
}
|
||||
|
@@ -1,9 +0,0 @@
|
||||
#include "msgpack/type/array.hpp"
|
||||
#include "msgpack/type/boolean.hpp"
|
||||
#include "msgpack/type/float.hpp"
|
||||
#include "msgpack/type/integer.hpp"
|
||||
#include "msgpack/type/map.hpp"
|
||||
#include "msgpack/type/nil.hpp"
|
||||
#include "msgpack/type/raw.hpp"
|
||||
#include "msgpack/type/tuple.hpp"
|
||||
|
@@ -52,7 +52,7 @@ static int template_execute(template_context* ctx,
|
||||
|
||||
|
||||
static inline msgpack_object template_callback_root(unpack_user* u)
|
||||
{ msgpack_object o; return o; }
|
||||
{ msgpack_object o = {}; return o; }
|
||||
|
||||
static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_object* o)
|
||||
{ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; o->via.u64 = d; return 0; }
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -377,14 +377,14 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
|
||||
size_t noff = 0;
|
||||
if(off != NULL) { noff = *off; }
|
||||
|
||||
int ret = template_execute(&ctx, data, len, &noff);
|
||||
if(ret < 0) {
|
||||
int e = template_execute(&ctx, data, len, &noff);
|
||||
if(e < 0) {
|
||||
return MSGPACK_UNPACK_PARSE_ERROR;
|
||||
}
|
||||
|
||||
if(off != NULL) { *off = noff; }
|
||||
|
||||
if(ret == 0) {
|
||||
if(e == 0) {
|
||||
return MSGPACK_UNPACK_CONTINUE;
|
||||
}
|
||||
|
201
cpp/vrefbuffer.c
Normal file
201
cpp/vrefbuffer.c
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
@@ -125,7 +104,6 @@ static inline void init_finalizer_array(msgpack_zone_finalizer_array* fa)
|
||||
|
||||
static inline void call_finalizer_array(msgpack_zone_finalizer_array* fa)
|
||||
{
|
||||
// 逆順に呼び出し
|
||||
msgpack_zone_finalizer* fin = fa->tail;
|
||||
for(; fin != fa->array; --fin) {
|
||||
(*(fin-1)->func)((fin-1)->data);
|
||||
@@ -153,9 +131,6 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
|
||||
|
||||
size_t nnext;
|
||||
if(nused == 0) {
|
||||
// 初回の呼び出し:fa->tail == fa->end == fa->array == NULL
|
||||
|
||||
// glibcは72バイト以下のmallocが高速
|
||||
nnext = (sizeof(msgpack_zone_finalizer) < 72/2) ?
|
||||
72 / sizeof(msgpack_zone_finalizer) : 8;
|
||||
|
||||
@@ -185,19 +160,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 +192,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;
|
||||
}
|
||||
|
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);
|
||||
|
24
haskell/LICENSE
Normal file
24
haskell/LICENSE
Normal file
@@ -0,0 +1,24 @@
|
||||
Copyright (c) 2009, Hideyuki Tanaka
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Hideyuki Tanaka nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY Hideyuki Tanaka ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
3
haskell/Setup.lhs
Normal file
3
haskell/Setup.lhs
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env runhaskell
|
||||
> import Distribution.Simple
|
||||
> main = defaultMain
|
137
haskell/cbits/msgpack.c
Normal file
137
haskell/cbits/msgpack.c
Normal file
@@ -0,0 +1,137 @@
|
||||
#include <msgpack.h>
|
||||
|
||||
void msgpack_sbuffer_init_wrap(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
msgpack_sbuffer_init(sbuf);
|
||||
}
|
||||
|
||||
void msgpack_sbuffer_destroy_wrap(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
msgpack_sbuffer_destroy(sbuf);
|
||||
}
|
||||
|
||||
int msgpack_sbuffer_write_wrap(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
return msgpack_sbuffer_write(data, buf, len);
|
||||
}
|
||||
|
||||
msgpack_packer* msgpack_packer_new_wrap(void *data, msgpack_packer_write callback)
|
||||
{
|
||||
return msgpack_packer_new(data, callback);
|
||||
}
|
||||
|
||||
void msgpack_packer_free_wrap(msgpack_packer* pk)
|
||||
{
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint8_wrap(msgpack_packer* pk, uint8_t d)
|
||||
{
|
||||
return msgpack_pack_uint8(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint16_wrap(msgpack_packer* pk, uint16_t d)
|
||||
{
|
||||
return msgpack_pack_uint16(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint32_wrap(msgpack_packer* pk, uint32_t d)
|
||||
{
|
||||
return msgpack_pack_uint32(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint64_wrap(msgpack_packer* pk, uint64_t d)
|
||||
{
|
||||
return msgpack_pack_uint64(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int8_wrap(msgpack_packer* pk, int8_t d)
|
||||
{
|
||||
return msgpack_pack_int8(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int16_wrap(msgpack_packer* pk, int16_t d)
|
||||
{
|
||||
return msgpack_pack_int16(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int32_wrap(msgpack_packer* pk, int32_t d)
|
||||
{
|
||||
return msgpack_pack_int32(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int64_wrap(msgpack_packer* pk, int64_t d)
|
||||
{
|
||||
return msgpack_pack_int64(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_double_wrap(msgpack_packer* pk, double d)
|
||||
{
|
||||
return msgpack_pack_double(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_nil_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_nil(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_true_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_true(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_false_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_false(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_array_wrap(msgpack_packer* pk, unsigned int n)
|
||||
{
|
||||
return msgpack_pack_array(pk, n);
|
||||
}
|
||||
|
||||
int msgpack_pack_map_wrap(msgpack_packer* pk, unsigned int n)
|
||||
{
|
||||
return msgpack_pack_map(pk, n);
|
||||
}
|
||||
|
||||
int msgpack_pack_raw_wrap(msgpack_packer* pk, size_t l)
|
||||
{
|
||||
return msgpack_pack_raw(pk, l);
|
||||
}
|
||||
|
||||
int msgpack_pack_raw_body_wrap(msgpack_packer* pk, const void *b, size_t l)
|
||||
{
|
||||
return msgpack_pack_raw_body(pk, b, l);
|
||||
}
|
||||
|
||||
bool msgpack_unpacker_reserve_buffer_wrap(msgpack_unpacker *mpac, size_t size)
|
||||
{
|
||||
return msgpack_unpacker_reserve_buffer(mpac, size);
|
||||
}
|
||||
|
||||
char *msgpack_unpacker_buffer_wrap(msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_buffer(mpac);
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_buffer_capacity_wrap(const msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_buffer_capacity(mpac);
|
||||
}
|
||||
|
||||
void msgpack_unpacker_buffer_consumed_wrap(msgpack_unpacker *mpac, size_t size)
|
||||
{
|
||||
msgpack_unpacker_buffer_consumed(mpac, size);
|
||||
}
|
||||
|
||||
void msgpack_unpacker_data_wrap(msgpack_unpacker *mpac, msgpack_object *obj)
|
||||
{
|
||||
*obj=msgpack_unpacker_data(mpac);
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_message_size_wrap(const msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_message_size(mpac);
|
||||
}
|
||||
|
32
haskell/msgpack.cabal
Normal file
32
haskell/msgpack.cabal
Normal file
@@ -0,0 +1,32 @@
|
||||
Name: msgpack
|
||||
Version: 0.2.0
|
||||
License: BSD3
|
||||
License-File: LICENSE
|
||||
Author: Hideyuki Tanaka
|
||||
Maintainer: Hideyuki Tanaka <tanaka.hideyuki@gmail.com>
|
||||
Category: Data
|
||||
Synopsis: A Haskell binding to MessagePack
|
||||
Description:
|
||||
A Haskell binding to MessagePack <http://msgpack.sourceforge.jp/>
|
||||
Homepage: http://github.com/tanakh/hsmsgpack
|
||||
Stability: Experimental
|
||||
Tested-with: GHC==6.10.4
|
||||
Cabal-Version: >=1.2
|
||||
Build-Type: Simple
|
||||
|
||||
library
|
||||
build-depends: base>=4 && <5, mtl, bytestring
|
||||
ghc-options: -O2 -Wall
|
||||
hs-source-dirs: src
|
||||
extra-libraries: msgpackc
|
||||
|
||||
Exposed-modules:
|
||||
Data.MessagePack
|
||||
Data.MessagePack.Base
|
||||
Data.MessagePack.Class
|
||||
Data.MessagePack.Feed
|
||||
Data.MessagePack.Monad
|
||||
Data.MessagePack.Stream
|
||||
|
||||
C-Sources:
|
||||
cbits/msgpack.c
|
63
haskell/src/Data/MessagePack.hs
Normal file
63
haskell/src/Data/MessagePack.hs
Normal file
@@ -0,0 +1,63 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Simple interface to pack and unpack MessagePack data.
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack(
|
||||
module Data.MessagePack.Base,
|
||||
module Data.MessagePack.Class,
|
||||
module Data.MessagePack.Feed,
|
||||
module Data.MessagePack.Monad,
|
||||
module Data.MessagePack.Stream,
|
||||
|
||||
-- * Pack and Unpack
|
||||
packb,
|
||||
unpackb,
|
||||
|
||||
-- * Pure version of Pack and Unpack
|
||||
packb',
|
||||
unpackb',
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import System.IO.Unsafe
|
||||
|
||||
import Data.MessagePack.Base
|
||||
import Data.MessagePack.Class
|
||||
import Data.MessagePack.Feed
|
||||
import Data.MessagePack.Monad
|
||||
import Data.MessagePack.Stream
|
||||
|
||||
-- | Pack Haskell data to MessagePack string.
|
||||
packb :: OBJECT a => a -> IO ByteString
|
||||
packb dat = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
pack pc dat
|
||||
simpleBufferData sb
|
||||
|
||||
-- | Unpack MessagePack string to Haskell data.
|
||||
unpackb :: OBJECT a => ByteString -> IO (Result a)
|
||||
unpackb bs = do
|
||||
withZone $ \z -> do
|
||||
r <- unpackObject z bs
|
||||
return $ case r of
|
||||
Left err -> Left (show err)
|
||||
Right (_, dat) -> fromObject dat
|
||||
|
||||
-- | Pure version of 'packb'.
|
||||
packb' :: OBJECT a => a -> ByteString
|
||||
packb' dat = unsafePerformIO $ packb dat
|
||||
|
||||
-- | Pure version of 'unpackb'.
|
||||
unpackb' :: OBJECT a => ByteString -> Result a
|
||||
unpackb' bs = unsafePerformIO $ unpackb bs
|
581
haskell/src/Data/MessagePack/Base.hsc
Normal file
581
haskell/src/Data/MessagePack/Base.hsc
Normal file
@@ -0,0 +1,581 @@
|
||||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Base
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Low Level Interface to MessagePack C API
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Base(
|
||||
-- * Simple Buffer
|
||||
SimpleBuffer,
|
||||
newSimpleBuffer,
|
||||
simpleBufferData,
|
||||
|
||||
-- * Serializer
|
||||
Packer,
|
||||
newPacker,
|
||||
|
||||
packU8,
|
||||
packU16,
|
||||
packU32,
|
||||
packU64,
|
||||
packS8,
|
||||
packS16,
|
||||
packS32,
|
||||
packS64,
|
||||
|
||||
packTrue,
|
||||
packFalse,
|
||||
|
||||
packInt,
|
||||
packDouble,
|
||||
packNil,
|
||||
packBool,
|
||||
|
||||
packArray,
|
||||
packMap,
|
||||
packRAW,
|
||||
packRAWBody,
|
||||
packRAW',
|
||||
|
||||
-- * Stream Deserializer
|
||||
Unpacker,
|
||||
defaultInitialBufferSize,
|
||||
newUnpacker,
|
||||
unpackerReserveBuffer,
|
||||
unpackerBuffer,
|
||||
unpackerBufferCapacity,
|
||||
unpackerBufferConsumed,
|
||||
unpackerFeed,
|
||||
unpackerExecute,
|
||||
unpackerData,
|
||||
unpackerReleaseZone,
|
||||
unpackerResetZone,
|
||||
unpackerReset,
|
||||
unpackerMessageSize,
|
||||
|
||||
-- * MessagePack Object
|
||||
Object(..),
|
||||
packObject,
|
||||
|
||||
UnpackReturn(..),
|
||||
unpackObject,
|
||||
|
||||
-- * Memory Zone
|
||||
Zone,
|
||||
newZone,
|
||||
freeZone,
|
||||
withZone,
|
||||
) where
|
||||
|
||||
import Control.Exception
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS hiding (pack, unpack)
|
||||
import Data.Int
|
||||
import Data.Word
|
||||
import Foreign.C
|
||||
import Foreign.Concurrent
|
||||
import Foreign.ForeignPtr hiding (newForeignPtr)
|
||||
import Foreign.Marshal.Alloc
|
||||
import Foreign.Marshal.Array
|
||||
import Foreign.Ptr
|
||||
import Foreign.Storable
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
type SimpleBuffer = ForeignPtr ()
|
||||
|
||||
type WriteCallback = Ptr () -> CString -> CUInt -> IO CInt
|
||||
|
||||
-- | Create a new Simple Buffer. It will be deleted automatically.
|
||||
newSimpleBuffer :: IO SimpleBuffer
|
||||
newSimpleBuffer = do
|
||||
ptr <- mallocBytes (#size msgpack_sbuffer)
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_sbuffer_destroy ptr
|
||||
free ptr
|
||||
withForeignPtr fptr $ \p ->
|
||||
msgpack_sbuffer_init p
|
||||
return fptr
|
||||
|
||||
-- | Get data of Simple Buffer.
|
||||
simpleBufferData :: SimpleBuffer -> IO ByteString
|
||||
simpleBufferData sb =
|
||||
withForeignPtr sb $ \ptr -> do
|
||||
size <- (#peek msgpack_sbuffer, size) ptr
|
||||
dat <- (#peek msgpack_sbuffer, data) ptr
|
||||
BS.packCStringLen (dat, fromIntegral (size :: CSize))
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_init_wrap" msgpack_sbuffer_init ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_destroy_wrap" msgpack_sbuffer_destroy ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_write_wrap" msgpack_sbuffer_write ::
|
||||
WriteCallback
|
||||
|
||||
type Packer = ForeignPtr ()
|
||||
|
||||
-- | Create new Packer. It will be deleted automatically.
|
||||
newPacker :: SimpleBuffer -> IO Packer
|
||||
newPacker sbuf = do
|
||||
cb <- wrap_callback msgpack_sbuffer_write
|
||||
ptr <- withForeignPtr sbuf $ \ptr ->
|
||||
msgpack_packer_new ptr cb
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_packer_free ptr
|
||||
return fptr
|
||||
|
||||
foreign import ccall "msgpack_packer_new_wrap" msgpack_packer_new ::
|
||||
Ptr () -> FunPtr WriteCallback -> IO (Ptr ())
|
||||
|
||||
foreign import ccall "msgpack_packer_free_wrap" msgpack_packer_free ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "wrapper" wrap_callback ::
|
||||
WriteCallback -> IO (FunPtr WriteCallback)
|
||||
|
||||
packU8 :: Packer -> Word8 -> IO Int
|
||||
packU8 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint8 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint8_wrap" msgpack_pack_uint8 ::
|
||||
Ptr () -> Word8 -> IO CInt
|
||||
|
||||
packU16 :: Packer -> Word16 -> IO Int
|
||||
packU16 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint16 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint16_wrap" msgpack_pack_uint16 ::
|
||||
Ptr () -> Word16 -> IO CInt
|
||||
|
||||
packU32 :: Packer -> Word32 -> IO Int
|
||||
packU32 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint32 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint32_wrap" msgpack_pack_uint32 ::
|
||||
Ptr () -> Word32 -> IO CInt
|
||||
|
||||
packU64 :: Packer -> Word64 -> IO Int
|
||||
packU64 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint64 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint64_wrap" msgpack_pack_uint64 ::
|
||||
Ptr () -> Word64 -> IO CInt
|
||||
|
||||
packS8 :: Packer -> Int8 -> IO Int
|
||||
packS8 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int8 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int8_wrap" msgpack_pack_int8 ::
|
||||
Ptr () -> Int8 -> IO CInt
|
||||
|
||||
packS16 :: Packer -> Int16 -> IO Int
|
||||
packS16 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int16 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int16_wrap" msgpack_pack_int16 ::
|
||||
Ptr () -> Int16 -> IO CInt
|
||||
|
||||
packS32 :: Packer -> Int32 -> IO Int
|
||||
packS32 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int32 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int32_wrap" msgpack_pack_int32 ::
|
||||
Ptr () -> Int32 -> IO CInt
|
||||
|
||||
packS64 :: Packer -> Int64 -> IO Int
|
||||
packS64 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int64 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int64_wrap" msgpack_pack_int64 ::
|
||||
Ptr () -> Int64 -> IO CInt
|
||||
|
||||
-- | Pack an integral data.
|
||||
packInt :: Integral a => Packer -> a -> IO Int
|
||||
packInt pc n = packS64 pc $ fromIntegral n
|
||||
|
||||
-- | Pack a double data.
|
||||
packDouble :: Packer -> Double -> IO Int
|
||||
packDouble pc d =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_double ptr (realToFrac d)
|
||||
|
||||
foreign import ccall "msgpack_pack_double_wrap" msgpack_pack_double ::
|
||||
Ptr () -> CDouble -> IO CInt
|
||||
|
||||
-- | Pack a nil.
|
||||
packNil :: Packer -> IO Int
|
||||
packNil pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_nil ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_nil_wrap" msgpack_pack_nil ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
packTrue :: Packer -> IO Int
|
||||
packTrue pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_true ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_true_wrap" msgpack_pack_true ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
packFalse :: Packer -> IO Int
|
||||
packFalse pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_false ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_false_wrap" msgpack_pack_false ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
-- | Pack a bool data.
|
||||
packBool :: Packer -> Bool -> IO Int
|
||||
packBool pc True = packTrue pc
|
||||
packBool pc False = packFalse pc
|
||||
|
||||
-- | 'packArray' @p n@ starts packing an array.
|
||||
-- Next @n@ data will consist this array.
|
||||
packArray :: Packer -> Int -> IO Int
|
||||
packArray pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_array ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_array_wrap" msgpack_pack_array ::
|
||||
Ptr () -> CUInt -> IO CInt
|
||||
|
||||
-- | 'packMap' @p n@ starts packing a map.
|
||||
-- Next @n@ pairs of data (2*n data) will consist this map.
|
||||
packMap :: Packer -> Int -> IO Int
|
||||
packMap pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_map ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_map_wrap" msgpack_pack_map ::
|
||||
Ptr () -> CUInt -> IO CInt
|
||||
|
||||
-- | 'packRAW' @p n@ starts packing a byte sequence.
|
||||
-- Next total @n@ bytes of 'packRAWBody' call will consist this sequence.
|
||||
packRAW :: Packer -> Int -> IO Int
|
||||
packRAW pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_raw ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_raw_wrap" msgpack_pack_raw ::
|
||||
Ptr () -> CSize -> IO CInt
|
||||
|
||||
-- | Pack a byte sequence.
|
||||
packRAWBody :: Packer -> ByteString -> IO Int
|
||||
packRAWBody pc bs =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
BS.useAsCStringLen bs $ \(str, len) ->
|
||||
msgpack_pack_raw_body ptr (castPtr str) (fromIntegral len)
|
||||
|
||||
foreign import ccall "msgpack_pack_raw_body_wrap" msgpack_pack_raw_body ::
|
||||
Ptr () -> Ptr () -> CSize -> IO CInt
|
||||
|
||||
-- | Pack a single byte stream. It calls 'packRAW' and 'packRAWBody'.
|
||||
packRAW' :: Packer -> ByteString -> IO Int
|
||||
packRAW' pc bs = do
|
||||
packRAW pc (BS.length bs)
|
||||
packRAWBody pc bs
|
||||
|
||||
type Unpacker = ForeignPtr ()
|
||||
|
||||
defaultInitialBufferSize :: Int
|
||||
defaultInitialBufferSize = 32 * 1024 -- #const MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE
|
||||
|
||||
-- | 'newUnpacker' @initialBufferSize@ creates a new Unpacker. It will be deleted automatically.
|
||||
newUnpacker :: Int -> IO Unpacker
|
||||
newUnpacker initialBufferSize = do
|
||||
ptr <- msgpack_unpacker_new (fromIntegral initialBufferSize)
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_unpacker_free ptr
|
||||
return fptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_new" msgpack_unpacker_new ::
|
||||
CSize -> IO (Ptr ())
|
||||
|
||||
foreign import ccall "msgpack_unpacker_free" msgpack_unpacker_free ::
|
||||
Ptr() -> IO ()
|
||||
|
||||
-- | 'unpackerReserveBuffer' @up size@ reserves at least @size@ bytes of buffer.
|
||||
unpackerReserveBuffer :: Unpacker -> Int -> IO Bool
|
||||
unpackerReserveBuffer up size =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM (/=0) $ msgpack_unpacker_reserve_buffer ptr (fromIntegral size)
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reserve_buffer_wrap" msgpack_unpacker_reserve_buffer ::
|
||||
Ptr () -> CSize -> IO CChar
|
||||
|
||||
-- | Get a pointer of unpacker buffer.
|
||||
unpackerBuffer :: Unpacker -> IO (Ptr CChar)
|
||||
unpackerBuffer up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_buffer ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_wrap" msgpack_unpacker_buffer ::
|
||||
Ptr () -> IO (Ptr CChar)
|
||||
|
||||
-- | Get size of allocated buffer.
|
||||
unpackerBufferCapacity :: Unpacker -> IO Int
|
||||
unpackerBufferCapacity up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_buffer_capacity ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_capacity_wrap" msgpack_unpacker_buffer_capacity ::
|
||||
Ptr () -> IO CSize
|
||||
|
||||
-- | 'unpackerBufferConsumed' @up size@ notices that writed @size@ bytes to buffer.
|
||||
unpackerBufferConsumed :: Unpacker -> Int -> IO ()
|
||||
unpackerBufferConsumed up size =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_buffer_consumed ptr (fromIntegral size)
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_consumed_wrap" msgpack_unpacker_buffer_consumed ::
|
||||
Ptr () -> CSize -> IO ()
|
||||
|
||||
-- | Write byte sequence to Unpacker. It is utility funciton, calls 'unpackerReserveBuffer', 'unpackerBuffer' and 'unpackerBufferConsumed'.
|
||||
unpackerFeed :: Unpacker -> ByteString -> IO ()
|
||||
unpackerFeed up bs =
|
||||
BS.useAsCStringLen bs $ \(str, len) -> do
|
||||
True <- unpackerReserveBuffer up len
|
||||
ptr <- unpackerBuffer up
|
||||
copyArray ptr str len
|
||||
unpackerBufferConsumed up len
|
||||
|
||||
-- | Execute deserializing. It returns 0 when buffer contains not enough bytes, returns 1 when succeeded, returns negative value when it failed.
|
||||
unpackerExecute :: Unpacker -> IO Int
|
||||
unpackerExecute up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_execute ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_execute" msgpack_unpacker_execute ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
-- | Returns a deserialized object when 'unpackerExecute' returned 1.
|
||||
unpackerData :: Unpacker -> IO Object
|
||||
unpackerData up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
allocaBytes (#size msgpack_object) $ \pobj -> do
|
||||
msgpack_unpacker_data ptr pobj
|
||||
peekObject pobj
|
||||
|
||||
foreign import ccall "msgpack_unpacker_data_wrap" msgpack_unpacker_data ::
|
||||
Ptr () -> Ptr () -> IO ()
|
||||
|
||||
-- | Release memory zone. The returned zone must be freed by calling 'freeZone'.
|
||||
unpackerReleaseZone :: Unpacker -> IO Zone
|
||||
unpackerReleaseZone up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_release_zone ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_release_zone" msgpack_unpacker_release_zone ::
|
||||
Ptr () -> IO (Ptr ())
|
||||
|
||||
-- | Free memory zone used by Unapcker.
|
||||
unpackerResetZone :: Unpacker -> IO ()
|
||||
unpackerResetZone up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_reset_zone ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reset_zone" msgpack_unpacker_reset_zone ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
-- | Reset Unpacker state except memory zone.
|
||||
unpackerReset :: Unpacker -> IO ()
|
||||
unpackerReset up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_reset ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reset" msgpack_unpacker_reset ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
-- | Returns number of bytes of sequence of deserializing object.
|
||||
unpackerMessageSize :: Unpacker -> IO Int
|
||||
unpackerMessageSize up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_message_size ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_message_size_wrap" msgpack_unpacker_message_size ::
|
||||
Ptr () -> IO CSize
|
||||
|
||||
type Zone = Ptr ()
|
||||
|
||||
-- | Create a new memory zone. It must be freed manually.
|
||||
newZone :: IO Zone
|
||||
newZone =
|
||||
msgpack_zone_new (#const MSGPACK_ZONE_CHUNK_SIZE)
|
||||
|
||||
-- | Free a memory zone.
|
||||
freeZone :: Zone -> IO ()
|
||||
freeZone z =
|
||||
msgpack_zone_free z
|
||||
|
||||
-- | Create a memory zone, then execute argument, then free memory zone.
|
||||
withZone :: (Zone -> IO a) -> IO a
|
||||
withZone z =
|
||||
bracket newZone freeZone z
|
||||
|
||||
foreign import ccall "msgpack_zone_new" msgpack_zone_new ::
|
||||
CSize -> IO Zone
|
||||
|
||||
foreign import ccall "msgpack_zone_free" msgpack_zone_free ::
|
||||
Zone -> IO ()
|
||||
|
||||
-- | Object Representation of MessagePack data.
|
||||
data Object =
|
||||
ObjectNil
|
||||
| ObjectBool Bool
|
||||
| ObjectInteger Int
|
||||
| ObjectDouble Double
|
||||
| ObjectRAW ByteString
|
||||
| ObjectArray [Object]
|
||||
| ObjectMap [(Object, Object)]
|
||||
deriving (Show)
|
||||
|
||||
peekObject :: Ptr a -> IO Object
|
||||
peekObject ptr = do
|
||||
typ <- (#peek msgpack_object, type) ptr
|
||||
case (typ :: CInt) of
|
||||
(#const MSGPACK_OBJECT_NIL) ->
|
||||
return ObjectNil
|
||||
(#const MSGPACK_OBJECT_BOOLEAN) ->
|
||||
peekObjectBool ptr
|
||||
(#const MSGPACK_OBJECT_POSITIVE_INTEGER) ->
|
||||
peekObjectPositiveInteger ptr
|
||||
(#const MSGPACK_OBJECT_NEGATIVE_INTEGER) ->
|
||||
peekObjectNegativeInteger ptr
|
||||
(#const MSGPACK_OBJECT_DOUBLE) ->
|
||||
peekObjectDouble ptr
|
||||
(#const MSGPACK_OBJECT_RAW) ->
|
||||
peekObjectRAW ptr
|
||||
(#const MSGPACK_OBJECT_ARRAY) ->
|
||||
peekObjectArray ptr
|
||||
(#const MSGPACK_OBJECT_MAP) ->
|
||||
peekObjectMap ptr
|
||||
_ ->
|
||||
fail "peekObject: unknown object type"
|
||||
|
||||
peekObjectBool :: Ptr a -> IO Object
|
||||
peekObjectBool ptr = do
|
||||
b <- (#peek msgpack_object, via.boolean) ptr
|
||||
return $ ObjectBool $ (b :: CUChar) /= 0
|
||||
|
||||
peekObjectPositiveInteger :: Ptr a -> IO Object
|
||||
peekObjectPositiveInteger ptr = do
|
||||
n <- (#peek msgpack_object, via.u64) ptr
|
||||
return $ ObjectInteger $ fromIntegral (n :: Word64)
|
||||
|
||||
peekObjectNegativeInteger :: Ptr a -> IO Object
|
||||
peekObjectNegativeInteger ptr = do
|
||||
n <- (#peek msgpack_object, via.i64) ptr
|
||||
return $ ObjectInteger $ fromIntegral (n :: Int64)
|
||||
|
||||
peekObjectDouble :: Ptr a -> IO Object
|
||||
peekObjectDouble ptr = do
|
||||
d <- (#peek msgpack_object, via.dec) ptr
|
||||
return $ ObjectDouble $ realToFrac (d :: CDouble)
|
||||
|
||||
peekObjectRAW :: Ptr a -> IO Object
|
||||
peekObjectRAW ptr = do
|
||||
size <- (#peek msgpack_object, via.raw.size) ptr
|
||||
p <- (#peek msgpack_object, via.raw.ptr) ptr
|
||||
bs <- BS.packCStringLen (p, fromIntegral (size :: Word32))
|
||||
return $ ObjectRAW bs
|
||||
|
||||
peekObjectArray :: Ptr a -> IO Object
|
||||
peekObjectArray ptr = do
|
||||
size <- (#peek msgpack_object, via.array.size) ptr
|
||||
p <- (#peek msgpack_object, via.array.ptr) ptr
|
||||
objs <- mapM (\i -> peekObject $ p `plusPtr`
|
||||
((#size msgpack_object) * i))
|
||||
[0..size-1]
|
||||
return $ ObjectArray objs
|
||||
|
||||
peekObjectMap :: Ptr a -> IO Object
|
||||
peekObjectMap ptr = do
|
||||
size <- (#peek msgpack_object, via.map.size) ptr
|
||||
p <- (#peek msgpack_object, via.map.ptr) ptr
|
||||
dat <- mapM (\i -> peekObjectKV $ p `plusPtr`
|
||||
((#size msgpack_object_kv) * i))
|
||||
[0..size-1]
|
||||
return $ ObjectMap dat
|
||||
|
||||
peekObjectKV :: Ptr a -> IO (Object, Object)
|
||||
peekObjectKV ptr = do
|
||||
k <- peekObject $ ptr `plusPtr` (#offset msgpack_object_kv, key)
|
||||
v <- peekObject $ ptr `plusPtr` (#offset msgpack_object_kv, val)
|
||||
return (k, v)
|
||||
|
||||
-- | Pack a Object.
|
||||
packObject :: Packer -> Object -> IO ()
|
||||
packObject pc ObjectNil = packNil pc >> return ()
|
||||
|
||||
packObject pc (ObjectBool b) = packBool pc b >> return ()
|
||||
|
||||
packObject pc (ObjectInteger n) = packInt pc n >> return ()
|
||||
|
||||
packObject pc (ObjectDouble d) = packDouble pc d >> return ()
|
||||
|
||||
packObject pc (ObjectRAW bs) = packRAW' pc bs >> return ()
|
||||
|
||||
packObject pc (ObjectArray ls) = do
|
||||
packArray pc (length ls)
|
||||
mapM_ (packObject pc) ls
|
||||
|
||||
packObject pc (ObjectMap ls) = do
|
||||
packMap pc (length ls)
|
||||
mapM_ (\(a, b) -> packObject pc a >> packObject pc b) ls
|
||||
|
||||
data UnpackReturn =
|
||||
UnpackContinue -- ^ not enough bytes to unpack object
|
||||
| UnpackParseError -- ^ got invalid bytes
|
||||
| UnpackError -- ^ other error
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | Unpack a single MessagePack object from byte sequence.
|
||||
unpackObject :: Zone -> ByteString -> IO (Either UnpackReturn (Int, Object))
|
||||
unpackObject z dat =
|
||||
allocaBytes (#size msgpack_object) $ \ptr ->
|
||||
BS.useAsCStringLen dat $ \(str, len) ->
|
||||
alloca $ \poff -> do
|
||||
ret <- msgpack_unpack str (fromIntegral len) poff z ptr
|
||||
case ret of
|
||||
(#const MSGPACK_UNPACK_SUCCESS) -> do
|
||||
off <- peek poff
|
||||
obj <- peekObject ptr
|
||||
return $ Right (fromIntegral off, obj)
|
||||
(#const MSGPACK_UNPACK_EXTRA_BYTES) -> do
|
||||
off <- peek poff
|
||||
obj <- peekObject ptr
|
||||
return $ Right (fromIntegral off, obj)
|
||||
(#const MSGPACK_UNPACK_CONTINUE) ->
|
||||
return $ Left UnpackContinue
|
||||
(#const MSGPACK_UNPACK_PARSE_ERROR) ->
|
||||
return $ Left UnpackParseError
|
||||
_ ->
|
||||
return $ Left UnpackError
|
||||
|
||||
foreign import ccall "msgpack_unpack" msgpack_unpack ::
|
||||
Ptr CChar -> CSize -> Ptr CSize -> Zone -> Ptr () -> IO CInt
|
97
haskell/src/Data/MessagePack/Class.hs
Normal file
97
haskell/src/Data/MessagePack/Class.hs
Normal file
@@ -0,0 +1,97 @@
|
||||
{-# LANGUAGE TypeSynonymInstances #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE OverlappingInstances #-}
|
||||
{-# LANGUAGE IncoherentInstances #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Class
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Serializing Haskell values to and from MessagePack Objects.
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Class(
|
||||
-- * Serialization to and from Object
|
||||
OBJECT(..),
|
||||
Result,
|
||||
pack,
|
||||
) where
|
||||
|
||||
import Control.Monad.Error
|
||||
import Data.ByteString.Char8 (ByteString)
|
||||
import qualified Data.ByteString.Char8 as C8
|
||||
import Data.Either
|
||||
|
||||
import Data.MessagePack.Base
|
||||
|
||||
-- | The class of types serializable to and from MessagePack object
|
||||
class OBJECT a where
|
||||
toObject :: a -> Object
|
||||
fromObject :: Object -> Result a
|
||||
|
||||
-- | A type for parser results
|
||||
type Result a = Either String a
|
||||
|
||||
instance OBJECT Object where
|
||||
toObject = id
|
||||
fromObject = Right
|
||||
|
||||
fromObjectError :: String
|
||||
fromObjectError = "fromObject: cannot cast"
|
||||
|
||||
instance OBJECT Int where
|
||||
toObject = ObjectInteger
|
||||
fromObject (ObjectInteger n) = Right n
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Bool where
|
||||
toObject = ObjectBool
|
||||
fromObject (ObjectBool b) = Right b
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Double where
|
||||
toObject = ObjectDouble
|
||||
fromObject (ObjectDouble d) = Right d
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT ByteString where
|
||||
toObject = ObjectRAW
|
||||
fromObject (ObjectRAW bs) = Right bs
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT String where
|
||||
toObject = toObject . C8.pack
|
||||
fromObject obj = liftM C8.unpack $ fromObject obj
|
||||
|
||||
instance OBJECT a => OBJECT [a] where
|
||||
toObject = ObjectArray . map toObject
|
||||
fromObject (ObjectArray arr) =
|
||||
mapM fromObject arr
|
||||
fromObject _ =
|
||||
Left fromObjectError
|
||||
|
||||
instance (OBJECT a, OBJECT b) => OBJECT [(a, b)] where
|
||||
toObject =
|
||||
ObjectMap . map (\(a, b) -> (toObject a, toObject b))
|
||||
fromObject (ObjectMap mem) = do
|
||||
mapM (\(a, b) -> liftM2 (,) (fromObject a) (fromObject b)) mem
|
||||
fromObject _ =
|
||||
Left fromObjectError
|
||||
|
||||
instance OBJECT a => OBJECT (Maybe a) where
|
||||
toObject (Just a) = toObject a
|
||||
toObject Nothing = ObjectNil
|
||||
|
||||
fromObject ObjectNil = return Nothing
|
||||
fromObject obj = liftM Just $ fromObject obj
|
||||
|
||||
-- | Pack a serializable Haskell value.
|
||||
pack :: OBJECT a => Packer -> a -> IO ()
|
||||
pack pc = packObject pc . toObject
|
59
haskell/src/Data/MessagePack/Feed.hs
Normal file
59
haskell/src/Data/MessagePack/Feed.hs
Normal file
@@ -0,0 +1,59 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Feed
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Feeders for Stream Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Feed(
|
||||
-- * Feeder type
|
||||
Feeder,
|
||||
-- * Feeders
|
||||
feederFromHandle,
|
||||
feederFromFile,
|
||||
feederFromString,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.IORef
|
||||
import System.IO
|
||||
|
||||
-- | Feeder returns Just ByteString when bytes remains, otherwise Nothing.
|
||||
type Feeder = IO (Maybe ByteString)
|
||||
|
||||
-- | Feeder from Handle
|
||||
feederFromHandle :: Handle -> IO Feeder
|
||||
feederFromHandle h = return $ do
|
||||
bs <- BS.hGet h bufSize
|
||||
if BS.length bs > 0
|
||||
then return $ Just bs
|
||||
else do
|
||||
hClose h
|
||||
return Nothing
|
||||
where
|
||||
bufSize = 4096
|
||||
|
||||
-- | Feeder from File
|
||||
feederFromFile :: FilePath -> IO Feeder
|
||||
feederFromFile path =
|
||||
openFile path ReadMode >>= feederFromHandle
|
||||
|
||||
-- | Feeder from ByteString
|
||||
feederFromString :: ByteString -> IO Feeder
|
||||
feederFromString bs = do
|
||||
r <- newIORef (Just bs)
|
||||
return $ f r
|
||||
where
|
||||
f r = do
|
||||
mb <- readIORef r
|
||||
writeIORef r Nothing
|
||||
return mb
|
153
haskell/src/Data/MessagePack/Monad.hs
Normal file
153
haskell/src/Data/MessagePack/Monad.hs
Normal file
@@ -0,0 +1,153 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Monad
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Monadic Stream Serializers and Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Monad(
|
||||
-- * Classes
|
||||
MonadPacker(..),
|
||||
MonadUnpacker(..),
|
||||
|
||||
-- * Packer and Unpacker type
|
||||
PackerT(..),
|
||||
UnpackerT(..),
|
||||
|
||||
-- * Packers
|
||||
packToString,
|
||||
packToHandle,
|
||||
packToFile,
|
||||
|
||||
-- * Unpackers
|
||||
unpackFrom,
|
||||
unpackFromString,
|
||||
unpackFromHandle,
|
||||
unpackFromFile,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Control.Monad.Trans
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import System.IO
|
||||
|
||||
import Data.MessagePack.Base hiding (Unpacker)
|
||||
import qualified Data.MessagePack.Base as Base
|
||||
import Data.MessagePack.Class
|
||||
import Data.MessagePack.Feed
|
||||
|
||||
class Monad m => MonadPacker m where
|
||||
-- | Serialize a object
|
||||
put :: OBJECT a => a -> m ()
|
||||
|
||||
class Monad m => MonadUnpacker m where
|
||||
-- | Deserialize a object
|
||||
get :: OBJECT a => m a
|
||||
|
||||
-- | Serializer Type
|
||||
newtype PackerT m r = PackerT { runPackerT :: Base.Packer -> m r }
|
||||
|
||||
instance Monad m => Monad (PackerT m) where
|
||||
a >>= b =
|
||||
PackerT $ \pc -> do
|
||||
r <- runPackerT a pc
|
||||
runPackerT (b r) pc
|
||||
|
||||
return r =
|
||||
PackerT $ \_ -> return r
|
||||
|
||||
instance MonadTrans PackerT where
|
||||
lift m = PackerT $ \_ -> m
|
||||
|
||||
instance MonadIO m => MonadIO (PackerT m) where
|
||||
liftIO = lift . liftIO
|
||||
|
||||
instance MonadIO m => MonadPacker (PackerT m) where
|
||||
put v = PackerT $ \pc -> liftIO $ do
|
||||
pack pc v
|
||||
|
||||
-- | Execute given serializer and returns byte sequence.
|
||||
packToString :: MonadIO m => PackerT m r -> m ByteString
|
||||
packToString m = do
|
||||
sb <- liftIO $ newSimpleBuffer
|
||||
pc <- liftIO $ newPacker sb
|
||||
runPackerT m pc
|
||||
liftIO $ simpleBufferData sb
|
||||
|
||||
-- | Execcute given serializer and write byte sequence to Handle.
|
||||
packToHandle :: MonadIO m => Handle -> PackerT m r -> m ()
|
||||
packToHandle h m = do
|
||||
sb <- packToString m
|
||||
liftIO $ BS.hPut h sb
|
||||
liftIO $ hFlush h
|
||||
|
||||
-- | Execute given serializer and write byte sequence to file.
|
||||
packToFile :: MonadIO m => FilePath -> PackerT m r -> m ()
|
||||
packToFile p m = do
|
||||
sb <- packToString m
|
||||
liftIO $ BS.writeFile p sb
|
||||
|
||||
-- | Deserializer type
|
||||
newtype UnpackerT m r = UnpackerT { runUnpackerT :: Base.Unpacker -> Feeder -> m r }
|
||||
|
||||
instance Monad m => Monad (UnpackerT m) where
|
||||
a >>= b =
|
||||
UnpackerT $ \up feed -> do
|
||||
r <- runUnpackerT a up feed
|
||||
runUnpackerT (b r) up feed
|
||||
|
||||
return r =
|
||||
UnpackerT $ \_ _ -> return r
|
||||
|
||||
instance MonadTrans UnpackerT where
|
||||
lift m = UnpackerT $ \_ _ -> m
|
||||
|
||||
instance MonadIO m => MonadIO (UnpackerT m) where
|
||||
liftIO = lift . liftIO
|
||||
|
||||
instance MonadIO m => MonadUnpacker (UnpackerT m) where
|
||||
get = UnpackerT $ \up feed -> liftIO $ do
|
||||
resp <- unpackerExecute up
|
||||
guard $ resp>=0
|
||||
when (resp==0) $ do
|
||||
Just bs <- feed
|
||||
unpackerFeed up bs
|
||||
resp2 <- unpackerExecute up
|
||||
guard $ resp2==1
|
||||
obj <- unpackerData up
|
||||
freeZone =<< unpackerReleaseZone up
|
||||
unpackerReset up
|
||||
let Right r = fromObject obj
|
||||
return r
|
||||
|
||||
-- | Execute deserializer using given feeder.
|
||||
unpackFrom :: MonadIO m => Feeder -> UnpackerT m r -> m r
|
||||
unpackFrom f m = do
|
||||
up <- liftIO $ newUnpacker defaultInitialBufferSize
|
||||
runUnpackerT m up f
|
||||
|
||||
-- | Execute deserializer using given handle.
|
||||
unpackFromHandle :: MonadIO m => Handle -> UnpackerT m r -> m r
|
||||
unpackFromHandle h m =
|
||||
flip unpackFrom m =<< liftIO (feederFromHandle h)
|
||||
|
||||
-- | Execute deserializer using given file content.
|
||||
unpackFromFile :: MonadIO m => FilePath -> UnpackerT m r -> m r
|
||||
unpackFromFile p m = do
|
||||
h <- liftIO $ openFile p ReadMode
|
||||
r <- flip unpackFrom m =<< liftIO (feederFromHandle h)
|
||||
liftIO $ hClose h
|
||||
return r
|
||||
|
||||
-- | Execute deserializer from given byte sequence.
|
||||
unpackFromString :: MonadIO m => ByteString -> UnpackerT m r -> m r
|
||||
unpackFromString bs m = do
|
||||
flip unpackFrom m =<< liftIO (feederFromString bs)
|
84
haskell/src/Data/MessagePack/Stream.hs
Normal file
84
haskell/src/Data/MessagePack/Stream.hs
Normal file
@@ -0,0 +1,84 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Stream
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Lazy Stream Serializers and Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Stream(
|
||||
unpackObjects,
|
||||
unpackObjectsFromFile,
|
||||
unpackObjectsFromHandle,
|
||||
unpackObjectsFromString,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import System.IO
|
||||
import System.IO.Unsafe
|
||||
|
||||
import Data.MessagePack.Base
|
||||
import Data.MessagePack.Feed
|
||||
|
||||
-- | Unpack objects using given feeder.
|
||||
unpackObjects :: Feeder -> IO [Object]
|
||||
unpackObjects feeder = do
|
||||
up <- newUnpacker defaultInitialBufferSize
|
||||
f up
|
||||
where
|
||||
f up = unsafeInterleaveIO $ do
|
||||
mbo <- unpackOnce up
|
||||
case mbo of
|
||||
Just o -> do
|
||||
os <- f up
|
||||
return $ o:os
|
||||
Nothing ->
|
||||
return []
|
||||
|
||||
unpackOnce up = do
|
||||
resp <- unpackerExecute up
|
||||
case resp of
|
||||
0 -> do
|
||||
r <- feedOnce up
|
||||
if r
|
||||
then unpackOnce up
|
||||
else return Nothing
|
||||
1 -> do
|
||||
obj <- unpackerData up
|
||||
freeZone =<< unpackerReleaseZone up
|
||||
unpackerReset up
|
||||
return $ Just obj
|
||||
_ ->
|
||||
error $ "unpackerExecute fails: " ++ show resp
|
||||
|
||||
feedOnce up = do
|
||||
dat <- feeder
|
||||
case dat of
|
||||
Nothing ->
|
||||
return False
|
||||
Just bs -> do
|
||||
unpackerFeed up bs
|
||||
return True
|
||||
|
||||
-- | Unpack objects from file.
|
||||
unpackObjectsFromFile :: FilePath -> IO [Object]
|
||||
unpackObjectsFromFile fname =
|
||||
unpackObjects =<< feederFromFile fname
|
||||
|
||||
-- | Unpack objects from handle.
|
||||
unpackObjectsFromHandle :: Handle -> IO [Object]
|
||||
unpackObjectsFromHandle h =
|
||||
unpackObjects =<< feederFromHandle h
|
||||
|
||||
-- | Unpack oobjects from given byte sequence.
|
||||
unpackObjectsFromString :: ByteString -> IO [Object]
|
||||
unpackObjectsFromString bs =
|
||||
unpackObjects =<< feederFromString bs
|
16
haskell/test/Monad.hs
Normal file
16
haskell/test/Monad.hs
Normal file
@@ -0,0 +1,16 @@
|
||||
import Control.Monad.Trans
|
||||
import Data.MessagePack
|
||||
|
||||
main = do
|
||||
sb <- packToString $ do
|
||||
put [1,2,3::Int]
|
||||
put (3.14 :: Double)
|
||||
put "Hoge"
|
||||
|
||||
print sb
|
||||
|
||||
unpackFromString sb $ do
|
||||
arr <- get
|
||||
dbl <- get
|
||||
str <- get
|
||||
liftIO $ print (arr :: [Int], dbl :: Double, str :: String)
|
14
haskell/test/Stream.hs
Normal file
14
haskell/test/Stream.hs
Normal file
@@ -0,0 +1,14 @@
|
||||
import Control.Applicative
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.MessagePack
|
||||
|
||||
main = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
pack pc [1,2,3::Int]
|
||||
pack pc True
|
||||
pack pc "hoge"
|
||||
bs <- simpleBufferData sb
|
||||
|
||||
os <- unpackObjectsFromString bs
|
||||
mapM_ print os
|
36
haskell/test/Test.hs
Normal file
36
haskell/test/Test.hs
Normal file
@@ -0,0 +1,36 @@
|
||||
import Control.Monad
|
||||
import Data.MessagePack
|
||||
|
||||
{-
|
||||
main = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
|
||||
pack pc [(1,2),(2,3),(3::Int,4::Int)]
|
||||
pack pc [4,5,6::Int]
|
||||
pack pc "hoge"
|
||||
|
||||
bs <- simpleBufferData sb
|
||||
print bs
|
||||
|
||||
up <- newUnpacker defaultInitialBufferSize
|
||||
|
||||
unpackerFeed up bs
|
||||
|
||||
let f = do
|
||||
res <- unpackerExecute up
|
||||
when (res==1) $ do
|
||||
obj <- unpackerData up
|
||||
print obj
|
||||
f
|
||||
|
||||
f
|
||||
|
||||
return ()
|
||||
-}
|
||||
|
||||
main = do
|
||||
bs <- packb [(1,2),(2,3),(3::Int,4::Int)]
|
||||
print bs
|
||||
dat <- unpackb bs
|
||||
print (dat :: Result [(Int, Int)])
|
4
java/.gitignore
vendored
Executable file
4
java/.gitignore
vendored
Executable file
@@ -0,0 +1,4 @@
|
||||
target
|
||||
.project
|
||||
.classpath
|
||||
*~
|
5
java/.settings/org.eclipse.jdt.core.prefs
Executable file
5
java/.settings/org.eclipse.jdt.core.prefs
Executable file
@@ -0,0 +1,5 @@
|
||||
#Mon Apr 19 22:18:48 JST 2010
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
24
java/Makefile
Executable file
24
java/Makefile
Executable file
@@ -0,0 +1,24 @@
|
||||
|
||||
.PHONY: compile test eclipse clean package
|
||||
|
||||
all:
|
||||
compile
|
||||
|
||||
package:
|
||||
mvn package
|
||||
|
||||
install:
|
||||
mvn install
|
||||
|
||||
compile:
|
||||
mvn compile
|
||||
|
||||
test:
|
||||
mvn test
|
||||
|
||||
# generate .project and .classpath file for Eclipse
|
||||
eclipse:
|
||||
mvn eclipse:eclipse
|
||||
|
||||
clean:
|
||||
mvn clean
|
28
java/README
Executable file
28
java/README
Executable file
@@ -0,0 +1,28 @@
|
||||
|
||||
To build the JAR file of Message Pack, you need to install Maven (http://maven.apache.org), then type the following command:
|
||||
|
||||
$ mvn package
|
||||
|
||||
To locally install the project, type
|
||||
$ mvn install
|
||||
|
||||
To generate project files (.project, .classpath) for Eclipse, do
|
||||
|
||||
$ mvn eclipse:eclipse
|
||||
|
||||
then import the folder from your Eclipse.
|
||||
|
||||
Next, open the preference page in Eclipse and add the CLASSPATH variable:
|
||||
|
||||
M2_REPO = $HOME/.m2/repository
|
||||
|
||||
where $HOME is your home directory. In Windows XP, $HOME is:
|
||||
C:/Documents and Settings/(user name)/.m2/repository
|
||||
|
||||
|
||||
# How to release the project (compile, test, tagging, deploy)
|
||||
|
||||
$ mvn release:prepare
|
||||
$ mvn release:perform
|
||||
|
||||
|
303
java/build.xml
Executable file
303
java/build.xml
Executable file
@@ -0,0 +1,303 @@
|
||||
<project name="MessagePack for Java" default="jar"
|
||||
xmlns:ivy="antlib:org.apache.ivy.ant"
|
||||
xmlns:mvn="urn:maven-artifact-ant">
|
||||
|
||||
<property name="Name" value="MessagePack"/>
|
||||
<property name="name" value="msgpack"/>
|
||||
<property name="version" value="0.0.1"/>
|
||||
<property name="fullname" value="${name}-${version}"/>
|
||||
<property name="year" value="2010"/>
|
||||
|
||||
<!-- Load user's default properties. -->
|
||||
<property file="${user.home}/build.properties" />
|
||||
|
||||
<property name="src.dir" value="${basedir}/src/main/java"/>
|
||||
<property name="java.src.dir" value="${src.dir}/"/>
|
||||
<property name="build.dir" value="${basedir}/build"/>
|
||||
<property name="lib.dir" value="${basedir}/lib"/>
|
||||
<property name="dist.dir" value="${basedir}/dist"/>
|
||||
|
||||
<property name="build.classes" value="${build.dir}/classes"/>
|
||||
<property name="build.doc" value="${build.dir}/doc"/>
|
||||
<property name="build.javadoc" value="${build.doc}/api/"/>
|
||||
<property name="build.javadoc.log" value="${build.dir}/javadoc.log"/>
|
||||
|
||||
<property name="test.count" value="100"/>
|
||||
<property name="test.junit.output.format" value="plain"/>
|
||||
<property name="test.java.src.dir" value="${basedir}/src/test/java"/>
|
||||
<property name="test.java.build.dir" value="${build.dir}/test"/>
|
||||
<property name="test.java.classes" value="${test.java.build.dir}/classes"/>
|
||||
<property name="test.java.include" value="Test*"/>
|
||||
|
||||
<property name="javac.encoding" value="ISO-8859-1"/>
|
||||
<property name="javac.debug" value="on"/>
|
||||
<property name="javac.optimize" value="on"/>
|
||||
<property name="javac.deprecation" value="off"/>
|
||||
<property name="javac.version" value="1.6"/>
|
||||
<property name="javac.args" value=""/>
|
||||
<property name="javac.args.warnings" value="-Xlint:unchecked"/>
|
||||
|
||||
<property name="javadoc.link.java"
|
||||
value="http://java.sun.com/javase/6/docs/api/"/>
|
||||
<property name="javadoc.packages" value="org.${name}.*"/>
|
||||
|
||||
<!-- ivy settings -->
|
||||
<property name="ivy.version" value="2.1.0"/>
|
||||
<property name="ivy.url"
|
||||
value="http://repo2.maven.org/maven2/org/apache/ivy/ivy" />
|
||||
<property name="ivy.home" value="${user.home}/.ant" />
|
||||
<property name="ivy.lib" value="${build.dir}/lib"/>
|
||||
<property name="ivy.test.lib" value="${build.dir}/test/lib"/>
|
||||
<property name="mvn.repo"
|
||||
value="https://repository.apache.org/content/repositories/snapshots"/>
|
||||
|
||||
<!-- the normal classpath -->
|
||||
<path id="libs">
|
||||
<fileset dir="${ivy.lib}">
|
||||
<include name="**/*.jar" />
|
||||
</fileset>
|
||||
</path>
|
||||
<path id="java.classpath">
|
||||
<pathelement location="${build.classes}"/>
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="**/*.jar" />
|
||||
<exclude name="**/excluded/" />
|
||||
</fileset>
|
||||
<fileset dir="${ant.home}/lib">
|
||||
<include name="ant.jar" />
|
||||
</fileset>
|
||||
<path refid="libs" />
|
||||
</path>
|
||||
<path id="test.libs">
|
||||
<fileset dir="${ivy.test.lib}">
|
||||
<include name="**/*.jar" />
|
||||
</fileset>
|
||||
</path>
|
||||
<path id="test.java.classpath">
|
||||
<pathelement location="${test.java.classes}" />
|
||||
<path refid="java.classpath"/>
|
||||
<path refid="test.libs"/>
|
||||
</path>
|
||||
|
||||
<!-- init & clean -->
|
||||
<target name="init">
|
||||
<mkdir dir="${build.dir}" />
|
||||
<mkdir dir="${lib.dir}" />
|
||||
<mkdir dir="${build.classes}" />
|
||||
|
||||
<mkdir dir="${test.java.build.dir}"/>
|
||||
<mkdir dir="${test.java.classes}"/>
|
||||
|
||||
<mkdir dir="${ivy.lib}"/>
|
||||
<mkdir dir="${ivy.test.lib}"/>
|
||||
<condition property="ivy.jar.exists">
|
||||
<available file="${lib.dir}/ivy-${ivy.version}.jar"/>
|
||||
</condition>
|
||||
</target>
|
||||
<target name="clean">
|
||||
<delete dir="${build.dir}" />
|
||||
</target>
|
||||
|
||||
<!-- ivy targets -->
|
||||
<target name="ivy-download" unless="ivy.jar.exists" depends="init">
|
||||
<delete dir="${lib.dir}"
|
||||
includes="ivy-*.jar" excludes="ivy-${ivy.version}.jar"/>
|
||||
<get src="${ivy.url}/${ivy.version}/ivy-${ivy.version}.jar"
|
||||
dest="${lib.dir}/ivy-${ivy.version}.jar" usetimestamp="true"/>
|
||||
</target>
|
||||
<target name="ivy-init" depends="ivy-download" unless="ivy.initialized">
|
||||
<taskdef resource="org/apache/ivy/ant/antlib.xml"
|
||||
uri="antlib:org.apache.ivy.ant" classpathref="java.classpath"/>
|
||||
<!-- ensure that ivy taskdef is only run once, otw ant will error -->
|
||||
<property name="ivy.initialized" value="true"/>
|
||||
</target>
|
||||
|
||||
<target name="ivy-retrieve-build" depends="init,ivy-init">
|
||||
<ivy:retrieve type="jar" conf="build"
|
||||
pattern="${ivy.lib}/[artifact]-[revision].[ext]"/>
|
||||
</target>
|
||||
<target name="ivy-retrieve-test" depends="init,ivy-init">
|
||||
<ivy:retrieve type="jar" conf="test"
|
||||
pattern="${ivy.test.lib}/[artifact]-[revision].[ext]"/>
|
||||
</target>
|
||||
|
||||
<!-- compiler -->
|
||||
<macrodef name="java-compiler">
|
||||
<attribute name="dest" default="${build.classes}"/>
|
||||
<attribute name="includes" default="**/*.java"/>
|
||||
<attribute name="excludes" default=""/>
|
||||
<attribute name="classpath" default="java.classpath"/>
|
||||
<element name="src" implicit="yes"/>
|
||||
<sequential>
|
||||
<javac
|
||||
destdir="@{dest}"
|
||||
includes="@{includes}"
|
||||
excludes="@{excludes}"
|
||||
encoding="${javac.encoding}"
|
||||
debug="${javac.debug}"
|
||||
optimize="${javac.optimize}"
|
||||
target="${javac.version}"
|
||||
source="${javac.version}"
|
||||
deprecation="${javac.deprecation}">
|
||||
<compilerarg line="${javac.args} ${javac.args.warnings}" />
|
||||
<classpath refid="@{classpath}"/>
|
||||
<src />
|
||||
</javac>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<!-- compile -->
|
||||
<target name="compile" depends="init,ivy-retrieve-build">
|
||||
<java-compiler>
|
||||
<src path="${java.src.dir}"/>
|
||||
</java-compiler>
|
||||
</target>
|
||||
|
||||
<!-- test -->
|
||||
<macrodef name="test-runner">
|
||||
<attribute name="files.location" />
|
||||
<attribute name="tests.pattern" />
|
||||
<attribute name="test.dir" default="${test.java.build.dir}" />
|
||||
<sequential>
|
||||
<junit showoutput="yes"
|
||||
printsummary="withOutAndErr"
|
||||
haltonfailure="no"
|
||||
fork="yes" forkMode="once"
|
||||
errorProperty="tests.failed" failureProperty="tests.failed">
|
||||
<sysproperty key="test.count" value="${test.count}"/>
|
||||
<sysproperty key="test.dir" value="@{test.dir}"/>
|
||||
<classpath refid="test.java.classpath"/>
|
||||
<formatter type="${test.junit.output.format}"/>
|
||||
<batchtest todir="${test.java.build.dir}" unless="testcase">
|
||||
<fileset dir="@{files.location}"
|
||||
includes="@{tests.pattern}"
|
||||
excludes="**/${test.java.exclude}.java" />
|
||||
</batchtest>
|
||||
<batchtest todir="${test.java.build.dir}" if="testcase">
|
||||
<fileset dir="@{files.location}" includes="**/${testcase}.java"/>
|
||||
</batchtest>
|
||||
</junit>
|
||||
<fail if="tests.failed">Tests Failed!</fail>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
<target name="compile-test" depends="ivy-retrieve-test,compile">
|
||||
<java-compiler dest="${test.java.classes}"
|
||||
classpath="test.java.classpath">
|
||||
<src path="${test.java.src.dir}/org" />
|
||||
</java-compiler>
|
||||
</target>
|
||||
<target name="test" depends="init,compile-test">
|
||||
<test-runner files.location="${test.java.src.dir}"
|
||||
tests.pattern="**/${test.java.include}.java"/>
|
||||
</target>
|
||||
|
||||
<!-- jar -->
|
||||
<target name="jar" depends="compile">
|
||||
<jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.classes}" >
|
||||
<manifest>
|
||||
<section name="org/${name}">
|
||||
<attribute name="Implementation-Title" value="${Name}"/>
|
||||
<attribute name="Implementation-Version" value="${version}"/>
|
||||
</section>
|
||||
</manifest>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<!-- javadoc -->
|
||||
<target name="javadoc" depends="compile" description="Generate javadoc">
|
||||
<mkdir dir="${build.javadoc}"/>
|
||||
<record name="${build.javadoc.log}" action="start"/>
|
||||
<javadoc
|
||||
Locale="en_US"
|
||||
packagenames="org.${org}.${name}.*"
|
||||
destdir="${build.javadoc}"
|
||||
encoding="UTF-8"
|
||||
docencoding="UTF-8"
|
||||
author="true"
|
||||
version="true"
|
||||
use="true"
|
||||
windowtitle="${Name} ${version} API"
|
||||
doctitle="${Name} ${version} API"
|
||||
bottom="Copyright &copy; ${year} The ${Name} Project"
|
||||
>
|
||||
<packageset dir="${java.src.dir}"/>
|
||||
<link href="${javadoc.link.java}"/>
|
||||
<classpath >
|
||||
<path refid="java.classpath" />
|
||||
</classpath>
|
||||
</javadoc>
|
||||
<record name="${build.javadoc.log}" action="stop"/>
|
||||
<condition property="javadoc.warnings">
|
||||
<isfileselected file="${build.javadoc.log}">
|
||||
<contains text=": warning - "/>
|
||||
</isfileselected>
|
||||
</condition>
|
||||
<fail if="javadoc.warnings">Javadoc warnings!</fail>
|
||||
</target>
|
||||
<target name="javadoc-jar" depends="javadoc">
|
||||
<jar jarfile="${build.dir}/${fullname}-javadoc.jar">
|
||||
<fileset dir="${build.javadoc}" includes="**/*"/>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<!-- sources -->
|
||||
<target name="source">
|
||||
<jar jarfile="${build.dir}/${fullname}-sources.jar">
|
||||
<fileset dir="${java.src.dir}" includes="**/*.java"/>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<!-- pom -->
|
||||
<target name="pom" depends="ivy-init">
|
||||
<ivy:makepom ivyfile="${basedir}/ivy.xml"
|
||||
pomfile="${dist.dir}/${fullname}.pom">
|
||||
<mapping conf="default" scope="compile"/>
|
||||
<mapping conf="test" scope="test"/>
|
||||
</ivy:makepom>
|
||||
</target>
|
||||
|
||||
<!-- dist -->
|
||||
<target name="dist" depends="jar, pom, source, javadoc-jar"
|
||||
description="Build distribution">
|
||||
<mkdir dir="${dist.dir}"/>
|
||||
<copy todir="${dist.dir}">
|
||||
<fileset file="${build.dir}/${fullname}.jar"/>
|
||||
<fileset file="${build.dir}/${fullname}-sources.jar"/>
|
||||
<fileset file="${build.dir}/${fullname}-javadoc.jar"/>
|
||||
</copy>
|
||||
</target>
|
||||
|
||||
<!-- maven: install msgpack into local m2 cache -->
|
||||
<target name="mvn-install" depends="jar, pom, source, javadoc-jar"
|
||||
description="Installs msgpack to local m2 cache">
|
||||
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
|
||||
uri="urn:maven-artifact-ant"
|
||||
classpathref="java.classpath"/>
|
||||
<mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/>
|
||||
<mvn:install file="${build.dir}/${fullname}.jar">
|
||||
<attach file="${build.dir}/${fullname}-sources.jar"
|
||||
classifier="sources" />
|
||||
<attach file="${build.dir}/${fullname}-javadoc.jar"
|
||||
classifier="javadoc" />
|
||||
<pom refid="msgpack"/>
|
||||
</mvn:install>
|
||||
</target>
|
||||
|
||||
<!-- maven: create local repository into ${basedir}/maven2 -->
|
||||
<target name="mvn-deploy" depends="jar, pom, source, javadoc-jar"
|
||||
description="Deploys MessagePack to Maven repo.">
|
||||
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
|
||||
uri="urn:maven-artifact-ant"
|
||||
classpathref="java.classpath"/>
|
||||
<mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/>
|
||||
<mvn:deploy file="${build.dir}/${fullname}.jar">
|
||||
<remoteRepository url="file://localhost/${basedir}/maven2/"/>
|
||||
<attach file="${build.dir}/${fullname}-sources.jar"
|
||||
classifier="sources" />
|
||||
<attach file="${build.dir}/${fullname}-javadoc.jar"
|
||||
classifier="javadoc" />
|
||||
<pom refid="msgpack"/>
|
||||
</mvn:deploy>
|
||||
</target>
|
||||
|
||||
</project>
|
19
java/ivy.xml
Normal file
19
java/ivy.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<ivy-module version="2.0"
|
||||
xmlns:e="http://ant.apache.org/ivy/extra">
|
||||
|
||||
<info organisation="org"
|
||||
module="${name}" revision="${version}">
|
||||
<ivyauthor name="MessagePack Project" url="http://msgpack.sourceforge.net/"/>
|
||||
<description>MessagePack</description>
|
||||
</info>
|
||||
|
||||
<configurations defaultconfmapping="default">
|
||||
<conf name="default"/> <!-- "runtime" configuration -->
|
||||
<conf name="test"/>
|
||||
<conf name="build" extends="default"/>
|
||||
</configurations>
|
||||
|
||||
<dependencies>
|
||||
<dependency org="junit" name="junit" rev="4.8.1" conf="test->default"/>
|
||||
</dependencies>
|
||||
</ivy-module>
|
109
java/pom.xml
Executable file
109
java/pom.xml
Executable file
@@ -0,0 +1,109 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.msgpack</groupId>
|
||||
<artifactId>msgpack</artifactId>
|
||||
<name>MessagePack for Java</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<description>MessagePack for Java</description>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git://github.com/msgpack/msgpack.git</connection>
|
||||
</scm>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>src/test/resources</directory>
|
||||
</testResource>
|
||||
</testResources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-eclipse-plugin</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- do not run site-deploy goal, which is included in the default setting -->
|
||||
<goals>deploy</goals>
|
||||
<connectionUrl>scm:git://github.com/msgpack/msgpack.git</connectionUrl>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<!-- Generating JavaDoc -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<doctitle>${project.name} ${project.version} API</doctitle>
|
||||
<aggregate>true</aggregate>
|
||||
<locale>en_US</locale>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jxr-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-report-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<profiles>
|
||||
<!-- for sending artifacts to sourceforge.net repository -->
|
||||
<profile>
|
||||
<id>sourceforge</id>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>sourceforge.net</id>
|
||||
<name>Repository at sourceforge.net</name>
|
||||
<url>scpexe://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
23
java/src/main/java/org/msgpack/MessageMergeable.java
Normal file
23
java/src/main/java/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/main/java/org/msgpack/MessagePackable.java
Normal file
25
java/src/main/java/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/main/java/org/msgpack/MessageTypeException.java
Normal file
39
java/src/main/java/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());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
408
java/src/main/java/org/msgpack/Packer.java
Normal file
408
java/src/main/java/org/msgpack/Packer.java
Normal file
@@ -0,0 +1,408 @@
|
||||
//
|
||||
// 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.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()+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user