# sso_string_base.h

Go to the documentation of this file.

    00001 // Short-string-optimized versatile string base -*- C++ -*-
    00002
    00003 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file ext/sso_string_base.h
    00031  *  This file is a GNU extension to the Standard C++ Library.
    00032  *  This is an internal header file, included by other library headers.
    00033  *  You should not attempt to use it directly.
    00034  */
    00035
    00036 #ifndef _SSO_STRING_BASE_H
    00037 #define _SSO_STRING_BASE_H 1
    00038
    00039 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00040
    00041   template<typename _CharT, typename _Traits, typename _Alloc>
    00042     class __sso_string_base
    00043     : protected __vstring_utility<_CharT, _Traits, _Alloc>
    00044     {
    00045     public:
    00046       typedef _Traits                       traits_type;
    00047       typedef typename _Traits::char_type           value_type;
    00048
    00049       typedef __vstring_utility<_CharT, _Traits, _Alloc>    _Util_Base;
    00050       typedef typename _Util_Base::_CharT_alloc_type        _CharT_alloc_type;
    00051       typedef typename _CharT_alloc_type::size_type     size_type;
    00052
    00053     private:
    00054       // Data Members:
    00055       typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
    00056                                                             _M_dataplus;
    00057       size_type                                             _M_string_length;
    00058
    00059       enum { _S_local_capacity = 15 };
    00060
    00061       union
    00062       {
    00063     _CharT           _M_local_data[_S_local_capacity + 1];
    00064     size_type        _M_allocated_capacity;
    00065       };
    00066
    00067       void
    00068       _M_data(_CharT* __p)
    00069       { _M_dataplus._M_p = __p; }
    00070
    00071       void
    00072       _M_length(size_type __length)
    00073       { _M_string_length = __length; }
    00074
    00075       void
    00076       _M_capacity(size_type __capacity)
    00077       { _M_allocated_capacity = __capacity; }
    00078
    00079       bool
    00080       _M_is_local() const
    00081       { return _M_data() == _M_local_data; }
    00082
    00083       // Create & Destroy
    00084       _CharT*
    00085       _M_create(size_type&, size_type);
    00086
    00087       void
    00088       _M_dispose()
    00089       {
    00090     if (!_M_is_local())
    00091       _M_destroy(_M_allocated_capacity);
    00092       }
    00093
    00094       void
    00095       _M_destroy(size_type __size) throw()
    00096       { _M_get_allocator().deallocate(_M_data(), __size + 1); }
    00097
    00098       // _M_construct_aux is used to implement the 21.3.1 para 15 which
    00099       // requires special behaviour if _InIterator is an integral type
    00100       template<typename _InIterator>
    00101         void
    00102         _M_construct_aux(_InIterator __beg, _InIterator __end,
    00103              std::__false_type)
    00104     {
    00105           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
    00106           _M_construct(__beg, __end, _Tag());
    00107     }
    00108
    00109       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00110       // 438. Ambiguity in the "do the right thing" clause
    00111       template<typename _Integer>
    00112         void
    00113         _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
    00114     { _M_construct(static_cast<size_type>(__beg), __end); }
    00115
    00116       template<typename _InIterator>
    00117         void
    00118         _M_construct(_InIterator __beg, _InIterator __end)
    00119     {
    00120       typedef typename std::__is_integer<_InIterator>::__type _Integral;
    00121       _M_construct_aux(__beg, __end, _Integral());
    00122         }
    00123
    00124       // For Input Iterators, used in istreambuf_iterators, etc.
    00125       template<typename _InIterator>
    00126         void
    00127         _M_construct(_InIterator __beg, _InIterator __end,
    00128              std::input_iterator_tag);
    00129
    00130       // For forward_iterators up to random_access_iterators, used for
    00131       // string::iterator, _CharT*, etc.
    00132       template<typename _FwdIterator>
    00133         void
    00134         _M_construct(_FwdIterator __beg, _FwdIterator __end,
    00135              std::forward_iterator_tag);
    00136
    00137       void
    00138       _M_construct(size_type __req, _CharT __c);
    00139
    00140     public:
    00141       size_type
    00142       _M_max_size() const
    00143       { return (_M_get_allocator().max_size() - 1) / 2; }
    00144
    00145       _CharT*
    00146       _M_data() const
    00147       { return _M_dataplus._M_p; }
    00148
    00149       size_type
    00150       _M_length() const
    00151       { return _M_string_length; }
    00152
    00153       size_type
    00154       _M_capacity() const
    00155       {
    00156     return _M_is_local() ? size_type(_S_local_capacity)
    00157                          : _M_allocated_capacity;
    00158       }
    00159
    00160       bool
    00161       _M_is_shared() const
    00162       { return false; }
    00163
    00164       void
    00165       _M_set_leaked() { }
    00166
    00167       void
    00168       _M_leak() { }
    00169
    00170       void
    00171       _M_set_length(size_type __n)
    00172       {
    00173     _M_length(__n);
    00174     traits_type::assign(_M_data()[__n], _CharT());
    00175       }
    00176
    00177       __sso_string_base()
    00178       : _M_dataplus(_M_local_data)
    00179       { _M_set_length(0); }
    00180
    00181       __sso_string_base(const _Alloc& __a);
    00182
    00183       __sso_string_base(const __sso_string_base& __rcs);
    00184
    00185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00186       __sso_string_base(__sso_string_base&& __rcs);
    00187 #endif
    00188
    00189       __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
    00190
    00191       template<typename _InputIterator>
    00192         __sso_string_base(_InputIterator __beg, _InputIterator __end,
    00193               const _Alloc& __a);
    00194
    00195       ~__sso_string_base()
    00196       { _M_dispose(); }
    00197
    00198       _CharT_alloc_type&
    00199       _M_get_allocator()
    00200       { return _M_dataplus; }
    00201
    00202       const _CharT_alloc_type&
    00203       _M_get_allocator() const
    00204       { return _M_dataplus; }
    00205
    00206       void
    00207       _M_swap(__sso_string_base& __rcs);
    00208
    00209       void
    00210       _M_assign(const __sso_string_base& __rcs);
    00211
    00212       void
    00213       _M_reserve(size_type __res);
    00214
    00215       void
    00216       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
    00217         size_type __len2);
    00218
    00219       void
    00220       _M_erase(size_type __pos, size_type __n);
    00221
    00222       void
    00223       _M_clear()
    00224       { _M_set_length(0); }
    00225
    00226       bool
    00227       _M_compare(const __sso_string_base&) const
    00228       { return false; }
    00229     };
    00230
    00231   template<typename _CharT, typename _Traits, typename _Alloc>
    00232     void
    00233     __sso_string_base<_CharT, _Traits, _Alloc>::
    00234     _M_swap(__sso_string_base& __rcs)
    00235     {
    00236       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00237       // 431. Swapping containers with unequal allocators.
    00238       std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
    00239                              __rcs._M_get_allocator());
    00240
    00241       if (_M_is_local())
    00242     if (__rcs._M_is_local())
    00243       {
    00244         if (_M_length() && __rcs._M_length())
    00245           {
    00246         _CharT __tmp_data[_S_local_capacity + 1];
    00247         traits_type::copy(__tmp_data, __rcs._M_local_data,
    00248                   _S_local_capacity + 1);
    00249         traits_type::copy(__rcs._M_local_data, _M_local_data,
    00250                   _S_local_capacity + 1);
    00251         traits_type::copy(_M_local_data, __tmp_data,
    00252                   _S_local_capacity + 1);
    00253           }
    00254         else if (__rcs._M_length())
    00255           {
    00256         traits_type::copy(_M_local_data, __rcs._M_local_data,
    00257                   _S_local_capacity + 1);
    00258         _M_length(__rcs._M_length());
    00259         __rcs._M_set_length(0);
    00260         return;
    00261           }
    00262         else if (_M_length())
    00263           {
    00264         traits_type::copy(__rcs._M_local_data, _M_local_data,
    00265                   _S_local_capacity + 1);
    00266         __rcs._M_length(_M_length());
    00267         _M_set_length(0);
    00268         return;
    00269           }
    00270       }
    00271     else
    00272       {
    00273         const size_type __tmp_capacity = __rcs._M_allocated_capacity;
    00274         traits_type::copy(__rcs._M_local_data, _M_local_data,
    00275                   _S_local_capacity + 1);
    00276         _M_data(__rcs._M_data());
    00277         __rcs._M_data(__rcs._M_local_data);
    00278         _M_capacity(__tmp_capacity);
    00279       }
    00280       else
    00281     {
    00282       const size_type __tmp_capacity = _M_allocated_capacity;
    00283       if (__rcs._M_is_local())
    00284         {
    00285           traits_type::copy(_M_local_data, __rcs._M_local_data,
    00286                 _S_local_capacity + 1);
    00287           __rcs._M_data(_M_data());
    00288           _M_data(_M_local_data);
    00289         }
    00290       else
    00291         {
    00292           _CharT* __tmp_ptr = _M_data();
    00293           _M_data(__rcs._M_data());
    00294           __rcs._M_data(__tmp_ptr);
    00295           _M_capacity(__rcs._M_allocated_capacity);
    00296         }
    00297       __rcs._M_capacity(__tmp_capacity);
    00298     }
    00299
    00300       const size_type __tmp_length = _M_length();
    00301       _M_length(__rcs._M_length());
    00302       __rcs._M_length(__tmp_length);
    00303     }
    00304
    00305   template<typename _CharT, typename _Traits, typename _Alloc>
    00306     _CharT*
    00307     __sso_string_base<_CharT, _Traits, _Alloc>::
    00308     _M_create(size_type& __capacity, size_type __old_capacity)
    00309     {
    00310       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00311       // 83.  String::npos vs. string::max_size()
    00312       if (__capacity > _M_max_size())
    00313     std::__throw_length_error(__N("__sso_string_base::_M_create"));
    00314
    00315       // The below implements an exponential growth policy, necessary to
    00316       // meet amortized linear time requirements of the library: see
    00317       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
    00318       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
    00319     {
    00320       __capacity = 2 * __old_capacity;
    00321       // Never allocate a string bigger than max_size.
    00322       if (__capacity > _M_max_size())
    00323         __capacity = _M_max_size();
    00324     }
    00325
    00326       // NB: Need an array of char_type[__capacity], plus a terminating
    00327       // null char_type() element.
    00328       return _M_get_allocator().allocate(__capacity + 1);
    00329     }
    00330
    00331   template<typename _CharT, typename _Traits, typename _Alloc>
    00332     __sso_string_base<_CharT, _Traits, _Alloc>::
    00333     __sso_string_base(const _Alloc& __a)
    00334     : _M_dataplus(__a, _M_local_data)
    00335     { _M_set_length(0); }
    00336
    00337   template<typename _CharT, typename _Traits, typename _Alloc>
    00338     __sso_string_base<_CharT, _Traits, _Alloc>::
    00339     __sso_string_base(const __sso_string_base& __rcs)
    00340     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
    00341     { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
    00342
    00343 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00344   template<typename _CharT, typename _Traits, typename _Alloc>
    00345     __sso_string_base<_CharT, _Traits, _Alloc>::
    00346     __sso_string_base(__sso_string_base&& __rcs)
    00347     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
    00348     {
    00349       if (__rcs._M_is_local())
    00350     {
    00351       if (__rcs._M_length())
    00352         traits_type::copy(_M_local_data, __rcs._M_local_data,
    00353                   _S_local_capacity + 1);
    00354     }
    00355       else
    00356     {
    00357       _M_data(__rcs._M_data());
    00358       _M_capacity(__rcs._M_allocated_capacity);
    00359     }
    00360
    00361       _M_length(__rcs._M_length());
    00362       __rcs._M_length(0);
    00363       __rcs._M_data(__rcs._M_local_data);
    00364     }
    00365 #endif
    00366
    00367   template<typename _CharT, typename _Traits, typename _Alloc>
    00368     __sso_string_base<_CharT, _Traits, _Alloc>::
    00369     __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
    00370     : _M_dataplus(__a, _M_local_data)
    00371     { _M_construct(__n, __c); }
    00372
    00373   template<typename _CharT, typename _Traits, typename _Alloc>
    00374     template<typename _InputIterator>
    00375     __sso_string_base<_CharT, _Traits, _Alloc>::
    00376     __sso_string_base(_InputIterator __beg, _InputIterator __end,
    00377               const _Alloc& __a)
    00378     : _M_dataplus(__a, _M_local_data)
    00379     { _M_construct(__beg, __end); }
    00380
    00381   // NB: This is the special case for Input Iterators, used in
    00382   // istreambuf_iterators, etc.
    00383   // Input Iterators have a cost structure very different from
    00384   // pointers, calling for a different coding style.
    00385   template<typename _CharT, typename _Traits, typename _Alloc>
    00386     template<typename _InIterator>
    00387       void
    00388       __sso_string_base<_CharT, _Traits, _Alloc>::
    00389       _M_construct(_InIterator __beg, _InIterator __end,
    00390            std::input_iterator_tag)
    00391       {
    00392     size_type __len = 0;
    00393     size_type __capacity = size_type(_S_local_capacity);
    00394
    00395     while (__beg != __end && __len < __capacity)
    00396       {
    00397         _M_data()[__len++] = *__beg;
    00398         ++__beg;
    00399       }
    00400
    00401     try
    00402       {
    00403         while (__beg != __end)
    00404           {
    00405         if (__len == __capacity)
    00406           {
    00407             // Allocate more space.
    00408             __capacity = __len + 1;
    00409             _CharT* __another = _M_create(__capacity, __len);
    00410             _S_copy(__another, _M_data(), __len);
    00411             _M_dispose();
    00412             _M_data(__another);
    00413             _M_capacity(__capacity);
    00414           }
    00415         _M_data()[__len++] = *__beg;
    00416         ++__beg;
    00417           }
    00418       }
    00419     catch(...)
    00420       {
    00421         _M_dispose();
    00422         __throw_exception_again;
    00423       }
    00424
    00425     _M_set_length(__len);
    00426       }
    00427
    00428   template<typename _CharT, typename _Traits, typename _Alloc>
    00429     template<typename _InIterator>
    00430       void
    00431       __sso_string_base<_CharT, _Traits, _Alloc>::
    00432       _M_construct(_InIterator __beg, _InIterator __end,
    00433            std::forward_iterator_tag)
    00434       {
    00435     // NB: Not required, but considered best practice.
    00436     if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
    00437       std::__throw_logic_error(__N("__sso_string_base::"
    00438                        "_M_construct NULL not valid"));
    00439
    00440     size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
    00441
    00442     if (__dnew > size_type(_S_local_capacity))
    00443       {
    00444         _M_data(_M_create(__dnew, size_type(0)));
    00445         _M_capacity(__dnew);
    00446       }
    00447
    00448     // Check for out_of_range and length_error exceptions.
    00449     try
    00450       { _S_copy_chars(_M_data(), __beg, __end); }
    00451     catch(...)
    00452       {
    00453         _M_dispose();
    00454         __throw_exception_again;
    00455       }
    00456
    00457     _M_set_length(__dnew);
    00458       }
    00459
    00460   template<typename _CharT, typename _Traits, typename _Alloc>
    00461     void
    00462     __sso_string_base<_CharT, _Traits, _Alloc>::
    00463     _M_construct(size_type __n, _CharT __c)
    00464     {
    00465       if (__n > size_type(_S_local_capacity))
    00466     {
    00467       _M_data(_M_create(__n, size_type(0)));
    00468       _M_capacity(__n);
    00469     }
    00470
    00471       if (__n)
    00472     _S_assign(_M_data(), __n, __c);
    00473
    00474       _M_set_length(__n);
    00475     }
    00476
    00477   template<typename _CharT, typename _Traits, typename _Alloc>
    00478     void
    00479     __sso_string_base<_CharT, _Traits, _Alloc>::
    00480     _M_assign(const __sso_string_base& __rcs)
    00481     {
    00482       if (this != &__rcs)
    00483     {
    00484       const size_type __rsize = __rcs._M_length();
    00485       const size_type __capacity = _M_capacity();
    00486
    00487       if (__rsize > __capacity)
    00488         {
    00489           size_type __new_capacity = __rsize;
    00490           _CharT* __tmp = _M_create(__new_capacity, __capacity);
    00491           _M_dispose();
    00492           _M_data(__tmp);
    00493           _M_capacity(__new_capacity);
    00494         }
    00495
    00496       if (__rsize)
    00497         _S_copy(_M_data(), __rcs._M_data(), __rsize);
    00498
    00499       _M_set_length(__rsize);
    00500     }
    00501     }
    00502
    00503   template<typename _CharT, typename _Traits, typename _Alloc>
    00504     void
    00505     __sso_string_base<_CharT, _Traits, _Alloc>::
    00506     _M_reserve(size_type __res)
    00507     {
    00508       // Make sure we don't shrink below the current size.
    00509       if (__res < _M_length())
    00510     __res = _M_length();
    00511
    00512       const size_type __capacity = _M_capacity();
    00513       if (__res != __capacity)
    00514     {
    00515       if (__res > __capacity
    00516           || __res > size_type(_S_local_capacity))
    00517         {
    00518           _CharT* __tmp = _M_create(__res, __capacity);
    00519           _S_copy(__tmp, _M_data(), _M_length() + 1);
    00520           _M_dispose();
    00521           _M_data(__tmp);
    00522           _M_capacity(__res);
    00523         }
    00524       else if (!_M_is_local())
    00525         {
    00526           _S_copy(_M_local_data, _M_data(), _M_length() + 1);
    00527           _M_destroy(__capacity);
    00528           _M_data(_M_local_data);
    00529         }
    00530     }
    00531     }
    00532
    00533   template<typename _CharT, typename _Traits, typename _Alloc>
    00534     void
    00535     __sso_string_base<_CharT, _Traits, _Alloc>::
    00536     _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
    00537           const size_type __len2)
    00538     {
    00539       const size_type __how_much = _M_length() - __pos - __len1;
    00540
    00541       size_type __new_capacity = _M_length() + __len2 - __len1;
    00542       _CharT* __r = _M_create(__new_capacity, _M_capacity());
    00543
    00544       if (__pos)
    00545     _S_copy(__r, _M_data(), __pos);
    00546       if (__s && __len2)
    00547     _S_copy(__r + __pos, __s, __len2);
    00548       if (__how_much)
    00549     _S_copy(__r + __pos + __len2,
    00550         _M_data() + __pos + __len1, __how_much);
    00551
    00552       _M_dispose();
    00553       _M_data(__r);
    00554       _M_capacity(__new_capacity);
    00555     }
    00556
    00557   template<typename _CharT, typename _Traits, typename _Alloc>
    00558     void
    00559     __sso_string_base<_CharT, _Traits, _Alloc>::
    00560     _M_erase(size_type __pos, size_type __n)
    00561     {
    00562       const size_type __how_much = _M_length() - __pos - __n;
    00563
    00564       if (__how_much && __n)
    00565     _S_move(_M_data() + __pos, _M_data() + __pos + __n,
    00566         __how_much);
    00567
    00568       _M_set_length(_M_length() - __n);
    00569     }
    00570
    00571 _GLIBCXX_END_NAMESPACE
    00572
    00573 #endif /* _SSO_STRING_BASE_H */