ListBase.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #ifdef URHO3D_IS_BUILDING
  5. #include "Urho3D.h"
  6. #else
  7. #include <Urho3D/Urho3D.h>
  8. #endif
  9. #include "../Container/Allocator.h"
  10. #include "../Container/Swap.h"
  11. namespace Urho3D
  12. {
  13. /// Doubly-linked list node base class.
  14. struct ListNodeBase
  15. {
  16. /// Construct.
  17. ListNodeBase() :
  18. prev_(nullptr),
  19. next_(nullptr)
  20. {
  21. }
  22. /// Previous node.
  23. ListNodeBase* prev_;
  24. /// Next node.
  25. ListNodeBase* next_;
  26. };
  27. /// Doubly-linked list iterator base class.
  28. struct ListIteratorBase
  29. {
  30. /// Construct.
  31. ListIteratorBase() :
  32. ptr_(nullptr)
  33. {
  34. }
  35. /// Construct with a node pointer.
  36. explicit ListIteratorBase(ListNodeBase* ptr) :
  37. ptr_(ptr)
  38. {
  39. }
  40. /// Test for equality with another iterator.
  41. bool operator ==(const ListIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  42. /// Test for inequality with another iterator.
  43. bool operator !=(const ListIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  44. /// Go to the next node.
  45. void GotoNext()
  46. {
  47. if (ptr_)
  48. ptr_ = ptr_->next_;
  49. }
  50. /// Go to the previous node.
  51. void GotoPrev()
  52. {
  53. if (ptr_)
  54. ptr_ = ptr_->prev_;
  55. }
  56. /// Node pointer.
  57. ListNodeBase* ptr_;
  58. };
  59. /// Doubly-linked list base class.
  60. class URHO3D_API ListBase
  61. {
  62. public:
  63. /// Construct.
  64. ListBase() :
  65. head_(nullptr),
  66. tail_(nullptr),
  67. allocator_(nullptr),
  68. size_(0)
  69. {
  70. }
  71. /// Swap with another linked list.
  72. void Swap(ListBase& rhs)
  73. {
  74. Urho3D::Swap(head_, rhs.head_);
  75. Urho3D::Swap(tail_, rhs.tail_);
  76. Urho3D::Swap(allocator_, rhs.allocator_);
  77. Urho3D::Swap(size_, rhs.size_);
  78. }
  79. protected:
  80. /// Head node pointer.
  81. ListNodeBase* head_;
  82. /// Tail node pointer.
  83. ListNodeBase* tail_;
  84. /// Node allocator.
  85. AllocatorBlock* allocator_;
  86. /// Number of nodes.
  87. i32 size_;
  88. };
  89. }