1 // 2 // MessagePack for C++ deflate buffer implementation 3 // 4 // Copyright (C) 2010 FURUHASHI Sadayuki 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 // 18 #ifndef MSGPACK_ZBUFFER_HPP__ 19 #define MSGPACK_ZBUFFER_HPP__ 20 21 #include "zbuffer.h" 22 #include <stdexcept> 23 24 namespace msgpack { 25 26 27 class zbuffer : public msgpack_zbuffer { 28 public: zbuffer(int level=Z_DEFAULT_COMPRESSION,size_t init_size=MSGPACK_ZBUFFER_INIT_SIZE)29 zbuffer(int level = Z_DEFAULT_COMPRESSION, 30 size_t init_size = MSGPACK_ZBUFFER_INIT_SIZE) 31 { 32 msgpack_zbuffer_init(this, level, init_size); 33 } 34 ~zbuffer()35 ~zbuffer() 36 { 37 msgpack_zbuffer_destroy(this); 38 } 39 40 public: write(const char * buf,unsigned int len)41 void write(const char* buf, unsigned int len) 42 { 43 if(msgpack_zbuffer_write(this, buf, len) < 0) { 44 throw std::bad_alloc(); 45 } 46 } 47 flush()48 char* flush() 49 { 50 char* buf = msgpack_zbuffer_flush(this); 51 if(!buf) { 52 throw std::bad_alloc(); 53 } 54 return buf; 55 } 56 data()57 char* data() 58 { 59 return base::data; 60 } 61 data() const62 const char* data() const 63 { 64 return base::data; 65 } 66 size() const67 size_t size() const 68 { 69 return msgpack_zbuffer_size(this); 70 } 71 reset()72 void reset() 73 { 74 if(!msgpack_zbuffer_reset(this)) { 75 throw std::bad_alloc(); 76 } 77 } 78 reset_buffer()79 void reset_buffer() 80 { 81 msgpack_zbuffer_reset_buffer(this); 82 } 83 release_buffer()84 char* release_buffer() 85 { 86 return msgpack_zbuffer_release_buffer(this); 87 } 88 89 private: 90 typedef msgpack_zbuffer base; 91 92 private: 93 zbuffer(const zbuffer&); 94 }; 95 96 97 } // namespace msgpack 98 99 #endif /* msgpack/zbuffer.hpp */ 100 101