Update concepts branch to revision 131834
[official-gcc.git] / libstdc++-v3 / include / ext / sso_string_base.h
blob9c8c1bc79d6e8e84b7b83bd7063682af373c52f2
1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 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 // _GLIBCXX_RESOLVE_LIB_DEFECTS
110 // 438. Ambiguity in the "do the right thing" clause
111 template<typename _Integer>
112 void
113 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
114 { _M_construct(static_cast<size_type>(__beg), __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(_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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
186 __sso_string_base(__sso_string_base&& __rcs);
187 #endif
189 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
191 template<typename _InputIterator>
192 __sso_string_base(_InputIterator __beg, _InputIterator __end,
193 const _Alloc& __a);
195 ~__sso_string_base()
196 { _M_dispose(); }
198 _CharT_alloc_type&
199 _M_get_allocator()
200 { return _M_dataplus; }
202 const _CharT_alloc_type&
203 _M_get_allocator() const
204 { return _M_dataplus; }
206 void
207 _M_swap(__sso_string_base& __rcs);
209 void
210 _M_assign(const __sso_string_base& __rcs);
212 void
213 _M_reserve(size_type __res);
215 void
216 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
217 size_type __len2);
219 void
220 _M_erase(size_type __pos, size_type __n);
222 void
223 _M_clear()
224 { _M_set_length(0); }
226 bool
227 _M_compare(const __sso_string_base&) const
228 { return false; }
231 template<typename _CharT, typename _Traits, typename _Alloc>
232 void
233 __sso_string_base<_CharT, _Traits, _Alloc>::
234 _M_swap(__sso_string_base& __rcs)
236 // _GLIBCXX_RESOLVE_LIB_DEFECTS
237 // 431. Swapping containers with unequal allocators.
238 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
239 __rcs._M_get_allocator());
241 if (_M_is_local())
242 if (__rcs._M_is_local())
244 if (_M_length() && __rcs._M_length())
246 _CharT __tmp_data[_S_local_capacity + 1];
247 traits_type::copy(__tmp_data, __rcs._M_local_data,
248 _S_local_capacity + 1);
249 traits_type::copy(__rcs._M_local_data, _M_local_data,
250 _S_local_capacity + 1);
251 traits_type::copy(_M_local_data, __tmp_data,
252 _S_local_capacity + 1);
254 else if (__rcs._M_length())
256 traits_type::copy(_M_local_data, __rcs._M_local_data,
257 _S_local_capacity + 1);
258 _M_length(__rcs._M_length());
259 __rcs._M_set_length(0);
260 return;
262 else if (_M_length())
264 traits_type::copy(__rcs._M_local_data, _M_local_data,
265 _S_local_capacity + 1);
266 __rcs._M_length(_M_length());
267 _M_set_length(0);
268 return;
271 else
273 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
274 traits_type::copy(__rcs._M_local_data, _M_local_data,
275 _S_local_capacity + 1);
276 _M_data(__rcs._M_data());
277 __rcs._M_data(__rcs._M_local_data);
278 _M_capacity(__tmp_capacity);
280 else
282 const size_type __tmp_capacity = _M_allocated_capacity;
283 if (__rcs._M_is_local())
285 traits_type::copy(_M_local_data, __rcs._M_local_data,
286 _S_local_capacity + 1);
287 __rcs._M_data(_M_data());
288 _M_data(_M_local_data);
290 else
292 _CharT* __tmp_ptr = _M_data();
293 _M_data(__rcs._M_data());
294 __rcs._M_data(__tmp_ptr);
295 _M_capacity(__rcs._M_allocated_capacity);
297 __rcs._M_capacity(__tmp_capacity);
300 const size_type __tmp_length = _M_length();
301 _M_length(__rcs._M_length());
302 __rcs._M_length(__tmp_length);
305 template<typename _CharT, typename _Traits, typename _Alloc>
306 _CharT*
307 __sso_string_base<_CharT, _Traits, _Alloc>::
308 _M_create(size_type& __capacity, size_type __old_capacity)
310 // _GLIBCXX_RESOLVE_LIB_DEFECTS
311 // 83. String::npos vs. string::max_size()
312 if (__capacity > _M_max_size())
313 std::__throw_length_error(__N("__sso_string_base::_M_create"));
315 // The below implements an exponential growth policy, necessary to
316 // meet amortized linear time requirements of the library: see
317 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
318 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
320 __capacity = 2 * __old_capacity;
321 // Never allocate a string bigger than max_size.
322 if (__capacity > _M_max_size())
323 __capacity = _M_max_size();
326 // NB: Need an array of char_type[__capacity], plus a terminating
327 // null char_type() element.
328 return _M_get_allocator().allocate(__capacity + 1);
331 template<typename _CharT, typename _Traits, typename _Alloc>
332 __sso_string_base<_CharT, _Traits, _Alloc>::
333 __sso_string_base(const _Alloc& __a)
334 : _M_dataplus(__a, _M_local_data)
335 { _M_set_length(0); }
337 template<typename _CharT, typename _Traits, typename _Alloc>
338 __sso_string_base<_CharT, _Traits, _Alloc>::
339 __sso_string_base(const __sso_string_base& __rcs)
340 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
341 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
343 #ifdef __GXX_EXPERIMENTAL_CXX0X__
344 template<typename _CharT, typename _Traits, typename _Alloc>
345 __sso_string_base<_CharT, _Traits, _Alloc>::
346 __sso_string_base(__sso_string_base&& __rcs)
347 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
349 if (__rcs._M_is_local())
351 if (__rcs._M_length())
352 traits_type::copy(_M_local_data, __rcs._M_local_data,
353 _S_local_capacity + 1);
355 else
357 _M_data(__rcs._M_data());
358 _M_capacity(__rcs._M_allocated_capacity);
361 _M_length(__rcs._M_length());
362 __rcs._M_length(0);
363 __rcs._M_data(__rcs._M_local_data);
365 #endif
367 template<typename _CharT, typename _Traits, typename _Alloc>
368 __sso_string_base<_CharT, _Traits, _Alloc>::
369 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
370 : _M_dataplus(__a, _M_local_data)
371 { _M_construct(__n, __c); }
373 template<typename _CharT, typename _Traits, typename _Alloc>
374 template<typename _InputIterator>
375 __sso_string_base<_CharT, _Traits, _Alloc>::
376 __sso_string_base(_InputIterator __beg, _InputIterator __end,
377 const _Alloc& __a)
378 : _M_dataplus(__a, _M_local_data)
379 { _M_construct(__beg, __end); }
381 // NB: This is the special case for Input Iterators, used in
382 // istreambuf_iterators, etc.
383 // Input Iterators have a cost structure very different from
384 // pointers, calling for a different coding style.
385 template<typename _CharT, typename _Traits, typename _Alloc>
386 template<typename _InIterator>
387 void
388 __sso_string_base<_CharT, _Traits, _Alloc>::
389 _M_construct(_InIterator __beg, _InIterator __end,
390 std::input_iterator_tag)
392 size_type __len = 0;
393 size_type __capacity = size_type(_S_local_capacity);
395 while (__beg != __end && __len < __capacity)
397 _M_data()[__len++] = *__beg;
398 ++__beg;
403 while (__beg != __end)
405 if (__len == __capacity)
407 // Allocate more space.
408 __capacity = __len + 1;
409 _CharT* __another = _M_create(__capacity, __len);
410 _S_copy(__another, _M_data(), __len);
411 _M_dispose();
412 _M_data(__another);
413 _M_capacity(__capacity);
415 _M_data()[__len++] = *__beg;
416 ++__beg;
419 catch(...)
421 _M_dispose();
422 __throw_exception_again;
425 _M_set_length(__len);
428 template<typename _CharT, typename _Traits, typename _Alloc>
429 template<typename _InIterator>
430 void
431 __sso_string_base<_CharT, _Traits, _Alloc>::
432 _M_construct(_InIterator __beg, _InIterator __end,
433 std::forward_iterator_tag)
435 // NB: Not required, but considered best practice.
436 if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
437 std::__throw_logic_error(__N("__sso_string_base::"
438 "_M_construct NULL not valid"));
440 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
442 if (__dnew > size_type(_S_local_capacity))
444 _M_data(_M_create(__dnew, size_type(0)));
445 _M_capacity(__dnew);
448 // Check for out_of_range and length_error exceptions.
450 { _S_copy_chars(_M_data(), __beg, __end); }
451 catch(...)
453 _M_dispose();
454 __throw_exception_again;
457 _M_set_length(__dnew);
460 template<typename _CharT, typename _Traits, typename _Alloc>
461 void
462 __sso_string_base<_CharT, _Traits, _Alloc>::
463 _M_construct(size_type __n, _CharT __c)
465 if (__n > size_type(_S_local_capacity))
467 _M_data(_M_create(__n, size_type(0)));
468 _M_capacity(__n);
471 if (__n)
472 _S_assign(_M_data(), __n, __c);
474 _M_set_length(__n);
477 template<typename _CharT, typename _Traits, typename _Alloc>
478 void
479 __sso_string_base<_CharT, _Traits, _Alloc>::
480 _M_assign(const __sso_string_base& __rcs)
482 if (this != &__rcs)
484 const size_type __rsize = __rcs._M_length();
485 const size_type __capacity = _M_capacity();
487 if (__rsize > __capacity)
489 size_type __new_capacity = __rsize;
490 _CharT* __tmp = _M_create(__new_capacity, __capacity);
491 _M_dispose();
492 _M_data(__tmp);
493 _M_capacity(__new_capacity);
496 if (__rsize)
497 _S_copy(_M_data(), __rcs._M_data(), __rsize);
499 _M_set_length(__rsize);
503 template<typename _CharT, typename _Traits, typename _Alloc>
504 void
505 __sso_string_base<_CharT, _Traits, _Alloc>::
506 _M_reserve(size_type __res)
508 // Make sure we don't shrink below the current size.
509 if (__res < _M_length())
510 __res = _M_length();
512 const size_type __capacity = _M_capacity();
513 if (__res != __capacity)
515 if (__res > __capacity
516 || __res > size_type(_S_local_capacity))
518 _CharT* __tmp = _M_create(__res, __capacity);
519 _S_copy(__tmp, _M_data(), _M_length() + 1);
520 _M_dispose();
521 _M_data(__tmp);
522 _M_capacity(__res);
524 else if (!_M_is_local())
526 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
527 _M_destroy(__capacity);
528 _M_data(_M_local_data);
533 template<typename _CharT, typename _Traits, typename _Alloc>
534 void
535 __sso_string_base<_CharT, _Traits, _Alloc>::
536 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
537 const size_type __len2)
539 const size_type __how_much = _M_length() - __pos - __len1;
541 size_type __new_capacity = _M_length() + __len2 - __len1;
542 _CharT* __r = _M_create(__new_capacity, _M_capacity());
544 if (__pos)
545 _S_copy(__r, _M_data(), __pos);
546 if (__s && __len2)
547 _S_copy(__r + __pos, __s, __len2);
548 if (__how_much)
549 _S_copy(__r + __pos + __len2,
550 _M_data() + __pos + __len1, __how_much);
552 _M_dispose();
553 _M_data(__r);
554 _M_capacity(__new_capacity);
557 template<typename _CharT, typename _Traits, typename _Alloc>
558 void
559 __sso_string_base<_CharT, _Traits, _Alloc>::
560 _M_erase(size_type __pos, size_type __n)
562 const size_type __how_much = _M_length() - __pos - __n;
564 if (__how_much && __n)
565 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
566 __how_much);
568 _M_set_length(_M_length() - __n);
571 _GLIBCXX_END_NAMESPACE
573 #endif /* _SSO_STRING_BASE_H */