4 #ifndef VAR_CHAR_BUF_H_INCLUDED 5 #define VAR_CHAR_BUF_H_INCLUDED 18 template <
size_t SIZE,
20 class Traits = std::char_traits<CharT>,
21 class Alloc = std::allocator<CharT> >
27 typedef value_type* pointer;
28 typedef const value_type* const_pointer;
29 typedef value_type& reference;
30 typedef const value_type& const_reference;
31 typedef size_t size_type;
32 typedef ptrdiff_t difference_type;
33 typedef const value_type* const_iterator;
34 typedef value_type* iterator;
35 typedef std::reverse_iterator<const_iterator>
36 const_reverse_iterator;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef Alloc allocator_type;
42 static const size_type
npos;
48 _num_chars_allocated(0) {}
53 _num_chars_allocated(0) {
54 range_initialize(s.begin(), s.end());
61 range_initialize(s.begin(), s.end());
65 template <
class InputIter>
66 void insert(iterator pos, InputIter first, InputIter last) {
68 size_type xtra = std::distance(first,last);
69 size_type newlen = _end-
_buffer+xtra;
70 if (newlen >= capacity()) {
76 Traits::move(pos+xtra,pos,_end-pos);
77 std::copy(first,last,pos);
83 iterator erase(iterator first, iterator last) {
85 Traits::move(first, last, (_end - last) + 1);
86 const iterator new_finish = _end - (last - first);
100 iterator begin() {
return _buffer;}
101 const_iterator begin()
const {
return _buffer;}
102 iterator end() {
return _end;}
103 const_iterator end()
const {
return _end;}
116 return std::min((
size_t)0xFFFF,
_allocator.max_size());
118 bool empty()
const {
return (_end ==
_buffer); }
119 size_type size()
const {
return _end -
_buffer;}
123 if (_buffer == _internal_buf)
124 for(
int i=0;i<SIZE;++i) {
125 std::swap(_buffer[i],s.
_buffer[i]);
129 std::swap(_num_chars_allocated,s._num_chars_allocated);
136 if (requested_chars < SIZE && (
_buffer==_internal_buf))
138 if (requested_chars < _num_chars_allocated)
141 throw std::length_error(
"Max size exceeded");
144 CharT *p =
_allocator.allocate(requested_chars+1);
147 size_type sz = size();
152 _num_chars_allocated = requested_chars+1;
155 size_type capacity()
const {
156 return _num_chars_allocated ?
157 _num_chars_allocated : SIZE;
160 const CharT* c_str()
const {
165 const CharT* data()
const {
170 void release_resources() {
171 if (
_buffer != _internal_buf) {
173 _num_chars_allocated);
178 void element_initialize(size_type n, value_type c) {
180 std::uninitialized_fill_n(begin(), n, c);
185 template <
class InputIter>
186 void range_initialize(InputIter first, InputIter last) {
187 size_type n = std::distance(first,last);
190 std::uninitialized_copy(first,last,
_buffer);
196 mutable CharT *_end ;
198 CharT _internal_buf[SIZE+1];
199 size_type _num_chars_allocated;
205 template <
size_t SIZE,
class CharT,
class Traits,
class Alloc>
206 const var_char_buf<SIZE,CharT,Traits,Alloc>::size_type
208 = (var_char_buf<SIZE,CharT,Traits,Alloc>::size_type) -1;
212 template <
size_t SIZE,
class CharT,
class Traits,
class Alloc>
213 var_char_buf<SIZE,CharT,Traits,Alloc>::allocator_type
CharT value_type
STL container interface typedefs:
Definition: var_char_buf.h:26
static const size_type npos
Definition of "npos" constant:
Definition: var_char_buf.h:42
static allocator_type _allocator
Definition of _allocator instance, shared between all strings of a given type:
Definition: var_char_buf.h:200
CharT * _buffer
A variable size buffer implementation:
Definition: var_char_buf.h:195
const allocator_type & get_allocator() const
(Replace can be defined in terms of erase and insert, so the minimal interface can skip it...
Definition: var_char_buf.h:96
This class implements the minimal string interface and is intended to be used as a base class for xst...
Definition: var_char_buf.h:22
void reserve(size_type requested_chars=0)
Definition: var_char_buf.h:135
Traits traits_type
String interface typedefs:
Definition: var_char_buf.h:41
size_t max_size() const
Proper thing to do is probably to return _allocator.max_size(), but this causes problems: My default ...
Definition: var_char_buf.h:115