Iterator.Function.tlh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <optional>
  3. #include "Iterator.Base.tlh"
  4. //-----------------------------------------------------------------------------
  5. // Function_Iterator
  6. //-----------------------------------------------------------------------------
  7. namespace Iterator
  8. {
  9. template <typename T>
  10. class Function_Iterator : public __CoreIterator
  11. {
  12. typedef __CoreIterator inherited;
  13. public:
  14. using FuncType = std::function <std::optional <T> ()>;
  15. public:
  16. using value_type = typename T;
  17. using data_type = typename T&;
  18. using deref_type = value_type;
  19. public:
  20. // Function_Iterator () = delete;
  21. Function_Iterator () = default;
  22. Function_Iterator (const FuncType & fun) : m_GetAndNext (fun)
  23. {
  24. m_Count = INT_MAX;
  25. m_Index = 0;
  26. m_Current = m_GetAndNext ();
  27. }
  28. Function_Iterator (FuncType & fun) : m_GetAndNext (fun)
  29. {
  30. m_Count = INT_MAX;
  31. m_Index = 0;
  32. m_Current = m_GetAndNext ();
  33. }
  34. public:
  35. inline virtual void Next () override
  36. {
  37. if (! IsEmpty ())
  38. {
  39. m_Current = m_GetAndNext ();
  40. inherited::Next ();
  41. }
  42. }
  43. inline data_type Current () { return m_Current.value (); }
  44. inline data_type operator () () { return Current (); }
  45. inline data_type operator * () { return Current (); }
  46. inline virtual bool IsEmpty () const override
  47. {
  48. if (inherited::IsEmpty ())
  49. return true;
  50. return ! m_Current.has_value ();
  51. }
  52. inline void Skip (int delta)
  53. {
  54. for (; ! IsEmpty () && delta > 0; delta --)
  55. Next ();
  56. }
  57. protected:
  58. void InitFrom (FuncType & fun)
  59. {
  60. m_GetAndNext = fun;
  61. m_Count = INT_MAX;
  62. m_Index = 0;
  63. m_Current = m_GetAndNext ();
  64. }
  65. private:
  66. FuncType m_GetAndNext;
  67. std::optional <T> m_Current;
  68. };
  69. };