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
16 #if BOOST_CB_ENABLE_DEBUG
19 #if defined(BOOST_NO_STDC_NAMESPACE)
25 #endif // BOOST_CB_ENABLE_DEBUG
28 namespace cb_details {
30 #if BOOST_CB_ENABLE_DEBUG
32 // The value the uninitialized memory is filled with.
33 const int UNINITIALIZED = 0xcc;
36 inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
37 std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
41 inline void do_fill_uninitialized_memory(T& /*data*/, std::size_t /*size_in_bytes*/) BOOST_NOEXCEPT {
46 class debug_iterator_registry;
49 \class debug_iterator_base
50 \brief Registers/unregisters iterators into the registry of valid iterators.
52 This class is intended to be a base class of an iterator.
54 class debug_iterator_base {
59 //! Iterator registry.
60 mutable const debug_iterator_registry* m_registry;
62 //! Next iterator in the iterator chain.
63 mutable const debug_iterator_base* m_next;
66 // Construction/destruction
68 //! Default constructor.
69 debug_iterator_base();
71 //! Constructor taking the iterator registry as a parameter.
72 debug_iterator_base(const debug_iterator_registry* registry);
75 debug_iterator_base(const debug_iterator_base& rhs);
78 ~debug_iterator_base();
83 debug_iterator_base& operator = (const debug_iterator_base& rhs);
85 //! Is the iterator valid?
86 bool is_valid(const debug_iterator_registry* registry) const;
88 //! Invalidate the iterator.
90 \note The method is const in order to invalidate const iterators, too.
92 void invalidate() const;
94 //! Return the next iterator in the iterator chain.
95 const debug_iterator_base* next() const;
97 //! Set the next iterator in the iterator chain.
99 \note The method is const in order to set a next iterator to a const iterator, too.
101 void set_next(const debug_iterator_base* it) const;
106 //! Register self as a valid iterator.
107 void register_self();
109 //! Unregister self from valid iterators.
110 void unregister_self();
114 \class debug_iterator_registry
115 \brief Registry of valid iterators.
117 This class is intended to be a base class of a container.
119 class debug_iterator_registry {
121 //! Pointer to the chain of valid iterators.
122 mutable const debug_iterator_base* m_iterators;
127 //! Default constructor.
128 debug_iterator_registry() : m_iterators(0) {}
130 //! Register an iterator into the list of valid iterators.
132 \note The method is const in order to register iterators into const containers, too.
134 void register_iterator(const debug_iterator_base* it) const {
135 it->set_next(m_iterators);
139 //! Unregister an iterator from the list of valid iterators.
141 \note The method is const in order to unregister iterators from const containers, too.
143 void unregister_iterator(const debug_iterator_base* it) const {
144 const debug_iterator_base* previous = 0;
145 for (const debug_iterator_base* p = m_iterators; p != it; previous = p, p = p->next()) {}
146 remove(it, previous);
149 //! Invalidate every iterator pointing to the same element as the iterator passed as a parameter.
150 template <class Iterator>
151 void invalidate_iterators(const Iterator& it) {
152 const debug_iterator_base* previous = 0;
153 for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
154 if (((Iterator*)p)->m_it == it.m_it) {
163 //! Invalidate all iterators except an iterator poining to the same element as the iterator passed as a parameter.
164 template <class Iterator>
165 void invalidate_iterators_except(const Iterator& it) {
166 const debug_iterator_base* previous = 0;
167 for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
168 if (((Iterator*)p)->m_it != it.m_it) {
177 //! Invalidate all iterators.
178 void invalidate_all_iterators() {
179 for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next())
187 //! Remove the current iterator from the iterator chain.
188 void remove(const debug_iterator_base* current,
189 const debug_iterator_base* previous) const {
191 m_iterators = m_iterators->next();
193 previous->set_next(current->next());
197 // Implementation of the debug_iterator_base methods.
199 inline debug_iterator_base::debug_iterator_base() : m_registry(0), m_next(0) {}
201 inline debug_iterator_base::debug_iterator_base(const debug_iterator_registry* registry)
202 : m_registry(registry), m_next(0) {
206 inline debug_iterator_base::debug_iterator_base(const debug_iterator_base& rhs)
207 : m_registry(rhs.m_registry), m_next(0) {
211 inline debug_iterator_base::~debug_iterator_base() { unregister_self(); }
213 inline debug_iterator_base& debug_iterator_base::operator = (const debug_iterator_base& rhs) {
214 if (m_registry == rhs.m_registry)
217 m_registry = rhs.m_registry;
222 inline bool debug_iterator_base::is_valid(const debug_iterator_registry* registry) const {
223 return m_registry == registry;
226 inline void debug_iterator_base::invalidate() const { m_registry = 0; }
228 inline const debug_iterator_base* debug_iterator_base::next() const { return m_next; }
230 inline void debug_iterator_base::set_next(const debug_iterator_base* it) const { m_next = it; }
232 inline void debug_iterator_base::register_self() {
234 m_registry->register_iterator(this);
237 inline void debug_iterator_base::unregister_self() {
239 m_registry->unregister_iterator(this);
242 #endif // #if BOOST_CB_ENABLE_DEBUG
244 } // namespace cb_details
248 #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)