rc_string_base.h

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

Generated on Tue Feb 2 16:56:24 2010 for GNU C++ STL by  doxygen 1.4.7