OSDN Git Service

2006-11-11 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / safe_iterator.tcc
1 // Debugging iterator implementation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /** @file safe_iterator.tcc
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
35
36 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
37 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
38
39 namespace __gnu_debug
40 {
41   template<typename _Iterator, typename _Sequence>
42     bool
43     _Safe_iterator<_Iterator, _Sequence>::
44     _M_can_advance(const difference_type& __n) const
45     {
46       typedef typename _Sequence::const_iterator const_iterator;
47
48       if (this->_M_singular())
49         return false;
50       if (__n == 0)
51         return true;
52       if (__n < 0)
53         {
54           const_iterator __begin =
55             static_cast<const _Sequence*>(_M_sequence)->begin();
56           std::pair<difference_type, _Distance_precision> __dist =
57             this->_M_get_distance(__begin, *this);
58           bool __ok =  (__dist.second == __dp_exact && __dist.first >= -__n
59                         || __dist.second != __dp_exact && __dist.first > 0);
60           return __ok;
61         }
62       else
63         {
64           const_iterator __end =
65             static_cast<const _Sequence*>(_M_sequence)->end();
66           std::pair<difference_type, _Distance_precision> __dist =
67             this->_M_get_distance(*this, __end);
68           bool __ok = (__dist.second == __dp_exact && __dist.first >= __n
69                        || __dist.second != __dp_exact && __dist.first > 0);
70           return __ok;
71         }
72     }
73
74   template<typename _Iterator, typename _Sequence>
75     template<typename _Other>
76       bool
77       _Safe_iterator<_Iterator, _Sequence>::
78       _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
79       {
80         if (!_M_can_compare(__rhs))
81           return false;
82
83         /* Determine if we can order the iterators without the help of
84            the container */
85         std::pair<difference_type, _Distance_precision> __dist =
86           this->_M_get_distance(*this, __rhs);
87         switch (__dist.second) {
88         case __dp_equality:
89           if (__dist.first == 0)
90             return true;
91           break;
92
93         case __dp_sign:
94         case __dp_exact:
95           return __dist.first >= 0;
96         }
97
98         /* We can only test for equality, but check if one of the
99            iterators is at an extreme. */
100         if (_M_is_begin() || __rhs._M_is_end())
101           return true;
102         else if (_M_is_end() || __rhs._M_is_begin())
103           return false;
104
105         // Assume that this is a valid range; we can't check anything else
106         return true;
107       }
108
109   template<typename _Iterator, typename _Sequence>
110     void
111     _Safe_iterator<_Iterator, _Sequence>::
112     _M_invalidate()
113     {
114       __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
115       _M_invalidate_single();
116     }
117
118   template<typename _Iterator, typename _Sequence>
119     void
120     _Safe_iterator<_Iterator, _Sequence>::
121     _M_invalidate_single()
122     {
123       typedef typename _Sequence::iterator iterator;
124       typedef typename _Sequence::const_iterator const_iterator;
125
126       if (!this->_M_singular())
127         {
128           for (_Safe_iterator_base* __iter = _M_sequence->_M_iterators;
129                __iter; __iter = __iter->_M_next)
130             {
131               iterator* __victim = static_cast<iterator*>(__iter);
132               if (this->base() == __victim->base())
133                 __victim->_M_version = 0;
134             }
135
136           for (_Safe_iterator_base* __iter2 = _M_sequence->_M_const_iterators;
137                __iter2; __iter2 = __iter2->_M_next)
138             {
139               const_iterator* __victim = static_cast<const_iterator*>(__iter2);
140               if (__victim->base() == this->base())
141                 __victim->_M_version = 0;
142             }
143           _M_version = 0;
144         }
145     }
146 } // namespace __gnu_debug
147
148 #endif
149