ListBase.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Atomic/Atomic.h"
  24. #include "../Container/Allocator.h"
  25. #include "../Container/Swap.h"
  26. namespace Atomic
  27. {
  28. /// Doubly-linked list node base class.
  29. struct ListNodeBase
  30. {
  31. /// Construct.
  32. ListNodeBase() :
  33. prev_(0),
  34. next_(0)
  35. {
  36. }
  37. /// Previous node.
  38. ListNodeBase* prev_;
  39. /// Next node.
  40. ListNodeBase* next_;
  41. };
  42. /// Doubly-linked list iterator base class.
  43. struct ListIteratorBase
  44. {
  45. /// Construct.
  46. ListIteratorBase() :
  47. ptr_(0)
  48. {
  49. }
  50. /// Construct with a node pointer.
  51. explicit ListIteratorBase(ListNodeBase* ptr) :
  52. ptr_(ptr)
  53. {
  54. }
  55. /// Test for equality with another iterator.
  56. bool operator ==(const ListIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  57. /// Test for inequality with another iterator.
  58. bool operator !=(const ListIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  59. /// Go to the next node.
  60. void GotoNext()
  61. {
  62. if (ptr_)
  63. ptr_ = ptr_->next_;
  64. }
  65. /// Go to the previous node.
  66. void GotoPrev()
  67. {
  68. if (ptr_)
  69. ptr_ = ptr_->prev_;
  70. }
  71. /// Node pointer.
  72. ListNodeBase* ptr_;
  73. };
  74. /// Doubly-linked list base class.
  75. class ATOMIC_API ListBase
  76. {
  77. public:
  78. /// Construct.
  79. ListBase() :
  80. head_(0),
  81. tail_(0),
  82. allocator_(0),
  83. size_(0)
  84. {
  85. }
  86. /// Swap with another linked list.
  87. void Swap(ListBase& rhs)
  88. {
  89. Atomic::Swap(head_, rhs.head_);
  90. Atomic::Swap(tail_, rhs.tail_);
  91. Atomic::Swap(allocator_, rhs.allocator_);
  92. Atomic::Swap(size_, rhs.size_);
  93. }
  94. protected:
  95. /// Head node pointer.
  96. ListNodeBase* head_;
  97. /// Tail node pointer.
  98. ListNodeBase* tail_;
  99. /// Node allocator.
  100. AllocatorBlock* allocator_;
  101. /// Number of nodes.
  102. unsigned size_;
  103. };
  104. }