debug.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Debug support for the circular buffer library.
  2. // Copyright (c) 2003-2008 Jan Gaspar
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)
  7. #define BOOST_CIRCULAR_BUFFER_DEBUG_HPP
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #if BOOST_CB_ENABLE_DEBUG
  12. #include <cstring>
  13. #if defined(BOOST_NO_STDC_NAMESPACE)
  14. namespace std {
  15. using ::memset;
  16. }
  17. #endif
  18. #endif // BOOST_CB_ENABLE_DEBUG
  19. namespace boost {
  20. namespace cb_details {
  21. #if BOOST_CB_ENABLE_DEBUG
  22. // The value the uninitialized memory is filled with.
  23. const int UNINITIALIZED = 0xcc;
  24. template <class T>
  25. inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
  26. std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
  27. }
  28. template <class T>
  29. inline void do_fill_uninitialized_memory(T& /*data*/, std::size_t /*size_in_bytes*/) BOOST_NOEXCEPT {
  30. // Do nothing
  31. }
  32. class debug_iterator_registry;
  33. /*!
  34. \class debug_iterator_base
  35. \brief Registers/unregisters iterators into the registry of valid iterators.
  36. This class is intended to be a base class of an iterator.
  37. */
  38. class debug_iterator_base {
  39. private:
  40. // Members
  41. //! Iterator registry.
  42. mutable const debug_iterator_registry* m_registry;
  43. //! Next iterator in the iterator chain.
  44. mutable const debug_iterator_base* m_next;
  45. public:
  46. // Construction/destruction
  47. //! Default constructor.
  48. debug_iterator_base();
  49. //! Constructor taking the iterator registry as a parameter.
  50. debug_iterator_base(const debug_iterator_registry* registry);
  51. //! Copy constructor.
  52. debug_iterator_base(const debug_iterator_base& rhs);
  53. //! Destructor.
  54. ~debug_iterator_base();
  55. // Methods
  56. //! Assign operator.
  57. debug_iterator_base& operator = (const debug_iterator_base& rhs);
  58. //! Is the iterator valid?
  59. bool is_valid(const debug_iterator_registry* registry) const;
  60. //! Invalidate the iterator.
  61. /*!
  62. \note The method is const in order to invalidate const iterators, too.
  63. */
  64. void invalidate() const;
  65. //! Return the next iterator in the iterator chain.
  66. const debug_iterator_base* next() const;
  67. //! Set the next iterator in the iterator chain.
  68. /*!
  69. \note The method is const in order to set a next iterator to a const iterator, too.
  70. */
  71. void set_next(const debug_iterator_base* it) const;
  72. private:
  73. // Helpers
  74. //! Register self as a valid iterator.
  75. void register_self();
  76. //! Unregister self from valid iterators.
  77. void unregister_self();
  78. };
  79. /*!
  80. \class debug_iterator_registry
  81. \brief Registry of valid iterators.
  82. This class is intended to be a base class of a container.
  83. */
  84. class debug_iterator_registry {
  85. //! Pointer to the chain of valid iterators.
  86. mutable const debug_iterator_base* m_iterators;
  87. public:
  88. // Methods
  89. //! Default constructor.
  90. debug_iterator_registry() : m_iterators(0) {}
  91. //! Register an iterator into the list of valid iterators.
  92. /*!
  93. \note The method is const in order to register iterators into const containers, too.
  94. */
  95. void register_iterator(const debug_iterator_base* it) const {
  96. it->set_next(m_iterators);
  97. m_iterators = it;
  98. }
  99. //! Unregister an iterator from the list of valid iterators.
  100. /*!
  101. \note The method is const in order to unregister iterators from const containers, too.
  102. */
  103. void unregister_iterator(const debug_iterator_base* it) const {
  104. const debug_iterator_base* previous = 0;
  105. for (const debug_iterator_base* p = m_iterators; p != it; previous = p, p = p->next()) {}
  106. remove(it, previous);
  107. }
  108. //! Invalidate every iterator pointing to the same element as the iterator passed as a parameter.
  109. template <class Iterator>
  110. void invalidate_iterators(const Iterator& it) {
  111. const debug_iterator_base* previous = 0;
  112. for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
  113. if (((Iterator*)p)->m_it == it.m_it) {
  114. p->invalidate();
  115. remove(p, previous);
  116. continue;
  117. }
  118. previous = p;
  119. }
  120. }
  121. //! Invalidate all iterators except an iterator poining to the same element as the iterator passed as a parameter.
  122. template <class Iterator>
  123. void invalidate_iterators_except(const Iterator& it) {
  124. const debug_iterator_base* previous = 0;
  125. for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) {
  126. if (((Iterator*)p)->m_it != it.m_it) {
  127. p->invalidate();
  128. remove(p, previous);
  129. continue;
  130. }
  131. previous = p;
  132. }
  133. }
  134. //! Invalidate all iterators.
  135. void invalidate_all_iterators() {
  136. for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next())
  137. p->invalidate();
  138. m_iterators = 0;
  139. }
  140. private:
  141. // Helpers
  142. //! Remove the current iterator from the iterator chain.
  143. void remove(const debug_iterator_base* current,
  144. const debug_iterator_base* previous) const {
  145. if (previous == 0)
  146. m_iterators = m_iterators->next();
  147. else
  148. previous->set_next(current->next());
  149. }
  150. };
  151. // Implementation of the debug_iterator_base methods.
  152. inline debug_iterator_base::debug_iterator_base() : m_registry(0), m_next(0) {}
  153. inline debug_iterator_base::debug_iterator_base(const debug_iterator_registry* registry)
  154. : m_registry(registry), m_next(0) {
  155. register_self();
  156. }
  157. inline debug_iterator_base::debug_iterator_base(const debug_iterator_base& rhs)
  158. : m_registry(rhs.m_registry), m_next(0) {
  159. register_self();
  160. }
  161. inline debug_iterator_base::~debug_iterator_base() { unregister_self(); }
  162. inline debug_iterator_base& debug_iterator_base::operator = (const debug_iterator_base& rhs) {
  163. if (m_registry == rhs.m_registry)
  164. return *this;
  165. unregister_self();
  166. m_registry = rhs.m_registry;
  167. register_self();
  168. return *this;
  169. }
  170. inline bool debug_iterator_base::is_valid(const debug_iterator_registry* registry) const {
  171. return m_registry == registry;
  172. }
  173. inline void debug_iterator_base::invalidate() const { m_registry = 0; }
  174. inline const debug_iterator_base* debug_iterator_base::next() const { return m_next; }
  175. inline void debug_iterator_base::set_next(const debug_iterator_base* it) const { m_next = it; }
  176. inline void debug_iterator_base::register_self() {
  177. if (m_registry != 0)
  178. m_registry->register_iterator(this);
  179. }
  180. inline void debug_iterator_base::unregister_self() {
  181. if (m_registry != 0)
  182. m_registry->unregister_iterator(this);
  183. }
  184. #endif // #if BOOST_CB_ENABLE_DEBUG
  185. } // namespace cb_details
  186. } // namespace boost
  187. #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP)