1 // Safe sequence implementation  -*- C++ -*-
 
    3 // Copyright (C) 2010-2014 Free Software Foundation, Inc.
 
    5 // This file is part of the GNU ISO C++ Library.  This library is free
 
    6 // software; you can redistribute it and/or modify it under the
 
    7 // terms of the GNU General Public License as published by the
 
    8 // Free Software Foundation; either version 3, or (at your option)
 
   11 // This library is distributed in the hope that it will be useful,
 
   12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   14 // GNU General Public License for more details.
 
   16 // Under Section 7 of GPL version 3, you are granted additional
 
   17 // permissions described in the GCC Runtime Library Exception, version
 
   18 // 3.1, as published by the Free Software Foundation.
 
   20 // You should have received a copy of the GNU General Public License and
 
   21 // a copy of the GCC Runtime Library Exception along with this program;
 
   22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
   23 // <http://www.gnu.org/licenses/>.
 
   25 /** @file debug/safe_sequence.tcc
 
   26  *  This file is a GNU debug extension to the Standard C++ Library.
 
   29 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC
 
   30 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC 1
 
   34   template<typename _Sequence>
 
   35     template<typename _Predicate>
 
   37       _Safe_sequence<_Sequence>::
 
   38       _M_invalidate_if(_Predicate __pred)
 
   40         typedef typename _Sequence::iterator iterator;
 
   41         typedef typename _Sequence::const_iterator const_iterator;
 
   43    __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
 
   44         for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
 
   46        iterator* __victim = static_cast<iterator*>(__iter);
 
   47        __iter = __iter->_M_next;
 
   48        if (!__victim->_M_singular() && __pred(__victim->base()))
 
   50        __victim->_M_invalidate();
 
   54         for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
 
   56        const_iterator* __victim = static_cast<const_iterator*>(__iter2);
 
   57        __iter2 = __iter2->_M_next;
 
   58        if (!__victim->_M_singular() && __pred(__victim->base()))
 
   60        __victim->_M_invalidate();
 
   65   template<typename _Sequence>
 
   66     template<typename _Predicate>
 
   68       _Safe_sequence<_Sequence>::
 
   69       _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred)
 
   71         typedef typename _Sequence::iterator iterator;
 
   72         typedef typename _Sequence::const_iterator const_iterator;
 
   74    _Safe_iterator_base* __transfered_iterators = 0;
 
   75    _Safe_iterator_base* __transfered_const_iterators = 0;
 
   76    _Safe_iterator_base* __last_iterator = 0;
 
   77    _Safe_iterator_base* __last_const_iterator = 0;
 
   79      // We lock __from first and detach iterator(s) to transfer
 
   80      __gnu_cxx::__scoped_lock sentry(__from._M_get_mutex());
 
   82           for (_Safe_iterator_base* __iter = __from._M_iterators; __iter;)
 
   84          iterator* __victim = static_cast<iterator*>(__iter);
 
   85          __iter = __iter->_M_next;
 
   86          if (!__victim->_M_singular() && __pred(__victim->base()))
 
   88          __victim->_M_detach_single();
 
   89          if (__transfered_iterators)
 
   91              __victim->_M_next = __transfered_iterators;
 
   92              __transfered_iterators->_M_prior = __victim;
 
   95            __last_iterator = __victim;
 
   96          __victim->_M_sequence = this;
 
   97          __victim->_M_version = this->_M_version;
 
   98          __transfered_iterators = __victim;
 
  102      for (_Safe_iterator_base* __iter2 = __from._M_const_iterators;
 
  105          const_iterator* __victim = static_cast<const_iterator*>(__iter2);
 
  106          __iter2 = __iter2->_M_next;
 
  107          if (!__victim->_M_singular() && __pred(__victim->base()))
 
  109          __victim->_M_detach_single();
 
  110          if (__transfered_const_iterators)
 
  112              __victim->_M_next = __transfered_const_iterators;
 
  113              __transfered_const_iterators->_M_prior = __victim;
 
  116            __last_const_iterator = __victim;
 
  117          __victim->_M_sequence = this;
 
  118          __victim->_M_version = this->_M_version;
 
  119          __transfered_const_iterators = __victim;
 
  124    // Now we can lock *this and add the transfered iterators if any
 
  125    if (__last_iterator || __last_const_iterator)
 
  127        __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
 
  130        if (this->_M_iterators)
 
  132            this->_M_iterators->_M_prior = __last_iterator;
 
  133            __last_iterator->_M_next = this->_M_iterators;
 
  135        this->_M_iterators = __transfered_iterators;
 
  137        if (__last_const_iterator)
 
  139        if (this->_M_const_iterators)
 
  141            this->_M_const_iterators->_M_prior = __last_const_iterator;
 
  142            __last_const_iterator->_M_next = this->_M_const_iterators;
 
  144        this->_M_const_iterators = __transfered_const_iterators;
 
  148 } // namespace __gnu_debug