ListBase.h 3.1 KB

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