Changeset 18666 for lang/c/msgpack

Show
Ignore:
Timestamp:
09/02/08 22:14:58 (5 years ago)
Author:
frsyuki
Message:

lang/c/msgpack: C++ binding: support non-MessagePack? message that follows after MessagePack? message

Location:
lang/c/msgpack/trunk/cpp
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/c/msgpack/trunk/cpp/test.cpp

    r18657 r18666  
    170170                while(stream.good() && total_bytes > 0) { 
    171171 
     172                        // 1. reserve buffer 
    172173                        upk.reserve_buffer(RESERVE_SIZE); 
     174 
     175                        // 2. read data to buffer() up to buffer_capacity() bytes 
    173176                        size_t sz = stream.readsome( 
    174177                                        (char*)upk.buffer(), 
     
    180183                                        << std::endl; 
    181184 
     185                        // 3. specify the number of  bytes actually copied 
    182186                        upk.buffer_consumed(sz); 
     187 
     188                        // 4. repeat execute() until it returns false 
    183189                        while( upk.execute() ) { 
    184190                                std::cout << "message parsed" << std::endl; 
     191 
     192                                // 5.1. take out the parsed object 
     193                                msgpack::object o = upk.data(); 
     194 
     195                                // 5.2. the parsed object is valid until the zone is deleted 
    185196                                boost::scoped_ptr<msgpack::zone> pz(upk.release_zone()); 
    186                                 msgpack::object o = upk.data(); 
    187                                 upk.reset(); 
     197 
    188198                                std::cout << o << std::endl; 
    189199                                ++num_msg; 
     200 
     201                                // 5.3 re-initialize unpacker 
     202                                upk.reset(); 
    190203                        } 
    191204 
  • lang/c/msgpack/trunk/cpp/unpack.cpp

    r18657 r18666  
    8383 
    8484                // FIXME realloc? 
    85  
    8685                void* tmp = malloc(next_size); 
    8786                if(!tmp) { throw std::bad_alloc(); } 
     
    122121                return false; 
    123122        } else { 
     123                expand_buffer(0); 
    124124                return true; 
    125125        } 
     
    128128zone* unpacker::release_zone() 
    129129{ 
     130        zone* nz = new zone(); 
    130131        zone* z = m_zone; 
    131         m_zone = NULL; 
    132         m_zone = new zone(); 
     132        m_zone = nz; 
    133133        m_ctx->user(m_zone); 
    134134        return z; 
     
    142142void unpacker::reset() 
    143143{ 
     144        if(m_off != 0) { expand_buffer(0); } 
    144145        if(!m_zone->empty()) { 
    145146                delete m_zone; 
     
    147148                m_zone = new zone(); 
    148149        } 
    149         expand_buffer(0); 
    150150        m_ctx->reset(); 
    151151} 
  • lang/c/msgpack/trunk/cpp/unpack.hpp

    r18657 r18666  
    2525 
    2626public: 
     27        /*! 1. reserve buffer. at least `len' bytes of capacity will be ready */ 
    2728        void reserve_buffer(size_t len); 
     29 
     30        /*! 2. read data to the buffer() up to buffer_capacity() bytes */ 
    2831        void* buffer(); 
    2932        size_t buffer_capacity() const; 
     33 
     34        /*! 3. specify the number of bytes actually copied */ 
    3035        void buffer_consumed(size_t len); 
     36 
     37        /*! 4. repeat execute() until it retunrs false */ 
    3138        bool execute(); 
    32         zone* release_zone();  // never throw 
     39 
     40        /*! 5.1. if execute() returns true, take out the parsed object */ 
    3341        object data(); 
     42 
     43        /*! 5.2. the parsed object is valid until the zone is deleted */ 
     44        // Note that once release_zone() from unpacker, you must delete it 
     45        // otherwise the memrory will leak. 
     46        zone* release_zone(); 
     47 
     48        /*! 5.3. after release_zone(), re-initialize unpacker */ 
    3449        void reset(); 
     50 
     51public: 
     52        // These functions are usable when non-MessagePack message follows after 
     53        // MessagePack message. 
     54        // Note that there are no parsed buffer when execute() returned true. 
     55 
     56        /*! get address of buffer that is not parsed */ 
     57        void* nonparsed_buffer(); 
     58        size_t nonparsed_size() const; 
     59 
     60        /*! get the number of bytes that is already parsed */ 
     61        size_t parsed_size() const; 
     62 
     63        /*! remove unparsed buffer from unpacker */ 
     64        // Note that reset() leaves non-parsed buffer. 
     65        void remove_nonparsed_buffer(); 
    3566 
    3667private: 
     
    73104 
    74105 
     106inline void* unpacker::nonparsed_buffer() 
     107        { return (void*)(((char*)m_buffer)+m_off); } 
     108 
     109inline size_t unpacker::nonparsed_size() const 
     110        { return m_used - m_off; } 
     111 
     112inline size_t unpacker::parsed_size() const 
     113        { return m_off; } 
     114 
     115inline void unpacker::remove_nonparsed_buffer() 
     116        { m_used = m_off; } 
     117 
     118 
    75119inline object unpack(const void* data, size_t len, zone& z) 
    76120{