to.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // to.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/29/23.
  6. //
  7. #pragma once
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <stream/common_view.h>
  12. #include <stream/forward.h>
  13. #define FWD(x) std::forward<decltype(x)>(x)
  14. namespace stream::ranges {
  15. template <typename C> class store {
  16. private:
  17. C & to_;
  18. public:
  19. store(C & to) : to_(to) {}
  20. template <typename Stream>
  21. friend void operator|(Stream && stream, store && store) {
  22. store.to_.emplace(store.to_.end(), stream.begin(), stream.end());
  23. }
  24. };
  25. template <template <typename...> class C> struct to_range {
  26. template <typename Stream> friend auto operator|(Stream && stream, to_range) {
  27. if constexpr (traits::is_sentinal_v<Stream>) {
  28. common_view common(FWD(stream));
  29. return C(common.begin(), common.end());
  30. } else {
  31. return C(stream.begin(), stream.end());
  32. }
  33. }
  34. };
  35. template <template <typename...> class C> auto to() { return to_range<C>(); }
  36. inline auto to_vector() { return to<std::vector>(); }
  37. inline auto to_map() { return to<std::map>(); }
  38. inline auto to_set() { return to<std::set>(); }
  39. }
  40. #undef FWD