]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/ext/sso_string_base.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / ext / sso_string_base.h
1 // Short-string-optimized versatile string base -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
29
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.
34  */
35
36 #ifndef _SSO_STRING_BASE_H
37 #define _SSO_STRING_BASE_H 1
38
39 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40
41   template<typename _CharT, typename _Traits, typename _Alloc>
42     class __sso_string_base
43     : protected __vstring_utility<_CharT, _Traits, _Alloc>
44     {
45     public:
46       typedef _Traits                                       traits_type;
47       typedef typename _Traits::char_type                   value_type;
48
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;
52       
53     private:
54       // Data Members:
55       typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
56                                                             _M_dataplus;
57       size_type                                             _M_string_length;
58
59       enum { _S_local_capacity = 15 };
60       
61       union
62       {
63         _CharT           _M_local_data[_S_local_capacity + 1];
64         size_type        _M_allocated_capacity;
65       };
66
67       void
68       _M_data(_CharT* __p)
69       { _M_dataplus._M_p = __p; }
70
71       void
72       _M_length(size_type __length)
73       { _M_string_length = __length; }
74
75       void
76       _M_capacity(size_type __capacity)
77       { _M_allocated_capacity = __capacity; }
78
79       bool
80       _M_is_local() const
81       { return _M_data() == _M_local_data; }
82
83       // Create & Destroy
84       _CharT*
85       _M_create(size_type&, size_type);
86       
87       void
88       _M_dispose()
89       {
90         if (!_M_is_local())
91           _M_destroy(_M_allocated_capacity);
92       }
93
94       void
95       _M_destroy(size_type __size) throw()
96       { _M_get_allocator().deallocate(_M_data(), __size + 1); }
97
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)
104         {
105           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
106           _M_construct(__beg, __end, _Tag());
107         }
108
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); }
115
116       template<typename _InIterator>
117         void
118         _M_construct(_InIterator __beg, _InIterator __end)
119         {
120           typedef typename std::__is_integer<_InIterator>::__type _Integral;
121           _M_construct_aux(__beg, __end, _Integral());
122         }
123
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);
129       
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);
136
137       void
138       _M_construct(size_type __req, _CharT __c);
139
140     public:
141       size_type
142       _M_max_size() const
143       { return (_M_get_allocator().max_size() - 1) / 2; }
144
145       _CharT*
146       _M_data() const
147       { return _M_dataplus._M_p; }
148
149       size_type
150       _M_length() const
151       { return _M_string_length; }
152
153       size_type
154       _M_capacity() const
155       {
156         return _M_is_local() ? size_type(_S_local_capacity)
157                              : _M_allocated_capacity; 
158       }
159
160       bool
161       _M_is_shared() const
162       { return false; }
163
164       void
165       _M_set_leaked() { }
166
167       void
168       _M_leak() { }
169
170       void
171       _M_set_length(size_type __n)
172       {
173         _M_length(__n);
174         traits_type::assign(_M_data()[__n], _CharT());
175       }
176
177       __sso_string_base()
178       : _M_dataplus(_M_local_data)
179       { _M_set_length(0); }
180
181       __sso_string_base(const _Alloc& __a);
182
183       __sso_string_base(const __sso_string_base& __rcs);
184
185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
186       __sso_string_base(__sso_string_base&& __rcs);
187 #endif
188
189       __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
190
191       template<typename _InputIterator>
192         __sso_string_base(_InputIterator __beg, _InputIterator __end,
193                           const _Alloc& __a);
194
195       ~__sso_string_base()
196       { _M_dispose(); }
197
198       _CharT_alloc_type&
199       _M_get_allocator()
200       { return _M_dataplus; }
201
202       const _CharT_alloc_type&
203       _M_get_allocator() const
204       { return _M_dataplus; }
205
206       void
207       _M_swap(__sso_string_base& __rcs);
208
209       void
210       _M_assign(const __sso_string_base& __rcs);
211
212       void
213       _M_reserve(size_type __res);
214
215       void
216       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
217                 size_type __len2);
218
219       void
220       _M_erase(size_type __pos, size_type __n);
221
222       void
223       _M_clear()
224       { _M_set_length(0); }
225
226       bool
227       _M_compare(const __sso_string_base&) const
228       { return false; }
229     };
230
231   template<typename _CharT, typename _Traits, typename _Alloc>
232     void
233     __sso_string_base<_CharT, _Traits, _Alloc>::
234     _M_swap(__sso_string_base& __rcs)
235     {
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());
240
241       if (_M_is_local())
242         if (__rcs._M_is_local())
243           {
244             if (_M_length() && __rcs._M_length())
245               {
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);
253               }
254             else if (__rcs._M_length())
255               {
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;
261               }
262             else if (_M_length())
263               {
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;
269               }
270           }
271         else
272           {
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);
279           }
280       else
281         {
282           const size_type __tmp_capacity = _M_allocated_capacity;
283           if (__rcs._M_is_local())
284             {
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);
289             }
290           else
291             {
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);
296             }
297           __rcs._M_capacity(__tmp_capacity);
298         }
299
300       const size_type __tmp_length = _M_length();
301       _M_length(__rcs._M_length());
302       __rcs._M_length(__tmp_length);
303     }
304
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)
309     {
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"));
314
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)
319         {
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();
324         }
325
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);
329     }
330
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); }
336
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()); }
342
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)
348     {
349       if (__rcs._M_is_local())
350         {
351           if (__rcs._M_length())
352             traits_type::copy(_M_local_data, __rcs._M_local_data,
353                               _S_local_capacity + 1);
354         }
355       else
356         {
357           _M_data(__rcs._M_data());
358           _M_capacity(__rcs._M_allocated_capacity);
359         }
360
361       _M_length(__rcs._M_length());
362       __rcs._M_length(0);
363       __rcs._M_data(__rcs._M_local_data);
364     }
365 #endif
366
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); }
372
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); }
380
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)
391       {
392         size_type __len = 0;
393         size_type __capacity = size_type(_S_local_capacity);
394
395         while (__beg != __end && __len < __capacity)
396           {
397             _M_data()[__len++] = *__beg;
398             ++__beg;
399           }
400         
401         try
402           {
403             while (__beg != __end)
404               {
405                 if (__len == __capacity)
406                   {
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);
414                   }
415                 _M_data()[__len++] = *__beg;
416                 ++__beg;
417               }
418           }
419         catch(...)
420           {
421             _M_dispose();
422             __throw_exception_again;
423           }
424
425         _M_set_length(__len);
426       }
427
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)
434       {
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"));
439
440         size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
441
442         if (__dnew > size_type(_S_local_capacity))
443           {
444             _M_data(_M_create(__dnew, size_type(0)));
445             _M_capacity(__dnew);
446           }
447
448         // Check for out_of_range and length_error exceptions.
449         try
450           { _S_copy_chars(_M_data(), __beg, __end); }
451         catch(...)
452           {
453             _M_dispose();
454             __throw_exception_again;
455           }
456
457         _M_set_length(__dnew);
458       }
459
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)
464     {
465       if (__n > size_type(_S_local_capacity))
466         {
467           _M_data(_M_create(__n, size_type(0)));
468           _M_capacity(__n);
469         }
470
471       if (__n)
472         _S_assign(_M_data(), __n, __c);
473
474       _M_set_length(__n);
475     }
476
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)
481     {
482       if (this != &__rcs)
483         {
484           const size_type __rsize = __rcs._M_length();
485           const size_type __capacity = _M_capacity();
486
487           if (__rsize > __capacity)
488             {
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);
494             }
495
496           if (__rsize)
497             _S_copy(_M_data(), __rcs._M_data(), __rsize);
498
499           _M_set_length(__rsize);
500         }
501     }
502
503   template<typename _CharT, typename _Traits, typename _Alloc>
504     void
505     __sso_string_base<_CharT, _Traits, _Alloc>::
506     _M_reserve(size_type __res)
507     {
508       // Make sure we don't shrink below the current size.
509       if (__res < _M_length())
510         __res = _M_length();
511
512       const size_type __capacity = _M_capacity();
513       if (__res != __capacity)
514         {
515           if (__res > __capacity
516               || __res > size_type(_S_local_capacity))
517             {
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);
523             }
524           else if (!_M_is_local())
525             {
526               _S_copy(_M_local_data, _M_data(), _M_length() + 1);
527               _M_destroy(__capacity);
528               _M_data(_M_local_data);
529             }
530         }
531     }
532
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)
538     {
539       const size_type __how_much = _M_length() - __pos - __len1;
540       
541       size_type __new_capacity = _M_length() + __len2 - __len1;
542       _CharT* __r = _M_create(__new_capacity, _M_capacity());
543
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);
551       
552       _M_dispose();
553       _M_data(__r);
554       _M_capacity(__new_capacity);
555     }
556
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)
561     {
562       const size_type __how_much = _M_length() - __pos - __n;
563
564       if (__how_much && __n)
565         _S_move(_M_data() + __pos, _M_data() + __pos + __n,
566                 __how_much);
567
568       _M_set_length(_M_length() - __n);
569     }
570
571 _GLIBCXX_END_NAMESPACE
572
573 #endif /* _SSO_STRING_BASE_H */