2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / ext / sso_string_base.h
blobc21dbbd3ab4cf1d04781f2010f0c2054cedbd846
1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005 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;
48 typedef _Alloc allocator_type;
50 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
51 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
52 typedef typename _CharT_alloc_type::size_type size_type;
54 private:
55 // The maximum number of individual char_type elements of an
56 // individual string is determined by _S_max_size. This is the
57 // value that will be returned by max_size(). (Whereas npos
58 // is the maximum number of bytes the allocator can allocate.)
59 // If one was to divvy up the theoretical largest size string,
60 // with a terminating character and m _CharT elements, it'd
61 // look like this:
62 // npos = m * sizeof(_CharT) + sizeof(_CharT)
63 // Solving for m:
64 // m = npos / sizeof(_CharT) - 1
65 // In addition, this implementation quarters this amount.
66 enum { _S_max_size = (((static_cast<size_type>(-1)
67 / sizeof(_CharT)) - 1) / 4) };
69 // Data Members (private):
70 typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
71 size_type _M_string_length;
73 enum { _S_local_capacity = 15 };
75 union
77 _CharT _M_local_data[_S_local_capacity + 1];
78 size_type _M_allocated_capacity;
81 void
82 _M_data(_CharT* __p)
83 { _M_dataplus._M_p = __p; }
85 void
86 _M_length(size_type __length)
87 { _M_string_length = __length; }
89 void
90 _M_capacity(size_type __capacity)
91 { _M_allocated_capacity = __capacity; }
93 bool
94 _M_is_local() const
95 { return _M_data() == _M_local_data; }
97 // Create & Destroy
98 _CharT*
99 _M_create(size_type&, size_type);
101 void
102 _M_dispose()
104 if (!_M_is_local())
105 _M_destroy(_M_allocated_capacity);
108 void
109 _M_destroy(size_type) throw();
111 // _M_construct_aux is used to implement the 21.3.1 para 15 which
112 // requires special behaviour if _InIterator is an integral type
113 template<typename _InIterator>
114 void
115 _M_construct_aux(_InIterator __beg, _InIterator __end, __false_type)
117 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
118 _M_construct(__beg, __end, _Tag());
121 template<typename _InIterator>
122 void
123 _M_construct_aux(_InIterator __beg, _InIterator __end, __true_type)
124 { _M_construct(static_cast<size_type>(__beg),
125 static_cast<value_type>(__end)); }
127 template<typename _InIterator>
128 void
129 _M_construct(_InIterator __beg, _InIterator __end)
131 typedef typename std::__is_integer<_InIterator>::__type _Integral;
132 _M_construct_aux(__beg, __end, _Integral());
135 // For Input Iterators, used in istreambuf_iterators, etc.
136 template<typename _InIterator>
137 void
138 _M_construct(_InIterator __beg, _InIterator __end,
139 std::input_iterator_tag);
141 // For forward_iterators up to random_access_iterators, used for
142 // string::iterator, _CharT*, etc.
143 template<typename _FwdIterator>
144 void
145 _M_construct(_FwdIterator __beg, _FwdIterator __end,
146 std::forward_iterator_tag);
148 void
149 _M_construct(size_type __req, _CharT __c);
151 public:
152 size_type
153 _M_max_size() const
154 { return size_type(_S_max_size); }
156 _CharT*
157 _M_data() const
158 { return _M_dataplus._M_p; }
160 size_type
161 _M_length() const
162 { return _M_string_length; }
164 size_type
165 _M_capacity() const
167 return _M_is_local() ? size_type(_S_local_capacity)
168 : _M_allocated_capacity;
171 bool
172 _M_is_shared() const
173 { return false; }
175 void
176 _M_set_leaked() { }
178 void
179 _M_leak() { }
181 void
182 _M_set_length(size_type __n)
184 _M_length(__n);
185 traits_type::assign(_M_data()[__n], _CharT());
188 __sso_string_base()
189 : _M_dataplus(_Alloc(), _M_local_data)
190 { _M_set_length(0); }
192 __sso_string_base(const _Alloc& __a);
194 __sso_string_base(const __sso_string_base& __rcs);
196 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
198 template<typename _InputIterator>
199 __sso_string_base(_InputIterator __beg, _InputIterator __end,
200 const _Alloc& __a);
202 ~__sso_string_base()
203 { _M_dispose(); }
205 allocator_type&
206 _M_get_allocator()
207 { return _M_dataplus; }
209 const allocator_type&
210 _M_get_allocator() const
211 { return _M_dataplus; }
213 void
214 _M_swap(__sso_string_base& __rcs);
216 void
217 _M_assign(const __sso_string_base& __rcs);
219 void
220 _M_reserve(size_type __res);
222 void
223 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
224 size_type __len2);
226 void
227 _M_erase(size_type __pos, size_type __n);
229 bool
230 _M_compare(const __sso_string_base&) const
231 { return false; }
234 template<typename _CharT, typename _Traits, typename _Alloc>
235 void
236 __sso_string_base<_CharT, _Traits, _Alloc>::
237 _M_destroy(size_type __size) throw()
238 { _CharT_alloc_type(_M_get_allocator()).deallocate(_M_data(), __size + 1); }
240 template<typename _CharT, typename _Traits, typename _Alloc>
241 void
242 __sso_string_base<_CharT, _Traits, _Alloc>::
243 _M_swap(__sso_string_base& __rcs)
245 // NB: Implement Option 3 of DR 431 (see N1599).
246 _M_dataplus._M_alloc_swap(__rcs._M_dataplus);
248 if (_M_is_local())
249 if (__rcs._M_is_local())
251 if (_M_length() && __rcs._M_length())
253 _CharT __tmp_data[_S_local_capacity + 1];
254 traits_type::copy(__tmp_data, __rcs._M_local_data,
255 _S_local_capacity + 1);
256 traits_type::copy(__rcs._M_local_data, _M_local_data,
257 _S_local_capacity + 1);
258 traits_type::copy(_M_local_data, __tmp_data,
259 _S_local_capacity + 1);
261 else if (__rcs._M_length())
263 traits_type::copy(_M_local_data, __rcs._M_local_data,
264 _S_local_capacity + 1);
265 _M_length(__rcs._M_length());
266 __rcs._M_set_length(0);
267 return;
269 else if (_M_length())
271 traits_type::copy(__rcs._M_local_data, _M_local_data,
272 _S_local_capacity + 1);
273 __rcs._M_length(_M_length());
274 _M_set_length(0);
275 return;
278 else
280 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
281 traits_type::copy(__rcs._M_local_data, _M_local_data,
282 _S_local_capacity + 1);
283 _M_data(__rcs._M_data());
284 __rcs._M_data(__rcs._M_local_data);
285 _M_capacity(__tmp_capacity);
287 else
289 const size_type __tmp_capacity = _M_allocated_capacity;
290 if (__rcs._M_is_local())
292 traits_type::copy(_M_local_data, __rcs._M_local_data,
293 _S_local_capacity + 1);
294 __rcs._M_data(_M_data());
295 _M_data(_M_local_data);
297 else
299 _CharT* __tmp_ptr = _M_data();
300 _M_data(__rcs._M_data());
301 __rcs._M_data(__tmp_ptr);
302 _M_capacity(__rcs._M_allocated_capacity);
304 __rcs._M_capacity(__tmp_capacity);
307 const size_type __tmp_length = _M_length();
308 _M_length(__rcs._M_length());
309 __rcs._M_length(__tmp_length);
312 template<typename _CharT, typename _Traits, typename _Alloc>
313 _CharT*
314 __sso_string_base<_CharT, _Traits, _Alloc>::
315 _M_create(size_type& __capacity, size_type __old_capacity)
317 // _GLIBCXX_RESOLVE_LIB_DEFECTS
318 // 83. String::npos vs. string::max_size()
319 if (__capacity > size_type(_S_max_size))
320 std::__throw_length_error(__N("__sso_string_base::_M_create"));
322 // The below implements an exponential growth policy, necessary to
323 // meet amortized linear time requirements of the library: see
324 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
325 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
326 __capacity = 2 * __old_capacity;
328 // NB: Need an array of char_type[__capacity], plus a terminating
329 // null char_type() element.
330 return _CharT_alloc_type(_M_get_allocator()).allocate(__capacity + 1);
333 template<typename _CharT, typename _Traits, typename _Alloc>
334 __sso_string_base<_CharT, _Traits, _Alloc>::
335 __sso_string_base(const _Alloc& __a)
336 : _M_dataplus(__a, _M_local_data)
337 { _M_set_length(0); }
339 template<typename _CharT, typename _Traits, typename _Alloc>
340 __sso_string_base<_CharT, _Traits, _Alloc>::
341 __sso_string_base(const __sso_string_base& __rcs)
342 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
343 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
345 template<typename _CharT, typename _Traits, typename _Alloc>
346 __sso_string_base<_CharT, _Traits, _Alloc>::
347 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
348 : _M_dataplus(__a, _M_local_data)
349 { _M_construct(__n, __c); }
351 template<typename _CharT, typename _Traits, typename _Alloc>
352 template<typename _InputIterator>
353 __sso_string_base<_CharT, _Traits, _Alloc>::
354 __sso_string_base(_InputIterator __beg, _InputIterator __end,
355 const _Alloc& __a)
356 : _M_dataplus(__a, _M_local_data)
357 { _M_construct(__beg, __end); }
359 // NB: This is the special case for Input Iterators, used in
360 // istreambuf_iterators, etc.
361 // Input Iterators have a cost structure very different from
362 // pointers, calling for a different coding style.
363 template<typename _CharT, typename _Traits, typename _Alloc>
364 template<typename _InIterator>
365 void
366 __sso_string_base<_CharT, _Traits, _Alloc>::
367 _M_construct(_InIterator __beg, _InIterator __end,
368 std::input_iterator_tag)
370 size_type __len = 0;
371 size_type __capacity = size_type(_S_local_capacity);
373 while (__beg != __end && __len < __capacity)
375 _M_data()[__len++] = *__beg;
376 ++__beg;
381 while (__beg != __end)
383 if (__len == __capacity)
385 // Allocate more space.
386 __capacity = __len + 1;
387 _CharT* __another = _M_create(__capacity, __len);
388 _S_copy(__another, _M_data(), __len);
389 _M_dispose();
390 _M_data(__another);
391 _M_capacity(__capacity);
393 _M_data()[__len++] = *__beg;
394 ++__beg;
397 catch(...)
399 _M_dispose();
400 __throw_exception_again;
403 _M_set_length(__len);
406 template<typename _CharT, typename _Traits, typename _Alloc>
407 template<typename _InIterator>
408 void
409 __sso_string_base<_CharT, _Traits, _Alloc>::
410 _M_construct(_InIterator __beg, _InIterator __end,
411 std::forward_iterator_tag)
413 // NB: Not required, but considered best practice.
414 if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
415 std::__throw_logic_error(__N("__sso_string_base::"
416 "_M_construct NULL not valid"));
418 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
420 if (__dnew > size_type(_S_local_capacity))
422 _M_data(_M_create(__dnew, size_type(0)));
423 _M_capacity(__dnew);
426 // Check for out_of_range and length_error exceptions.
428 { _S_copy_chars(_M_data(), __beg, __end); }
429 catch(...)
431 _M_dispose();
432 __throw_exception_again;
435 _M_set_length(__dnew);
438 template<typename _CharT, typename _Traits, typename _Alloc>
439 void
440 __sso_string_base<_CharT, _Traits, _Alloc>::
441 _M_construct(size_type __n, _CharT __c)
443 if (__n > size_type(_S_local_capacity))
445 _M_data(_M_create(__n, size_type(0)));
446 _M_capacity(__n);
449 if (__n)
450 _S_assign(_M_data(), __n, __c);
452 _M_set_length(__n);
455 template<typename _CharT, typename _Traits, typename _Alloc>
456 void
457 __sso_string_base<_CharT, _Traits, _Alloc>::
458 _M_assign(const __sso_string_base& __rcs)
460 if (this != &__rcs)
462 const size_type __rsize = __rcs._M_length();
463 const size_type __capacity = _M_capacity();
465 if (__rsize > __capacity)
467 size_type __new_capacity = __rsize;
468 _CharT* __tmp = _M_create(__new_capacity, __capacity);
469 _M_dispose();
470 _M_data(__tmp);
471 _M_capacity(__new_capacity);
474 if (__rsize)
475 _S_copy(_M_data(), __rcs._M_data(), __rsize);
477 _M_set_length(__rsize);
481 template<typename _CharT, typename _Traits, typename _Alloc>
482 void
483 __sso_string_base<_CharT, _Traits, _Alloc>::
484 _M_reserve(size_type __res)
486 // Make sure we don't shrink below the current size.
487 if (__res < _M_length())
488 __res = _M_length();
490 const size_type __capacity = _M_capacity();
491 if (__res != __capacity)
493 if (__res > __capacity
494 || __res > size_type(_S_local_capacity))
496 _CharT* __tmp = _M_create(__res, __capacity);
497 _S_copy(__tmp, _M_data(), _M_length() + 1);
498 _M_dispose();
499 _M_data(__tmp);
500 _M_capacity(__res);
502 else if (!_M_is_local())
504 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
505 _M_destroy(__capacity);
506 _M_data(_M_local_data);
511 template<typename _CharT, typename _Traits, typename _Alloc>
512 void
513 __sso_string_base<_CharT, _Traits, _Alloc>::
514 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
515 const size_type __len2)
517 const size_type __how_much = _M_length() - __pos - __len1;
519 size_type __new_capacity = _M_length() + __len2 - __len1;
520 _CharT* __r = _M_create(__new_capacity, _M_capacity());
522 if (__pos)
523 _S_copy(__r, _M_data(), __pos);
524 if (__s && __len2)
525 _S_copy(__r + __pos, __s, __len2);
526 if (__how_much)
527 _S_copy(__r + __pos + __len2,
528 _M_data() + __pos + __len1, __how_much);
530 _M_dispose();
531 _M_data(__r);
532 _M_capacity(__new_capacity);
535 template<typename _CharT, typename _Traits, typename _Alloc>
536 void
537 __sso_string_base<_CharT, _Traits, _Alloc>::
538 _M_erase(size_type __pos, size_type __n)
540 const size_type __how_much = _M_length() - __pos - __n;
542 if (__how_much && __n)
543 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
544 __how_much);
546 _M_set_length(_M_length() - __n);
549 template<>
550 inline bool
551 __sso_string_base<char, std::char_traits<char>,
552 std::allocator<char> >::
553 _M_compare(const __sso_string_base& __rcs) const
555 if (this == &__rcs)
556 return true;
557 return false;
560 template<>
561 inline bool
562 __sso_string_base<wchar_t, std::char_traits<wchar_t>,
563 std::allocator<wchar_t> >::
564 _M_compare(const __sso_string_base& __rcs) const
566 if (this == &__rcs)
567 return true;
568 return false;
571 _GLIBCXX_END_NAMESPACE
573 #endif /* _SSO_STRING_BASE_H */