PSP2SDK  dirty-f9e4f2d
The free SDK for PSP2
include/c++/ext/rc_string_base.h
Go to the documentation of this file.
00001 // Reference-counted versatile string base -*- C++ -*-
00002 
00003 // Copyright (C) 2015 PSP2SDK Project
00004 // This file is modified by PSP2SDK Team
00005 
00006 // Copyright (C) 2005-2014 Free Software Foundation, Inc.
00007 //
00008 // This file is part of the GNU ISO C++ Library.  This library is free
00009 // software; you can redistribute it and/or modify it under the
00010 // terms of the GNU General Public License as published by the
00011 // Free Software Foundation; either version 3, or (at your option)
00012 // any later version.
00013 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 
00019 // Under Section 7 of GPL version 3, you are granted additional
00020 // permissions described in the GCC Runtime Library Exception, version
00021 // 3.1, as published by the Free Software Foundation.
00022 
00023 // You should have received a copy of the GNU General Public License and
00024 // a copy of the GCC Runtime Library Exception along with this program;
00025 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00026 // <http://www.gnu.org/licenses/>.
00027 
00033 #ifndef _RC_STRING_BASE_H
00034 #define _RC_STRING_BASE_H 1
00035 
00036 #include <ext/atomicity.h>
00037 #include <bits/stl_iterator_base_funcs.h>
00038 #include <stdexcept>
00039 
00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00085  template<typename _CharT, typename _Traits, typename _Alloc>
00086     class __rc_string_base
00087     : protected __vstring_utility<_CharT, _Traits, _Alloc>
00088     {
00089     public:
00090       typedef _Traits                       traits_type;
00091       typedef typename _Traits::char_type           value_type;
00092       typedef _Alloc                        allocator_type;
00093 
00094       typedef __vstring_utility<_CharT, _Traits, _Alloc>    _Util_Base;
00095       typedef typename _Util_Base::_CharT_alloc_type        _CharT_alloc_type;
00096       typedef typename _CharT_alloc_type::size_type     size_type;
00097 
00098     private:
00099       // _Rep: string representation
00100       //   Invariants:
00101       //   1. String really contains _M_length + 1 characters: due to 21.3.4
00102       //      must be kept null-terminated.
00103       //   2. _M_capacity >= _M_length
00104       //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
00105       //   3. _M_refcount has three states:
00106       //      -1: leaked, one reference, no ref-copies allowed, non-const.
00107       //       0: one reference, non-const.
00108       //     n>0: n + 1 references, operations require a lock, const.
00109       //   4. All fields == 0 is an empty string, given the extra storage
00110       //      beyond-the-end for a null terminator; thus, the shared
00111       //      empty string representation needs no constructor.
00112       struct _Rep
00113       {
00114     union
00115     {
00116       struct
00117       {
00118         size_type       _M_length;
00119         size_type       _M_capacity;
00120         _Atomic_word    _M_refcount;
00121       }                 _M_info;
00122 
00123       // Only for alignment purposes.
00124       _CharT            _M_align;
00125     };
00126 
00127     typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
00128 
00129     _CharT*
00130     _M_refdata() throw()
00131     { return reinterpret_cast<_CharT*>(this + 1); }
00132 
00133     _CharT*
00134     _M_refcopy() throw()
00135     {
00136       __atomic_add_dispatch(&_M_info._M_refcount, 1);
00137       return _M_refdata();
00138     }  // XXX MT
00139 
00140     void
00141     _M_set_length(size_type __n)
00142     {
00143       _M_info._M_refcount = 0;  // One reference.
00144       _M_info._M_length = __n;
00145       // grrr. (per 21.3.4)
00146       // You cannot leave those LWG people alone for a second.
00147       traits_type::assign(_M_refdata()[__n], _CharT());
00148     }
00149 
00150     // Create & Destroy
00151     static _Rep*
00152     _S_create(size_type, size_type, const _Alloc&);
00153 
00154     void
00155     _M_destroy(const _Alloc&) throw();
00156 
00157     _CharT*
00158     _M_clone(const _Alloc&, size_type __res = 0);
00159       };
00160 
00161       struct _Rep_empty
00162       : public _Rep
00163       {
00164     _CharT              _M_terminal;
00165       };
00166 
00167       static _Rep_empty     _S_empty_rep;
00168 
00169       // The maximum number of individual char_type elements of an
00170       // individual string is determined by _S_max_size. This is the
00171       // value that will be returned by max_size().  (Whereas npos
00172       // is the maximum number of bytes the allocator can allocate.)
00173       // If one was to divvy up the theoretical largest size string,
00174       // with a terminating character and m _CharT elements, it'd
00175       // look like this:
00176       // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
00177       //        + sizeof(_Rep) - 1
00178       // (NB: last two terms for rounding reasons, see _M_create below)
00179       // Solving for m:
00180       // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
00181       // In addition, this implementation halves this amount.
00182       enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
00183                   + 1) / sizeof(_CharT)) - 1) / 2 };
00184 
00185       // Data Member (private):
00186       mutable typename _Util_Base::template _Alloc_hider<_Alloc>  _M_dataplus;
00187 
00188       void
00189       _M_data(_CharT* __p)
00190       { _M_dataplus._M_p = __p; }
00191 
00192       _Rep*
00193       _M_rep() const
00194       { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
00195 
00196       _CharT*
00197       _M_grab(const _Alloc& __alloc) const
00198       {
00199     return (!_M_is_leaked() && _M_get_allocator() == __alloc)
00200         ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
00201       }
00202 
00203       void
00204       _M_dispose()
00205       {
00206     // Be race-detector-friendly.  For more info see bits/c++config.
00207     _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
00208                         _M_refcount);
00209     if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
00210                     -1) <= 0)
00211       {
00212         _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
00213                            _M_refcount);
00214         _M_rep()->_M_destroy(_M_get_allocator());
00215       }
00216       }  // XXX MT
00217 
00218       bool
00219       _M_is_leaked() const
00220       { return _M_rep()->_M_info._M_refcount < 0; }
00221 
00222       void
00223       _M_set_sharable()
00224       { _M_rep()->_M_info._M_refcount = 0; }
00225 
00226       void
00227       _M_leak_hard();
00228 
00229       // _S_construct_aux is used to implement the 21.3.1 para 15 which
00230       // requires special behaviour if _InIterator is an integral type
00231       template<typename _InIterator>
00232     static _CharT*
00233     _S_construct_aux(_InIterator __beg, _InIterator __end,
00234              const _Alloc& __a, std::__false_type)
00235     {
00236       typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
00237       return _S_construct(__beg, __end, __a, _Tag());
00238     }
00239 
00240       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00241       // 438. Ambiguity in the "do the right thing" clause
00242       template<typename _Integer>
00243     static _CharT*
00244     _S_construct_aux(_Integer __beg, _Integer __end,
00245              const _Alloc& __a, std::__true_type)
00246     { return _S_construct_aux_2(static_cast<size_type>(__beg),
00247                     __end, __a); }
00248 
00249       static _CharT*
00250       _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
00251       { return _S_construct(__req, __c, __a); }
00252 
00253       template<typename _InIterator>
00254     static _CharT*
00255     _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
00256     {
00257       typedef typename std::__is_integer<_InIterator>::__type _Integral;
00258       return _S_construct_aux(__beg, __end, __a, _Integral());
00259     }
00260 
00261       // For Input Iterators, used in istreambuf_iterators, etc.
00262       template<typename _InIterator>
00263     static _CharT*
00264      _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
00265               std::input_iterator_tag);
00266 
00267       // For forward_iterators up to random_access_iterators, used for
00268       // string::iterator, _CharT*, etc.
00269       template<typename _FwdIterator>
00270     static _CharT*
00271     _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
00272              std::forward_iterator_tag);
00273 
00274       static _CharT*
00275       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
00276 
00277     public:
00278       size_type
00279       _M_max_size() const
00280       { return size_type(_S_max_size); }
00281 
00282       _CharT*
00283       _M_data() const
00284       { return _M_dataplus._M_p; }
00285 
00286       size_type
00287       _M_length() const
00288       { return _M_rep()->_M_info._M_length; }
00289 
00290       size_type
00291       _M_capacity() const
00292       { return _M_rep()->_M_info._M_capacity; }
00293 
00294       bool
00295       _M_is_shared() const
00296       { return _M_rep()->_M_info._M_refcount > 0; }
00297 
00298       void
00299       _M_set_leaked()
00300       { _M_rep()->_M_info._M_refcount = -1; }
00301 
00302       void
00303       _M_leak()    // for use in begin() & non-const op[]
00304       {
00305     if (!_M_is_leaked())
00306       _M_leak_hard();
00307       }
00308 
00309       void
00310       _M_set_length(size_type __n)
00311       { _M_rep()->_M_set_length(__n); }
00312 
00313       __rc_string_base()
00314       : _M_dataplus(_S_empty_rep._M_refcopy()) { }
00315 
00316       __rc_string_base(const _Alloc& __a);
00317 
00318       __rc_string_base(const __rc_string_base& __rcs);
00319 
00320 #if __cplusplus >= 201103L
00321       __rc_string_base(__rc_string_base&& __rcs)
00322       : _M_dataplus(__rcs._M_dataplus)
00323       { __rcs._M_data(_S_empty_rep._M_refcopy()); }
00324 #endif
00325 
00326       __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
00327 
00328       template<typename _InputIterator>
00329     __rc_string_base(_InputIterator __beg, _InputIterator __end,
00330              const _Alloc& __a);
00331 
00332       ~__rc_string_base()
00333       { _M_dispose(); }
00334 
00335       allocator_type&
00336       _M_get_allocator()
00337       { return _M_dataplus; }
00338 
00339       const allocator_type&
00340       _M_get_allocator() const
00341       { return _M_dataplus; }
00342 
00343       void
00344       _M_swap(__rc_string_base& __rcs);
00345 
00346       void
00347       _M_assign(const __rc_string_base& __rcs);
00348 
00349       void
00350       _M_reserve(size_type __res);
00351 
00352       void
00353       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
00354         size_type __len2);
00355 
00356       void
00357       _M_erase(size_type __pos, size_type __n);
00358 
00359       void
00360       _M_clear()
00361       { _M_erase(size_type(0), _M_length()); }
00362 
00363       bool
00364       _M_compare(const __rc_string_base&) const
00365       { return false; }
00366     };
00367 
00368   template<typename _CharT, typename _Traits, typename _Alloc>
00369     typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
00370     __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
00371 
00372   template<typename _CharT, typename _Traits, typename _Alloc>
00373     typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
00374     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
00375     _S_create(size_type __capacity, size_type __old_capacity,
00376           const _Alloc& __alloc)
00377     {
00378       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00379       // 83.  String::npos vs. string::max_size()
00380       if (__capacity > size_type(_S_max_size))
00381     std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
00382 
00383       // The standard places no restriction on allocating more memory
00384       // than is strictly needed within this layer at the moment or as
00385       // requested by an explicit application call to reserve().
00386 
00387       // Many malloc implementations perform quite poorly when an
00388       // application attempts to allocate memory in a stepwise fashion
00389       // growing each allocation size by only 1 char.  Additionally,
00390       // it makes little sense to allocate less linear memory than the
00391       // natural blocking size of the malloc implementation.
00392       // Unfortunately, we would need a somewhat low-level calculation
00393       // with tuned parameters to get this perfect for any particular
00394       // malloc implementation.  Fortunately, generalizations about
00395       // common features seen among implementations seems to suffice.
00396 
00397       // __pagesize need not match the actual VM page size for good
00398       // results in practice, thus we pick a common value on the low
00399       // side.  __malloc_header_size is an estimate of the amount of
00400       // overhead per memory allocation (in practice seen N * sizeof
00401       // (void*) where N is 0, 2 or 4).  According to folklore,
00402       // picking this value on the high side is better than
00403       // low-balling it (especially when this algorithm is used with
00404       // malloc implementations that allocate memory blocks rounded up
00405       // to a size which is a power of 2).
00406       const size_type __pagesize = 4096;
00407       const size_type __malloc_header_size = 4 * sizeof(void*);
00408 
00409       // The below implements an exponential growth policy, necessary to
00410       // meet amortized linear time requirements of the library: see
00411       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
00412       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
00413     {
00414       __capacity = 2 * __old_capacity;
00415       // Never allocate a string bigger than _S_max_size.
00416       if (__capacity > size_type(_S_max_size))
00417         __capacity = size_type(_S_max_size);
00418     }
00419 
00420       // NB: Need an array of char_type[__capacity], plus a terminating
00421       // null char_type() element, plus enough for the _Rep data structure,
00422       // plus sizeof(_Rep) - 1 to upper round to a size multiple of
00423       // sizeof(_Rep).
00424       // Whew. Seemingly so needy, yet so elemental.
00425       size_type __size = ((__capacity + 1) * sizeof(_CharT)
00426               + 2 * sizeof(_Rep) - 1);
00427 
00428       const size_type __adj_size = __size + __malloc_header_size;
00429       if (__adj_size > __pagesize && __capacity > __old_capacity)
00430     {
00431       const size_type __extra = __pagesize - __adj_size % __pagesize;
00432       __capacity += __extra / sizeof(_CharT);
00433       if (__capacity > size_type(_S_max_size))
00434         __capacity = size_type(_S_max_size);
00435       __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
00436     }
00437 
00438       // NB: Might throw, but no worries about a leak, mate: _Rep()
00439       // does not throw.
00440       _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
00441       _Rep* __p = new (__place) _Rep;
00442       __p->_M_info._M_capacity = __capacity;
00443       return __p;
00444     }
00445 
00446   template<typename _CharT, typename _Traits, typename _Alloc>
00447     void
00448     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
00449     _M_destroy(const _Alloc& __a) throw ()
00450     {
00451       const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
00452                 + 2 * sizeof(_Rep) - 1);
00453       _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
00454     }
00455 
00456   template<typename _CharT, typename _Traits, typename _Alloc>
00457     _CharT*
00458     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
00459     _M_clone(const _Alloc& __alloc, size_type __res)
00460     {
00461       // Requested capacity of the clone.
00462       const size_type __requested_cap = _M_info._M_length + __res;
00463       _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
00464                   __alloc);
00465 
00466       if (_M_info._M_length)
00467     __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
00468 
00469       __r->_M_set_length(_M_info._M_length);
00470       return __r->_M_refdata();
00471     }
00472 
00473   template<typename _CharT, typename _Traits, typename _Alloc>
00474     __rc_string_base<_CharT, _Traits, _Alloc>::
00475     __rc_string_base(const _Alloc& __a)
00476     : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
00477 
00478   template<typename _CharT, typename _Traits, typename _Alloc>
00479     __rc_string_base<_CharT, _Traits, _Alloc>::
00480     __rc_string_base(const __rc_string_base& __rcs)
00481     : _M_dataplus(__rcs._M_get_allocator(),
00482           __rcs._M_grab(__rcs._M_get_allocator())) { }
00483 
00484   template<typename _CharT, typename _Traits, typename _Alloc>
00485     __rc_string_base<_CharT, _Traits, _Alloc>::
00486     __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
00487     : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
00488 
00489   template<typename _CharT, typename _Traits, typename _Alloc>
00490     template<typename _InputIterator>
00491     __rc_string_base<_CharT, _Traits, _Alloc>::
00492     __rc_string_base(_InputIterator __beg, _InputIterator __end,
00493              const _Alloc& __a)
00494     : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
00495 
00496   template<typename _CharT, typename _Traits, typename _Alloc>
00497     void
00498     __rc_string_base<_CharT, _Traits, _Alloc>::
00499     _M_leak_hard()
00500     {
00501       if (_M_is_shared())
00502     _M_erase(0, 0);
00503       _M_set_leaked();
00504     }
00505 
00506   // NB: This is the special case for Input Iterators, used in
00507   // istreambuf_iterators, etc.
00508   // Input Iterators have a cost structure very different from
00509   // pointers, calling for a different coding style.
00510   template<typename _CharT, typename _Traits, typename _Alloc>
00511     template<typename _InIterator>
00512       _CharT*
00513       __rc_string_base<_CharT, _Traits, _Alloc>::
00514       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
00515            std::input_iterator_tag)
00516       {
00517     if (__beg == __end && __a == _Alloc())
00518       return _S_empty_rep._M_refcopy();
00519 
00520     // Avoid reallocation for common case.
00521     _CharT __buf[128];
00522     size_type __len = 0;
00523     while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
00524       {
00525         __buf[__len++] = *__beg;
00526         ++__beg;
00527       }
00528     _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
00529     _S_copy(__r->_M_refdata(), __buf, __len);
00530     __try
00531       {
00532         while (__beg != __end)
00533           {
00534         if (__len == __r->_M_info._M_capacity)
00535           {
00536             // Allocate more space.
00537             _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
00538             _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
00539             __r->_M_destroy(__a);
00540             __r = __another;
00541           }
00542         __r->_M_refdata()[__len++] = *__beg;
00543         ++__beg;
00544           }
00545       }
00546     __catch(...)
00547       {
00548         __r->_M_destroy(__a);
00549         __throw_exception_again;
00550       }
00551     __r->_M_set_length(__len);
00552     return __r->_M_refdata();
00553       }
00554 
00555   template<typename _CharT, typename _Traits, typename _Alloc>
00556     template<typename _InIterator>
00557       _CharT*
00558       __rc_string_base<_CharT, _Traits, _Alloc>::
00559       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
00560            std::forward_iterator_tag)
00561       {
00562     if (__beg == __end && __a == _Alloc())
00563       return _S_empty_rep._M_refcopy();
00564 
00565     // NB: Not required, but considered best practice.
00566     if (__is_null_pointer(__beg) && __beg != __end)
00567       throw logic_error(__N("__rc_string_base::"
00568                        "_S_construct null not valid"));
00569 
00570     const size_type __dnew = static_cast<size_type>(std::distance(__beg,
00571                                       __end));
00572     // Check for out_of_range and length_error exceptions.
00573     _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
00574     __try
00575       { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
00576     __catch(...)
00577       {
00578         __r->_M_destroy(__a);
00579         __throw_exception_again;
00580       }
00581     __r->_M_set_length(__dnew);
00582     return __r->_M_refdata();
00583       }
00584 
00585   template<typename _CharT, typename _Traits, typename _Alloc>
00586     _CharT*
00587     __rc_string_base<_CharT, _Traits, _Alloc>::
00588     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
00589     {
00590       if (__n == 0 && __a == _Alloc())
00591     return _S_empty_rep._M_refcopy();
00592 
00593       // Check for out_of_range and length_error exceptions.
00594       _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
00595       if (__n)
00596     __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
00597 
00598       __r->_M_set_length(__n);
00599       return __r->_M_refdata();
00600     }
00601 
00602   template<typename _CharT, typename _Traits, typename _Alloc>
00603     void
00604     __rc_string_base<_CharT, _Traits, _Alloc>::
00605     _M_swap(__rc_string_base& __rcs)
00606     {
00607       if (_M_is_leaked())
00608     _M_set_sharable();
00609       if (__rcs._M_is_leaked())
00610     __rcs._M_set_sharable();
00611 
00612       _CharT* __tmp = _M_data();
00613       _M_data(__rcs._M_data());
00614       __rcs._M_data(__tmp);
00615 
00616       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00617       // 431. Swapping containers with unequal allocators.
00618       std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
00619                           __rcs._M_get_allocator());
00620     }
00621 
00622   template<typename _CharT, typename _Traits, typename _Alloc>
00623     void
00624     __rc_string_base<_CharT, _Traits, _Alloc>::
00625     _M_assign(const __rc_string_base& __rcs)
00626     {
00627       if (_M_rep() != __rcs._M_rep())
00628     {
00629       _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
00630       _M_dispose();
00631       _M_data(__tmp);
00632     }
00633     }
00634 
00635   template<typename _CharT, typename _Traits, typename _Alloc>
00636     void
00637     __rc_string_base<_CharT, _Traits, _Alloc>::
00638     _M_reserve(size_type __res)
00639     {
00640       // Make sure we don't shrink below the current size.
00641       if (__res < _M_length())
00642     __res = _M_length();
00643 
00644       if (__res != _M_capacity() || _M_is_shared())
00645     {
00646       _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
00647                          __res - _M_length());
00648       _M_dispose();
00649       _M_data(__tmp);
00650     }
00651     }
00652 
00653   template<typename _CharT, typename _Traits, typename _Alloc>
00654     void
00655     __rc_string_base<_CharT, _Traits, _Alloc>::
00656     _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
00657           size_type __len2)
00658     {
00659       const size_type __how_much = _M_length() - __pos - __len1;
00660 
00661       _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
00662                   _M_capacity(), _M_get_allocator());
00663 
00664       if (__pos)
00665     this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
00666       if (__s && __len2)
00667     this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
00668       if (__how_much)
00669     this->_S_copy(__r->_M_refdata() + __pos + __len2,
00670         _M_data() + __pos + __len1, __how_much);
00671 
00672       _M_dispose();
00673       _M_data(__r->_M_refdata());
00674     }
00675 
00676   template<typename _CharT, typename _Traits, typename _Alloc>
00677     void
00678     __rc_string_base<_CharT, _Traits, _Alloc>::
00679     _M_erase(size_type __pos, size_type __n)
00680     {
00681       const size_type __new_size = _M_length() - __n;
00682       const size_type __how_much = _M_length() - __pos - __n;
00683 
00684       if (_M_is_shared())
00685     {
00686       // Must reallocate.
00687       _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
00688                       _M_get_allocator());
00689 
00690       if (__pos)
00691         this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
00692       if (__how_much)
00693         this->_S_copy(__r->_M_refdata() + __pos,
00694             _M_data() + __pos + __n, __how_much);
00695 
00696       _M_dispose();
00697       _M_data(__r->_M_refdata());
00698     }
00699       else if (__how_much && __n)
00700     {
00701       // Work in-place.
00702       this->_S_move(_M_data() + __pos,
00703           _M_data() + __pos + __n, __how_much);
00704     }
00705 
00706       _M_rep()->_M_set_length(__new_size);
00707     }
00708 
00709   template<>
00710     inline bool
00711     __rc_string_base<char, std::char_traits<char>,
00712              std::allocator<char> >::
00713     _M_compare(const __rc_string_base& __rcs) const
00714     {
00715       if (_M_rep() == __rcs._M_rep())
00716     return true;
00717       return false;
00718     }
00719 
00720 #ifdef _GLIBCXX_USE_WCHAR_T
00721   template<>
00722     inline bool
00723     __rc_string_base<wchar_t, std::char_traits<wchar_t>,
00724              std::allocator<wchar_t> >::
00725     _M_compare(const __rc_string_base& __rcs) const
00726     {
00727       if (_M_rep() == __rcs._M_rep())
00728     return true;
00729       return false;
00730     }
00731 #endif
00732 
00733 _GLIBCXX_END_NAMESPACE_VERSION
00734 } // namespace
00735 
00736 #endif /* _RC_STRING_BASE_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines