libstdc++
safe_unordered_base.h
Go to the documentation of this file.
00001 // Safe container/iterator base implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2011-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_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() { this->_M_detach(); }
00075 
00076     _Safe_unordered_container_base*
00077     _M_get_container() const noexcept;
00078 
00079     /** Attaches this iterator to the given container, detaching it
00080      *  from whatever container it was attached to originally. If the
00081      *  new container is the NULL pointer, the iterator is left
00082      *  unattached.
00083      */
00084     void
00085     _M_attach(_Safe_sequence_base* __seq, bool __constant);
00086 
00087     /** Likewise, but not thread-safe. */
00088     void
00089     _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
00090 
00091     /** Detach the iterator for whatever container it is attached to,
00092      *  if any.
00093     */
00094     void
00095     _M_detach();
00096 
00097     /** Likewise, but not thread-safe. */
00098     void
00099     _M_detach_single() throw ();
00100   };
00101 
00102   /**
00103    * @brief Base class that supports tracking of local iterators that
00104    * reference an unordered container.
00105    *
00106    * The %_Safe_unordered_container_base class provides basic support for
00107    * tracking iterators into an unordered container. Containers that track
00108    * iterators must derived from %_Safe_unordered_container_base publicly, so
00109    * that safe iterators (which inherit _Safe_iterator_base) can
00110    * attach to them. This class contains four linked lists of
00111    * iterators, one for constant iterators, one for mutable
00112    * iterators, one for constant local iterators, one for mutable local
00113    * iterators and a version number that allows very fast
00114    * invalidation of all iterators that reference the container.
00115    *
00116    * This class must ensure that no operation on it may throw an
00117    * exception, otherwise @a safe containers may fail to provide the
00118    * exception-safety guarantees required by the C++ standard.
00119    */
00120   class _Safe_unordered_container_base : public _Safe_sequence_base
00121   {
00122     friend class _Safe_local_iterator_base;
00123     typedef _Safe_sequence_base _Base;
00124 
00125   public:
00126     /// The list of mutable local iterators that reference this container
00127     _Safe_iterator_base* _M_local_iterators;
00128 
00129     /// The list of constant local iterators that reference this container
00130     _Safe_iterator_base* _M_const_local_iterators;
00131 
00132   protected:
00133     // Initialize with a version number of 1 and no iterators
00134     _Safe_unordered_container_base() noexcept
00135     : _M_local_iterators(nullptr), _M_const_local_iterators(nullptr)
00136     { }
00137 
00138     // Copy constructor does not copy iterators.
00139     _Safe_unordered_container_base(const _Safe_unordered_container_base&)
00140     noexcept
00141     : _Safe_unordered_container_base() { }
00142 
00143     // When moved unordered containers iterators are swapped.
00144     _Safe_unordered_container_base(_Safe_unordered_container_base&& __x)
00145     noexcept
00146     : _Safe_unordered_container_base()
00147     { this->_M_swap(__x); }
00148 
00149     /** Notify all iterators that reference this container that the
00150         container is being destroyed. */
00151     ~_Safe_unordered_container_base() noexcept
00152     { this->_M_detach_all(); }
00153 
00154     /** Detach all iterators, leaving them singular. */
00155     void
00156     _M_detach_all();
00157 
00158     /** Swap this container with the given container. This operation
00159      *  also swaps ownership of the iterators, so that when the
00160      *  operation is complete all iterators that originally referenced
00161      *  one container now reference the other container.
00162      */
00163     void
00164     _M_swap(_Safe_unordered_container_base& __x) noexcept;
00165 
00166   private:
00167     /** Attach an iterator to this container. */
00168     void
00169     _M_attach_local(_Safe_iterator_base* __it, bool __constant);
00170 
00171     /** Likewise but not thread safe. */
00172     void
00173     _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw ();
00174 
00175     /** Detach an iterator from this container */
00176     void
00177     _M_detach_local(_Safe_iterator_base* __it);
00178 
00179     /** Likewise but not thread safe. */
00180     void
00181     _M_detach_local_single(_Safe_iterator_base* __it) throw ();
00182   };
00183 } // namespace __gnu_debug
00184 
00185 #endif