Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:34:29

0001 // Debug support for the circular buffer library.
0002 
0003 // Copyright (c) 2003-2008 Jan Gaspar
0004 
0005 // Use, modification, and distribution is subject to the Boost Software
0006 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)
0010 #define BOOST_CIRCULAR_BUFFER_DEBUG_HPP
0011 
0012 #if defined(_MSC_VER)
0013     #pragma once
0014 #endif
0015 
0016 #if BOOST_CB_ENABLE_DEBUG
0017 #include <cstring>
0018 
0019 #if defined(BOOST_NO_STDC_NAMESPACE)
0020 namespace std {
0021     using ::memset;
0022 }
0023 #endif
0024 
0025 #endif // BOOST_CB_ENABLE_DEBUG
0026 namespace boost {
0027 
0028 namespace cb_details {
0029 
0030 #if BOOST_CB_ENABLE_DEBUG
0031 
0032 // The value the uninitialized memory is filled with.
0033 const int UNINITIALIZED = 0xcc;
0034 
0035 template <class T>
0036 inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
0037     std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
0038 }
0039 
0040 template <class T>
0041 inline void do_fill_uninitialized_memory(T& /*data*/, std::size_t /*size_in_bytes*/) BOOST_NOEXCEPT {
0042     // Do nothing
0043 }
0044 
0045 
0046 class debug_iterator_registry;
0047 
0048 /*!
0049     \class debug_iterator_base
0050     \brief Registers/unregisters iterators into the registry of valid iterators.
0051 
0052     This class is intended to be a base class of an iterator.
0053 */
0054 class debug_iterator_base {
0055 
0056 private:
0057 // Members
0058 
0059     //! Iterator registry.
0060     mutable const debug_iterator_registry* m_registry;
0061 
0062     //! Next iterator in the iterator chain.
0063     mutable const debug_iterator_base* m_next;
0064 
0065 public:
0066 // Construction/destruction
0067 
0068     //! Default constructor.
0069     debug_iterator_base();
0070 
0071     //! Constructor taking the iterator registry as a parameter.
0072     debug_iterator_base(const debug_iterator_registry* registry);
0073 
0074     //! Copy constructor.
0075     debug_iterator_base(const debug_iterator_base& rhs);
0076 
0077     //! Destructor.
0078     ~debug_iterator_base();
0079 
0080 // Methods
0081 
0082     //! Assign operator.
0083     debug_iterator_base& operator = (const debug_iterator_base& rhs);
0084 
0085     //! Is the iterator valid?
0086     bool is_valid(const debug_iterator_registry* registry) const;
0087 
0088     //! Invalidate the iterator.
0089     /*!
0090         \note The method is const in order to invalidate const iterators, too.
0091     */
0092     void invalidate() const;
0093 
0094     //! Return the next iterator in the iterator chain.
0095     const debug_iterator_base* next() const;
0096 
0097     //! Set the next iterator in the iterator chain.
0098     /*!
0099         \note The method is const in order to set a next iterator to a const iterator, too.
0100     */
0101     void set_next(const debug_iterator_base* it) const;
0102 
0103 private:
0104 // Helpers
0105 
0106     //! Register self as a valid iterator.
0107     void register_self();
0108 
0109     //! Unregister self from valid iterators.
0110     void unregister_self();
0111 };
0112 
0113 /*!
0114     \class debug_iterator_registry
0115     \brief Registry of valid iterators.
0116 
0117     This class is intended to be a base class of a container.
0118 */
0119 class debug_iterator_registry {
0120 
0121     //! Pointer to the chain of valid iterators.
0122     mutable const debug_iterator_base* m_iterators;
0123 
0124 public:
0125 // Methods
0126 
0127     //! Default constructor.
0128     debug_iterator_registry() : m_iterators(0) {}
0129 
0130     //! Register an iterator into the list of valid iterators.
0131     /*!
0132         \note The method is const in order to register iterators into const containers, too.
0133     */
0134     void register_iterator(const debug_iterator_base* it) const {
0135         it->set_next(m_iterators);
0136         m_iterators = it;
0137     }
0138 
0139     //! Unregister an iterator from the list of valid iterators.
0140     /*!
0141         \note The method is const in order to unregister iterators from const containers, too.
0142     */
0143     void unregister_iterator(const debug_iterator_base* it) const {
0144         const debug_iterator_base* previous = 0;
0145         for (const debug_iterator_base* p = m_iterators; p != it; previous = p, p = p->next()) {}
0146         remove(it, previous);
0147     }
0148 
0149     //! Invalidate every iterator pointing to the same element as the iterator passed as a parameter.
0150     template <class Iterator>
0151     void invalidate_iterators(const Iterator& it) {
0152         const debug_iterator_base* previous = 0;
0153         for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
0154             if (((Iterator*)p)->m_it == it.m_it) {
0155                 p->invalidate();
0156                 remove(p, previous);
0157                 continue;
0158             }
0159             previous = p;
0160         }
0161     }
0162 
0163     //! Invalidate all iterators except an iterator poining to the same element as the iterator passed as a parameter.
0164     template <class Iterator>
0165     void invalidate_iterators_except(const Iterator& it) {
0166         const debug_iterator_base* previous = 0;
0167         for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
0168             if (((Iterator*)p)->m_it != it.m_it) {
0169                 p->invalidate();
0170                 remove(p, previous);
0171                 continue;
0172             }
0173             previous = p;
0174         }
0175     }
0176 
0177     //! Invalidate all iterators.
0178     void invalidate_all_iterators() {
0179         for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next())
0180             p->invalidate();
0181         m_iterators = 0;
0182     }
0183 
0184 private:
0185 // Helpers
0186 
0187     //! Remove the current iterator from the iterator chain.
0188     void remove(const debug_iterator_base* current,
0189                 const debug_iterator_base* previous) const {
0190         if (previous == 0)
0191             m_iterators = m_iterators->next();
0192         else
0193             previous->set_next(current->next());
0194     }
0195 };
0196 
0197 // Implementation of the debug_iterator_base methods.
0198 
0199 inline debug_iterator_base::debug_iterator_base() : m_registry(0), m_next(0) {}
0200 
0201 inline debug_iterator_base::debug_iterator_base(const debug_iterator_registry* registry)
0202 : m_registry(registry), m_next(0) {
0203     register_self();
0204 }
0205 
0206 inline debug_iterator_base::debug_iterator_base(const debug_iterator_base& rhs)
0207 : m_registry(rhs.m_registry), m_next(0) {
0208     register_self();
0209 }
0210 
0211 inline debug_iterator_base::~debug_iterator_base() { unregister_self(); }
0212 
0213 inline debug_iterator_base& debug_iterator_base::operator = (const debug_iterator_base& rhs) {
0214     if (m_registry == rhs.m_registry)
0215         return *this;
0216     unregister_self();
0217     m_registry = rhs.m_registry;
0218     register_self();
0219     return *this;
0220 }
0221 
0222 inline bool debug_iterator_base::is_valid(const debug_iterator_registry* registry) const {
0223     return m_registry == registry;
0224 }
0225 
0226 inline void debug_iterator_base::invalidate() const { m_registry = 0; }
0227 
0228 inline const debug_iterator_base* debug_iterator_base::next() const { return m_next; }
0229 
0230 inline void debug_iterator_base::set_next(const debug_iterator_base* it) const { m_next = it; }
0231 
0232 inline void debug_iterator_base::register_self() {
0233     if (m_registry != 0)
0234         m_registry->register_iterator(this);
0235 }
0236 
0237 inline void debug_iterator_base::unregister_self() {
0238     if (m_registry != 0)
0239         m_registry->unregister_iterator(this);
0240 }
0241 
0242 #endif // #if BOOST_CB_ENABLE_DEBUG
0243 
0244 } // namespace cb_details
0245 
0246 } // namespace boost
0247 
0248 #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)