You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

safe_sequence.tcc 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Safe sequence implementation -*- C++ -*-
  2. // Copyright (C) 2010-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/safe_sequence.tcc
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC
  24. #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC 1
  25. namespace __gnu_debug
  26. {
  27. template<typename _Sequence>
  28. template<typename _Predicate>
  29. void
  30. _Safe_sequence<_Sequence>::
  31. _M_invalidate_if(_Predicate __pred)
  32. {
  33. typedef typename _Sequence::iterator iterator;
  34. typedef typename _Sequence::const_iterator const_iterator;
  35. __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
  36. for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
  37. {
  38. iterator* __victim = static_cast<iterator*>(__iter);
  39. __iter = __iter->_M_next;
  40. if (!__victim->_M_singular() && __pred(__victim->base()))
  41. {
  42. __victim->_M_invalidate();
  43. }
  44. }
  45. for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
  46. {
  47. const_iterator* __victim = static_cast<const_iterator*>(__iter2);
  48. __iter2 = __iter2->_M_next;
  49. if (!__victim->_M_singular() && __pred(__victim->base()))
  50. {
  51. __victim->_M_invalidate();
  52. }
  53. }
  54. }
  55. template<typename _Sequence>
  56. template<typename _Predicate>
  57. void
  58. _Safe_sequence<_Sequence>::
  59. _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred)
  60. {
  61. if (this == std::__addressof(__from))
  62. return;
  63. typedef typename _Sequence::iterator iterator;
  64. typedef typename _Sequence::const_iterator const_iterator;
  65. _Safe_iterator_base* __transfered_iterators = 0;
  66. _Safe_iterator_base* __transfered_const_iterators = 0;
  67. _Safe_iterator_base* __last_iterator = 0;
  68. _Safe_iterator_base* __last_const_iterator = 0;
  69. {
  70. // We lock __from first and detach iterator(s) to transfer
  71. __gnu_cxx::__scoped_lock sentry(__from._M_get_mutex());
  72. for (_Safe_iterator_base* __iter = __from._M_iterators; __iter;)
  73. {
  74. _Safe_iterator_base* __victim_base = __iter;
  75. iterator* __victim = static_cast<iterator*>(__victim_base);
  76. __iter = __iter->_M_next;
  77. if (!__victim->_M_singular() && __pred(__victim->base()))
  78. {
  79. __victim->_M_detach_single();
  80. if (__transfered_iterators)
  81. {
  82. __victim_base->_M_next = __transfered_iterators;
  83. __transfered_iterators->_M_prior = __victim_base;
  84. }
  85. else
  86. __last_iterator = __victim_base;
  87. __victim_base->_M_sequence = this;
  88. __victim_base->_M_version = this->_M_version;
  89. __transfered_iterators = __victim_base;
  90. }
  91. }
  92. for (_Safe_iterator_base* __iter2 = __from._M_const_iterators;
  93. __iter2;)
  94. {
  95. _Safe_iterator_base* __victim_base = __iter2;
  96. const_iterator* __victim =
  97. static_cast<const_iterator*>(__victim_base);
  98. __iter2 = __iter2->_M_next;
  99. if (!__victim->_M_singular() && __pred(__victim->base()))
  100. {
  101. __victim->_M_detach_single();
  102. if (__transfered_const_iterators)
  103. {
  104. __victim_base->_M_next = __transfered_const_iterators;
  105. __transfered_const_iterators->_M_prior = __victim_base;
  106. }
  107. else
  108. __last_const_iterator = __victim;
  109. __victim_base->_M_sequence = this;
  110. __victim_base->_M_version = this->_M_version;
  111. __transfered_const_iterators = __victim_base;
  112. }
  113. }
  114. }
  115. // Now we can lock *this and add the transfered iterators if any
  116. if (__last_iterator || __last_const_iterator)
  117. {
  118. __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
  119. if (__last_iterator)
  120. {
  121. if (this->_M_iterators)
  122. {
  123. this->_M_iterators->_M_prior = __last_iterator;
  124. __last_iterator->_M_next = this->_M_iterators;
  125. }
  126. this->_M_iterators = __transfered_iterators;
  127. }
  128. if (__last_const_iterator)
  129. {
  130. if (this->_M_const_iterators)
  131. {
  132. this->_M_const_iterators->_M_prior = __last_const_iterator;
  133. __last_const_iterator->_M_next = this->_M_const_iterators;
  134. }
  135. this->_M_const_iterators = __transfered_const_iterators;
  136. }
  137. }
  138. }
  139. } // namespace __gnu_debug
  140. #endif