libstdc++
safe_unordered_container.h
Go to the documentation of this file.
00001 // Safe container implementation  -*- C++ -*-
00002 
00003 // Copyright (C) 2011-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_unordered_container.h
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_CONTAINER_H
00030 #define _GLIBCXX_DEBUG_SAFE_UNORDERED_CONTAINER_H 1
00031 
00032 #include <debug/debug.h>
00033 #include <debug/macros.h>
00034 #include <debug/functions.h>
00035 #include <debug/safe_unordered_base.h>
00036 
00037 namespace __gnu_debug
00038 {
00039   /**
00040    * @brief Base class for constructing a @a safe unordered container type
00041    * that tracks iterators that reference it.
00042    *
00043    * The class template %_Safe_unordered_container simplifies the
00044    * construction of @a safe unordered containers that track the iterators
00045    * that reference the container, so that the iterators are notified of
00046    * changes in the container that may affect their operation, e.g., if
00047    * the container invalidates its iterators or is destructed. This class
00048    * template may only be used by deriving from it and passing the name
00049    * of the derived class as its template parameter via the curiously
00050    * recurring template pattern. The derived class must have @c
00051    * iterator and @c const_iterator types that are instantiations of
00052    * class template _Safe_iterator for this container and @c local_iterator
00053    * and @c const_local_iterator types that are instantiations of class
00054    * template _Safe_local_iterator for this container. Iterators will
00055    * then be tracked automatically.
00056    */
00057   template<typename _Container>
00058     class _Safe_unordered_container : public _Safe_unordered_container_base
00059     {
00060     private:
00061       _Container&
00062       _M_cont() noexcept
00063       { return *static_cast<_Container*>(this); }
00064 
00065     protected:
00066       void
00067       _M_invalidate_locals()
00068       {
00069         auto __local_end = _M_cont()._M_base().end(0);
00070         this->_M_invalidate_local_if(
00071                 [__local_end](__decltype(_M_cont()._M_base().cend(0)) __it)
00072                 { return __it != __local_end; });
00073       }
00074 
00075       void
00076       _M_invalidate_all()
00077       {
00078         auto __end = _M_cont()._M_base().end();
00079         this->_M_invalidate_if(
00080                 [__end](__decltype(_M_cont()._M_base().cend()) __it)
00081                 { return __it != __end; });
00082         _M_invalidate_locals();
00083       }
00084 
00085       /** Invalidates all iterators @c x that reference this container,
00086           are not singular, and for which @c __pred(x) returns @c
00087           true. @c __pred will be invoked with the normal iterators nested
00088           in the safe ones. */
00089       template<typename _Predicate>
00090         void
00091         _M_invalidate_if(_Predicate __pred);
00092 
00093       /** Invalidates all local iterators @c x that reference this container,
00094           are not singular, and for which @c __pred(x) returns @c
00095           true. @c __pred will be invoked with the normal ilocal iterators
00096           nested in the safe ones. */
00097       template<typename _Predicate>
00098         void
00099         _M_invalidate_local_if(_Predicate __pred);
00100     };
00101 } // namespace __gnu_debug
00102 
00103 #include <debug/safe_unordered_container.tcc>
00104 
00105 #endif