Iterator.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. namespace Framework
  3. {
  4. template<typename T> class BasicIterator
  5. {
  6. public:
  7. virtual ~BasicIterator() {}
  8. virtual operator bool() = 0;
  9. virtual T val() = 0;
  10. virtual void plusPlus() = 0;
  11. };
  12. /**
  13. * Iterator interface
  14. * \tparam T type of the value that is iterated
  15. * \tparam I type of the iterator subclass
  16. */
  17. template<typename T, typename I> class Iterator : public BasicIterator<T>
  18. {
  19. public:
  20. virtual ~Iterator()
  21. {
  22. static_assert(std::is_base_of<Iterator, I>::value,
  23. "Type Argument I must be an implementation of Iterator");
  24. }
  25. virtual bool hasNext()
  26. {
  27. return (bool)next();
  28. }
  29. virtual I next() = 0;
  30. virtual void plusPlus() override
  31. {
  32. *(I*)this = next();
  33. }
  34. virtual I& operator++() //! prefix
  35. {
  36. *(I*)this = next();
  37. return *(I*)this;
  38. }
  39. virtual I operator++(int) //! postfix
  40. {
  41. I tmp(*(I*)this);
  42. operator++();
  43. return tmp;
  44. }
  45. /**
  46. * adds a value before the current element.
  47. * after executing this function, the current element is the new value.
  48. *
  49. * \param val the new value to add
  50. */
  51. virtual void addBefore(T val) = 0;
  52. virtual void set(T val) = 0;
  53. virtual void remove() = 0;
  54. operator T()
  55. {
  56. return this->val();
  57. }
  58. virtual T operator->()
  59. {
  60. return this->val();
  61. }
  62. virtual T operator*()
  63. {
  64. return this->val();
  65. }
  66. };
  67. } // namespace Framework