#pragma once #include #include "Supplier.h" namespace Framework { template class Stream { private: Supplier* supplier; public: Stream(Supplier* supplier) : supplier(supplier) {} ~Stream() { supplier->release(); } Stream filter(std::function f) { return Stream(new FilteredSupplier( dynamic_cast*>(supplier->getThis()), f)); } template Stream map(std::function f) { return Stream(new MappedSupplier( dynamic_cast*>(supplier->getThis()), f)); } template Stream flatMap(std::function(T)> f) { return Stream(new FlatMappedSupplier( dynamic_cast*>(supplier->getThis()), [this, f](T arg) { return dynamic_cast*>( f(arg).zSupplier()->getThis()); })); } void forEach(std::function action) { while (true) { try { action(supplier->next()); } catch (Exceptions::EndOfSupplier) { return; } } } template R collect(std::function combinator, R initialValue) { while (true) { try { initialValue = combinator(initialValue, supplier->next()); } catch (Exceptions::EndOfSupplier) { return initialValue; } } } Supplier* zSupplier() { return supplier; } }; } // namespace Framework