streams.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include <algorithm>
  3. #include <memory>
  4. #include <numeric>
  5. #include <vector>
  6. #define DELEGATE_ITERATOR_IMPL_BASE(impl) \
  7. bool operator==(iterator const&other) const { \
  8. return impl == other.impl; \
  9. }
  10. #define DELEGATE_ITERATOR_IMPL(impl) \
  11. DELEGATE_ITERATOR_IMPL_BASE(impl) \
  12. iterator& operator++() { \
  13. ++impl; \
  14. return *this; \
  15. }
  16. namespace stream {
  17. #define STREAM_ITERATOR_COPY() \
  18. copy(other.copy), dereference(other.dereference), compare(other.compare), destroy(other.destroy), advance(other.advance), type_(other.type_)
  19. template <typename T>
  20. class iterator {
  21. public:
  22. using value_type = typename std::remove_reference<T>::type;
  23. using reference = value_type &;
  24. using pointer = value_type *;
  25. using difference_type = std::ptrdiff_t;
  26. using iterator_category = std::forward_iterator_tag;
  27. public:
  28. iterator() = default;
  29. template <typename Iter>
  30. iterator(Iter impl) {
  31. copy = [](void * p) { return (void*) new Iter(*(Iter*)(p)); };
  32. dereference = [](void * p) -> T { return **((Iter*)p); };
  33. compare = [](void * l, void * r) { return *((Iter*)l) == *((Iter*)r); };
  34. destroy = [](void * p) { delete (Iter*)(p); };
  35. advance = [](void * p) { ++(*(Iter*)(p)); };
  36. type_ = typeid(Iter).name();
  37. impl_ = copy(&impl);
  38. }
  39. iterator(iterator const& other)
  40. : STREAM_ITERATOR_COPY()
  41. , impl_(copy(other.impl_)) {
  42. }
  43. iterator(iterator && other)
  44. : STREAM_ITERATOR_COPY()
  45. , impl_(other.impl_) {
  46. other.impl_ = nullptr;
  47. }
  48. iterator & operator=(iterator const & other) {
  49. return *this = iterator{other};
  50. }
  51. iterator & operator=(iterator && other) {
  52. swap(*this, other);
  53. return *this;
  54. }
  55. ~iterator() { if (destroy) destroy(impl_); }
  56. T operator*() const { return dereference(impl_); }
  57. iterator& operator++() { advance(impl_); return *this; }
  58. bool operator==(iterator const&other) const {
  59. if (strcmp(type_, other.type_)) { return false; }
  60. return compare(impl_, other.impl_);
  61. }
  62. bool operator!=(iterator const&other) const {
  63. if (strcmp(type_, other.type_)) { return false; }
  64. return !compare(impl_, other.impl_);
  65. }
  66. private:
  67. friend void swap(iterator & lhs, iterator & rhs) {
  68. using std::swap;
  69. swap(lhs.copy, rhs.copy);
  70. swap(lhs.dereference, rhs.dereference);
  71. swap(lhs.compare, rhs.compare);
  72. swap(lhs.destroy, rhs.destroy);
  73. swap(lhs.advance, rhs.advance);
  74. swap(lhs.type_, rhs.type_);
  75. swap(lhs.impl_, rhs.impl_);
  76. }
  77. using delegate = void(*)(void*);
  78. void* (*copy)(void*){nullptr};
  79. T (*dereference)(void*){nullptr};
  80. bool (*compare)(void*, void*){nullptr};
  81. delegate destroy{nullptr}, advance{nullptr};
  82. char const * type_{nullptr};
  83. void * impl_{nullptr};
  84. };
  85. namespace detail {
  86. template <typename T, typename = void>
  87. class stream_base_pointer_impl {};
  88. template <typename T>
  89. class stream_base_pointer_impl<T, typename std::enable_if<traits::is_dereferencable<T>::value>::type> {
  90. private:
  91. using self_t = stream_base<T>;
  92. using pointer = typename std::remove_reference<T>::type;
  93. using element_type = typename std::pointer_traits<pointer>::element_type;
  94. public:
  95. auto deref() const & -> stream_base<element_type &> {
  96. return static_cast<self_t const *>(this)->map([](T const & p) -> element_type & { return *p; });
  97. }
  98. auto deref() && -> stream_base<element_type &> {
  99. return static_cast<self_t &&>(*this).map([](T const & p) -> element_type & { return *p; });
  100. }
  101. };
  102. template <typename T>
  103. class stream_base : public stream_base_pointer_impl<T> {
  104. private:
  105. using value_type = typename std::decay<T>::type;
  106. public:
  107. template <typename Stream>
  108. stream_base(std::shared_ptr<Stream> && impl) {
  109. do_begin = [](std::shared_ptr<void> p) -> iterator<T> { return std::static_pointer_cast<Stream>(p)->begin(); };
  110. do_end = [](std::shared_ptr<void> p) -> iterator<T> { return std::static_pointer_cast<Stream>(p)->end(); };
  111. impl_ = std::static_pointer_cast<void>(impl);
  112. }
  113. ::stream::iterator<T> begin() const { return do_begin(impl_); }
  114. ::stream::iterator<T> end () const { return do_end (impl_); }
  115. bool empty() const { return begin() == end(); }
  116. std::vector<value_type> collect() const {
  117. std::vector<value_type> coll;
  118. collect(coll);
  119. return coll;
  120. }
  121. template <typename C, typename = typename std::enable_if<!std::is_void<typename C::value_type>::value, void>::type>
  122. C & collect(C & coll) const {
  123. std::copy(begin(), end(), std::inserter(coll, coll.end()));
  124. return coll;
  125. }
  126. template <typename F>
  127. value_type accumulate(F&& fold, value_type const& accum) const {
  128. return std::accumulate(begin(), end(), accum, fold);
  129. }
  130. value_type accumulate(value_type const& accum) const {
  131. return std::accumulate(begin(), end(), accum);
  132. }
  133. template <typename F>
  134. void each(F && consumer) const {
  135. std::for_each(begin(), end(), consumer);
  136. }
  137. template <typename F>
  138. stream_base<traits::mapped_t<T, F>> map(F&& func) const &;
  139. template <typename F>
  140. stream_base<T> filter(F&& func) const &;
  141. template <typename F>
  142. stream_base<traits::fmapped_t<T, F>> flatmap(F&& func) const &;
  143. template <typename F>
  144. stream_base<traits::mapped_t<T, F>> map(F&& func) &&;
  145. template <typename F>
  146. stream_base<T> filter(F&& func) &&;
  147. template <typename F>
  148. stream_base<traits::fmapped_t<T, F>> flatmap(F&& func) &&;
  149. template <typename Cast>
  150. stream_base<Cast const &> cast() const & {
  151. return map([](T const & p) -> Cast const & { return p; });
  152. }
  153. template <typename Cast>
  154. stream_base<Cast const &> cast() && {
  155. return std::move(*this).map([](T const & p) -> Cast const & { return p; });
  156. }
  157. template <typename F, typename = traits::is_memvar_t<F>>
  158. stream_base<traits::memvar_f<F>> map(F && memvar) const & {
  159. return map(map_member_object<F>{memvar});
  160. }
  161. template <typename F, typename = traits::is_memfun_t<F>>
  162. stream_base<traits::memfun_f<F>> map(F && memvar) const & {
  163. return map(map_member_function<F>{memvar});
  164. }
  165. template <typename F, typename = traits::is_memvar_t<F>>
  166. stream_base<traits::memvar_f<F>> map(F && memvar) && {
  167. return std::move(*this).map(map_member_object<F>{memvar});
  168. }
  169. template <typename F, typename = traits::is_memfun_t<F>>
  170. stream_base<traits::memfun_f<F>> map(F && memvar) && {
  171. return std::move(*this).map(map_member_function<F>{memvar});
  172. }
  173. private:
  174. iterator<T> (*do_begin)(std::shared_ptr<void>){nullptr};
  175. iterator<T> (*do_end)(std::shared_ptr<void>){nullptr};
  176. std::shared_ptr<void> impl_{nullptr};
  177. };
  178. }
  179. }