libstdc++
safe_base.h
Go to the documentation of this file.
00001 // Safe sequence/iterator base implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file debug/safe_base.h
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
00031 #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
00032 
00033 #include <ext/concurrence.h>
00034 
00035 namespace __gnu_debug
00036 {
00037   class _Safe_sequence_base;
00038 
00039   /** \brief Basic functionality for a @a safe iterator.
00040    *
00041    *  The %_Safe_iterator_base base class implements the functionality
00042    *  of a safe iterator that is not specific to a particular iterator
00043    *  type. It contains a pointer back to the sequence it references
00044    *  along with iterator version information and pointers to form a
00045    *  doubly-linked list of iterators referenced by the container.
00046    *
00047    *  This class must not perform any operations that can throw an
00048    *  exception, or the exception guarantees of derived iterators will
00049    *  be broken.
00050    */
00051   class _Safe_iterator_base
00052   {
00053   public:
00054     /** The sequence this iterator references; may be NULL to indicate
00055     a singular iterator. */
00056     _Safe_sequence_base* _M_sequence;
00057 
00058     /** The version number of this iterator. The sentinel value 0 is
00059      *  used to indicate an invalidated iterator (i.e., one that is
00060      *  singular because of an operation on the container). This
00061      *  version number must equal the version number in the sequence
00062      *  referenced by _M_sequence for the iterator to be
00063      *  non-singular.
00064      */
00065     unsigned int         _M_version;
00066 
00067     /** Pointer to the previous iterator in the sequence's list of
00068     iterators. Only valid when _M_sequence != NULL. */
00069     _Safe_iterator_base* _M_prior;
00070 
00071     /** Pointer to the next iterator in the sequence's list of
00072     iterators. Only valid when _M_sequence != NULL. */
00073     _Safe_iterator_base* _M_next;
00074 
00075   protected:
00076     /** Initializes the iterator and makes it singular. */
00077     _Safe_iterator_base()
00078     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00079     { }
00080 
00081     /** Initialize the iterator to reference the sequence pointed to
00082      *  by @p __seq. @p __constant is true when we are initializing a
00083      *  constant iterator, and false if it is a mutable iterator. Note
00084      *  that @p __seq may be NULL, in which case the iterator will be
00085      *  singular. Otherwise, the iterator will reference @p __seq and
00086      *  be nonsingular.
00087      */
00088     _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
00089     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00090     { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
00091 
00092     /** Initializes the iterator to reference the same sequence that
00093     @p __x does. @p __constant is true if this is a constant
00094     iterator, and false if it is mutable. */
00095     _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
00096     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
00097     { this->_M_attach(__x._M_sequence, __constant); }
00098 
00099     _Safe_iterator_base&
00100     operator=(const _Safe_iterator_base&);
00101 
00102     explicit
00103     _Safe_iterator_base(const _Safe_iterator_base&);
00104 
00105     ~_Safe_iterator_base() { this->_M_detach(); }
00106 
00107     /** For use in _Safe_iterator. */
00108     __gnu_cxx::__mutex& _M_get_mutex() throw ();
00109 
00110   public:
00111     /** Attaches this iterator to the given sequence, detaching it
00112      *  from whatever sequence it was attached to originally. If the
00113      *  new sequence is the NULL pointer, the iterator is left
00114      *  unattached.
00115      */
00116     void _M_attach(_Safe_sequence_base* __seq, bool __constant);
00117 
00118     /** Likewise, but not thread-safe. */
00119     void _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
00120 
00121     /** Detach the iterator for whatever sequence it is attached to,
00122      *  if any.
00123     */
00124     void _M_detach();
00125 
00126     /** Likewise, but not thread-safe. */
00127     void _M_detach_single() throw ();
00128 
00129     /** Determines if we are attached to the given sequence. */
00130     bool _M_attached_to(const _Safe_sequence_base* __seq) const
00131     { return _M_sequence == __seq; }
00132 
00133     /** Is this iterator singular? */
00134     _GLIBCXX_PURE bool _M_singular() const throw ();
00135 
00136     /** Can we compare this iterator to the given iterator @p __x?
00137     Returns true if both iterators are nonsingular and reference
00138     the same sequence. */
00139     _GLIBCXX_PURE bool _M_can_compare(const _Safe_iterator_base& __x) const throw ();
00140 
00141     /** Invalidate the iterator, making it singular. */
00142     void
00143     _M_invalidate()
00144     { _M_version = 0; }
00145 
00146     /** Reset all member variables */
00147     void
00148     _M_reset() throw ();
00149 
00150     /** Unlink itself */
00151     void
00152     _M_unlink() throw ()
00153     {
00154       if (_M_prior)
00155     _M_prior->_M_next = _M_next;
00156       if (_M_next)
00157     _M_next->_M_prior = _M_prior;
00158     }
00159   };
00160 
00161   /**
00162    * @brief Base class that supports tracking of iterators that
00163    * reference a sequence.
00164    *
00165    * The %_Safe_sequence_base class provides basic support for
00166    * tracking iterators into a sequence. Sequences that track
00167    * iterators must derived from %_Safe_sequence_base publicly, so
00168    * that safe iterators (which inherit _Safe_iterator_base) can
00169    * attach to them. This class contains two linked lists of
00170    * iterators, one for constant iterators and one for mutable
00171    * iterators, and a version number that allows very fast
00172    * invalidation of all iterators that reference the container.
00173    *
00174    * This class must ensure that no operation on it may throw an
00175    * exception, otherwise @a safe sequences may fail to provide the
00176    * exception-safety guarantees required by the C++ standard.
00177    */
00178   class _Safe_sequence_base
00179   {
00180   public:
00181     /// The list of mutable iterators that reference this container
00182     _Safe_iterator_base* _M_iterators;
00183 
00184     /// The list of constant iterators that reference this container
00185     _Safe_iterator_base* _M_const_iterators;
00186 
00187     /// The container version number. This number may never be 0.
00188     mutable unsigned int _M_version;
00189 
00190   protected:
00191     // Initialize with a version number of 1 and no iterators
00192     _Safe_sequence_base()
00193     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
00194     { }
00195 
00196     /** Notify all iterators that reference this sequence that the
00197     sequence is being destroyed. */
00198     ~_Safe_sequence_base()
00199     { this->_M_detach_all(); }
00200 
00201     /** Detach all iterators, leaving them singular. */
00202     void
00203     _M_detach_all();
00204 
00205     /** Detach all singular iterators.
00206      *  @post for all iterators i attached to this sequence,
00207      *   i->_M_version == _M_version.
00208      */
00209     void
00210     _M_detach_singular();
00211 
00212     /** Revalidates all attached singular iterators.  This method may
00213      *  be used to validate iterators that were invalidated before
00214      *  (but for some reason, such as an exception, need to become
00215      *  valid again).
00216      */
00217     void
00218     _M_revalidate_singular();
00219 
00220     /** Swap this sequence with the given sequence. This operation
00221      *  also swaps ownership of the iterators, so that when the
00222      *  operation is complete all iterators that originally referenced
00223      *  one container now reference the other container.
00224      */
00225     void
00226     _M_swap(_Safe_sequence_base& __x);
00227 
00228     /** For use in _Safe_sequence. */
00229     __gnu_cxx::__mutex& _M_get_mutex() throw ();
00230 
00231   public:
00232     /** Invalidates all iterators. */
00233     void
00234     _M_invalidate_all() const
00235     { if (++_M_version == 0) _M_version = 1; }
00236 
00237     /** Attach an iterator to this sequence. */
00238     void
00239     _M_attach(_Safe_iterator_base* __it, bool __constant);
00240 
00241     /** Likewise but not thread safe. */
00242     void
00243     _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
00244 
00245     /** Detach an iterator from this sequence */
00246     void
00247     _M_detach(_Safe_iterator_base* __it);
00248 
00249     /** Likewise but not thread safe. */
00250     void
00251     _M_detach_single(_Safe_iterator_base* __it) throw ();
00252   };
00253 } // namespace __gnu_debug
00254 
00255 #endif