xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/debug/safe_sequence.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Safe sequence implementation  -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2003-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file debug/safe_sequence.h
26*38fd1498Szrj  *  This file is a GNU debug extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
30*38fd1498Szrj #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
31*38fd1498Szrj 
32*38fd1498Szrj #include <debug/assertions.h>
33*38fd1498Szrj #include <debug/macros.h>
34*38fd1498Szrj #include <debug/functions.h>
35*38fd1498Szrj #include <debug/safe_base.h>
36*38fd1498Szrj 
37*38fd1498Szrj namespace __gnu_debug
38*38fd1498Szrj {
39*38fd1498Szrj   /** A simple function object that returns true if the passed-in
40*38fd1498Szrj    *  value is not equal to the stored value. It saves typing over
41*38fd1498Szrj    *  using both bind1st and not_equal.
42*38fd1498Szrj    */
43*38fd1498Szrj   template<typename _Type>
44*38fd1498Szrj     class _Not_equal_to
45*38fd1498Szrj     {
46*38fd1498Szrj       _Type __value;
47*38fd1498Szrj 
48*38fd1498Szrj     public:
_Not_equal_to(const _Type & __v)49*38fd1498Szrj       explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
50*38fd1498Szrj 
51*38fd1498Szrj       bool
operator()52*38fd1498Szrj       operator()(const _Type& __x) const
53*38fd1498Szrj       { return __value != __x; }
54*38fd1498Szrj     };
55*38fd1498Szrj 
56*38fd1498Szrj   /** A simple function object that returns true if the passed-in
57*38fd1498Szrj    *  value is equal to the stored value. */
58*38fd1498Szrj   template <typename _Type>
59*38fd1498Szrj     class _Equal_to
60*38fd1498Szrj     {
61*38fd1498Szrj       _Type __value;
62*38fd1498Szrj 
63*38fd1498Szrj     public:
_Equal_to(const _Type & __v)64*38fd1498Szrj       explicit _Equal_to(const _Type& __v) : __value(__v) { }
65*38fd1498Szrj 
66*38fd1498Szrj       bool
operator()67*38fd1498Szrj       operator()(const _Type& __x) const
68*38fd1498Szrj       { return __value == __x; }
69*38fd1498Szrj     };
70*38fd1498Szrj 
71*38fd1498Szrj   /** A function object that returns true when the given random access
72*38fd1498Szrj       iterator is at least @c n steps away from the given iterator. */
73*38fd1498Szrj   template<typename _Iterator>
74*38fd1498Szrj     class _After_nth_from
75*38fd1498Szrj     {
76*38fd1498Szrj       typedef typename std::iterator_traits<_Iterator>::difference_type
77*38fd1498Szrj       difference_type;
78*38fd1498Szrj 
79*38fd1498Szrj       _Iterator _M_base;
80*38fd1498Szrj       difference_type _M_n;
81*38fd1498Szrj 
82*38fd1498Szrj     public:
_After_nth_from(const difference_type & __n,const _Iterator & __base)83*38fd1498Szrj       _After_nth_from(const difference_type& __n, const _Iterator& __base)
84*38fd1498Szrj       : _M_base(__base), _M_n(__n) { }
85*38fd1498Szrj 
86*38fd1498Szrj       bool
operator()87*38fd1498Szrj       operator()(const _Iterator& __x) const
88*38fd1498Szrj       { return __x - _M_base >= _M_n; }
89*38fd1498Szrj     };
90*38fd1498Szrj 
91*38fd1498Szrj   /**
92*38fd1498Szrj    * @brief Base class for constructing a @a safe sequence type that
93*38fd1498Szrj    * tracks iterators that reference it.
94*38fd1498Szrj    *
95*38fd1498Szrj    * The class template %_Safe_sequence simplifies the construction of
96*38fd1498Szrj    * @a safe sequences that track the iterators that reference the
97*38fd1498Szrj    * sequence, so that the iterators are notified of changes in the
98*38fd1498Szrj    * sequence that may affect their operation, e.g., if the container
99*38fd1498Szrj    * invalidates its iterators or is destructed. This class template
100*38fd1498Szrj    * may only be used by deriving from it and passing the name of the
101*38fd1498Szrj    * derived class as its template parameter via the curiously
102*38fd1498Szrj    * recurring template pattern. The derived class must have @c
103*38fd1498Szrj    * iterator and @c const_iterator types that are instantiations of
104*38fd1498Szrj    * class template _Safe_iterator for this sequence. Iterators will
105*38fd1498Szrj    * then be tracked automatically.
106*38fd1498Szrj    */
107*38fd1498Szrj   template<typename _Sequence>
108*38fd1498Szrj     class _Safe_sequence : public _Safe_sequence_base
109*38fd1498Szrj     {
110*38fd1498Szrj     public:
111*38fd1498Szrj       /** Invalidates all iterators @c x that reference this sequence,
112*38fd1498Szrj 	  are not singular, and for which @c __pred(x) returns @c
113*38fd1498Szrj 	  true. @c __pred will be invoked with the normal iterators nested
114*38fd1498Szrj 	  in the safe ones. */
115*38fd1498Szrj       template<typename _Predicate>
116*38fd1498Szrj 	void
117*38fd1498Szrj 	_M_invalidate_if(_Predicate __pred);
118*38fd1498Szrj 
119*38fd1498Szrj       /** Transfers all iterators @c x that reference @c from sequence,
120*38fd1498Szrj 	  are not singular, and for which @c __pred(x) returns @c
121*38fd1498Szrj 	  true. @c __pred will be invoked with the normal iterators nested
122*38fd1498Szrj 	  in the safe ones. */
123*38fd1498Szrj       template<typename _Predicate>
124*38fd1498Szrj 	void
125*38fd1498Szrj 	_M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred);
126*38fd1498Szrj     };
127*38fd1498Szrj 
128*38fd1498Szrj   /// Like _Safe_sequence but with a special _M_invalidate_all implementation
129*38fd1498Szrj   /// not invalidating past-the-end iterators. Used by node based sequence.
130*38fd1498Szrj   template<typename _Sequence>
131*38fd1498Szrj     class _Safe_node_sequence
132*38fd1498Szrj     : public _Safe_sequence<_Sequence>
133*38fd1498Szrj     {
134*38fd1498Szrj     protected:
135*38fd1498Szrj       void
_M_invalidate_all()136*38fd1498Szrj       _M_invalidate_all()
137*38fd1498Szrj       {
138*38fd1498Szrj 	typedef typename _Sequence::const_iterator _Const_iterator;
139*38fd1498Szrj 	typedef typename _Const_iterator::iterator_type _Base_const_iterator;
140*38fd1498Szrj 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
141*38fd1498Szrj 	const _Sequence& __seq = *static_cast<_Sequence*>(this);
142*38fd1498Szrj 	this->_M_invalidate_if(_Not_equal(__seq._M_base().end()));
143*38fd1498Szrj       }
144*38fd1498Szrj     };
145*38fd1498Szrj 
146*38fd1498Szrj } // namespace __gnu_debug
147*38fd1498Szrj 
148*38fd1498Szrj #include <debug/safe_sequence.tcc>
149*38fd1498Szrj 
150*38fd1498Szrj #endif
151