31 #ifndef _RC_STRING_BASE_H
32 #define _RC_STRING_BASE_H 1
37 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
82 template<
typename _CharT,
typename _Traits,
typename _Alloc>
84 :
protected __vstring_utility<_CharT, _Traits, _Alloc>
87 typedef _Traits traits_type;
88 typedef typename _Traits::char_type value_type;
89 typedef _Alloc allocator_type;
91 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
92 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
93 typedef typename _CharT_alloc_type::size_type size_type;
116 size_type _M_capacity;
117 _Atomic_word _M_refcount;
124 typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
128 {
return reinterpret_cast<_CharT*
>(
this + 1); }
133 __atomic_add_dispatch(&_M_info._M_refcount, 1);
138 _M_set_length(size_type __n)
140 _M_info._M_refcount = 0;
141 _M_info._M_length = __n;
144 traits_type::assign(_M_refdata()[__n], _CharT());
149 _S_create(size_type, size_type,
const _Alloc&);
152 _M_destroy(
const _Alloc&)
throw();
155 _M_clone(
const _Alloc&, size_type __res = 0);
164 static _Rep_empty _S_empty_rep;
179 enum { _S_max_size = (((
static_cast<size_type
>(-1) - 2 *
sizeof(_Rep)
180 + 1) /
sizeof(_CharT)) - 1) / 2 };
183 mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
187 { _M_dataplus._M_p = __p; }
191 {
return &((
reinterpret_cast<_Rep*
>(_M_data()))[-1]); }
194 _M_grab(
const _Alloc& __alloc)
const
196 return (!_M_is_leaked() && _M_get_allocator() == __alloc)
197 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
204 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
206 if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
209 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
211 _M_rep()->_M_destroy(_M_get_allocator());
217 {
return _M_rep()->_M_info._M_refcount < 0; }
221 { _M_rep()->_M_info._M_refcount = 0; }
228 template<
typename _InIterator>
230 _S_construct_aux(_InIterator __beg, _InIterator __end,
231 const _Alloc& __a, std::__false_type)
233 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
234 return _S_construct(__beg, __end, __a, _Tag());
239 template<
typename _Integer>
241 _S_construct_aux(_Integer __beg, _Integer __end,
242 const _Alloc& __a, std::__true_type)
243 {
return _S_construct_aux_2(static_cast<size_type>(__beg),
247 _S_construct_aux_2(size_type __req, _CharT __c,
const _Alloc& __a)
248 {
return _S_construct(__req, __c, __a); }
250 template<
typename _InIterator>
252 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a)
254 typedef typename std::__is_integer<_InIterator>::__type _Integral;
255 return _S_construct_aux(__beg, __end, __a, _Integral());
259 template<
typename _InIterator>
261 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
266 template<
typename _FwdIterator>
268 _S_construct(_FwdIterator __beg, _FwdIterator __end,
const _Alloc& __a,
272 _S_construct(size_type __req, _CharT __c,
const _Alloc& __a);
277 {
return size_type(_S_max_size); }
281 {
return _M_dataplus._M_p; }
285 {
return _M_rep()->_M_info._M_length; }
289 {
return _M_rep()->_M_info._M_capacity; }
293 {
return _M_rep()->_M_info._M_refcount > 0; }
297 { _M_rep()->_M_info._M_refcount = -1; }
307 _M_set_length(size_type __n)
308 { _M_rep()->_M_set_length(__n); }
311 : _M_dataplus(_S_empty_rep._M_refcopy()) { }
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319 : _M_dataplus(__rcs._M_dataplus)
320 { __rcs._M_data(_S_empty_rep._M_refcopy()); }
325 template<
typename _InputIterator>
334 {
return _M_dataplus; }
336 const allocator_type&
337 _M_get_allocator()
const
338 {
return _M_dataplus; }
347 _M_reserve(size_type __res);
350 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
354 _M_erase(size_type __pos, size_type __n);
358 { _M_erase(size_type(0), _M_length()); }
365 template<
typename _CharT,
typename _Traits,
typename _Alloc>
369 template<
typename _CharT,
typename _Traits,
typename _Alloc>
372 _S_create(size_type __capacity, size_type __old_capacity,
373 const _Alloc& __alloc)
377 if (__capacity > size_type(_S_max_size))
378 std::__throw_length_error(__N(
"__rc_string_base::_Rep::_S_create"));
403 const size_type __pagesize = 4096;
404 const size_type __malloc_header_size = 4 *
sizeof(
void*);
409 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
411 __capacity = 2 * __old_capacity;
413 if (__capacity > size_type(_S_max_size))
414 __capacity = size_type(_S_max_size);
422 size_type __size = ((__capacity + 1) *
sizeof(_CharT)
423 + 2 *
sizeof(_Rep) - 1);
425 const size_type __adj_size = __size + __malloc_header_size;
426 if (__adj_size > __pagesize && __capacity > __old_capacity)
428 const size_type __extra = __pagesize - __adj_size % __pagesize;
429 __capacity += __extra /
sizeof(_CharT);
430 if (__capacity > size_type(_S_max_size))
431 __capacity = size_type(_S_max_size);
432 __size = (__capacity + 1) *
sizeof(_CharT) + 2 *
sizeof(_Rep) - 1;
437 _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size /
sizeof(_Rep));
438 _Rep* __p =
new (__place) _Rep;
439 __p->_M_info._M_capacity = __capacity;
443 template<
typename _CharT,
typename _Traits,
typename _Alloc>
445 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
446 _M_destroy(
const _Alloc& __a)
throw ()
448 const size_type __size = ((_M_info._M_capacity + 1) *
sizeof(_CharT)
449 + 2 *
sizeof(_Rep) - 1);
450 _Rep_alloc_type(__a).deallocate(
this, __size /
sizeof(_Rep));
453 template<
typename _CharT,
typename _Traits,
typename _Alloc>
455 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
456 _M_clone(
const _Alloc& __alloc, size_type __res)
459 const size_type __requested_cap = _M_info._M_length + __res;
460 _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
463 if (_M_info._M_length)
464 __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
466 __r->_M_set_length(_M_info._M_length);
467 return __r->_M_refdata();
470 template<
typename _CharT,
typename _Traits,
typename _Alloc>
471 __rc_string_base<_CharT, _Traits, _Alloc>::
472 __rc_string_base(
const _Alloc& __a)
473 : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
475 template<
typename _CharT,
typename _Traits,
typename _Alloc>
476 __rc_string_base<_CharT, _Traits, _Alloc>::
477 __rc_string_base(
const __rc_string_base& __rcs)
478 : _M_dataplus(__rcs._M_get_allocator(),
479 __rcs._M_grab(__rcs._M_get_allocator())) { }
481 template<
typename _CharT,
typename _Traits,
typename _Alloc>
482 __rc_string_base<_CharT, _Traits, _Alloc>::
483 __rc_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
484 : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
486 template<
typename _CharT,
typename _Traits,
typename _Alloc>
487 template<
typename _InputIterator>
488 __rc_string_base<_CharT, _Traits, _Alloc>::
489 __rc_string_base(_InputIterator __beg, _InputIterator __end,
491 : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
493 template<
typename _CharT,
typename _Traits,
typename _Alloc>
495 __rc_string_base<_CharT, _Traits, _Alloc>::
507 template<
typename _CharT,
typename _Traits,
typename _Alloc>
508 template<
typename _InIterator>
510 __rc_string_base<_CharT, _Traits, _Alloc>::
511 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
514 if (__beg == __end && __a == _Alloc())
515 return _S_empty_rep._M_refcopy();
520 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
522 __buf[__len++] = *__beg;
525 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
526 _S_copy(__r->_M_refdata(), __buf, __len);
529 while (__beg != __end)
531 if (__len == __r->_M_info._M_capacity)
534 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
535 _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
536 __r->_M_destroy(__a);
539 __r->_M_refdata()[__len++] = *__beg;
545 __r->_M_destroy(__a);
546 __throw_exception_again;
548 __r->_M_set_length(__len);
549 return __r->_M_refdata();
552 template<
typename _CharT,
typename _Traits,
typename _Alloc>
553 template<
typename _InIterator>
555 __rc_string_base<_CharT, _Traits, _Alloc>::
556 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
559 if (__beg == __end && __a == _Alloc())
560 return _S_empty_rep._M_refcopy();
563 if (__is_null_pointer(__beg) && __beg != __end)
564 std::__throw_logic_error(__N(
"__rc_string_base::"
565 "_S_construct null not valid"));
567 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
570 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
572 { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
575 __r->_M_destroy(__a);
576 __throw_exception_again;
578 __r->_M_set_length(__dnew);
579 return __r->_M_refdata();
582 template<
typename _CharT,
typename _Traits,
typename _Alloc>
584 __rc_string_base<_CharT, _Traits, _Alloc>::
585 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
587 if (__n == 0 && __a == _Alloc())
588 return _S_empty_rep._M_refcopy();
591 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
593 __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
595 __r->_M_set_length(__n);
596 return __r->_M_refdata();
599 template<
typename _CharT,
typename _Traits,
typename _Alloc>
601 __rc_string_base<_CharT, _Traits, _Alloc>::
602 _M_swap(__rc_string_base& __rcs)
606 if (__rcs._M_is_leaked())
607 __rcs._M_set_sharable();
609 _CharT* __tmp = _M_data();
610 _M_data(__rcs._M_data());
611 __rcs._M_data(__tmp);
615 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
616 __rcs._M_get_allocator());
619 template<
typename _CharT,
typename _Traits,
typename _Alloc>
621 __rc_string_base<_CharT, _Traits, _Alloc>::
622 _M_assign(
const __rc_string_base& __rcs)
624 if (_M_rep() != __rcs._M_rep())
626 _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
632 template<
typename _CharT,
typename _Traits,
typename _Alloc>
634 __rc_string_base<_CharT, _Traits, _Alloc>::
635 _M_reserve(size_type __res)
638 if (__res < _M_length())
641 if (__res != _M_capacity() || _M_is_shared())
643 _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
644 __res - _M_length());
650 template<
typename _CharT,
typename _Traits,
typename _Alloc>
652 __rc_string_base<_CharT, _Traits, _Alloc>::
653 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
656 const size_type __how_much = _M_length() - __pos - __len1;
658 _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
659 _M_capacity(), _M_get_allocator());
662 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
664 this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
666 this->_S_copy(__r->_M_refdata() + __pos + __len2,
667 _M_data() + __pos + __len1, __how_much);
670 _M_data(__r->_M_refdata());
673 template<
typename _CharT,
typename _Traits,
typename _Alloc>
675 __rc_string_base<_CharT, _Traits, _Alloc>::
676 _M_erase(size_type __pos, size_type __n)
678 const size_type __new_size = _M_length() - __n;
679 const size_type __how_much = _M_length() - __pos - __n;
684 _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
688 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
690 this->_S_copy(__r->_M_refdata() + __pos,
691 _M_data() + __pos + __n, __how_much);
694 _M_data(__r->_M_refdata());
696 else if (__how_much && __n)
699 this->_S_move(_M_data() + __pos,
700 _M_data() + __pos + __n, __how_much);
703 _M_rep()->_M_set_length(__new_size);
708 __rc_string_base<char, std::char_traits<char>,
710 _M_compare(
const __rc_string_base& __rcs)
const
712 if (_M_rep() == __rcs._M_rep())
717 #ifdef _GLIBCXX_USE_WCHAR_T
720 __rc_string_base<wchar_t, std::char_traits<wchar_t>,
722 _M_compare(
const __rc_string_base& __rcs)
const
724 if (_M_rep() == __rcs._M_rep())
730 _GLIBCXX_END_NAMESPACE_VERSION
The standard allocator, as per [20.4].
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Forward iterators support a superset of input iterator operations.