Buffer.h
Go to the documentation of this file.
00001 /* Copyright 2006 Savarese Software Research Corporation 00002 * 00003 * Licensed under the Apache License, Version 2.0 (the "License"); 00004 * you may not use this file except in compliance with the License. 00005 * You may obtain a copy of the License at 00006 * 00007 * http://www.savarese.com/software/ApacheLicense-2.0 00008 * 00009 * Unless required by applicable law or agreed to in writing, software 00010 * distributed under the License is distributed on an "AS IS" BASIS, 00011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 * See the License for the specific language governing permissions and 00013 * limitations under the License. 00014 */ 00015 00021 #ifndef __SSRC_SPREAD_DETAIL_BUFFER_H 00022 #define __SSRC_SPREAD_DETAIL_BUFFER_H 00023 00024 #include <cstring> 00025 #include <iterator> 00026 00027 #include <ssrc/libssrcspread-packages.h> 00028 00029 __BEGIN_NS_SSRC_SPREAD 00030 00031 namespace detail { 00032 00048 template<typename type> 00049 class Buffer { 00050 00051 unsigned int _capacity; 00052 unsigned int _size; 00053 type * _data; 00054 00055 public: 00056 typedef type value_type; 00057 typedef value_type & reference; 00058 typedef const reference const_reference; 00059 typedef value_type* pointer; 00060 typedef const value_type* const_pointer; 00061 typedef pointer iterator; 00062 typedef const_pointer const_iterator; 00063 typedef std::reverse_iterator<iterator> reverse_iterator; 00064 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00065 00071 explicit Buffer(const unsigned int capacity) : 00072 _capacity(capacity), _size(0), _data(new value_type[capacity]) 00073 { } 00074 00078 ~Buffer() { delete[] _data; } 00079 00086 unsigned int capacity() const { 00087 return _capacity; 00088 } 00089 00096 reference operator[](const unsigned int index) { 00097 return _data[index]; 00098 } 00099 00106 const_reference operator[](const unsigned int index) const { 00107 return _data[index]; 00108 } 00109 00115 iterator begin() { 00116 return _data; 00117 } 00118 00124 const_iterator begin() const { 00125 return _data; 00126 } 00127 00135 iterator end() { 00136 return (_data + _size); 00137 } 00138 00146 const_iterator end() const { 00147 return (_data + _size); 00148 } 00149 00155 reverse_iterator rbegin() { 00156 return reverse_iterator(end()); 00157 } 00158 00164 const_reverse_iterator rbegin() const { 00165 return const_reverse_iterator(end()); 00166 } 00167 00173 reverse_iterator rend() { 00174 return reverse_iterator(begin()); 00175 } 00176 00182 const_reverse_iterator rend() const { 00183 return const_reverse_iterator(begin()); 00184 } 00185 00195 void reserve(const unsigned int capacity) { 00196 if(capacity > _capacity) { 00197 pointer new_data = new value_type[capacity]; 00198 std::memcpy(new_data, _data, _size*sizeof(value_type)); 00199 delete[] _data; 00200 _data = new_data; 00201 _capacity = capacity; 00202 } 00203 } 00204 00214 void resize(const unsigned int size) { 00215 reserve(size); 00216 _size = size; 00217 } 00218 00223 unsigned int size() const { 00224 return _size; 00225 } 00226 00228 void clear() { 00229 resize(0); 00230 } 00231 00241 void add(const type & value) { 00242 if(capacity() <= _size) { 00243 reserve(_size << 1); 00244 } 00245 _data[_size++] = value; 00246 } 00247 }; 00248 } 00249 00250 __END_NS_SSRC_SPREAD 00251 00252 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.
Copyright © 2011 Savarese Software Research Corporation. All rights reserved