diff options
| author | frsyuki <frsyuki@users.sourceforge.jp> | 2010-06-01 07:15:58 +0900 |
|---|---|---|
| committer | frsyuki <frsyuki@users.sourceforge.jp> | 2010-06-01 07:15:58 +0900 |
| commit | 684bca203ad862f30558429081d1a27681fe4559 (patch) | |
| tree | 147dc99cd2554c5f2477311e8482f4955bac3f95 /cpp/src/msgpack | |
| parent | d42ecccf6f201eb92b6c04b0dde1b86a8861e58e (diff) | |
| download | msgpack-python-684bca203ad862f30558429081d1a27681fe4559.tar.gz | |
cpp: adds msgpack_unpacker_next and msgpack_unpack_next
Diffstat (limited to 'cpp/src/msgpack')
| -rw-r--r-- | cpp/src/msgpack/unpack.h | 50 | ||||
| -rw-r--r-- | cpp/src/msgpack/unpack.hpp | 7 |
2 files changed, 53 insertions, 4 deletions
diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h index e17d0d8..1f43ab7 100644 --- a/cpp/src/msgpack/unpack.h +++ b/cpp/src/msgpack/unpack.h @@ -20,6 +20,15 @@ #include "msgpack/zone.h" #include "msgpack/object.h" +#include <string.h> + +#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE +#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024) +#endif + +#ifndef MSGPACK_UNPACKER_RESERVE_SIZE +#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024) +#endif #ifdef __cplusplus extern "C" { @@ -37,6 +46,10 @@ typedef struct msgpack_unpacker { void* ctx; } msgpack_unpacker; +typedef struct msgpack_unpacked { + msgpack_zone* zone; + msgpack_object data; +} msgpack_unpacked; bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size); void msgpack_unpacker_destroy(msgpack_unpacker* mpac); @@ -49,6 +62,13 @@ static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac); static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac); static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size); +bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac); + +static inline void msgpack_unpacked_init(msgpack_unpacked* result); +static inline void msgpack_unpacked_destroy(msgpack_unpacked* result); + +static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result); + int msgpack_unpacker_execute(msgpack_unpacker* mpac); @@ -63,6 +83,9 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac); static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac); +bool msgpack_unpack_next(msgpack_unpacked* result, + const char* data, size_t len, size_t* off); + typedef enum { MSGPACK_UNPACK_SUCCESS = 2, @@ -73,7 +96,7 @@ typedef enum { msgpack_unpack_return msgpack_unpack(const char* data, size_t len, size_t* off, - msgpack_zone* z, msgpack_object* result); + msgpack_zone* result_zone, msgpack_object* result); static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac); @@ -115,6 +138,31 @@ size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac) } +void msgpack_unpacked_init(msgpack_unpacked* result) +{ + memset(result, 0, sizeof(msgpack_unpacked)); +} + +void msgpack_unpacked_destroy(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone_free(result->zone); + result->zone = NULL; + memset(&result->data, 0, sizeof(msgpack_object)); + } +} + +msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone* z = result->zone; + result->zone = NULL; + return z; + } + return NULL; +} + + #ifdef __cplusplus } #endif diff --git a/cpp/src/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp index e1617ef..7b45017 100644 --- a/cpp/src/msgpack/unpack.hpp +++ b/cpp/src/msgpack/unpack.hpp @@ -24,8 +24,9 @@ #include <memory> #include <stdexcept> +// backward compatibility #ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE -#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE (32*1024) +#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE #endif namespace msgpack { @@ -64,12 +65,12 @@ private: class unpacker : public msgpack_unpacker { public: - unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE); + unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE); ~unpacker(); public: /*! 1. reserve buffer. at least `size' bytes of capacity will be ready */ - void reserve_buffer(size_t size); + void reserve_buffer(size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); /*! 2. read data to the buffer() up to buffer_capacity() bytes */ char* buffer(); |
