#ifndef _RC_STRING_BASE_H
#define _RC_STRING_BASE_H 1
#include <ext/atomicity.h>
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
template<typename _CharT, typename _Traits, typename _Alloc>
class __rc_string_base
: protected __vstring_utility<_CharT, _Traits, _Alloc>
{
public:
typedef _Traits traits_type;
typedef typename _Traits::char_type value_type;
typedef _Alloc allocator_type;
typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
typedef typename _CharT_alloc_type::size_type size_type;
private:
struct _Rep
{
union
{
struct
{
size_type _M_length;
size_type _M_capacity;
_Atomic_word _M_refcount;
} _M_info;
_CharT _M_align;
};
typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
_CharT*
_M_refdata() throw()
{ return reinterpret_cast<_CharT*>(this + 1); }
_CharT*
_M_refcopy() throw()
{
__atomic_add_dispatch(&_M_info._M_refcount, 1);
return _M_refdata();
}
void
_M_set_length(size_type __n)
{
_M_info._M_refcount = 0; _M_info._M_length = __n;
traits_type::assign(_M_refdata()[__n], _CharT());
}
static _Rep*
_S_create(size_type, size_type, const _Alloc&);
void
_M_destroy(const _Alloc&) throw();
_CharT*
_M_clone(const _Alloc&, size_type __res = 0);
};
struct _Rep_empty
: public _Rep
{
_CharT _M_terminal;
};
static _Rep_empty _S_empty_rep;
enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
+ 1) / sizeof(_CharT)) - 1) / 2 };
mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
void
_M_data(_CharT* __p)
{ _M_dataplus._M_p = __p; }
_Rep*
_M_rep() const
{ return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
_CharT*
_M_grab(const _Alloc& __alloc) const
{
return (!_M_is_leaked() && _M_get_allocator() == __alloc)
? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
}
void
_M_dispose()
{
if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
-1) <= 0)
_M_rep()->_M_destroy(_M_get_allocator());
}
bool
_M_is_leaked() const
{ return _M_rep()->_M_info._M_refcount < 0; }
void
_M_set_sharable()
{ _M_rep()->_M_info._M_refcount = 0; }
void
_M_leak_hard();
template<typename _InIterator>
static _CharT*
_S_construct_aux(_InIterator __beg, _InIterator __end,
const _Alloc& __a, std::__false_type)
{
typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
return _S_construct(__beg, __end, __a, _Tag());
}
template<typename _InIterator>
static _CharT*
_S_construct_aux(_InIterator __beg, _InIterator __end,
const _Alloc& __a, std::__true_type)
{ return _S_construct(static_cast<size_type>(__beg),
static_cast<value_type>(__end), __a); }
template<typename _InIterator>
static _CharT*
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
{
typedef typename std::__is_integer<_InIterator>::__type _Integral;
return _S_construct_aux(__beg, __end, __a, _Integral());
}
template<typename _InIterator>
static _CharT*
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
std::input_iterator_tag);
template<typename _FwdIterator>
static _CharT*
_S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
std::forward_iterator_tag);
static _CharT*
_S_construct(size_type __req, _CharT __c, const _Alloc& __a);
public:
size_type
_M_max_size() const
{ return size_type(_S_max_size); }
_CharT*
_M_data() const
{ return _M_dataplus._M_p; }
size_type
_M_length() const
{ return _M_rep()->_M_info._M_length; }
size_type
_M_capacity() const
{ return _M_rep()->_M_info._M_capacity; }
bool
_M_is_shared() const
{ return _M_rep()->_M_info._M_refcount > 0; }
void
_M_set_leaked()
{ _M_rep()->_M_info._M_refcount = -1; }
void
_M_leak() {
if (!_M_is_leaked())
_M_leak_hard();
}
void
_M_set_length(size_type __n)
{ _M_rep()->_M_set_length(__n); }
__rc_string_base()
: _M_dataplus(_Alloc(), _S_empty_rep._M_refcopy()) { }
__rc_string_base(const _Alloc& __a);
__rc_string_base(const __rc_string_base& __rcs);
__rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
template<typename _InputIterator>
__rc_string_base(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a);
~__rc_string_base()
{ _M_dispose(); }
allocator_type&
_M_get_allocator()
{ return _M_dataplus; }
const allocator_type&
_M_get_allocator() const
{ return _M_dataplus; }
void
_M_swap(__rc_string_base& __rcs);
void
_M_assign(const __rc_string_base& __rcs);
void
_M_reserve(size_type __res);
void
_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>
typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
__rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
template<typename _CharT, typename _Traits, typename _Alloc>
typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
__rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
_S_create(size_type __capacity, size_type __old_capacity,
const _Alloc& __alloc)
{
if (__capacity > size_type(_S_max_size))
std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
const size_type __pagesize = 4096;
const size_type __malloc_header_size = 4 * sizeof(void*);
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
{
__capacity = 2 * __old_capacity;
if (__capacity > size_type(_S_max_size))
__capacity = size_type(_S_max_size);
}
size_type __size = ((__capacity + 1) * sizeof(_CharT)
+ 2 * sizeof(_Rep) - 1);
const size_type __adj_size = __size + __malloc_header_size;
if (__adj_size > __pagesize && __capacity > __old_capacity)
{
const size_type __extra = __pagesize - __adj_size % __pagesize;
__capacity += __extra / sizeof(_CharT);
if (__capacity > size_type(_S_max_size))
__capacity = size_type(_S_max_size);
__size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
}
_Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
_Rep* __p = new (__place) _Rep;
__p->_M_info._M_capacity = __capacity;
return __p;
}
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
_M_destroy(const _Alloc& __a) throw ()
{
const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
+ 2 * sizeof(_Rep) - 1);
_Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
}
template<typename _CharT, typename _Traits, typename _Alloc>
_CharT*
__rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
_M_clone(const _Alloc& __alloc, size_type __res)
{
const size_type __requested_cap = _M_info._M_length + __res;
_Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
__alloc);
if (_M_info._M_length)
_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
__r->_M_set_length(_M_info._M_length);
return __r->_M_refdata();
}
template<typename _CharT, typename _Traits, typename _Alloc>
__rc_string_base<_CharT, _Traits, _Alloc>::
__rc_string_base(const _Alloc& __a)
: _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
template<typename _CharT, typename _Traits, typename _Alloc>
__rc_string_base<_CharT, _Traits, _Alloc>::
__rc_string_base(const __rc_string_base& __rcs)
: _M_dataplus(__rcs._M_get_allocator(),
__rcs._M_grab(__rcs._M_get_allocator())) { }
template<typename _CharT, typename _Traits, typename _Alloc>
__rc_string_base<_CharT, _Traits, _Alloc>::
__rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
: _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InputIterator>
__rc_string_base<_CharT, _Traits, _Alloc>::
__rc_string_base(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a)
: _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_leak_hard()
{
if (_M_is_shared())
_M_erase(0, 0);
_M_set_leaked();
}
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InIterator>
_CharT*
__rc_string_base<_CharT, _Traits, _Alloc>::
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
std::input_iterator_tag)
{
if (__beg == __end && __a == _Alloc())
return _S_empty_rep._M_refcopy();
_CharT __buf[128];
size_type __len = 0;
while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
{
__buf[__len++] = *__beg;
++__beg;
}
_Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
_S_copy(__r->_M_refdata(), __buf, __len);
try
{
while (__beg != __end)
{
if (__len == __r->_M_info._M_capacity)
{
_Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
_S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
__r->_M_destroy(__a);
__r = __another;
}
__r->_M_refdata()[__len++] = *__beg;
++__beg;
}
}
catch(...)
{
__r->_M_destroy(__a);
__throw_exception_again;
}
__r->_M_set_length(__len);
return __r->_M_refdata();
}
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InIterator>
_CharT*
__rc_string_base<_CharT, _Traits, _Alloc>::
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
std::forward_iterator_tag)
{
if (__beg == __end && __a == _Alloc())
return _S_empty_rep._M_refcopy();
if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
std::__throw_logic_error(__N("__rc_string_base::"
"_S_construct NULL not valid"));
const size_type __dnew = static_cast<size_type>(std::distance(__beg,
__end));
_Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
try
{ _S_copy_chars(__r->_M_refdata(), __beg, __end); }
catch(...)
{
__r->_M_destroy(__a);
__throw_exception_again;
}
__r->_M_set_length(__dnew);
return __r->_M_refdata();
}
template<typename _CharT, typename _Traits, typename _Alloc>
_CharT*
__rc_string_base<_CharT, _Traits, _Alloc>::
_S_construct(size_type __n, _CharT __c, const _Alloc& __a)
{
if (__n == 0 && __a == _Alloc())
return _S_empty_rep._M_refcopy();
_Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
if (__n)
_S_assign(__r->_M_refdata(), __n, __c);
__r->_M_set_length(__n);
return __r->_M_refdata();
}
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_swap(__rc_string_base& __rcs)
{
if (_M_is_leaked())
_M_set_sharable();
if (__rcs._M_is_leaked())
__rcs._M_set_sharable();
_CharT* __tmp = _M_data();
_M_data(__rcs._M_data());
__rcs._M_data(__tmp);
std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
__rcs._M_get_allocator());
}
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_assign(const __rc_string_base& __rcs)
{
if (_M_rep() != __rcs._M_rep())
{
_CharT* __tmp = __rcs._M_grab(_M_get_allocator());
_M_dispose();
_M_data(__tmp);
}
}
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_reserve(size_type __res)
{
if (__res < _M_length())
__res = _M_length();
if (__res != _M_capacity() || _M_is_shared())
{
_CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
__res - _M_length());
_M_dispose();
_M_data(__tmp);
}
}
template<typename _CharT, typename _Traits, typename _Alloc>
void
__rc_string_base<_CharT, _Traits, _Alloc>::
_M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
size_type __len2)
{
const size_type __how_much = _M_length() - __pos - __len1;
_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())
{
_Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
_M_get_allocator());
if (__pos)
_S_copy(__r->_M_refdata(), _M_data(), __pos);
if (__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 && __n)
{
_S_move(_M_data() + __pos,
_M_data() + __pos + __n, __how_much);
}
_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;
}
#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