safe_unordered_base.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Safe container/iterator base implementation -*- C++ -*-
  2. // Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/safe_unordered_base.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H
  24. #define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1
  25. #include <debug/safe_base.h>
  26. namespace __gnu_debug
  27. {
  28. class _Safe_unordered_container_base;
  29. /** \brief Basic functionality for a @a safe iterator.
  30. *
  31. * The %_Safe_local_iterator_base base class implements the functionality
  32. * of a safe local iterator that is not specific to a particular iterator
  33. * type. It contains a pointer back to the container it references
  34. * along with iterator version information and pointers to form a
  35. * doubly-linked list of local iterators referenced by the container.
  36. *
  37. * This class must not perform any operations that can throw an
  38. * exception, or the exception guarantees of derived iterators will
  39. * be broken.
  40. */
  41. class _Safe_local_iterator_base : public _Safe_iterator_base
  42. {
  43. protected:
  44. /** Initializes the iterator and makes it singular. */
  45. _Safe_local_iterator_base()
  46. { }
  47. /** Initialize the iterator to reference the container pointed to
  48. * by @p __seq. @p __constant is true when we are initializing a
  49. * constant local iterator, and false if it is a mutable local iterator.
  50. * Note that @p __seq may be NULL, in which case the iterator will be
  51. * singular. Otherwise, the iterator will reference @p __seq and
  52. * be nonsingular.
  53. */
  54. _Safe_local_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
  55. { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
  56. /** Initializes the iterator to reference the same container that
  57. @p __x does. @p __constant is true if this is a constant
  58. iterator, and false if it is mutable. */
  59. _Safe_local_iterator_base(const _Safe_local_iterator_base& __x,
  60. bool __constant)
  61. { this->_M_attach(__x._M_sequence, __constant); }
  62. ~_Safe_local_iterator_base() { this->_M_detach(); }
  63. _Safe_unordered_container_base*
  64. _M_get_container() const noexcept;
  65. /** Attaches this iterator to the given container, detaching it
  66. * from whatever container it was attached to originally. If the
  67. * new container is the NULL pointer, the iterator is left
  68. * unattached.
  69. */
  70. void
  71. _M_attach(_Safe_sequence_base* __seq, bool __constant);
  72. /** Likewise, but not thread-safe. */
  73. void
  74. _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
  75. /** Detach the iterator for whatever container it is attached to,
  76. * if any.
  77. */
  78. void
  79. _M_detach();
  80. /** Likewise, but not thread-safe. */
  81. void
  82. _M_detach_single() throw ();
  83. };
  84. /**
  85. * @brief Base class that supports tracking of local iterators that
  86. * reference an unordered container.
  87. *
  88. * The %_Safe_unordered_container_base class provides basic support for
  89. * tracking iterators into an unordered container. Containers that track
  90. * iterators must derived from %_Safe_unordered_container_base publicly, so
  91. * that safe iterators (which inherit _Safe_iterator_base) can
  92. * attach to them. This class contains four linked lists of
  93. * iterators, one for constant iterators, one for mutable
  94. * iterators, one for constant local iterators, one for mutable local
  95. * iterators and a version number that allows very fast
  96. * invalidation of all iterators that reference the container.
  97. *
  98. * This class must ensure that no operation on it may throw an
  99. * exception, otherwise @a safe containers may fail to provide the
  100. * exception-safety guarantees required by the C++ standard.
  101. */
  102. class _Safe_unordered_container_base : public _Safe_sequence_base
  103. {
  104. friend class _Safe_local_iterator_base;
  105. typedef _Safe_sequence_base _Base;
  106. public:
  107. /// The list of mutable local iterators that reference this container
  108. _Safe_iterator_base* _M_local_iterators;
  109. /// The list of constant local iterators that reference this container
  110. _Safe_iterator_base* _M_const_local_iterators;
  111. protected:
  112. // Initialize with a version number of 1 and no iterators
  113. _Safe_unordered_container_base() noexcept
  114. : _M_local_iterators(nullptr), _M_const_local_iterators(nullptr)
  115. { }
  116. // Copy constructor does not copy iterators.
  117. _Safe_unordered_container_base(const _Safe_unordered_container_base&)
  118. noexcept
  119. : _Safe_unordered_container_base() { }
  120. // When moved unordered containers iterators are swapped.
  121. _Safe_unordered_container_base(_Safe_unordered_container_base&& __x)
  122. noexcept
  123. : _Safe_unordered_container_base()
  124. { this->_M_swap(__x); }
  125. /** Notify all iterators that reference this container that the
  126. container is being destroyed. */
  127. ~_Safe_unordered_container_base() noexcept
  128. { this->_M_detach_all(); }
  129. /** Detach all iterators, leaving them singular. */
  130. void
  131. _M_detach_all();
  132. /** Swap this container with the given container. This operation
  133. * also swaps ownership of the iterators, so that when the
  134. * operation is complete all iterators that originally referenced
  135. * one container now reference the other container.
  136. */
  137. void
  138. _M_swap(_Safe_unordered_container_base& __x) noexcept;
  139. private:
  140. /** Attach an iterator to this container. */
  141. void
  142. _M_attach_local(_Safe_iterator_base* __it, bool __constant);
  143. /** Likewise but not thread safe. */
  144. void
  145. _M_attach_local_single(_Safe_iterator_base* __it, bool __constant) throw ();
  146. /** Detach an iterator from this container */
  147. void
  148. _M_detach_local(_Safe_iterator_base* __it);
  149. /** Likewise but not thread safe. */
  150. void
  151. _M_detach_local_single(_Safe_iterator_base* __it) throw ();
  152. };
  153. } // namespace __gnu_debug
  154. #endif