mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-29 23:42:40 +02:00
lang/c/msgpack: C++ binding: changed calback function of packer from Stream& append(const char*, size_t) to SomeType write(SomePointerType, SomeSizeType)
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@62 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
4d13f614b6
commit
a0a798d79e
@ -49,14 +49,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
inline simple_buffer& append(const char* buf, size_t len)
|
inline void write(const void* buf, size_t len)
|
||||||
{
|
{
|
||||||
if(m_allocated - m_used < len) {
|
if(m_allocated - m_used < len) {
|
||||||
expand_buffer(len);
|
expand_buffer(len);
|
||||||
}
|
}
|
||||||
memcpy(m_storage + m_used, buf, len);
|
memcpy(m_storage + m_used, buf, len);
|
||||||
m_used += len;
|
m_used += len;
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
|
47
cpp/pack.hpp
47
cpp/pack.hpp
@ -73,7 +73,7 @@ private:
|
|||||||
static void pack_string_impl(Stream& x, const char* b);
|
static void pack_string_impl(Stream& x, const char* b);
|
||||||
static void pack_raw_impl(Stream& x, const void* b, size_t l);
|
static void pack_raw_impl(Stream& x, const void* b, size_t l);
|
||||||
static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len)
|
static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len)
|
||||||
{ x.append((const char*)buf, len); }
|
{ x.write((const char*)buf, len); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Stream& m_stream;
|
Stream& m_stream;
|
||||||
@ -100,14 +100,13 @@ public:
|
|||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
dynamic_stream(Stream& s);
|
dynamic_stream(Stream& s);
|
||||||
public:
|
public:
|
||||||
dynamic_stream& append(const char* buf, size_t len)
|
void write(const char* buf, size_t len)
|
||||||
{ (*m_function)(m_object, buf, len); return *this; }
|
{ (*m_function)(m_object, buf, len); }
|
||||||
private:
|
private:
|
||||||
void* m_object;
|
void* m_object;
|
||||||
void (*m_function)(void* object, const char* buf, size_t len);
|
void (*m_function)(void* object, const char* buf, size_t len);
|
||||||
private:
|
private:
|
||||||
template <typename Stream, Stream& (Stream::*MemFun)(const char*, size_t)>
|
struct write_trampoline;
|
||||||
static void append_trampoline(void* object, const char* buf, size_t len);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -137,8 +136,6 @@ public:
|
|||||||
void pack_string(const char* b) { pack_string_impl(m_stream, b); }
|
void pack_string(const char* b) { pack_string_impl(m_stream, b); }
|
||||||
void pack_raw(const void* b, size_t l) { pack_raw_impl(m_stream, b, l); }
|
void pack_raw(const void* b, size_t l) { pack_raw_impl(m_stream, b, l); }
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void pack_int_impl(dynamic_stream& x, int d);
|
static void pack_int_impl(dynamic_stream& x, int d);
|
||||||
static void pack_unsigned_int_impl(dynamic_stream& x, unsigned int d);
|
static void pack_unsigned_int_impl(dynamic_stream& x, unsigned int d);
|
||||||
@ -160,7 +157,7 @@ private:
|
|||||||
static void pack_string_impl(dynamic_stream& x, const char* b);
|
static void pack_string_impl(dynamic_stream& x, const char* b);
|
||||||
static void pack_raw_impl(dynamic_stream& x, const void* b, size_t l);
|
static void pack_raw_impl(dynamic_stream& x, const void* b, size_t l);
|
||||||
static void append_buffer(dynamic_stream& x, const unsigned char* buf, unsigned int len)
|
static void append_buffer(dynamic_stream& x, const unsigned char* buf, unsigned int len)
|
||||||
{ x.append((const char*)buf, len); }
|
{ x.write((const char*)buf, len); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
dynamic_stream m_stream;
|
dynamic_stream m_stream;
|
||||||
@ -181,17 +178,37 @@ private:
|
|||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
dynamic_packer::dynamic_packer(Stream& s) : m_stream(s) { }
|
dynamic_packer::dynamic_packer(Stream& s) : m_stream(s) { }
|
||||||
|
|
||||||
|
struct dynamic_stream::write_trampoline {
|
||||||
|
private:
|
||||||
|
template <typename R>
|
||||||
|
struct ret_type {
|
||||||
|
typedef R (*type)(void*, const char*, size_t);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Stream, typename R,
|
||||||
|
typename Ptr, typename Sz, R (Stream::*MemFun)(Ptr*, Sz)>
|
||||||
|
static R trampoline(void* obj, const char* buf, size_t len)
|
||||||
|
{
|
||||||
|
return (reinterpret_cast<Stream*>(obj)->*MemFun)(buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <typename Stream, typename R, typename Ptr, typename Sz>
|
||||||
|
static typename ret_type<R>::type get(R (Stream::*func)(Ptr*, Sz))
|
||||||
|
{
|
||||||
|
R (*f)(void*, const char*, size_t) =
|
||||||
|
&trampoline<Stream, R, Ptr, Sz, &Stream::write>;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
dynamic_stream::dynamic_stream(Stream& s)
|
dynamic_stream::dynamic_stream(Stream& s)
|
||||||
{
|
{
|
||||||
m_object = reinterpret_cast<void*>(&s);
|
m_object = reinterpret_cast<void*>(&s);
|
||||||
m_function = &dynamic_stream::append_trampoline<Stream, &Stream::append>;
|
m_function = reinterpret_cast<void (*)(void*, const char*, size_t)>(
|
||||||
}
|
write_trampoline::get(&Stream::write)
|
||||||
|
);
|
||||||
template <typename Stream, Stream& (Stream::*MemFun)(const char*, size_t)>
|
|
||||||
void dynamic_stream::append_trampoline(void* object, const char* buf, size_t len)
|
|
||||||
{
|
|
||||||
(reinterpret_cast<Stream*>(object)->*MemFun)(buf, len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,9 +28,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string s;
|
std::stringstream s;
|
||||||
msgpack::pack(s, o);
|
msgpack::pack(s, o);
|
||||||
object ro = msgpack::unpack(s.data(), s.size(), m_zone);
|
std::string str(s.str());
|
||||||
|
object ro = msgpack::unpack(str.data(), str.size(), m_zone);
|
||||||
if(ro != o) { throw std::runtime_error("NOT MATCH"); }
|
if(ro != o) { throw std::runtime_error("NOT MATCH"); }
|
||||||
} catch (std::runtime_error& e) {
|
} catch (std::runtime_error& e) {
|
||||||
std::cout << "** REUNPACK FAILED **" << std::endl;
|
std::cout << "** REUNPACK FAILED **" << std::endl;
|
||||||
|
@ -99,10 +99,10 @@ void unpacker::expand_buffer(size_t len)
|
|||||||
else { next_size = UNPACKER_INITIAL_BUFFER_SIZE; }
|
else { next_size = UNPACKER_INITIAL_BUFFER_SIZE; }
|
||||||
while(next_size < len + m_used) { next_size *= 2; }
|
while(next_size < len + m_used) { next_size *= 2; }
|
||||||
|
|
||||||
void* tmp = realloc(m_buffer, next_size);
|
char* tmp = (char*)realloc(m_buffer, next_size);
|
||||||
if(!tmp) { throw std::bad_alloc(); }
|
if(!tmp) { throw std::bad_alloc(); }
|
||||||
m_buffer = tmp;
|
m_buffer = tmp;
|
||||||
//void* tmp = malloc(next_size);
|
//char* tmp = (char*)malloc(next_size);
|
||||||
//if(!tmp) { throw std::bad_alloc(); }
|
//if(!tmp) { throw std::bad_alloc(); }
|
||||||
//memcpy(tmp, m_buffer, m_used);
|
//memcpy(tmp, m_buffer, m_used);
|
||||||
//free(m_buffer);
|
//free(m_buffer);
|
||||||
@ -114,9 +114,9 @@ void unpacker::expand_buffer(size_t len)
|
|||||||
size_t next_size = UNPACKER_INITIAL_BUFFER_SIZE;
|
size_t next_size = UNPACKER_INITIAL_BUFFER_SIZE;
|
||||||
while(next_size < len + m_used - m_off) { next_size *= 2; }
|
while(next_size < len + m_used - m_off) { next_size *= 2; }
|
||||||
|
|
||||||
void* tmp = malloc(next_size);
|
char* tmp = (char*)malloc(next_size);
|
||||||
if(!tmp) { throw std::bad_alloc(); }
|
if(!tmp) { throw std::bad_alloc(); }
|
||||||
memcpy(tmp, ((char*)m_buffer)+m_off, m_used-m_off);
|
memcpy(tmp, m_buffer+m_off, m_used-m_off);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
m_zone->push_finalizer<void>(&zone::finalize_free, NULL, m_buffer);
|
m_zone->push_finalizer<void>(&zone::finalize_free, NULL, m_buffer);
|
||||||
|
@ -48,7 +48,7 @@ public:
|
|||||||
void reserve_buffer(size_t len);
|
void reserve_buffer(size_t len);
|
||||||
|
|
||||||
/*! 2. read data to the buffer() up to buffer_capacity() bytes */
|
/*! 2. read data to the buffer() up to buffer_capacity() bytes */
|
||||||
void* buffer();
|
char* buffer();
|
||||||
size_t buffer_capacity() const;
|
size_t buffer_capacity() const;
|
||||||
|
|
||||||
/*! 3. specify the number of bytes actually copied */
|
/*! 3. specify the number of bytes actually copied */
|
||||||
@ -74,7 +74,7 @@ public:
|
|||||||
// Note that there are no parsed buffer when execute() returned true.
|
// Note that there are no parsed buffer when execute() returned true.
|
||||||
|
|
||||||
/*! get address of buffer that is not parsed */
|
/*! get address of buffer that is not parsed */
|
||||||
void* nonparsed_buffer();
|
char* nonparsed_buffer();
|
||||||
size_t nonparsed_size() const;
|
size_t nonparsed_size() const;
|
||||||
|
|
||||||
/*! get the number of bytes that is already parsed */
|
/*! get the number of bytes that is already parsed */
|
||||||
@ -90,7 +90,7 @@ private:
|
|||||||
struct context;
|
struct context;
|
||||||
context* m_ctx;
|
context* m_ctx;
|
||||||
|
|
||||||
void* m_buffer;
|
char* m_buffer;
|
||||||
size_t m_used;
|
size_t m_used;
|
||||||
size_t m_free;
|
size_t m_free;
|
||||||
size_t m_off;
|
size_t m_off;
|
||||||
@ -110,8 +110,8 @@ inline void unpacker::reserve_buffer(size_t len)
|
|||||||
expand_buffer(len);
|
expand_buffer(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void* unpacker::buffer()
|
inline char* unpacker::buffer()
|
||||||
{ return (void*)(((char*)m_buffer)+m_used); }
|
{ return m_buffer + m_used; }
|
||||||
|
|
||||||
inline size_t unpacker::buffer_capacity() const
|
inline size_t unpacker::buffer_capacity() const
|
||||||
{ return m_free; }
|
{ return m_free; }
|
||||||
@ -123,8 +123,8 @@ inline void unpacker::buffer_consumed(size_t len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void* unpacker::nonparsed_buffer()
|
inline char* unpacker::nonparsed_buffer()
|
||||||
{ return (void*)(((char*)m_buffer)+m_off); }
|
{ return m_buffer + m_off; }
|
||||||
|
|
||||||
inline size_t unpacker::nonparsed_size() const
|
inline size_t unpacker::nonparsed_size() const
|
||||||
{ return m_used - m_off; }
|
{ return m_used - m_off; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user