mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-14 18:10:30 +01:00
lang/c/msgpack: added C++ binding
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@50 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
9f460f17d7
commit
529a50633d
21
cpp/Makefile
Normal file
21
cpp/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
CXXFLAGS = -I.. -I. -Wall -g -O4
|
||||
LDFLAGS = -L.
|
||||
|
||||
NEED_PREPROCESS = zone.hpp
|
||||
|
||||
all: test
|
||||
|
||||
%.hpp: %.hpp.erb
|
||||
erb $< > $@
|
||||
|
||||
test: $(NEED_PREPROCESS) unpack.o unpack_inline.o zone.o test.o object.hpp unpack.hpp
|
||||
$(CXX) $(LDFLAGS) unpack.o unpack_inline.o zone.o test.o -o $@
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(RM) unpack.o unpack_inline.o zone.o
|
||||
$(RM) test.o
|
||||
$(RM) test
|
||||
$(RM) $(NEED_PREPROCESS)
|
||||
|
1
cpp/msgpack
Symbolic link
1
cpp/msgpack
Symbolic link
@ -0,0 +1 @@
|
||||
.
|
760
cpp/object.hpp
Normal file
760
cpp/object.hpp
Normal file
@ -0,0 +1,760 @@
|
||||
#ifndef MSGPACK_OBJECT_HPP__
|
||||
#define MSGPACK_OBJECT_HPP__
|
||||
#include <iostream>
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
class type_error : public std::bad_cast { };
|
||||
class cast_error : public type_error { };
|
||||
class overflow_error : public type_error { };
|
||||
class underflow_error : public type_error { };
|
||||
class positive_overflow_error : public overflow_error { };
|
||||
class negative_overflow_error : public overflow_error { };
|
||||
|
||||
|
||||
struct raw {
|
||||
explicit raw() : ptr(NULL), len(0) {}
|
||||
explicit raw(void* p, size_t l) : ptr(p), len(l) {}
|
||||
public:
|
||||
void* ptr;
|
||||
size_t len;
|
||||
public:
|
||||
std::string str() { return std::string((const char*)ptr, len); }
|
||||
};
|
||||
|
||||
struct const_raw {
|
||||
const_raw() : ptr(NULL), len(0) {}
|
||||
const_raw(const void* p, size_t l) : ptr(p), len(l) {}
|
||||
public:
|
||||
const void* ptr;
|
||||
size_t len;
|
||||
public:
|
||||
std::string str() { return std::string((const char*)ptr, len); }
|
||||
};
|
||||
|
||||
|
||||
struct object;
|
||||
|
||||
typedef std::map<object, object> map;
|
||||
typedef std::vector<object> array;
|
||||
|
||||
|
||||
template <typename T, typename X, bool TSigned, bool XSigned>
|
||||
struct numeric_overflow_signed_impl;
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_overflow_signed_impl<T, X, true, true> {
|
||||
static int test(X x) {
|
||||
if( ( std::numeric_limits<T>::is_integer && std::numeric_limits<X>::is_integer) ||
|
||||
(!std::numeric_limits<T>::is_integer && !std::numeric_limits<X>::is_integer) ) {
|
||||
if( sizeof(T) < sizeof(X) ) {
|
||||
if( static_cast<X>( std::numeric_limits<T>::max()) < x ) { return 1; }
|
||||
if( static_cast<X>(-std::numeric_limits<T>::max()) > x ) { return -1; }
|
||||
}
|
||||
} else if(std::numeric_limits<T>::is_integer) {
|
||||
if( static_cast<X>( std::numeric_limits<T>::max()) < x) { return 1; }
|
||||
if( static_cast<X>(-std::numeric_limits<T>::max()) > x) { return -1; }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_overflow_signed_impl<T, X, true, false> {
|
||||
static int test(X x) {
|
||||
if( ( std::numeric_limits<T>::is_integer && std::numeric_limits<X>::is_integer) ||
|
||||
(!std::numeric_limits<T>::is_integer && !std::numeric_limits<X>::is_integer) ) {
|
||||
if( sizeof(T) <= sizeof(X) ) {
|
||||
if( static_cast<X>(std::numeric_limits<T>::max()) < x ) { return 1; }
|
||||
}
|
||||
} else if(std::numeric_limits<T>::is_integer) {
|
||||
if( static_cast<X>( std::numeric_limits<T>::max()) < x) { return 1; }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_overflow_signed_impl<T, X, false, true> {
|
||||
static int test(X x) {
|
||||
if( static_cast<X>(0) > x ) { return -1; }
|
||||
if( ( std::numeric_limits<T>::is_integer && std::numeric_limits<X>::is_integer) ||
|
||||
(!std::numeric_limits<T>::is_integer && !std::numeric_limits<X>::is_integer) ) {
|
||||
if( sizeof(T) < sizeof(X) ) {
|
||||
if( static_cast<X>(std::numeric_limits<T>::max()) < x ) { return 1; }
|
||||
}
|
||||
} else if(std::numeric_limits<T>::is_integer) {
|
||||
if( static_cast<X>( std::numeric_limits<T>::max()) < x) { return 1; }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_overflow_signed_impl<T, X, false, false> {
|
||||
static int test(X x) {
|
||||
if( ( std::numeric_limits<T>::is_integer && std::numeric_limits<X>::is_integer) ||
|
||||
(!std::numeric_limits<T>::is_integer && !std::numeric_limits<X>::is_integer) ) {
|
||||
if( sizeof(T) < sizeof(X) ) {
|
||||
if( static_cast<X>(std::numeric_limits<T>::max()) < x ) { return 1; }
|
||||
}
|
||||
} else if(std::numeric_limits<T>::is_integer) {
|
||||
if( static_cast<X>(std::numeric_limits<T>::max()) < x ) { return 1; }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_overflow {
|
||||
static int test(X x) {
|
||||
return numeric_overflow_signed_impl<T, X, std::numeric_limits<T>::is_signed, std::numeric_limits<X>::is_signed>::test(x);
|
||||
}
|
||||
static void check(X x) {
|
||||
int r = test(x);
|
||||
if(r == 1) { throw positive_overflow_error(); }
|
||||
if(r == -1) { throw negative_overflow_error(); }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename T, typename X>
|
||||
struct numeric_underflow {
|
||||
static bool test(X x) {
|
||||
return static_cast<X>(static_cast<T>(x)) != x;
|
||||
}
|
||||
static void check(X x) {
|
||||
if(test(x)) { throw underflow_error(); }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct object_class {
|
||||
virtual ~object_class() {}
|
||||
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 raw xraw() { throw cast_error(); }
|
||||
virtual array& xarray() { throw cast_error(); }
|
||||
virtual map& xmap() { throw cast_error(); }
|
||||
virtual const_raw xraw() const { throw cast_error(); }
|
||||
virtual const array& xarray() const { throw cast_error(); }
|
||||
virtual const map& xmap() const { throw cast_error(); }
|
||||
virtual bool operator== (const object_class* x) const { return false; }
|
||||
bool operator!= (const object_class* x) const { return !(this->operator==(x)); }
|
||||
virtual bool operator< (const object_class* x) const { throw cast_error(); }
|
||||
virtual bool operator> (const object_class* x) const { throw cast_error(); }
|
||||
operator bool() const { return xbool(); } // FIXME !isnil();
|
||||
operator uint8_t() const { return xu8(); }
|
||||
operator uint16_t() const { return xu16(); }
|
||||
operator uint32_t() const { return xu32(); }
|
||||
operator uint64_t() const { return xu64(); }
|
||||
operator int8_t() const { return xi8(); }
|
||||
operator int16_t() const { return xi16(); }
|
||||
operator int32_t() const { return xi32(); }
|
||||
operator int64_t() const { return xi64(); }
|
||||
operator float() const { return xfloat(); }
|
||||
operator double() const { return xdouble(); }
|
||||
operator raw() { return xraw(); }
|
||||
operator array&() { return xarray(); }
|
||||
operator map&() { return xmap(); }
|
||||
operator const_raw() const { return xraw(); }
|
||||
operator const array&() const { return xarray(); }
|
||||
operator const map&() const { return xmap(); }
|
||||
virtual const object_class* inspect(std::ostream& s) const
|
||||
{ s << '<' << typeid(*this).name() << '>'; return this; }
|
||||
protected:
|
||||
template <typename T, typename X>
|
||||
static void check_overflow(X x) { numeric_overflow<T, X>::check(x); }
|
||||
template <typename T, typename X>
|
||||
static void check_underflow(X x) { numeric_underflow<T, X>::check(x); }
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& s, const object_class* o)
|
||||
{ o->inspect(s); return s; }
|
||||
|
||||
|
||||
struct object_container_mixin {};
|
||||
struct object_constructor_mixin {};
|
||||
|
||||
|
||||
struct object {
|
||||
explicit object() : val(NULL) {}
|
||||
object(object_class* v) : val(v) {}
|
||||
//object(object_class& v) : val(&v) {}
|
||||
~object() {}
|
||||
bool isnil() const { return val->isnil(); }
|
||||
bool xbool() const { return val->xbool(); }
|
||||
uint8_t xu8() const { return val->xu8(); }
|
||||
uint16_t xu16() const { return val->xu16(); }
|
||||
uint32_t xu32() const { return val->xu32(); }
|
||||
uint64_t xu64() const { return val->xu64(); }
|
||||
int8_t xi8() const { return val->xi8(); }
|
||||
int16_t xi16() const { return val->xi16(); }
|
||||
int32_t xi32() const { return val->xi32(); }
|
||||
int64_t xi64() const { return val->xi64(); }
|
||||
float xfloat() const { return val->xfloat(); }
|
||||
double xdouble() const { return val->xdouble(); }
|
||||
raw xraw() { return val->xraw(); }
|
||||
array& xarray() { return val->xarray(); }
|
||||
map& xmap() { return val->xmap(); }
|
||||
const_raw xraw() const { return const_cast<const object_class*>(val)->xraw(); }
|
||||
const array& xarray() const { return const_cast<const object_class*>(val)->xarray(); }
|
||||
const map& xmap() const { return const_cast<const object_class*>(val)->xmap(); }
|
||||
bool operator== (object x) const { return val->operator== (x.val); }
|
||||
bool operator!= (object x) const { return val->operator!= (x.val); }
|
||||
bool operator< (object x) const { return val->operator< (x.val); }
|
||||
bool operator> (object x) const { return val->operator> (x.val); }
|
||||
operator bool() const { return val->operator bool(); }
|
||||
operator uint8_t() const { return val->operator uint8_t(); }
|
||||
operator uint16_t() const { return val->operator uint16_t(); }
|
||||
operator uint32_t() const { return val->operator uint32_t(); }
|
||||
operator uint64_t() const { return val->operator uint64_t(); }
|
||||
operator int8_t() const { return val->operator int8_t(); }
|
||||
operator int16_t() const { return val->operator int16_t(); }
|
||||
operator int32_t() const { return val->operator int32_t(); }
|
||||
operator int64_t() const { return val->operator int64_t(); }
|
||||
operator float() const { return val->operator float(); }
|
||||
operator double() const { return val->operator double(); }
|
||||
operator raw() { return val->operator raw(); }
|
||||
operator array&() { return val->operator array&(); }
|
||||
operator map&() { return val->operator map&(); }
|
||||
operator raw() const { return val->operator raw(); }
|
||||
operator array&() const { return val->operator array&(); }
|
||||
operator map&() const { return val->operator map&(); }
|
||||
const object& inspect(std::ostream& s) const
|
||||
{ val->inspect(s); return *this; }
|
||||
private:
|
||||
friend class object_container_mixin;
|
||||
friend class object_constructor_mixin;
|
||||
object_class* val;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& s, const object& o)
|
||||
{ o.inspect(s); return s; }
|
||||
|
||||
|
||||
struct object_nil : object_class {
|
||||
bool isnil() const { return true; }
|
||||
bool operator== (const object_class* x) const { return typeid(*this) == typeid(*x); }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << "nil"; return this; }
|
||||
};
|
||||
|
||||
struct object_true : object_class {
|
||||
bool xbool() const { return true; }
|
||||
bool operator== (const object_class* x) const { return typeid(*this) == typeid(*x); }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << "true"; return this; }
|
||||
};
|
||||
|
||||
struct object_false : object_class {
|
||||
bool xbool() const { return false; }
|
||||
bool operator== (const object_class* x) const { return typeid(*this) == typeid(*x); }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << "false"; return this; }
|
||||
};
|
||||
|
||||
struct object_u8 : object_class {
|
||||
explicit object_u8(uint8_t v) : val(v) {}
|
||||
uint8_t xu8() const { return val; }
|
||||
uint16_t xu16() const { return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xu8(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xu8(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xu8(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << (uint16_t)val; return this; }
|
||||
private:
|
||||
uint8_t val;
|
||||
};
|
||||
|
||||
struct object_u16 : object_class {
|
||||
explicit object_u16(uint16_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { return val; }
|
||||
uint32_t xu32() const { return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<int16_t>(val);
|
||||
return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xu16(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xu16(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xu16(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (type_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
uint16_t val;
|
||||
};
|
||||
|
||||
struct object_u32 : object_class {
|
||||
explicit object_u32(uint32_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { return val; }
|
||||
uint64_t xu64() const { return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<int16_t>(val);
|
||||
return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { check_overflow<int32_t>(val);
|
||||
return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { check_underflow<float>(val);
|
||||
return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xu32(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xu32(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xu32(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
uint32_t val;
|
||||
};
|
||||
|
||||
struct object_u64 : object_class {
|
||||
explicit object_u64(uint64_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint32_t>(val);
|
||||
return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { return val; }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<int16_t>(val);
|
||||
return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { check_overflow<int32_t>(val);
|
||||
return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { check_overflow<int64_t>(val);
|
||||
return static_cast<int64_t>(val); }
|
||||
float xfloat() const { check_underflow<float>(val);
|
||||
return static_cast<float>(val); }
|
||||
double xdouble() const { check_underflow<double>(val);
|
||||
return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xu64(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xu64(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xu64(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
uint64_t val;
|
||||
};
|
||||
|
||||
struct object_i8 : object_class {
|
||||
explicit object_i8(int8_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint32_t>(val);
|
||||
return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint64_t>(val);
|
||||
return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { return val; }
|
||||
int16_t xi16() const { return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xi8(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xi8(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xi8(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << (int16_t)val; return this; }
|
||||
private:
|
||||
int8_t val;
|
||||
};
|
||||
|
||||
struct object_i16 : object_class {
|
||||
explicit object_i16(int16_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint32_t>(val);
|
||||
return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint64_t>(val);
|
||||
return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { return val; }
|
||||
int32_t xi32() const { return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xi16(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xi16(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xi16(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (type_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
int16_t val;
|
||||
};
|
||||
|
||||
struct object_i32 : object_class {
|
||||
explicit object_i32(int32_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint32_t>(val);
|
||||
return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint64_t>(val);
|
||||
return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<int16_t>(val);
|
||||
return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { return val; }
|
||||
int64_t xi64() const { return static_cast<int64_t>(val); }
|
||||
float xfloat() const { check_underflow<float>(val);
|
||||
return static_cast<float>(val); }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xi32(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xi32(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xi32(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
int32_t val;
|
||||
};
|
||||
|
||||
struct object_i64 : object_class {
|
||||
explicit object_i64(int64_t v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint16_t>(val);
|
||||
return static_cast<uint16_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint32_t>(val);
|
||||
return static_cast<uint32_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint64_t>(val);
|
||||
return static_cast<uint64_t>(val); }
|
||||
int8_t xi8() const { check_overflow<int8_t>(val);
|
||||
return static_cast<int8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<int16_t>(val);
|
||||
return static_cast<int16_t>(val); }
|
||||
int32_t xi32() const { check_overflow<int32_t>(val);
|
||||
return static_cast<int32_t>(val); }
|
||||
int64_t xi64() const { return val; }
|
||||
float xfloat() const { check_underflow<float>(val);
|
||||
return static_cast<float>(val); }
|
||||
double xdouble() const { check_underflow<double>(val);
|
||||
return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xi64(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xi64(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xi64(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
int64_t val;
|
||||
};
|
||||
|
||||
|
||||
struct object_float : object_class {
|
||||
object_float(float v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int8_t xi8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int32_t xi32() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int64_t xi64() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
float xfloat() const { return val; }
|
||||
double xdouble() const { return static_cast<double>(val); }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xfloat(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return static_cast<double>(val) < x->xdouble(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; }
|
||||
catch (underflow_error&) {
|
||||
if(val < 0.0) {
|
||||
if(numeric_overflow<int64_t, float>::test(val) == -1) { return true; }
|
||||
try { return static_cast<int64_t>(val) < x->xi64(); }
|
||||
catch (type_error&) { return true; }
|
||||
} else {
|
||||
if(numeric_overflow<uint64_t, float>::test(val) == 1) { return false; }
|
||||
try { return static_cast<uint64_t>(val) < x->xu64(); }
|
||||
catch (type_error&) { return false; }
|
||||
}
|
||||
} }
|
||||
bool operator> (const object_class* x) const { try { return static_cast<double>(val) > x->xdouble(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; }
|
||||
catch (underflow_error&) {
|
||||
if(val < 0.0) {
|
||||
if(numeric_overflow<int64_t, float>::test(val) == -1) { return false; }
|
||||
try { return static_cast<int64_t>(val) > x->xi64(); }
|
||||
catch (type_error&) { return false; }
|
||||
} else {
|
||||
if(numeric_overflow<uint64_t, float>::test(val) == 1) { return true; }
|
||||
try { return static_cast<uint64_t>(val) > x->xu64(); }
|
||||
catch (type_error&) { return true; }
|
||||
}
|
||||
} }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
float val;
|
||||
};
|
||||
|
||||
|
||||
struct object_double : object_class {
|
||||
object_double(double v) : val(v) {}
|
||||
uint8_t xu8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint16_t xu16() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint32_t xu32() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
uint64_t xu64() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int8_t xi8() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int16_t xi16() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int32_t xi32() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
int64_t xi64() const { check_overflow<uint8_t>(val);
|
||||
return static_cast<uint8_t>(val); }
|
||||
float xfloat() const { check_overflow<float>(val);
|
||||
check_underflow<float>(val);
|
||||
return static_cast<float>(val); }
|
||||
double xdouble() const { return val; }
|
||||
bool operator== (const object_class* x) const { try { return val == x->xdouble(); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { try { return val < x->xdouble(); }
|
||||
catch (positive_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; }
|
||||
catch (underflow_error&) {
|
||||
if(val < 0.0) {
|
||||
if(numeric_overflow<int64_t, double>::test(val) == -1) { return true; }
|
||||
try { return static_cast<int64_t>(val) < x->xi64(); }
|
||||
catch (type_error&) { return true; }
|
||||
} else {
|
||||
if(numeric_overflow<uint64_t, double>::test(val) == 1) { return false; }
|
||||
try { return static_cast<uint64_t>(val) < x->xu64(); }
|
||||
catch (type_error&) { return false; }
|
||||
}
|
||||
} }
|
||||
bool operator> (const object_class* x) const { try { return val > x->xdouble(); }
|
||||
catch (negative_overflow_error&) { return true; }
|
||||
catch (overflow_error&) { return false; }
|
||||
catch (underflow_error&) {
|
||||
if(val < 0.0) {
|
||||
if(numeric_overflow<int64_t, double>::test(val) == -1) { return false; }
|
||||
try { return static_cast<int64_t>(val) > x->xi64(); }
|
||||
catch (type_error&) { return false; }
|
||||
} else {
|
||||
if(numeric_overflow<uint64_t, double>::test(val) == 1) { return true; }
|
||||
try { return static_cast<uint64_t>(val) > x->xu64(); }
|
||||
catch (type_error&) { return true; }
|
||||
}
|
||||
} }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ s << val; return this; }
|
||||
private:
|
||||
double val;
|
||||
};
|
||||
|
||||
|
||||
struct object_raw : object_class {
|
||||
explicit object_raw(void* p, uint32_t l) : ptr(p), len(l) {}
|
||||
raw xraw() { return raw(ptr, len); }
|
||||
const_raw xraw() const { return const_raw(ptr, len); }
|
||||
bool operator== (const object_class* x) const { try { const_raw xr(x->xraw());
|
||||
return len == xr.len && (ptr == xr.ptr || memcmp(ptr, xr.ptr, len) == 0); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { const_raw xr(x->xraw());
|
||||
if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) < 0; }
|
||||
else { return len < xr.len; } }
|
||||
bool operator> (const object_class* x) const { const_raw xr(x->xraw());
|
||||
if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) > 0; }
|
||||
else { return len > xr.len; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ (s << '"').write((const char*)ptr, len) << '"'; return this; } // FIXME escape
|
||||
private:
|
||||
void* ptr;
|
||||
uint32_t len;
|
||||
};
|
||||
|
||||
struct object_const_raw : object_class {
|
||||
explicit object_const_raw(const void* p, uint32_t l) : ptr(p), len(l) {}
|
||||
const_raw xraw() const { return const_raw(ptr, len); }
|
||||
bool operator== (const object_class* x) const { try { const_raw xr(x->xraw());
|
||||
return len == xr.len && (ptr == xr.ptr || memcmp(ptr, xr.ptr, len) == 0); }
|
||||
catch (type_error&) { return false; } }
|
||||
bool operator< (const object_class* x) const { const_raw xr(x->xraw());
|
||||
if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) < 0; }
|
||||
else { return len < xr.len; } }
|
||||
bool operator> (const object_class* x) const { const_raw xr(x->xraw());
|
||||
if(len == xr.len) { return ptr != xr.ptr && memcmp(ptr, xr.ptr, len) > 0; }
|
||||
else { return len > xr.len; } }
|
||||
const object_class* inspect(std::ostream& s) const
|
||||
{ (s << '"').write((const char*)ptr, len) << '"'; return this; } // FIXME escape
|
||||
private:
|
||||
const void* ptr;
|
||||
uint32_t len;
|
||||
};
|
||||
|
||||
struct object_array : object_class, object_container_mixin {
|
||||
explicit object_array() {}
|
||||
explicit object_array(uint32_t n) { val.reserve(n); }
|
||||
array& xarray() { return val; }
|
||||
const array& xarray() const { return val; }
|
||||
bool operator== (const object_class* x) const { try {
|
||||
const std::vector<object>& xa(x->xarray());
|
||||
if(val.size() != xa.size()) { return false; }
|
||||
for(std::vector<object>::const_iterator iv(val.begin()), iv_end(val.end()), ix(xa.begin());
|
||||
iv != iv_end;
|
||||
++iv, ++ix) {
|
||||
if(*iv != *ix) { return false; }
|
||||
}
|
||||
return true;
|
||||
} catch (type_error&) { return false; } }
|
||||
// FIXME operator< operator>
|
||||
const object_class* inspect(std::ostream& s) const {
|
||||
s << '[';
|
||||
if(!val.empty()) {
|
||||
std::vector<object>::const_iterator it(val.begin());
|
||||
s << *it;
|
||||
++it;
|
||||
for(std::vector<object>::const_iterator it_end(val.end());
|
||||
it != it_end;
|
||||
++it) {
|
||||
s << ", " << *it;
|
||||
}
|
||||
}
|
||||
s << ']';
|
||||
return this; }
|
||||
public:
|
||||
void push_back(object o) { val.push_back(o); }
|
||||
private:
|
||||
std::vector<object> val;
|
||||
};
|
||||
|
||||
// FIXME hash, operator==: nil, true, false, containerを入れられない
|
||||
struct object_map : object_class, object_container_mixin {
|
||||
explicit object_map() {}
|
||||
map& xmap() { return val; }
|
||||
const map& xmap() const { return val; }
|
||||
bool operator== (const object_class* x) const { try {
|
||||
const std::map<object, object>& xm(x->xmap());
|
||||
if(val.size() != xm.size()) { return false; }
|
||||
for(std::map<object, object>::const_iterator iv(val.begin()), iv_end(val.end()), ix(xm.begin());
|
||||
iv != iv_end;
|
||||
++iv, ++ix) {
|
||||
if(iv->first != ix->first || iv->second != ix->first) { return false; }
|
||||
}
|
||||
return true;
|
||||
} catch (type_error&) { return false; } }
|
||||
// FIXME operator< operator>
|
||||
const object_class* inspect(std::ostream& s) const {
|
||||
s << '{';
|
||||
if(!val.empty()) {
|
||||
std::map<object, object>::const_iterator it(val.begin());
|
||||
s << it->first << "=>" << it->second;
|
||||
++it;
|
||||
for(std::map<object, object>::const_iterator it_end(val.end());
|
||||
it != it_end;
|
||||
++it) {
|
||||
s << ", " << it->first << "=>" << it->second;
|
||||
}
|
||||
}
|
||||
s << '}';
|
||||
return this; }
|
||||
public:
|
||||
void store(object k, object v) { val[k] = v; }
|
||||
private:
|
||||
std::map<object, object> val;
|
||||
};
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/object.hpp */
|
||||
|
110
cpp/test.cpp
Normal file
110
cpp/test.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
#include <iostream>
|
||||
#include <msgpack/unpack.hpp>
|
||||
|
||||
class checker {
|
||||
public:
|
||||
void check(const char* d, size_t len, msgpack::object should) {
|
||||
try {
|
||||
std::cout << "----" << std::endl;
|
||||
msgpack::object o;
|
||||
try {
|
||||
o = msgpack::unpack(d, len, m_zone);
|
||||
} catch (std::runtime_error& e) {
|
||||
std::cout << should << std::endl;
|
||||
std::cout << "**" << e.what() << "**" << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << o << std::endl;
|
||||
if(o != should) {
|
||||
std::cout << "** TEST FAILED **" << std::endl;
|
||||
}
|
||||
} catch (...) { m_zone.clear(); throw; }
|
||||
m_zone.clear();
|
||||
}
|
||||
private:
|
||||
msgpack::zone m_zone;
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
checker c;
|
||||
|
||||
{ // SimpleValue
|
||||
msgpack::zone z;
|
||||
const char d[] = {
|
||||
0x93, 0xc0, 0xc2, 0xc3,
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
z.narray(
|
||||
z.nnil(), z.nfalse(), z.ntrue()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // Fixnum
|
||||
msgpack::zone z;
|
||||
const char d[] = {
|
||||
0x92,
|
||||
0x93, 0x00, 0x40, 0x7f,
|
||||
0x93, 0xe0, 0xf0, 0xff,
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
z.narray(
|
||||
z.narray(
|
||||
z.nu8(0),
|
||||
z.nu8(64),
|
||||
z.nu8(127)
|
||||
),
|
||||
z.narray(
|
||||
z.ni8(-32),
|
||||
z.ni8(-16),
|
||||
z.ni8(-1)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // FixArray
|
||||
msgpack::zone z;
|
||||
const char d[] = {
|
||||
0x92,
|
||||
0x90,
|
||||
0x91,
|
||||
0x91, 0xc0,
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
z.narray(
|
||||
z.narray(),
|
||||
z.narray(
|
||||
z.narray(
|
||||
z.nnil()
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
{ // FixRaw
|
||||
msgpack::zone z;
|
||||
const char d[] = {
|
||||
0x94,
|
||||
0xa0,
|
||||
0xa1, 'a',
|
||||
0xa2, 'b', 'c',
|
||||
0xa3, 'd', 'e', 'f',
|
||||
};
|
||||
c.check(d, sizeof(d),
|
||||
z.narray(
|
||||
z.nraw("", 0),
|
||||
z.nraw("a", 1),
|
||||
z.nraw("bc", 2),
|
||||
z.nraw("def", 3)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
|
96
cpp/unpack.cpp
Normal file
96
cpp/unpack.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include "msgpack/unpack.hpp"
|
||||
#include "unpack_context.hpp"
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
struct unpacker::context {
|
||||
context(zone& z)
|
||||
{
|
||||
msgpack_unpacker_init(&m_ctx);
|
||||
m_ctx.user = &z;
|
||||
}
|
||||
|
||||
~context() { }
|
||||
|
||||
int execute(const void* data, size_t len, size_t* off)
|
||||
{
|
||||
return msgpack_unpacker_execute(&m_ctx, (const char*)data, len, off);
|
||||
}
|
||||
|
||||
object_class* data()
|
||||
{
|
||||
return msgpack_unpacker_data(&m_ctx);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
zone* z = m_ctx.user;
|
||||
msgpack_unpacker_init(&m_ctx);
|
||||
m_ctx.user = z;
|
||||
}
|
||||
|
||||
private:
|
||||
msgpack_unpacker m_ctx;
|
||||
|
||||
private:
|
||||
context();
|
||||
context(const context&);
|
||||
};
|
||||
|
||||
|
||||
unpacker::unpacker(zone& z) :
|
||||
m_ctx(new context(z)),
|
||||
m_zone(z),
|
||||
m_finished(false)
|
||||
{ }
|
||||
|
||||
|
||||
unpacker::~unpacker() { delete m_ctx; }
|
||||
|
||||
|
||||
size_t unpacker::execute(const void* data, size_t len, size_t off)
|
||||
{
|
||||
int ret = m_ctx->execute(data, len, &off);
|
||||
if(ret < 0) {
|
||||
throw unpack_error("parse error");
|
||||
} else if(ret > 0) {
|
||||
m_finished = true;
|
||||
return off;
|
||||
} else {
|
||||
m_finished = false;
|
||||
return off;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
object unpacker::data()
|
||||
{
|
||||
return object(m_ctx->data());
|
||||
}
|
||||
|
||||
|
||||
void unpacker::reset()
|
||||
{
|
||||
m_ctx->reset();
|
||||
}
|
||||
|
||||
|
||||
object unpacker::unpack(const void* data, size_t len, zone& z)
|
||||
{
|
||||
context ctx(z);
|
||||
size_t off = 0;
|
||||
int ret = ctx.execute(data, len, &off);
|
||||
if(ret < 0) {
|
||||
throw unpack_error("parse error");
|
||||
} else if(ret == 0) {
|
||||
throw unpack_error("insufficient bytes");
|
||||
} else if(off < len) {
|
||||
throw unpack_error("extra bytes");
|
||||
}
|
||||
return ctx.data();
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
126
cpp/unpack.h
126
cpp/unpack.h
@ -1,126 +0,0 @@
|
||||
#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 */
|
||||
|
||||
|
48
cpp/unpack.hpp
Normal file
48
cpp/unpack.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef MSGPACK_UNPACK_HPP__
|
||||
#define MSGPACK_UNPACK_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include "msgpack/zone.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
struct unpack_error : public std::runtime_error {
|
||||
unpack_error(const std::string& msg) :
|
||||
std::runtime_error(msg) { }
|
||||
};
|
||||
|
||||
|
||||
class unpacker {
|
||||
public:
|
||||
unpacker(zone& z);
|
||||
~unpacker();
|
||||
public:
|
||||
size_t execute(const void* data, size_t len, size_t off);
|
||||
bool is_finished() { return m_finished; }
|
||||
object data();
|
||||
void reset();
|
||||
private:
|
||||
struct context;
|
||||
context* m_ctx;
|
||||
zone& m_zone;
|
||||
bool m_finished;
|
||||
private:
|
||||
unpacker();
|
||||
unpacker(const unpacker&);
|
||||
public:
|
||||
static object unpack(const void* data, size_t len, zone& z);
|
||||
};
|
||||
|
||||
|
||||
inline object unpack(const void* data, size_t len, zone& z)
|
||||
{
|
||||
return unpacker::unpack(data, len, z);
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/unpack.hpp */
|
||||
|
@ -1,13 +0,0 @@
|
||||
#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 */
|
||||
|
14
cpp/unpack_context.hpp
Normal file
14
cpp/unpack_context.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef UNPACK_CONTEXT_HPP__
|
||||
#define UNPACK_CONTEXT_HPP__
|
||||
|
||||
#include "msgpack/zone.hpp"
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
typedef msgpack::object_class* msgpack_object;
|
||||
|
||||
typedef msgpack::zone* msgpack_unpack_context;
|
||||
|
||||
#include "msgpack/unpack/inline_context.h"
|
||||
|
||||
#endif /* unpack_context.h */
|
||||
|
72
cpp/unpack_inline.cpp
Normal file
72
cpp/unpack_inline.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "unpack_context.hpp"
|
||||
|
||||
|
||||
extern "C" {
|
||||
using namespace msgpack;
|
||||
|
||||
|
||||
static inline object_class* msgpack_unpack_init(zone** z)
|
||||
{ return NULL; }
|
||||
|
||||
static inline object_class* msgpack_unpack_unsigned_int_8(zone** z, uint8_t d)
|
||||
{ return (*z)->nu8(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_unsigned_int_16(zone** z, uint16_t d)
|
||||
{ return (*z)->nu16(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_unsigned_int_32(zone** z, uint32_t d)
|
||||
{ return (*z)->nu32(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_unsigned_int_64(zone** z, uint64_t d)
|
||||
{ return (*z)->nu64(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_signed_int_8(zone** z, int8_t d)
|
||||
{ return (*z)->ni8(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_signed_int_16(zone** z, int16_t d)
|
||||
{ return (*z)->ni16(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_signed_int_32(zone** z, int32_t d)
|
||||
{ return (*z)->ni32(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_signed_int_64(zone** z, int64_t d)
|
||||
{ return (*z)->ni64(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_float(zone** z, float d)
|
||||
{ return (*z)->nfloat(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_double(zone** z, double d)
|
||||
{ return (*z)->ndouble(d); }
|
||||
|
||||
static inline object_class* msgpack_unpack_nil(zone** z)
|
||||
{ return (*z)->nnil(); }
|
||||
|
||||
static inline object_class* msgpack_unpack_true(zone** z)
|
||||
{ return (*z)->ntrue(); }
|
||||
|
||||
static inline object_class* msgpack_unpack_false(zone** z)
|
||||
{ return (*z)->nfalse(); }
|
||||
|
||||
static inline object_class* msgpack_unpack_array_start(zone** z, unsigned int n)
|
||||
{ return (*z)->narray(n); }
|
||||
|
||||
static inline void msgpack_unpack_array_item(zone** z, object_class* c, object_class* o)
|
||||
{ reinterpret_cast<object_array*>(c)->push_back(o); }
|
||||
|
||||
static inline object_class* msgpack_unpack_map_start(zone** z, unsigned int n)
|
||||
{ return (*z)->narray(); }
|
||||
|
||||
static inline void msgpack_unpack_map_item(zone** z, object_class* c, object_class* k, object_class* v)
|
||||
{ reinterpret_cast<object_map*>(c)->store(k, v); }
|
||||
|
||||
static inline object_class* msgpack_unpack_string(zone** z, const void* b, size_t l)
|
||||
{ return (*z)->nraw(b, l); }
|
||||
|
||||
static inline object_class* msgpack_unpack_raw(zone** z, const void* b, size_t l)
|
||||
{ return (*z)->nraw(b, l); }
|
||||
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#include "msgpack/unpack/inline_impl.h"
|
||||
|
40
cpp/zone.cpp
Normal file
40
cpp/zone.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "zone.hpp"
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
void* zone::alloc()
|
||||
{
|
||||
if(m_used >= m_pool.size()*MSGPACK_ZONE_CHUNK_SIZE) {
|
||||
m_pool.push_back(chunk_t());
|
||||
}
|
||||
void* data = m_pool[m_used/MSGPACK_ZONE_CHUNK_SIZE].cells[m_used%MSGPACK_ZONE_CHUNK_SIZE].data;
|
||||
++m_used;
|
||||
return data;
|
||||
}
|
||||
|
||||
void zone::clear()
|
||||
{
|
||||
for(size_t b=0; b < m_used/MSGPACK_ZONE_CHUNK_SIZE; ++b) {
|
||||
cell_t* c(m_pool[b].cells);
|
||||
for(size_t e=0; e < MSGPACK_ZONE_CHUNK_SIZE; ++e) {
|
||||
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
||||
}
|
||||
}
|
||||
cell_t* c(m_pool.back().cells);
|
||||
for(size_t e=0; e < m_used%MSGPACK_ZONE_CHUNK_SIZE; ++e) {
|
||||
reinterpret_cast<object_class*>(c[e].data)->~object_class();
|
||||
}
|
||||
m_used = 0;
|
||||
m_pool.resize(1);
|
||||
for(user_finalizer_t::reverse_iterator it(m_user_finalizer.rbegin()), it_end(m_user_finalizer.rend());
|
||||
it != it_end;
|
||||
++it) {
|
||||
it->call();
|
||||
}
|
||||
m_user_finalizer.clear();
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
123
cpp/zone.hpp.erb
Normal file
123
cpp/zone.hpp.erb
Normal file
@ -0,0 +1,123 @@
|
||||
#ifndef MSGPACK_ZONE_HPP__
|
||||
#define MSGPACK_ZONE_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#ifndef MSGPACK_ZONE_CHUNK_SIZE
|
||||
#define MSGPACK_ZONE_CHUNK_SIZE 64
|
||||
#endif
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
class zone {
|
||||
public:
|
||||
zone() : m_used(0), m_pool(1) { }
|
||||
~zone() { clear(); }
|
||||
|
||||
public:
|
||||
template <typename T>
|
||||
void push_finalizer(void (*func)(void* obj, void* user), T* obj, void* user);
|
||||
|
||||
public:
|
||||
object_nil* nnil() { return new (alloc()) object_nil(); }
|
||||
object_true* ntrue() { return new (alloc()) object_true(); }
|
||||
object_false* nfalse() { return new (alloc()) object_false(); }
|
||||
object_u8* nu8( uint8_t v) { return new (alloc()) object_u8(v); }
|
||||
object_u16* nu16(uint16_t v) { return new (alloc()) object_u16(v); }
|
||||
object_u32* nu32(uint32_t v) { return new (alloc()) object_u32(v); }
|
||||
object_u64* nu64(uint64_t v) { return new (alloc()) object_u64(v); }
|
||||
object_i8* ni8( int8_t v) { return new (alloc()) object_i8(v); }
|
||||
object_i16* ni16( int16_t v) { return new (alloc()) object_i16(v); }
|
||||
object_i32* ni32( int32_t v) { return new (alloc()) object_i32(v); }
|
||||
object_i64* ni64( int64_t v) { return new (alloc()) object_i64(v); }
|
||||
object_float* nfloat( float v) { return new (alloc()) object_float(v); }
|
||||
object_double* ndouble( double v) { return new (alloc()) object_double(v); }
|
||||
|
||||
object_raw* nraw(void* ptr, uint32_t len)
|
||||
{ return new (alloc()) object_raw(ptr, len); }
|
||||
|
||||
object_const_raw* nraw(const void* ptr, uint32_t len)
|
||||
{ return new (alloc()) object_const_raw(ptr, len); }
|
||||
|
||||
object_array* narray()
|
||||
{ return new (alloc()) object_array(); }
|
||||
|
||||
object_array* narray(size_t reserve_size)
|
||||
{ return new (alloc()) object_array(reserve_size); }
|
||||
|
||||
object_map* nmap()
|
||||
{ return new (alloc()) object_map(); }
|
||||
|
||||
<% GENERATION_SIZE = 16 %>
|
||||
<% 1.upto(GENERATION_SIZE) {|i| %>
|
||||
object_array* narray(<% 1.upto(i-1) {|n| %>object o<%=n%>, <% } %>object o<%=i%>)
|
||||
{ object_array* a = new (alloc()) object_array(<%=i%>);
|
||||
<% 1.upto(i) {|n| %>a->push_back(o<%=n%>);
|
||||
<% } %>return a; }
|
||||
<% } %>
|
||||
|
||||
<% 1.upto(GENERATION_SIZE) {|i| %>
|
||||
object_map* nmap(<% 1.upto(i-1) {|n| %>object k<%=n%>, object v<%=n%>, <% } %>object k<%=i%>, object v<%=i%>)
|
||||
{ object_map* m = new (alloc()) object_map();
|
||||
<% 1.upto(i) {|n| %>m->store(k<%=n%>, v<%=n%>);
|
||||
<% } %>return m; }
|
||||
<% } %>
|
||||
|
||||
public:
|
||||
void clear();
|
||||
|
||||
private:
|
||||
void* alloc();
|
||||
|
||||
private:
|
||||
size_t m_used;
|
||||
|
||||
static const size_t MAX_OBJECT_SIZE =
|
||||
sizeof(object_raw) > sizeof(object_array)
|
||||
? ( sizeof(object_raw) > sizeof(object_map)
|
||||
? sizeof(object_raw)
|
||||
: sizeof(object_map)
|
||||
)
|
||||
: ( sizeof(object_array) > sizeof(object_map)
|
||||
? sizeof(object_array)
|
||||
: sizeof(object_map)
|
||||
)
|
||||
;
|
||||
|
||||
struct cell_t {
|
||||
char data[MAX_OBJECT_SIZE];
|
||||
};
|
||||
|
||||
struct chunk_t {
|
||||
cell_t cells[MSGPACK_ZONE_CHUNK_SIZE];
|
||||
};
|
||||
|
||||
typedef std::vector<chunk_t> pool_t;
|
||||
pool_t m_pool;
|
||||
|
||||
|
||||
class finalizer {
|
||||
public:
|
||||
finalizer(void (*func)(void*, void*), void* obj, void* user) :
|
||||
m_obj(obj), m_user(user), m_func(func) {}
|
||||
void call() { (*m_func)(m_obj, m_user); }
|
||||
private:
|
||||
void* m_obj;
|
||||
void* m_user;
|
||||
void (*m_func)(void*, void*);
|
||||
};
|
||||
|
||||
typedef std::vector<finalizer> user_finalizer_t;
|
||||
user_finalizer_t m_user_finalizer;
|
||||
|
||||
private:
|
||||
zone(const zone&);
|
||||
};
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/zone.hpp */
|
||||
|
@ -1,2 +0,0 @@
|
||||
|
||||
|
@ -23,25 +23,25 @@
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
|
||||
#define STORE_16(d) \
|
||||
#define STORE_BE16(d) \
|
||||
((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#define STORE_32(d) \
|
||||
#define STORE_BE32(d) \
|
||||
((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#define STORE_64(d) \
|
||||
#define STORE_BE64(d) \
|
||||
((char*)&d)[7], ((char*)&d)[6], ((char*)&d)[5], ((char*)&d)[4], \
|
||||
((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
|
||||
|
||||
#elif __BIG_ENDIAN__
|
||||
|
||||
#define STORE_16(d) \
|
||||
((char*)&d)[2], ((char*)&d)[3]
|
||||
#define STORE_BE16(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1]
|
||||
|
||||
#define STORE_32(d) \
|
||||
#define STORE_BE32(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3]
|
||||
|
||||
#define STORE_32(d) \
|
||||
#define STORE_BE64(d) \
|
||||
((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3], \
|
||||
((char*)&d)[4], ((char*)&d)[5], ((char*)&d)[6], ((char*)&d)[7]
|
||||
|
||||
@ -57,10 +57,10 @@ inline void msgpack_pack_int(msgpack_pack_context x, int d)
|
||||
{
|
||||
if(d < -32) {
|
||||
if(d < -32768) { // signed 32
|
||||
const unsigned char buf[5] = {0xd2, STORE_32(d)};
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
} else if(d < -128) { // signed 16
|
||||
const unsigned char buf[3] = {0xd1, STORE_16(d)};
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else { // signed 8
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d};
|
||||
@ -75,11 +75,11 @@ inline void msgpack_pack_int(msgpack_pack_context x, int d)
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
} else if(d < 65536) {
|
||||
// unsigned 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_16(d)};
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
const unsigned char buf[5] = {0xce, STORE_32(d)};
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@ -97,11 +97,11 @@ inline void msgpack_pack_unsigned_int(msgpack_pack_context x, unsigned int d)
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
} else if(d < 65536) {
|
||||
// unsigned 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_16(d)};
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
const unsigned char buf[5] = {0xce, STORE_32(d)};
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@ -118,20 +118,20 @@ inline void msgpack_pack_unsigned_int_8(msgpack_pack_context x, uint8_t d)
|
||||
|
||||
inline void msgpack_pack_unsigned_int_16(msgpack_pack_context x, uint16_t d)
|
||||
{
|
||||
const unsigned char buf[3] = {0xcd, STORE_16(d)};
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
}
|
||||
|
||||
inline void msgpack_pack_unsigned_int_32(msgpack_pack_context x, uint32_t d)
|
||||
{
|
||||
const unsigned char buf[5] = {0xce, STORE_32(d)};
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
inline void msgpack_pack_unsigned_int_64(msgpack_pack_context x, uint64_t d)
|
||||
{
|
||||
// FIXME
|
||||
const unsigned char buf[9] = {0xcf, STORE_64(d)};
|
||||
// FIXME optimization
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
@ -149,20 +149,20 @@ inline void msgpack_pack_signed_int_8(msgpack_pack_context x, int8_t d)
|
||||
|
||||
inline void msgpack_pack_signed_int_16(msgpack_pack_context x, int16_t d)
|
||||
{
|
||||
const unsigned char buf[3] = {0xd1, STORE_16(d)};
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
}
|
||||
|
||||
inline void msgpack_pack_signed_int_32(msgpack_pack_context x, int32_t d)
|
||||
{
|
||||
const unsigned char buf[5] = {0xd2, STORE_32(d)};
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
inline void msgpack_pack_signed_int_64(msgpack_pack_context x, int64_t d)
|
||||
{
|
||||
// FIXME
|
||||
const unsigned char buf[9] = {0xd3, STORE_64(d)};
|
||||
// FIXME optimization
|
||||
const unsigned char buf[9] = {0xd3, STORE_BE64(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
@ -174,14 +174,14 @@ inline void msgpack_pack_signed_int_64(msgpack_pack_context x, int64_t d)
|
||||
inline void msgpack_pack_float(msgpack_pack_context x, float d)
|
||||
{
|
||||
uint32_t n = *((uint32_t*)&d); // FIXME
|
||||
const unsigned char buf[5] = {0xca, STORE_32(n)};
|
||||
const unsigned char buf[5] = {0xca, STORE_BE32(n)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
|
||||
inline void msgpack_pack_double(msgpack_pack_context x, double d)
|
||||
{
|
||||
uint64_t n = *((uint64_t*)&d); // FIXME
|
||||
const unsigned char buf[9] = {0xcb, STORE_64(n)};
|
||||
const unsigned char buf[9] = {0xcb, STORE_BE64(n)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
|
||||
@ -200,6 +200,7 @@ inline void msgpack_pack_nil(msgpack_pack_context x)
|
||||
/*
|
||||
* Boolean
|
||||
*/
|
||||
|
||||
inline void msgpack_pack_true(msgpack_pack_context x)
|
||||
{
|
||||
static const unsigned char d = 0xc3;
|
||||
@ -224,11 +225,11 @@ inline void msgpack_pack_array(msgpack_pack_context x, unsigned int n)
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
} else if(n < 65536) {
|
||||
uint16_t d = (uint16_t)n;
|
||||
unsigned char buf[3] = {0xdc, STORE_16(d)};
|
||||
unsigned char buf[3] = {0xdc, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)n;
|
||||
unsigned char buf[5] = {0xdd, STORE_32(d)};
|
||||
unsigned char buf[5] = {0xdd, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
@ -245,18 +246,18 @@ inline void msgpack_pack_map(msgpack_pack_context x, unsigned int n)
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
} else if(n < 65536) {
|
||||
uint16_t d = (uint16_t)n;
|
||||
unsigned char buf[3] = {0xde, STORE_16(d)};
|
||||
unsigned char buf[3] = {0xde, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)n;
|
||||
unsigned char buf[5] = {0xdf, STORE_32(d)};
|
||||
unsigned char buf[5] = {0xdf, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* String
|
||||
* Raw
|
||||
*/
|
||||
|
||||
inline void msgpack_pack_string(msgpack_pack_context x, const char* b)
|
||||
@ -272,16 +273,21 @@ inline void msgpack_pack_raw(msgpack_pack_context x, const void* b, size_t l)
|
||||
msgpack_pack_append_buffer(x, &d, 1);
|
||||
} else if(l < 65536) {
|
||||
uint16_t d = (uint16_t)l;
|
||||
unsigned char buf[3] = {0xda, STORE_16(d)};
|
||||
unsigned char buf[3] = {0xda, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
uint32_t d = (uint32_t)l;
|
||||
unsigned char buf[5] = {0xdb, STORE_32(d)};
|
||||
unsigned char buf[5] = {0xdb, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
msgpack_pack_append_buffer(x, b, l);
|
||||
}
|
||||
|
||||
|
||||
#undef STORE_BE16(d)
|
||||
#undef STORE_BE32(d)
|
||||
#undef STORE_BE64(d)
|
||||
|
||||
|
||||
#endif /* msgpack/pack/inline_impl.h */
|
||||
|
||||
|
@ -25,6 +25,10 @@
|
||||
#define MSG_STACK_SIZE 16
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
msgpack_object obj;
|
||||
size_t count;
|
||||
@ -47,6 +51,9 @@ void msgpack_unpacker_init(msgpack_unpacker* ctx);
|
||||
int msgpack_unpacker_execute(msgpack_unpacker* ctx, const char* data, size_t len, size_t* off);
|
||||
#define msgpack_unpacker_data(unpacker) (unpacker)->stack[0].obj
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/unpack/inline_context.h */
|
||||
|
||||
|
@ -23,6 +23,10 @@
|
||||
#include <arpa/inet.h>
|
||||
/*#include <stdio.h>*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Positive FixNum 0xxxxxxx 0x00 - 0x7f
|
||||
// Negative FixNum 111xxxxx 0xe0 - 0xff
|
||||
// Variable 110xxxxx 0xc0 - 0xdf
|
||||
@ -71,13 +75,16 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static inline uint64_t ntohll(uint64_t x) {
|
||||
#ifdef __LITTLE_ENDIAN__ // FIXME
|
||||
#define betoh16(x) ntohs(x)
|
||||
#define betoh32(x) ntohl(x)
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
#if defined(__bswap_64)
|
||||
return __bswap_64(x);
|
||||
# define betoh64(x) __bswap_64(x)
|
||||
#elif defined(__DARWIN_OSSwapInt64)
|
||||
return __DARWIN_OSSwapInt64(x);
|
||||
# define betoh64(x) __DARWIN_OSSwapInt64(x)
|
||||
#else
|
||||
static inline uint64_t betoh64(uint64_t x) {
|
||||
return ((x << 56) & 0xff00000000000000ULL ) |
|
||||
((x << 40) & 0x00ff000000000000ULL ) |
|
||||
((x << 24) & 0x0000ff0000000000ULL ) |
|
||||
@ -86,11 +93,12 @@ static inline uint64_t ntohll(uint64_t x) {
|
||||
((x >> 24) & 0x0000000000ff0000ULL ) |
|
||||
((x >> 40) & 0x000000000000ff00ULL ) |
|
||||
((x >> 56) & 0x00000000000000ffULL ) ;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
return x;
|
||||
#define betoh64(x) (x)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
typedef enum {
|
||||
CS_HEADER = 0x00, // nil
|
||||
@ -212,9 +220,9 @@ int msgpack_unpacker_execute(msgpack_unpacker* ctx, const char* data, size_t len
|
||||
((unsigned int)*p & 0x1f)
|
||||
|
||||
#define PTR_CAST_8(ptr) (*(uint8_t*)ptr)
|
||||
#define PTR_CAST_16(ptr) ntohs(*(uint16_t*)ptr)
|
||||
#define PTR_CAST_32(ptr) ntohl(*(uint32_t*)ptr)
|
||||
#define PTR_CAST_64(ptr) ntohll(*(uint64_t*)ptr)
|
||||
#define PTR_CAST_16(ptr) betoh16(*(uint16_t*)ptr)
|
||||
#define PTR_CAST_32(ptr) betoh32(*(uint32_t*)ptr)
|
||||
#define PTR_CAST_64(ptr) betoh64(*(uint64_t*)ptr)
|
||||
|
||||
if(p == pe) { goto _out; }
|
||||
do {
|
||||
@ -434,5 +442,21 @@ _end:
|
||||
}
|
||||
|
||||
|
||||
#ifdef betoh16(x)
|
||||
#undef betoh16(x)
|
||||
#endif
|
||||
|
||||
#ifdef betoh32(x)
|
||||
#undef betoh32(x)
|
||||
#endif
|
||||
|
||||
#ifdef betoh64(x)
|
||||
#undef betoh64(x)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/unpack/inline_impl.h */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user