libstdc++
safe_sequence.tcc
Go to the documentation of this file.
00001 // Safe sequence implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2015 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         typedef typename _Sequence::iterator iterator;
00072         typedef typename _Sequence::const_iterator const_iterator;
00073 
00074         _Safe_iterator_base* __transfered_iterators = 0;
00075         _Safe_iterator_base* __transfered_const_iterators = 0;
00076         _Safe_iterator_base* __last_iterator = 0;
00077         _Safe_iterator_base* __last_const_iterator = 0;
00078         {
00079           // We lock __from first and detach iterator(s) to transfer
00080           __gnu_cxx::__scoped_lock sentry(__from._M_get_mutex());
00081 
00082           for (_Safe_iterator_base* __iter = __from._M_iterators; __iter;)
00083             {
00084               _Safe_iterator_base* __victim_base = __iter;
00085               iterator* __victim = static_cast<iterator*>(__victim_base);
00086               __iter = __iter->_M_next;
00087               if (!__victim->_M_singular() && __pred(__victim->base()))
00088                 {
00089                   __victim->_M_detach_single();
00090                   if (__transfered_iterators)
00091                     {
00092                       __victim_base->_M_next = __transfered_iterators;
00093                       __transfered_iterators->_M_prior = __victim_base;
00094                     }
00095                   else
00096                     __last_iterator = __victim_base;
00097                   __victim_base->_M_sequence = this;
00098                   __victim_base->_M_version = this->_M_version;
00099                   __transfered_iterators = __victim_base;
00100                 }
00101             }
00102 
00103           for (_Safe_iterator_base* __iter2 = __from._M_const_iterators;
00104                  __iter2;)
00105             {
00106               _Safe_iterator_base* __victim_base = __iter2;
00107               const_iterator* __victim =
00108                 static_cast<const_iterator*>(__victim_base);
00109               __iter2 = __iter2->_M_next;
00110               if (!__victim->_M_singular() && __pred(__victim->base()))
00111                 {
00112                   __victim->_M_detach_single();
00113                   if (__transfered_const_iterators)
00114                     {
00115                       __victim_base->_M_next = __transfered_const_iterators;
00116                       __transfered_const_iterators->_M_prior = __victim_base;
00117                     }
00118                   else
00119                     __last_const_iterator = __victim;
00120                   __victim_base->_M_sequence = this;
00121                   __victim_base->_M_version = this->_M_version;
00122                   __transfered_const_iterators = __victim_base;
00123                 }
00124             }
00125         }
00126 
00127         // Now we can lock *this and add the transfered iterators if any
00128         if (__last_iterator || __last_const_iterator)
00129           {
00130             __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
00131             if (__last_iterator)
00132               {
00133                 if (this->_M_iterators)
00134                   {
00135                     this->_M_iterators->_M_prior = __last_iterator;
00136                     __last_iterator->_M_next = this->_M_iterators;
00137                   }
00138                 this->_M_iterators = __transfered_iterators;
00139               }
00140             if (__last_const_iterator)
00141               {
00142                 if (this->_M_const_iterators)
00143                   {
00144                     this->_M_const_iterators->_M_prior = __last_const_iterator;
00145                     __last_const_iterator->_M_next = this->_M_const_iterators;
00146                   }
00147                 this->_M_const_iterators = __transfered_const_iterators;
00148               }
00149           }
00150       }
00151 } // namespace __gnu_debug
00152 
00153 #endif