00001 /* 00002 * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #ifndef BUFFER_H_ 00020 #define BUFFER_H_ 00021 00022 #include "Pool.h" 00023 #include "FastAlloc.h" 00024 00025 namespace adchpp { 00026 00030 class Buffer : 00031 public FastAlloc<Buffer>, 00032 private boost::noncopyable 00033 { 00034 public: 00035 Buffer(const std::string& str) : bufp(pool.get()) { append((uint8_t*)str.data(), (uint8_t*)str.data() + str.size()); } 00036 Buffer(const void* ptr, const size_t size) : bufp(pool.get()) { append((uint8_t*) ptr, ((uint8_t*)ptr)+size); } 00037 Buffer(const size_t size) : bufp(pool.get()) { resize(size); } 00038 00039 operator const ByteVector&() const { return buf(); } 00040 operator ByteVector&() { return buf(); } 00041 00042 void resize(size_t new_size) { buf().resize(new_size); } 00043 size_t size() const { return buf().size(); } 00044 const uint8_t* data() const { return &buf()[0]; } 00045 uint8_t* data() { return &buf()[0]; } 00046 00048 void erase_first(size_t n) { buf().erase(buf().begin(), buf().begin() + n); } 00049 00050 template<typename InputIterator> 00051 void append(InputIterator start, InputIterator end) { buf().insert(buf().end(), start, end); } 00052 00053 static void setDefaultBufferSize(size_t newSize) { defaultBufferSize = newSize; } 00054 static size_t getDefaultBufferSize() { return defaultBufferSize; } 00055 00056 virtual ~Buffer() { pool.put(bufp); } 00057 private: 00058 static size_t defaultBufferSize; 00059 00060 const ByteVector& buf() const { return *bufp; } 00061 ByteVector& buf() { return *bufp; } 00062 00063 ByteVector* bufp; 00064 00065 struct Clear { 00066 ADCHPP_DLL void operator()(ByteVector& x); 00067 }; 00068 00069 ADCHPP_DLL static SimplePool<ByteVector, Clear> pool; 00070 }; 00071 00072 typedef shared_ptr<Buffer> BufferPtr; 00073 typedef std::vector<BufferPtr> BufferList; 00074 00075 } 00076 00077 #endif /*BUFFER_H_*/