ListBase.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2014 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 "../Container/Allocator.h"
  24. #include "../Container/Swap.h"
  25. namespace Urho3D
  26. {
  27. /// Doubly-linked list node base class.
  28. struct ListNodeBase
  29. {
  30. /// Construct.
  31. ListNodeBase() :
  32. prev_(0),
  33. next_(0)
  34. {
  35. }
  36. /// Previous node.
  37. ListNodeBase* prev_;
  38. /// Next node.
  39. ListNodeBase* next_;
  40. };
  41. /// Doubly-linked list iterator base class.
  42. struct ListIteratorBase
  43. {
  44. /// Construct.
  45. ListIteratorBase() :
  46. ptr_(0)
  47. {
  48. }
  49. /// Construct with a node pointer.
  50. explicit ListIteratorBase(ListNodeBase* ptr) :
  51. ptr_(ptr)
  52. {
  53. }
  54. /// Test for equality with another iterator.
  55. bool operator == (const ListIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  56. /// Test for inequality with another iterator.
  57. bool operator != (const ListIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  58. /// Go to the next node.
  59. void GotoNext()
  60. {
  61. if (ptr_)
  62. ptr_ = ptr_->next_;
  63. }
  64. /// Go to the previous node.
  65. void GotoPrev()
  66. {
  67. if (ptr_)
  68. ptr_ = ptr_->prev_;
  69. }
  70. /// Node pointer.
  71. ListNodeBase* ptr_;
  72. };
  73. /// Doubly-linked list base class.
  74. class URHO3D_API ListBase
  75. {
  76. public:
  77. /// Construct.
  78. ListBase() :
  79. allocator_(0),
  80. size_(0)
  81. {
  82. }
  83. /// Swap with another linked list.
  84. void Swap(ListBase& rhs)
  85. {
  86. Urho3D::Swap(head_, rhs.head_);
  87. Urho3D::Swap(tail_, rhs.tail_);
  88. Urho3D::Swap(allocator_, rhs.allocator_);
  89. Urho3D::Swap(size_, rhs.size_);
  90. }
  91. protected:
  92. /// Head node pointer.
  93. ListNodeBase* head_;
  94. /// Tail node pointer.
  95. ListNodeBase* tail_;
  96. /// Node allocator.
  97. AllocatorBlock* allocator_;
  98. /// Number of nodes.
  99. unsigned size_;
  100. };
  101. }