2007-01-28 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / libstdc++-v3 / include / ext / sso_string_base.h
blobc95b48ecd8d0e53084a9d8c3c8b26e672173687a
1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file ext/sso_string_base.h
31 * This file is a GNU extension to the Standard C++ Library.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #ifndef _SSO_STRING_BASE_H
37 #define _SSO_STRING_BASE_H 1
39 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
41 template<typename _CharT, typename _Traits, typename _Alloc>
42 class __sso_string_base
43 : protected __vstring_utility<_CharT, _Traits, _Alloc>
45 public:
46 typedef _Traits traits_type;
47 typedef typename _Traits::char_type value_type;
49 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
50 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
51 typedef typename _CharT_alloc_type::size_type size_type;
53 private:
54 // Data Members:
55 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
56 _M_dataplus;
57 size_type _M_string_length;
59 enum { _S_local_capacity = 15 };
61 union
63 _CharT _M_local_data[_S_local_capacity + 1];
64 size_type _M_allocated_capacity;
67 void
68 _M_data(_CharT* __p)
69 { _M_dataplus._M_p = __p; }
71 void
72 _M_length(size_type __length)
73 { _M_string_length = __length; }
75 void
76 _M_capacity(size_type __capacity)
77 { _M_allocated_capacity = __capacity; }
79 bool
80 _M_is_local() const
81 { return _M_data() == _M_local_data; }
83 // Create & Destroy
84 _CharT*
85 _M_create(size_type&, size_type);
87 void
88 _M_dispose()
90 if (!_M_is_local())
91 _M_destroy(_M_allocated_capacity);
94 void
95 _M_destroy(size_type __size) throw()
96 { _M_get_allocator().deallocate(_M_data(), __size + 1); }
98 // _M_construct_aux is used to implement the 21.3.1 para 15 which
99 // requires special behaviour if _InIterator is an integral type
100 template<typename _InIterator>
101 void
102 _M_construct_aux(_InIterator __beg, _InIterator __end,
103 std::__false_type)
105 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
106 _M_construct(__beg, __end, _Tag());
109 template<typename _InIterator>
110 void
111 _M_construct_aux(_InIterator __beg, _InIterator __end,
112 std::__true_type)
113 { _M_construct(static_cast<size_type>(__beg),
114 static_cast<value_type>(__end)); }
116 template<typename _InIterator>
117 void
118 _M_construct(_InIterator __beg, _InIterator __end)
120 typedef typename std::__is_integer<_InIterator>::__type _Integral;
121 _M_construct_aux(__beg, __end, _Integral());
124 // For Input Iterators, used in istreambuf_iterators, etc.
125 template<typename _InIterator>
126 void
127 _M_construct(_InIterator __beg, _InIterator __end,
128 std::input_iterator_tag);
130 // For forward_iterators up to random_access_iterators, used for
131 // string::iterator, _CharT*, etc.
132 template<typename _FwdIterator>
133 void
134 _M_construct(_FwdIterator __beg, _FwdIterator __end,
135 std::forward_iterator_tag);
137 void
138 _M_construct(size_type __req, _CharT __c);
140 public:
141 size_type
142 _M_max_size() const
143 { return (_M_get_allocator().max_size() - 1) / 2; }
145 _CharT*
146 _M_data() const
147 { return _M_dataplus._M_p; }
149 size_type
150 _M_length() const
151 { return _M_string_length; }
153 size_type
154 _M_capacity() const
156 return _M_is_local() ? size_type(_S_local_capacity)
157 : _M_allocated_capacity;
160 bool
161 _M_is_shared() const
162 { return false; }
164 void
165 _M_set_leaked() { }
167 void
168 _M_leak() { }
170 void
171 _M_set_length(size_type __n)
173 _M_length(__n);
174 traits_type::assign(_M_data()[__n], _CharT());
177 __sso_string_base()
178 : _M_dataplus(_Alloc(), _M_local_data)
179 { _M_set_length(0); }
181 __sso_string_base(const _Alloc& __a);
183 __sso_string_base(const __sso_string_base& __rcs);
185 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
187 template<typename _InputIterator>
188 __sso_string_base(_InputIterator __beg, _InputIterator __end,
189 const _Alloc& __a);
191 ~__sso_string_base()
192 { _M_dispose(); }
194 _CharT_alloc_type&
195 _M_get_allocator()
196 { return _M_dataplus; }
198 const _CharT_alloc_type&
199 _M_get_allocator() const
200 { return _M_dataplus; }
202 void
203 _M_swap(__sso_string_base& __rcs);
205 void
206 _M_assign(const __sso_string_base& __rcs);
208 void
209 _M_reserve(size_type __res);
211 void
212 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
213 size_type __len2);
215 void
216 _M_erase(size_type __pos, size_type __n);
218 void
219 _M_clear()
220 { _M_set_length(0); }
222 bool
223 _M_compare(const __sso_string_base&) const
224 { return false; }
227 template<typename _CharT, typename _Traits, typename _Alloc>
228 void
229 __sso_string_base<_CharT, _Traits, _Alloc>::
230 _M_swap(__sso_string_base& __rcs)
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // 431. Swapping containers with unequal allocators.
234 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
235 __rcs._M_get_allocator());
237 if (_M_is_local())
238 if (__rcs._M_is_local())
240 if (_M_length() && __rcs._M_length())
242 _CharT __tmp_data[_S_local_capacity + 1];
243 traits_type::copy(__tmp_data, __rcs._M_local_data,
244 _S_local_capacity + 1);
245 traits_type::copy(__rcs._M_local_data, _M_local_data,
246 _S_local_capacity + 1);
247 traits_type::copy(_M_local_data, __tmp_data,
248 _S_local_capacity + 1);
250 else if (__rcs._M_length())
252 traits_type::copy(_M_local_data, __rcs._M_local_data,
253 _S_local_capacity + 1);
254 _M_length(__rcs._M_length());
255 __rcs._M_set_length(0);
256 return;
258 else if (_M_length())
260 traits_type::copy(__rcs._M_local_data, _M_local_data,
261 _S_local_capacity + 1);
262 __rcs._M_length(_M_length());
263 _M_set_length(0);
264 return;
267 else
269 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
270 traits_type::copy(__rcs._M_local_data, _M_local_data,
271 _S_local_capacity + 1);
272 _M_data(__rcs._M_data());
273 __rcs._M_data(__rcs._M_local_data);
274 _M_capacity(__tmp_capacity);
276 else
278 const size_type __tmp_capacity = _M_allocated_capacity;
279 if (__rcs._M_is_local())
281 traits_type::copy(_M_local_data, __rcs._M_local_data,
282 _S_local_capacity + 1);
283 __rcs._M_data(_M_data());
284 _M_data(_M_local_data);
286 else
288 _CharT* __tmp_ptr = _M_data();
289 _M_data(__rcs._M_data());
290 __rcs._M_data(__tmp_ptr);
291 _M_capacity(__rcs._M_allocated_capacity);
293 __rcs._M_capacity(__tmp_capacity);
296 const size_type __tmp_length = _M_length();
297 _M_length(__rcs._M_length());
298 __rcs._M_length(__tmp_length);
301 template<typename _CharT, typename _Traits, typename _Alloc>
302 _CharT*
303 __sso_string_base<_CharT, _Traits, _Alloc>::
304 _M_create(size_type& __capacity, size_type __old_capacity)
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 83. String::npos vs. string::max_size()
308 if (__capacity > _M_max_size())
309 std::__throw_length_error(__N("__sso_string_base::_M_create"));
311 // The below implements an exponential growth policy, necessary to
312 // meet amortized linear time requirements of the library: see
313 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
314 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
316 __capacity = 2 * __old_capacity;
317 // Never allocate a string bigger than max_size.
318 if (__capacity > _M_max_size())
319 __capacity = _M_max_size();
322 // NB: Need an array of char_type[__capacity], plus a terminating
323 // null char_type() element.
324 return _M_get_allocator().allocate(__capacity + 1);
327 template<typename _CharT, typename _Traits, typename _Alloc>
328 __sso_string_base<_CharT, _Traits, _Alloc>::
329 __sso_string_base(const _Alloc& __a)
330 : _M_dataplus(__a, _M_local_data)
331 { _M_set_length(0); }
333 template<typename _CharT, typename _Traits, typename _Alloc>
334 __sso_string_base<_CharT, _Traits, _Alloc>::
335 __sso_string_base(const __sso_string_base& __rcs)
336 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
337 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
339 template<typename _CharT, typename _Traits, typename _Alloc>
340 __sso_string_base<_CharT, _Traits, _Alloc>::
341 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
342 : _M_dataplus(__a, _M_local_data)
343 { _M_construct(__n, __c); }
345 template<typename _CharT, typename _Traits, typename _Alloc>
346 template<typename _InputIterator>
347 __sso_string_base<_CharT, _Traits, _Alloc>::
348 __sso_string_base(_InputIterator __beg, _InputIterator __end,
349 const _Alloc& __a)
350 : _M_dataplus(__a, _M_local_data)
351 { _M_construct(__beg, __end); }
353 // NB: This is the special case for Input Iterators, used in
354 // istreambuf_iterators, etc.
355 // Input Iterators have a cost structure very different from
356 // pointers, calling for a different coding style.
357 template<typename _CharT, typename _Traits, typename _Alloc>
358 template<typename _InIterator>
359 void
360 __sso_string_base<_CharT, _Traits, _Alloc>::
361 _M_construct(_InIterator __beg, _InIterator __end,
362 std::input_iterator_tag)
364 size_type __len = 0;
365 size_type __capacity = size_type(_S_local_capacity);
367 while (__beg != __end && __len < __capacity)
369 _M_data()[__len++] = *__beg;
370 ++__beg;
375 while (__beg != __end)
377 if (__len == __capacity)
379 // Allocate more space.
380 __capacity = __len + 1;
381 _CharT* __another = _M_create(__capacity, __len);
382 _S_copy(__another, _M_data(), __len);
383 _M_dispose();
384 _M_data(__another);
385 _M_capacity(__capacity);
387 _M_data()[__len++] = *__beg;
388 ++__beg;
391 catch(...)
393 _M_dispose();
394 __throw_exception_again;
397 _M_set_length(__len);
400 template<typename _CharT, typename _Traits, typename _Alloc>
401 template<typename _InIterator>
402 void
403 __sso_string_base<_CharT, _Traits, _Alloc>::
404 _M_construct(_InIterator __beg, _InIterator __end,
405 std::forward_iterator_tag)
407 // NB: Not required, but considered best practice.
408 if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
409 std::__throw_logic_error(__N("__sso_string_base::"
410 "_M_construct NULL not valid"));
412 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
414 if (__dnew > size_type(_S_local_capacity))
416 _M_data(_M_create(__dnew, size_type(0)));
417 _M_capacity(__dnew);
420 // Check for out_of_range and length_error exceptions.
422 { _S_copy_chars(_M_data(), __beg, __end); }
423 catch(...)
425 _M_dispose();
426 __throw_exception_again;
429 _M_set_length(__dnew);
432 template<typename _CharT, typename _Traits, typename _Alloc>
433 void
434 __sso_string_base<_CharT, _Traits, _Alloc>::
435 _M_construct(size_type __n, _CharT __c)
437 if (__n > size_type(_S_local_capacity))
439 _M_data(_M_create(__n, size_type(0)));
440 _M_capacity(__n);
443 if (__n)
444 _S_assign(_M_data(), __n, __c);
446 _M_set_length(__n);
449 template<typename _CharT, typename _Traits, typename _Alloc>
450 void
451 __sso_string_base<_CharT, _Traits, _Alloc>::
452 _M_assign(const __sso_string_base& __rcs)
454 if (this != &__rcs)
456 const size_type __rsize = __rcs._M_length();
457 const size_type __capacity = _M_capacity();
459 if (__rsize > __capacity)
461 size_type __new_capacity = __rsize;
462 _CharT* __tmp = _M_create(__new_capacity, __capacity);
463 _M_dispose();
464 _M_data(__tmp);
465 _M_capacity(__new_capacity);
468 if (__rsize)
469 _S_copy(_M_data(), __rcs._M_data(), __rsize);
471 _M_set_length(__rsize);
475 template<typename _CharT, typename _Traits, typename _Alloc>
476 void
477 __sso_string_base<_CharT, _Traits, _Alloc>::
478 _M_reserve(size_type __res)
480 // Make sure we don't shrink below the current size.
481 if (__res < _M_length())
482 __res = _M_length();
484 const size_type __capacity = _M_capacity();
485 if (__res != __capacity)
487 if (__res > __capacity
488 || __res > size_type(_S_local_capacity))
490 _CharT* __tmp = _M_create(__res, __capacity);
491 _S_copy(__tmp, _M_data(), _M_length() + 1);
492 _M_dispose();
493 _M_data(__tmp);
494 _M_capacity(__res);
496 else if (!_M_is_local())
498 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
499 _M_destroy(__capacity);
500 _M_data(_M_local_data);
505 template<typename _CharT, typename _Traits, typename _Alloc>
506 void
507 __sso_string_base<_CharT, _Traits, _Alloc>::
508 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
509 const size_type __len2)
511 const size_type __how_much = _M_length() - __pos - __len1;
513 size_type __new_capacity = _M_length() + __len2 - __len1;
514 _CharT* __r = _M_create(__new_capacity, _M_capacity());
516 if (__pos)
517 _S_copy(__r, _M_data(), __pos);
518 if (__s && __len2)
519 _S_copy(__r + __pos, __s, __len2);
520 if (__how_much)
521 _S_copy(__r + __pos + __len2,
522 _M_data() + __pos + __len1, __how_much);
524 _M_dispose();
525 _M_data(__r);
526 _M_capacity(__new_capacity);
529 template<typename _CharT, typename _Traits, typename _Alloc>
530 void
531 __sso_string_base<_CharT, _Traits, _Alloc>::
532 _M_erase(size_type __pos, size_type __n)
534 const size_type __how_much = _M_length() - __pos - __n;
536 if (__how_much && __n)
537 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
538 __how_much);
540 _M_set_length(_M_length() - __n);
543 template<>
544 inline bool
545 __sso_string_base<char, std::char_traits<char>,
546 std::allocator<char> >::
547 _M_compare(const __sso_string_base& __rcs) const
549 if (this == &__rcs)
550 return true;
551 return false;
554 #ifdef _GLIBCXX_USE_WCHAR_T
555 template<>
556 inline bool
557 __sso_string_base<wchar_t, std::char_traits<wchar_t>,
558 std::allocator<wchar_t> >::
559 _M_compare(const __sso_string_base& __rcs) const
561 if (this == &__rcs)
562 return true;
563 return false;
565 #endif
567 _GLIBCXX_END_NAMESPACE
569 #endif /* _SSO_STRING_BASE_H */