libstdc++
safe_sequence.tcc
Go to the documentation of this file.
00001 // Safe sequence implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file debug/safe_sequence.tcc
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC
00030 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC 1
00031 
00032 namespace __gnu_debug
00033 {
00034   template<typename _Sequence>
00035     template<typename _Predicate>
00036       void
00037       _Safe_sequence<_Sequence>::
00038       _M_invalidate_if(_Predicate __pred)
00039       {
00040         typedef typename _Sequence::iterator iterator;
00041         typedef typename _Sequence::const_iterator const_iterator;
00042 
00043         __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
00044         for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
00045           {
00046             iterator* __victim = static_cast<iterator*>(__iter);
00047             __iter = __iter->_M_next;
00048             if (!__victim->_M_singular() && __pred(__victim->base()))
00049               {
00050                 __victim->_M_invalidate();
00051               }
00052           }
00053 
00054         for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
00055           {
00056             const_iterator* __victim = static_cast<const_iterator*>(__iter2);
00057             __iter2 = __iter2->_M_next;
00058             if (!__victim->_M_singular() && __pred(__victim->base()))
00059               {
00060                 __victim->_M_invalidate();
00061               }
00062           }
00063       }
00064 
00065   template<typename _Sequence>
00066     template<typename _Predicate>
00067       void
00068       _Safe_sequence<_Sequence>::
00069       _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred)
00070       {
00071         if (this == std::__addressof(__from))
00072           return;
00073 
00074         typedef typename _Sequence::iterator iterator;
00075         typedef typename _Sequence::const_iterator const_iterator;
00076 
00077         _Safe_iterator_base* __transfered_iterators = 0;
00078         _Safe_iterator_base* __transfered_const_iterators = 0;
00079         _Safe_iterator_base* __last_iterator = 0;
00080         _Safe_iterator_base* __last_const_iterator = 0;
00081         {
00082           // We lock __from first and detach iterator(s) to transfer
00083           __gnu_cxx::__scoped_lock sentry(__from._M_get_mutex());
00084 
00085           for (_Safe_iterator_base* __iter = __from._M_iterators; __iter;)
00086             {
00087               _Safe_iterator_base* __victim_base = __iter;
00088               iterator* __victim = static_cast<iterator*>(__victim_base);
00089               __iter = __iter->_M_next;
00090               if (!__victim->_M_singular() && __pred(__victim->base()))
00091                 {
00092                   __victim->_M_detach_single();
00093                   if (__transfered_iterators)
00094                     {
00095                       __victim_base->_M_next = __transfered_iterators;
00096                       __transfered_iterators->_M_prior = __victim_base;
00097                     }
00098                   else
00099                     __last_iterator = __victim_base;
00100                   __victim_base->_M_sequence = this;
00101                   __victim_base->_M_version = this->_M_version;
00102                   __transfered_iterators = __victim_base;
00103                 }
00104             }
00105 
00106           for (_Safe_iterator_base* __iter2 = __from._M_const_iterators;
00107                  __iter2;)
00108             {
00109               _Safe_iterator_base* __victim_base = __iter2;
00110               const_iterator* __victim =
00111                 static_cast<const_iterator*>(__victim_base);
00112               __iter2 = __iter2->_M_next;
00113               if (!__victim->_M_singular() && __pred(__victim->base()))
00114                 {
00115                   __victim->_M_detach_single();
00116                   if (__transfered_const_iterators)
00117                     {
00118                       __victim_base->_M_next = __transfered_const_iterators;
00119                       __transfered_const_iterators->_M_prior = __victim_base;
00120                     }
00121                   else
00122                     __last_const_iterator = __victim;
00123                   __victim_base->_M_sequence = this;
00124                   __victim_base->_M_version = this->_M_version;
00125                   __transfered_const_iterators = __victim_base;
00126                 }
00127             }
00128         }
00129 
00130         // Now we can lock *this and add the transfered iterators if any
00131         if (__last_iterator || __last_const_iterator)
00132           {
00133             __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
00134             if (__last_iterator)
00135               {
00136                 if (this->_M_iterators)
00137                   {
00138                     this->_M_iterators->_M_prior = __last_iterator;
00139                     __last_iterator->_M_next = this->_M_iterators;
00140                   }
00141                 this->_M_iterators = __transfered_iterators;
00142               }
00143             if (__last_const_iterator)
00144               {
00145                 if (this->_M_const_iterators)
00146                   {
00147                     this->_M_const_iterators->_M_prior = __last_const_iterator;
00148                     __last_const_iterator->_M_next = this->_M_const_iterators;
00149                   }
00150                 this->_M_const_iterators = __transfered_const_iterators;
00151               }
00152           }
00153       }
00154 } // namespace __gnu_debug
00155 
00156 #endif