diff options
| author | frsyuki <frsyuki@users.sourceforge.jp> | 2010-02-06 20:09:41 +0900 |
|---|---|---|
| committer | frsyuki <frsyuki@users.sourceforge.jp> | 2010-02-06 20:09:41 +0900 |
| commit | cd10fbc1fe39308045f0b7bf9cce5f8153f25f1d (patch) | |
| tree | 44e4a1e075deebc3339cdd44e1f550a3ea914e88 /cpp/msgpack/sbuffer.hpp | |
| parent | ca0b085d156260b6d916139495a6d306a1e7f886 (diff) | |
| download | msgpack-python-cd10fbc1fe39308045f0b7bf9cce5f8153f25f1d.tar.gz | |
removed symbolic links
Diffstat (limited to 'cpp/msgpack/sbuffer.hpp')
| -rw-r--r-- | cpp/msgpack/sbuffer.hpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cpp/msgpack/sbuffer.hpp b/cpp/msgpack/sbuffer.hpp new file mode 100644 index 0000000..ca06884e --- /dev/null +++ b/cpp/msgpack/sbuffer.hpp @@ -0,0 +1,103 @@ +// +// MessagePack for C++ simple buffer implementation +// +// Copyright (C) 2008-2009 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#ifndef MSGPACK_SBUFFER_HPP__ +#define MSGPACK_SBUFFER_HPP__ + +#include "msgpack/sbuffer.h" +#include <stdexcept> + +namespace msgpack { + + +class sbuffer : public msgpack_sbuffer { +public: + sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE) + { + base::data = (char*)::malloc(initsz); + if(!base::data) { + throw std::bad_alloc(); + } + + base::size = 0; + base::alloc = initsz; + } + + ~sbuffer() + { + ::free(base::data); + } + +public: + void write(const char* buf, unsigned int len) + { + if(base::alloc - base::size < len) { + expand_buffer(len); + } + memcpy(base::data + base::size, buf, len); + base::size += len; + } + + char* data() + { + return base::data; + } + + const char* data() const + { + return base::data; + } + + size_t size() const + { + return base::size; + } + + char* release() + { + return msgpack_sbuffer_release(this); + } + +private: + void expand_buffer(size_t len) + { + size_t nsize = (base::alloc) ? + base::alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE; + + while(nsize < base::size + len) { nsize *= 2; } + + void* tmp = realloc(base::data, nsize); + if(!tmp) { + throw std::bad_alloc(); + } + + base::data = (char*)tmp; + base::alloc = nsize; + } + +private: + typedef msgpack_sbuffer base; + +private: + sbuffer(const sbuffer&); +}; + + +} // namespace msgpack + +#endif /* msgpack/sbuffer.hpp */ + |
