Iterator.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. virtual void set(T val) = 0;
  46. virtual void remove() = 0;
  47. operator T()
  48. {
  49. return this->val();
  50. }
  51. virtual T operator->()
  52. {
  53. return this->val();
  54. }
  55. virtual T operator*()
  56. {
  57. return this->val();
  58. }
  59. };
  60. } // namespace Framework