// Reference-counted versatile string base -*- C++ -*-
-// Copyright (C) 2005 Free Software Foundation, Inc.
+// Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 2, or (at your option)
+// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING. If not, write to the Free
-// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
-// As a special exception, you may use this file as part of a free software
-// library without restriction. Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License. This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
/** @file ext/rc_string_base.h
* This file is a GNU extension to the Standard C++ Library.
#ifndef _RC_STRING_BASE_H
#define _RC_STRING_BASE_H 1
-#include <bits/atomicity.h>
+#include <ext/atomicity.h>
+#include <bits/stl_iterator_base_funcs.h>
+
+_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
-namespace __gnu_cxx
-{
/**
- * @if maint
* Documentation? What's that?
* Nathan Myers <ncm@cantrip.org>.
*
*
* All but the last paragraph is considered pretty conventional
* for a C++ string implementation.
- * @endif
*/
template<typename _CharT, typename _Traits, typename _Alloc>
class __rc_string_base
_CharT*
_M_refcopy() throw()
{
- __atomic_add(&_M_info._M_refcount, 1);
+ __atomic_add_dispatch(&_M_info._M_refcount, 1);
return _M_refdata();
} // XXX MT
// with a terminating character and m _CharT elements, it'd
// look like this:
// npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
+ // + sizeof(_Rep) - 1
+ // (NB: last two terms for rounding reasons, see _M_create below)
// Solving for m:
- // m = ((npos - sizeof(_Rep)) / sizeof(_CharT)) - 1
- // In addition, this implementation quarters this amount.
- enum { _S_max_size = (((static_cast<size_type>(-1) - sizeof(_Rep))
- / sizeof(_CharT)) - 1) / 4 };
+ // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
+ // In addition, this implementation halves this amount.
+ enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
+ + 1) / sizeof(_CharT)) - 1) / 2 };
// Data Member (private):
mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
void
_M_dispose()
{
- if (__exchange_and_add(&_M_rep()->_M_info._M_refcount, -1) <= 0)
+ if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
+ -1) <= 0)
_M_rep()->_M_destroy(_M_get_allocator());
} // XXX MT
template<typename _InIterator>
static _CharT*
_S_construct_aux(_InIterator __beg, _InIterator __end,
- const _Alloc& __a, __false_type)
+ const _Alloc& __a, std::__false_type)
{
typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
return _S_construct(__beg, __end, __a, _Tag());
}
- template<typename _InIterator>
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 438. Ambiguity in the "do the right thing" clause
+ template<typename _Integer>
static _CharT*
- _S_construct_aux(_InIterator __beg, _InIterator __end,
- const _Alloc& __a, __true_type)
- { return _S_construct(static_cast<size_type>(__beg),
- static_cast<value_type>(__end), __a); }
+ _S_construct_aux(_Integer __beg, _Integer __end,
+ const _Alloc& __a, std::__true_type)
+ { return _S_construct(static_cast<size_type>(__beg), __end, __a); }
template<typename _InIterator>
static _CharT*
{ _M_rep()->_M_set_length(__n); }
__rc_string_base()
- : _M_dataplus(_Alloc(), _S_empty_rep._M_refcopy()) { }
+ : _M_dataplus(_S_empty_rep._M_refcopy()) { }
__rc_string_base(const _Alloc& __a);
__rc_string_base(const __rc_string_base& __rcs);
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+ __rc_string_base(__rc_string_base&& __rcs)
+ : _M_dataplus(__rcs._M_get_allocator(), __rcs._M_data())
+ { __rcs._M_data(_S_empty_rep._M_refcopy()); }
+#endif
+
__rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
template<typename _InputIterator>
~__rc_string_base()
{ _M_dispose(); }
+ allocator_type&
+ _M_get_allocator()
+ { return _M_dataplus; }
+
const allocator_type&
_M_get_allocator() const
{ return _M_dataplus; }
_M_reserve(size_type __res);
void
- _M_mutate(size_type __pos, size_type __len1, size_type __len2);
+ _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
+ size_type __len2);
+
+ void
+ _M_erase(size_type __pos, size_type __n);
+
+ void
+ _M_clear()
+ { _M_erase(size_type(0), _M_length()); }
+
+ bool
+ _M_compare(const __rc_string_base&) const
+ { return false; }
};
template<typename _CharT, typename _Traits, typename _Alloc>
// meet amortized linear time requirements of the library: see
// http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
- __capacity = 2 * __old_capacity;
+ {
+ __capacity = 2 * __old_capacity;
+ // Never allocate a string bigger than _S_max_size.
+ if (__capacity > size_type(_S_max_size))
+ __capacity = size_type(_S_max_size);
+ }
// NB: Need an array of char_type[__capacity], plus a terminating
// null char_type() element, plus enough for the _Rep data structure,
- // plus sizeof(size_type) - 1 to upper round to a size multiple
- // of sizeof(size_type).
+ // plus sizeof(_Rep) - 1 to upper round to a size multiple of
+ // sizeof(_Rep).
// Whew. Seemingly so needy, yet so elemental.
size_type __size = ((__capacity + 1) * sizeof(_CharT)
+ 2 * sizeof(_Rep) - 1);
{
const size_type __extra = __pagesize - __adj_size % __pagesize;
__capacity += __extra / sizeof(_CharT);
- // Never allocate a string bigger than _S_max_size.
if (__capacity > size_type(_S_max_size))
__capacity = size_type(_S_max_size);
__size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
_M_leak_hard()
{
if (_M_is_shared())
- _M_mutate(0, 0, 0);
+ _M_erase(0, 0);
_M_set_leaked();
}
}
_Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
_S_copy(__r->_M_refdata(), __buf, __len);
- try
+ __try
{
while (__beg != __end)
{
- if (__len == __r->_M_capacity)
+ if (__len == __r->_M_info._M_capacity)
{
// Allocate more space.
_Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
++__beg;
}
}
- catch(...)
+ __catch(...)
{
__r->_M_destroy(__a);
__throw_exception_again;
return _S_empty_rep._M_refcopy();
// NB: Not required, but considered best practice.
- if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
+ if (__is_null_pointer(__beg) && __beg != __end)
std::__throw_logic_error(__N("__rc_string_base::"
"_S_construct NULL not valid"));
__end));
// Check for out_of_range and length_error exceptions.
_Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
- try
+ __try
{ _S_copy_chars(__r->_M_refdata(), __beg, __end); }
- catch(...)
+ __catch(...)
{
__r->_M_destroy(__a);
__throw_exception_again;
_CharT* __tmp = _M_data();
_M_data(__rcs._M_data());
__rcs._M_data(__tmp);
-
- // NB: Implement Option 3 of DR 431 (see N1599).
- _M_dataplus._M_alloc_swap(__rcs._M_dataplus);
+
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 431. Swapping containers with unequal allocators.
+ std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
+ __rcs._M_get_allocator());
}
template<typename _CharT, typename _Traits, typename _Alloc>
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_reserve(size_type __res)
{
+ // Make sure we don't shrink below the current size.
+ if (__res < _M_length())
+ __res = _M_length();
+
if (__res != _M_capacity() || _M_is_shared())
{
- // Make sure we don't shrink below the current size.
- if (__res < _M_length())
- __res = _M_length();
-
_CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
__res - _M_length());
_M_dispose();
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
- _M_mutate(size_type __pos, size_type __len1, size_type __len2)
+ _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
+ size_type __len2)
{
- const size_type __old_size = _M_length();
- const size_type __new_size = __old_size + __len2 - __len1;
- const size_type __how_much = __old_size - __pos - __len1;
+ const size_type __how_much = _M_length() - __pos - __len1;
- if (__new_size > _M_capacity() || _M_is_shared())
+ _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
+ _M_capacity(), _M_get_allocator());
+
+ if (__pos)
+ _S_copy(__r->_M_refdata(), _M_data(), __pos);
+ if (__s && __len2)
+ _S_copy(__r->_M_refdata() + __pos, __s, __len2);
+ if (__how_much)
+ _S_copy(__r->_M_refdata() + __pos + __len2,
+ _M_data() + __pos + __len1, __how_much);
+
+ _M_dispose();
+ _M_data(__r->_M_refdata());
+ }
+
+ template<typename _CharT, typename _Traits, typename _Alloc>
+ void
+ __rc_string_base<_CharT, _Traits, _Alloc>::
+ _M_erase(size_type __pos, size_type __n)
+ {
+ const size_type __new_size = _M_length() - __n;
+ const size_type __how_much = _M_length() - __pos - __n;
+
+ if (_M_is_shared())
{
// Must reallocate.
_Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
if (__pos)
_S_copy(__r->_M_refdata(), _M_data(), __pos);
if (__how_much)
- _S_copy(__r->_M_refdata() + __pos + __len2,
- _M_data() + __pos + __len1, __how_much);
+ _S_copy(__r->_M_refdata() + __pos,
+ _M_data() + __pos + __n, __how_much);
_M_dispose();
_M_data(__r->_M_refdata());
}
- else if (__how_much && __len1 != __len2)
+ else if (__how_much && __n)
{
// Work in-place.
- _S_move(_M_data() + __pos + __len2,
- _M_data() + __pos + __len1, __how_much);
+ _S_move(_M_data() + __pos,
+ _M_data() + __pos + __n, __how_much);
}
- _M_rep()->_M_set_length(__new_size);
+
+ _M_rep()->_M_set_length(__new_size);
+ }
+
+ template<>
+ inline bool
+ __rc_string_base<char, std::char_traits<char>,
+ std::allocator<char> >::
+ _M_compare(const __rc_string_base& __rcs) const
+ {
+ if (_M_rep() == __rcs._M_rep())
+ return true;
+ return false;
}
-} // namespace __gnu_cxx
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+ template<>
+ inline bool
+ __rc_string_base<wchar_t, std::char_traits<wchar_t>,
+ std::allocator<wchar_t> >::
+ _M_compare(const __rc_string_base& __rcs) const
+ {
+ if (_M_rep() == __rcs._M_rep())
+ return true;
+ return false;
+ }
+#endif
+
+_GLIBCXX_END_NAMESPACE
#endif /* _RC_STRING_BASE_H */