1 // Debug support for the circular buffer library.
3 // Copyright (c) 2003-2008 Jan Gaspar
5 // Use, modification, and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)
10 #define BOOST_CIRCULAR_BUFFER_DEBUG_HPP
12 #if defined(_MSC_VER) && _MSC_VER >= 1200
18 namespace cb_details
{
20 #if BOOST_CB_ENABLE_DEBUG
22 // The value the uninitialized memory is filled with.
23 const int UNINITIALIZED
= 0xcc;
25 class debug_iterator_registry
;
28 \class debug_iterator_base
29 \brief Registers/unregisters iterators into the registry of valid iterators.
31 This class is intended to be a base class of an iterator.
33 class debug_iterator_base
{
38 //! Iterator registry.
39 mutable const debug_iterator_registry
* m_registry
;
41 //! Next iterator in the iterator chain.
42 mutable const debug_iterator_base
* m_next
;
45 // Construction/destruction
47 //! Default constructor.
48 debug_iterator_base();
50 //! Constructor taking the iterator registry as a parameter.
51 debug_iterator_base(const debug_iterator_registry
* registry
);
54 debug_iterator_base(const debug_iterator_base
& rhs
);
57 ~debug_iterator_base();
62 debug_iterator_base
& operator = (const debug_iterator_base
& rhs
);
64 //! Is the iterator valid?
65 bool is_valid(const debug_iterator_registry
* registry
) const;
67 //! Invalidate the iterator.
69 \note The method is const in order to invalidate const iterators, too.
71 void invalidate() const;
73 //! Return the next iterator in the iterator chain.
74 const debug_iterator_base
* next() const;
76 //! Set the next iterator in the iterator chain.
78 \note The method is const in order to set a next iterator to a const iterator, too.
80 void set_next(const debug_iterator_base
* it
) const;
85 //! Register self as a valid iterator.
88 //! Unregister self from valid iterators.
89 void unregister_self();
93 \class debug_iterator_registry
94 \brief Registry of valid iterators.
96 This class is intended to be a base class of a container.
98 class debug_iterator_registry
{
100 //! Pointer to the chain of valid iterators.
101 mutable const debug_iterator_base
* m_iterators
;
106 //! Default constructor.
107 debug_iterator_registry() : m_iterators(0) {}
109 //! Register an iterator into the list of valid iterators.
111 \note The method is const in order to register iterators into const containers, too.
113 void register_iterator(const debug_iterator_base
* it
) const {
114 it
->set_next(m_iterators
);
118 //! Unregister an iterator from the list of valid iterators.
120 \note The method is const in order to unregister iterators from const containers, too.
122 void unregister_iterator(const debug_iterator_base
* it
) const {
123 const debug_iterator_base
* previous
= 0;
124 for (const debug_iterator_base
* p
= m_iterators
; p
!= it
; previous
= p
, p
= p
->next()) {}
125 remove(it
, previous
);
128 //! Invalidate every iterator pointing to the same element as the iterator passed as a parameter.
129 template <class Iterator
>
130 void invalidate_iterators(const Iterator
& it
) {
131 const debug_iterator_base
* previous
= 0;
132 for (const debug_iterator_base
* p
= m_iterators
; p
!= 0; p
= p
->next()) {
133 if (static_cast<Iterator
*>(p
)->m_it
== it
.m_it
) {
142 //! Invalidate all iterators except an iterator poining to the same element as the iterator passed as a parameter.
143 template <class Iterator
>
144 void invalidate_iterators_except(const Iterator
& it
) {
145 const debug_iterator_base
* previous
= 0;
146 for (const debug_iterator_base
* p
= m_iterators
; p
!= 0; p
= p
->next()) {
147 if (static_cast<Iterator
*>(p
)->m_it
!= it
.m_it
) {
156 //! Invalidate all iterators.
157 void invalidate_all_iterators() {
158 for (const debug_iterator_base
* p
= m_iterators
; p
!= 0; p
= p
->next())
166 //! Remove the current iterator from the iterator chain.
167 void remove(const debug_iterator_base
* current
,
168 const debug_iterator_base
* previous
) const {
170 m_iterators
= m_iterators
->next();
172 previous
->set_next(current
->next());
176 // Implementation of the debug_iterator_base methods.
178 inline debug_iterator_base::debug_iterator_base() : m_registry(0), m_next(0) {}
180 inline debug_iterator_base::debug_iterator_base(const debug_iterator_registry
* registry
)
181 : m_registry(registry
), m_next(0) {
185 inline debug_iterator_base::debug_iterator_base(const debug_iterator_base
& rhs
)
186 : m_registry(rhs
.m_registry
), m_next(0) {
190 inline debug_iterator_base::~debug_iterator_base() { unregister_self(); }
192 inline debug_iterator_base
& debug_iterator_base::operator = (const debug_iterator_base
& rhs
) {
193 if (m_registry
== rhs
.m_registry
)
196 m_registry
= rhs
.m_registry
;
201 inline bool debug_iterator_base::is_valid(const debug_iterator_registry
* registry
) const {
202 return m_registry
== registry
;
205 inline void debug_iterator_base::invalidate() const { m_registry
= 0; }
207 inline const debug_iterator_base
* debug_iterator_base::next() const { return m_next
; }
209 inline void debug_iterator_base::set_next(const debug_iterator_base
* it
) const { m_next
= it
; }
211 inline void debug_iterator_base::register_self() {
213 m_registry
->register_iterator(this);
216 inline void debug_iterator_base::unregister_self() {
218 m_registry
->unregister_iterator(this);
221 #endif // #if BOOST_CB_ENABLE_DEBUG
223 } // namespace cb_details
227 #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)