Added reference version of unpacker::next().

This commit is contained in:
Takatoshi Kondo 2014-08-04 00:52:04 +09:00
parent e4d32b176e
commit fd566afeb4
2 changed files with 57 additions and 5 deletions

View File

@ -786,6 +786,7 @@ public:
/*! 4. repeat next() until it retunrs false */ /*! 4. repeat next() until it retunrs false */
bool next(unpacked* result); bool next(unpacked* result);
bool next(unpacked& result);
/*! 5. check if the size of message doesn't exceed assumption. */ /*! 5. check if the size of message doesn't exceed assumption. */
std::size_t message_size() const; std::size_t message_size() const;
@ -1058,7 +1059,7 @@ inline void unpacker::buffer_consumed(std::size_t size)
m_free -= size; m_free -= size;
} }
inline bool unpacker::next(unpacked* result) inline bool unpacker::next(unpacked& result)
{ {
int ret = execute_imp(); int ret = execute_imp();
@ -1067,18 +1068,23 @@ inline bool unpacker::next(unpacked* result)
} }
if(ret == 0) { if(ret == 0) {
result->zone().reset(); result.zone().reset();
result->set(object()); result.set(object());
return false; return false;
} else { } else {
result->zone().reset( release_zone() ); result.zone().reset( release_zone() );
result->set(data()); result.set(data());
reset(); reset();
return true; return true;
} }
} }
inline bool unpacker::next(unpacked* result)
{
return next(*result);
}
inline bool unpacker::execute() inline bool unpacker::execute()
{ {

View File

@ -17,6 +17,52 @@ TEST(streaming, basic)
msgpack::unpacker pac; msgpack::unpacker pac;
msgpack::unpacked result; msgpack::unpacked result;
int count = 0;
while(count < 3) {
pac.reserve_buffer(32*1024);
// read buffer into pac.buffer() upto
// pac.buffer_capacity() bytes.
size_t len = 1;
memcpy(pac.buffer(), input, len);
input += len;
pac.buffer_consumed(len);
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;
}
}
EXPECT_TRUE(input < eof);
}
}
TEST(streaming, basic_pointer)
{
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack(1);
pk.pack(2);
pk.pack(3);
const char* input = buffer.data();
const char* const eof = input + buffer.size();
msgpack::unpacker pac;
msgpack::unpacked result;
int count = 0; int count = 0;
while(count < 3) { while(count < 3) {
pac.reserve_buffer(32*1024); pac.reserve_buffer(32*1024);