streams.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include <algorithm>
  3. #include <memory>
  4. #include <numeric>
  5. #define DELEGATE_ITERATOR_IMPL_BASE(impl) \
  6. bool operator==(iterator const&other) const { \
  7. return impl == other.impl; \
  8. }
  9. #define DELEGATE_ITERATOR_IMPL(impl) \
  10. DELEGATE_ITERATOR_IMPL_BASE(impl) \
  11. iterator& operator++() { \
  12. ++impl; \
  13. return *this; \
  14. }
  15. std::map<void*, bool> data;
  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>
  87. class stream_impl {
  88. public:
  89. virtual ~stream_impl() { }
  90. virtual ::stream::iterator<T> begin() = 0;
  91. virtual ::stream::iterator<T> end() = 0;
  92. };
  93. template <typename T, typename = void>
  94. class stream_base_pointer_impl {};
  95. template <typename T>
  96. class stream_base_pointer_impl<T, typename std::enable_if<detail::is_dereferencable<typename std::remove_reference<T>::type>::value>::type> {
  97. private:
  98. using self = stream_base<T>;
  99. using noref = typename std::remove_reference<T>::type;
  100. using element_type = typename std::pointer_traits<noref>::element_type;
  101. public:
  102. auto deref() const -> stream_base<element_type &> {
  103. return static_cast<stream_base<T> const *>(this)->map([](T const & p) -> element_type & { return *p; });
  104. }
  105. };
  106. template <typename T>
  107. class stream_base : public stream_base_pointer_impl<T> {
  108. private:
  109. template <typename F>
  110. using map_f = decltype(std::declval<F>()(std::declval<T>()));
  111. template <typename F>
  112. using flatmap_f = typename decltype(std::declval<F>()(std::declval<T>()))::value_type;
  113. using self = stream_base<T>;
  114. using noref = typename std::remove_reference<T>::type;
  115. using clean = typename std::decay<T>::type;
  116. public:
  117. stream_base(std::shared_ptr<stream_impl<T>> const & impl) : impl_(impl) {}
  118. ::stream::iterator<T> begin() const { return impl_->begin(); }
  119. ::stream::iterator<T> end () const { return impl_->end (); }
  120. bool empty() const { return begin() == end(); }
  121. std::vector<clean> collect() const {
  122. std::vector<clean> coll;
  123. collect(coll);
  124. return coll;
  125. }
  126. template <typename C, typename = typename std::enable_if<!std::is_void<typename C::value_type>::value, void>::type>
  127. C& collect(C & coll) const {
  128. std::copy(begin(), end(), std::inserter(coll, coll.end()));
  129. return coll;
  130. }
  131. template <typename F>
  132. clean accumulate(F&& fold, clean const& accum) {
  133. return std::accumulate(begin(), end(), accum, fold);
  134. }
  135. clean accumulate(clean const& accum) {
  136. return std::accumulate(begin(), end(), accum);
  137. }
  138. template <typename F>
  139. void each(F && consumer) {
  140. std::for_each(begin(), end(), consumer);
  141. }
  142. template <typename F>
  143. stream_base<map_f<F>> map(F&& func) const;
  144. template <typename F, typename = typename std::enable_if<std::is_member_object_pointer<F>::value>::type>
  145. auto map(F && memvar) const -> stream_base<typename map_member_object<F>::type> {
  146. return map(map_member_object<F>{memvar});
  147. }
  148. template <typename F, typename = typename std::enable_if<std::is_member_function_pointer<F>::value>::type>
  149. auto map(F && memvar) const -> stream_base<typename map_member_function<F>::type> {
  150. return map(map_member_function<F>{memvar});
  151. }
  152. template <typename Cast>
  153. auto cast() const -> stream_base<Cast const &> {
  154. return map([](T const & p) -> Cast const & { return p; });
  155. }
  156. template <typename F>
  157. stream_base<T> filter(F&& func) const;
  158. template <typename F>
  159. stream_base<flatmap_f<F>> flatmap(F&& func) const;
  160. private:
  161. std::shared_ptr<stream_impl<T>> impl_;
  162. };
  163. }
  164. }