summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:55 +0000
committerfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:55 +0000
commit269cda016dd6ea92b66e55ebe283965924e67bc1 (patch)
treefbfe2f8e3720be165a0180045b9be941d506417b /cpp
downloadmsgpack-python-269cda016dd6ea92b66e55ebe283965924e67bc1.tar.gz
lang/c/msgpack: added Messagepack, a binary-based efficient data interchange format.
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@48 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
Diffstat (limited to 'cpp')
-rw-r--r--cpp/unpack.cc30
-rw-r--r--cpp/unpack.h126
-rw-r--r--cpp/unpack_context.h13
3 files changed, 169 insertions, 0 deletions
diff --git a/cpp/unpack.cc b/cpp/unpack.cc
new file mode 100644
index 0000000..ce57e67
--- /dev/null
+++ b/cpp/unpack.cc
@@ -0,0 +1,30 @@
+#include "unpack.h"
+#include "unpack_context.h"
+#include <stdlib.h>
+
+msgpack_unpack_t* msgpack_unpack_new(void)
+{
+ msgpack_unpacker* ctx;
+ ctx = (msgpack_unpacker*)calloc(1, sizeof(msgpack_unpacker));
+ if(ctx == NULL) { return NULL; }
+ msgpack_unpacker_init(ctx);
+ return (msgpack_unpack_t*)ctx;
+}
+
+void msgpack_unpack_free(msgpack_unpack_t* ctx)
+{
+ free((msgpack_unpacker*)ctx);
+}
+
+int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off)
+{
+ return msgpack_unpacker_execute(
+ (msgpack_unpacker*)ctx,
+ data, len, off);
+}
+
+void* msgpack_unpack_data(msgpack_unpack_t* ctx)
+{
+ return msgpack_unpacker_data((msgpack_unpacker*)ctx);
+}
+
diff --git a/cpp/unpack.h b/cpp/unpack.h
new file mode 100644
index 0000000..cf7a168
--- /dev/null
+++ b/cpp/unpack.h
@@ -0,0 +1,126 @@
+#ifndef MSGPACK_UNPACK_H__
+#define MSGPACK_UNPACK_H__
+
+#include <stdint.h>
+#include <stddef.h>
+
+namespace MessagePack {
+
+class Unpacker {
+ class object {
+ template <typename T>
+ object(const T& x) : m_obj(new holder<T>(x)) {}
+ };
+
+ class type_error : public std::exception { };
+ class cast_error : public type_error { };
+ class overflow_error : public type_error { };
+ class underflow_error : public type_error { };
+
+ struct object {
+ virtual ~object() {}
+ virtual bool isnil() const { return false; }
+ virtual bool xbool() const { throw cast_error(); }
+ virtual uint8_t xu8() const { throw cast_error(); }
+ virtual uint16_t xu16() const { throw cast_error(); }
+ virtual uint32_t xu32() const { throw cast_error(); }
+ virtual uint64_t xu64() const { throw cast_error(); }
+ virtual int8_t xi8() const { throw cast_error(); }
+ virtual int16_t xi16() const { throw cast_error(); }
+ virtual int32_t xi32() const { throw cast_error(); }
+ virtual int64_t xi64() const { throw cast_error(); }
+ virtual float xfloat() const { throw cast_error(); }
+ virtual double xdouble() const { throw cast_error(); }
+ virtual std::map<object, object>& xmap() const { throw cast_error(); }
+ virtual std::string& xstring() const { throw cast_error(); }
+ virtual std::pair<const char*, size_t> xraw() const { throw cast_error(); }
+ public:
+ template <typename T, typename X>
+ inline void check_overflow(X x) {
+ if(std::numeric_limits<T>::max() < x) { throw overflow_error(); }
+ }
+ template <typename T, typename X>
+ inline void check_underflow(X x) {
+ if(std::numeric_limits<T>::min() > x) { throw overflow_error(); }
+ }
+ };
+
+private:
+ struct object_nil : object {
+ bool isnil() const { return true; }
+ };
+
+ struct object_true : object {
+ bool xbool() const { return true; }
+ };
+
+ struct object_false : object {
+ bool xbool() const { return false; }
+ };
+
+ struct object_u8 : object {
+ object_u8(uint8_t val) : m_val(val) {}
+ uint8_t xu8() const { return m_val; }
+ uint16_t xu16() const { return static_cast<uint16_t>(m_val); }
+ uint32_t xu32() const { return static_cast<uint32_t>(m_val); }
+ uint64_t xu64() const { return static_cast<uint64_t>(m_val); }
+ int8_t xi8() const { check_overflow<int8_t>(m_val); return m_val; }
+ int16_t xi16() const { return static_cast<int16_t>(m_val); }
+ int32_t xi32() const { return static_cast<int32_t>(m_val); }
+ int64_t xi64() const { return static_cast<int64_t>(m_val); }
+ private:
+ uint8_t m_val;
+ };
+
+ struct object_u16 : object {
+ object_u16(uint16_t val) : m_val(val) {}
+ uint8_t xu8() const { check_overflow<uint8_t>(m_val); return m_val; }
+ uint16_t xu16() const { return m_val; }
+ uint32_t xu32() const { return static_cast<uint32_t>(m_val); }
+ uint64_t xu64() const { return static_cast<uint64_t>(m_val); }
+ int8_t xi8() const { check_overflow< int8_t>(m_val); return m_val; }
+ int16_t xi16() const { check_overflow<int16_t>(m_val); return m_val; }
+ int32_t xi32() const { return static_cast<int32_t>(m_val); }
+ int64_t xi64() const { return static_cast<int64_t>(m_val); }
+ private:
+ uint16_t m_val;
+ };
+
+ ...
+};
+
+} // namespace MessagePack
+
+typedef struct {
+ void* (*unpack_unsigned_int_8)(void* data, uint8_t d);
+ void* (*unpack_unsigned_int_16)(void* data, uint16_t d);
+ void* (*unpack_unsigned_int_32)(void* data, uint32_t d);
+ void* (*unpack_unsigned_int_64)(void* data, uint64_t d);
+ void* (*unpack_signed_int_8)(void* data, int8_t d);
+ void* (*unpack_signed_int_16)(void* data, int16_t d);
+ void* (*unpack_signed_int_32)(void* data, int32_t d);
+ void* (*unpack_signed_int_64)(void* data, int64_t d);
+ void* (*unpack_float)(void* data, float d);
+ void* (*unpack_double)(void* data, double d);
+ void* (*unpack_big_int)(void* data, const void* b, unsigned int l);
+ void* (*unpack_big_float)(void* data, const void* b, unsigned int l);
+ void* (*unpack_nil)(void* data);
+ void* (*unpack_true)(void* data);
+ void* (*unpack_false)(void* data);
+ void* (*unpack_array_start)(void* data, unsigned int n);
+ void (*unpack_array_item)(void* data, void* c, void* o);
+ void* (*unpack_map_start)(void* data, unsigned int n);
+ void (*unpack_map_item)(void* data, void* c, void* k, void* v);
+ void* (*unpack_string)(void* data, const void* b, size_t l);
+ void* (*unpack_raw)(void* data, const void* b, size_t l);
+ void* data;
+} msgpack_unpack_t;
+
+msgpack_unpack_t* msgpack_unpack_new(void);
+void msgpack_unpack_free(msgpack_unpack_t* ctx);
+int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off);
+void* msgpack_unpack_data(msgpack_unpack_t* ctx);
+
+#endif /* msgpack/unpack.h */
+
+
diff --git a/cpp/unpack_context.h b/cpp/unpack_context.h
new file mode 100644
index 0000000..caf2271
--- /dev/null
+++ b/cpp/unpack_context.h
@@ -0,0 +1,13 @@
+#ifndef UNPACK_CONTEXT_H__
+#define UNPACK_CONTEXT_H__
+
+#include "unpack.h"
+
+typedef void* msgpack_object;
+
+typedef msgpack_unpack_t msgpack_unpack_context;
+
+#include "msgpack/unpack/inline_context.h"
+
+#endif /* unpack_context.h */
+