Supplier.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <functional>
  3. #include "Iterator.h"
  4. #include "ReferenceCounter.h"
  5. namespace Framework
  6. {
  7. namespace Exceptions
  8. {
  9. class EndOfSupplier
  10. {
  11. const char* msg;
  12. public:
  13. EndOfSupplier()
  14. : msg("End of Supplier")
  15. {}
  16. };
  17. } // namespace Exceptions
  18. template<typename T> class Supplier : public ReferenceCounter
  19. {
  20. public:
  21. Supplier()
  22. : ReferenceCounter()
  23. {}
  24. virtual ~Supplier() {}
  25. virtual T next() noexcept(false) = 0;
  26. };
  27. template<typename T> class IteratorSupplier : public Supplier<T>
  28. {
  29. private:
  30. BasicIterator<T>* iterator;
  31. public:
  32. IteratorSupplier(BasicIterator<T>* iterator)
  33. : Supplier<T>(),
  34. iterator(iterator)
  35. {}
  36. ~IteratorSupplier()
  37. {
  38. delete iterator;
  39. }
  40. T next() noexcept(false) override
  41. {
  42. if (!*iterator)
  43. {
  44. throw Exceptions::EndOfSupplier();
  45. }
  46. T val = iterator->val();
  47. iterator->plusPlus();
  48. return val;
  49. }
  50. };
  51. template<typename T> class FilteredSupplier : public Supplier<T>
  52. {
  53. private:
  54. Supplier<T>* supplier;
  55. std::function<bool(T)> filter;
  56. public:
  57. FilteredSupplier(Supplier<T>* supplier, std::function<bool(T)> filter)
  58. : Supplier<T>(),
  59. supplier(supplier),
  60. filter(filter)
  61. {}
  62. ~FilteredSupplier()
  63. {
  64. supplier->release();
  65. }
  66. T next() noexcept(false) override
  67. {
  68. T val = supplier->next();
  69. while (!filter(val))
  70. {
  71. val = supplier->next();
  72. }
  73. return val;
  74. }
  75. };
  76. template<typename T, typename R> class MappedSupplier : public Supplier<R>
  77. {
  78. private:
  79. Supplier<T>* supplier;
  80. std::function<R(T)> mapper;
  81. public:
  82. MappedSupplier(Supplier<T>* supplier, std::function<R(T)> mapper)
  83. : Supplier<T>(),
  84. supplier(supplier),
  85. mapper(mapper)
  86. {}
  87. ~MappedSupplier()
  88. {
  89. supplier->release();
  90. }
  91. R next() noexcept(false) override
  92. {
  93. return mapper(supplier->next());
  94. }
  95. };
  96. template<typename T> class Stream;
  97. template<typename T, typename R> class FlatMappedSupplier
  98. : public Supplier<R>
  99. {
  100. private:
  101. Supplier<T>* supplier;
  102. std::function<Supplier<R>*(T)> mapper;
  103. Supplier<R>* currentSupplier;
  104. public:
  105. FlatMappedSupplier(
  106. Supplier<T>* supplier, std::function<Supplier<R>*(T)> mapper)
  107. : Supplier<R>(),
  108. supplier(supplier),
  109. mapper(mapper),
  110. currentSupplier(nullptr)
  111. {}
  112. ~FlatMappedSupplier()
  113. {
  114. if (currentSupplier != nullptr)
  115. {
  116. currentSupplier->release();
  117. }
  118. supplier->release();
  119. }
  120. R next() noexcept(false) override
  121. {
  122. while (true)
  123. {
  124. if (currentSupplier == nullptr)
  125. {
  126. currentSupplier = mapper(supplier->next());
  127. }
  128. try
  129. {
  130. return currentSupplier->next();
  131. } catch (Exceptions::EndOfSupplier)
  132. {
  133. currentSupplier->release();
  134. currentSupplier = nullptr;
  135. }
  136. }
  137. }
  138. };
  139. } // namespace Framework