libstdc++
safe_unordered_base.h
Go to the documentation of this file.
00001 // Safe container/iterator base implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2011 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_unordered_base.h
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H
00030 #define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1
00031 
00032 #include <debug/safe_base.h>
00033 
00034 namespace __gnu_debug
00035 {
00036   class _Safe_unordered_container_base;
00037 
00038   /** \brief Basic functionality for a @a safe iterator.
00039    *
00040    *  The %_Safe_local_iterator_base base class implements the functionality
00041    *  of a safe local iterator that is not specific to a particular iterator
00042    *  type. It contains a pointer back to the container it references
00043    *  along with iterator version information and pointers to form a
00044    *  doubly-linked list of local iterators referenced by the container.
00045    *
00046    *  This class must not perform any operations that can throw an
00047    *  exception, or the exception guarantees of derived iterators will
00048    *  be broken.
00049    */
00050   class _Safe_local_iterator_base : public _Safe_iterator_base
00051   {
00052   protected:
00053     /** Initializes the iterator and makes it singular. */
00054     _Safe_local_iterator_base()
00055     { }
00056 
00057     /** Initialize the iterator to reference the container pointed to
00058      *  by @p __seq. @p __constant is true when we are initializing a
00059      *  constant local iterator, and false if it is a mutable local iterator.
00060      *  Note that @p __seq may be NULL, in which case the iterator will be
00061      *  singular. Otherwise, the iterator will reference @p __seq and
00062      *  be nonsingular.
00063      */
00064     _Safe_local_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
00065     { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
00066 
00067     /** Initializes the iterator to reference the same container that
00068     @p __x does. @p __constant is true if this is a constant
00069     iterator, and false if it is mutable. */
00070     _Safe_local_iterator_base(const _Safe_local_iterator_base& __x,
00071                   bool __constant)
00072     { this->_M_attach(__x._M_sequence, __constant); }
00073 
00074     _Safe_local_iterator_base&
00075     operator=(const _Safe_local_iterator_base&);
00076 
00077     explicit
00078     _Safe_local_iterator_base(const _Safe_local_iterator_base&);
00079 
00080     ~_Safe_local_iterator_base() { this->_M_detach(); }
00081 
00082     _Safe_unordered_container_base*
00083     _M_get_container() const _GLIBCXX_NOEXCEPT;
00084 
00085   public:
00086     /** Attaches this iterator to the given container, detaching it
00087      *  from whatever container it was attached to originally. If the
00088      *  new container is the NULL pointer, the iterator is left
00089      *  unattached.
00090      */
00091     void _M_attach(_Safe_sequence_base* __seq, bool __constant);
00092 
00093     /** Likewise, but not thread-safe. */
00094     void _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
00095 
00096     /** Detach the iterator for whatever container it is attached to,
00097      *  if any.
00098     */
00099     void _M_detach();
00100 
00101     /** Likewise, but not thread-safe. */
00102     void _M_detach_single() throw ();
00103   };
00104 
00105   /**
00106    * @brief Base class that supports tracking of local iterators that
00107    * reference an unordered container.
00108    *
00109    * The %_Safe_unordered_container_base class provides basic support for
00110    * tracking iterators into an unordered container. Containers that track
00111    * iterators must derived from %_Safe_unordered_container_base publicly, so
00112    * that safe iterators (which inherit _Safe_iterator_base) can
00113    * attach to them. This class contains four linked lists of
00114    * iterators, one for constant iterators, one for mutable
00115    * iterators, one for constant local iterators, one for mutable local
00116    * iterators and a version number that allows very fast
00117    * invalidation of all iterators that reference the container.
00118    *
00119    * This class must ensure that no operation on it may throw an
00120    * exception, otherwise @a safe containers may fail to provide the
00121    * exception-safety guarantees required by the C++ standard.
00122    */
00123   class _Safe_unordered_container_base : public _Safe_sequence_base
00124   {
00125     typedef _Safe_sequence_base _Base;
00126   public:
00127     /// The list of mutable local iterators that reference this container
00128     _Safe_iterator_base* _M_local_iterators;
00129 
00130     /// The list of constant local iterators that reference this container
00131     _Safe_iterator_base* _M_const_local_iterators;
00132 
00133   protected:
00134     // Initialize with a version number of 1 and no iterators
00135     _Safe_unordered_container_base()
00136     : _M_local_iterators(0), _M_const_local_iterators(0)
00137     { }
00138 
00139     /** Notify all iterators that reference this container that the
00140     container is being destroyed. */
00141     ~_Safe_unordered_container_base()
00142     { this->_M_detach_all(); }
00143 
00144     /** Detach all iterators, leaving them singular. */
00145     void
00146     _M_detach_all();
00147 
00148     /** Swap this container with the given container. This operation
00149      *  also swaps ownership of the iterators, so that when the
00150      *  operation is complete all iterators that originally referenced
00151      *  one container now reference the other container.
00152      */
00153     void
00154     _M_swap(_Safe_unordered_container_base& __x);
00155 
00156   public:
00157     /** Attach an iterator to this container. */
00158     void
00159     _M_attach_local(_Safe_iterator_base* __it, bool __constant);
00160 
00161     /** Likewise but not thread safe. */
00162     void
00163     _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw ();
00164 
00165     /** Detach an iterator from this container */
00166     void
00167     _M_detach_local(_Safe_iterator_base* __it);
00168 
00169     /** Likewise but not thread safe. */
00170     void
00171     _M_detach_local_single(_Safe_iterator_base* __it) throw ();
00172   };
00173 } // namespace __gnu_debug
00174 
00175 #endif