From 8d246c4568bf6b7b53e17c270dc4cc176ae88a21 Mon Sep 17 00:00:00 2001 From: Tudor Bosman Date: Mon, 12 Nov 2012 16:42:43 -0800 Subject: [PATCH] movify generators (but mostly operators) Summary: Make things work with non-copyable generators. Test Plan: folly/experimental/test Reviewed By: tjackson@fb.com FB internal diff: D628520 --- folly/experimental/Gen-inl.h | 245 +++++++++++++++++++++++++++-------- 1 file changed, 194 insertions(+), 51 deletions(-) diff --git a/folly/experimental/Gen-inl.h b/folly/experimental/Gen-inl.h index 037c330c..5079ae22 100644 --- a/folly/experimental/Gen-inl.h +++ b/folly/experimental/Gen-inl.h @@ -93,21 +93,48 @@ class Operator : public FBounded { class ResultGen = void> ResultGen compose(const GenImpl& source) const; - /** - * operator|() - For composing two operators without binding it to a - * particular generator. - */ - template> - Composed operator|(const Operator& op) const { - return Composed(this->self(), op.self()); - } protected: Operator() = default; Operator(const Operator&) = default; Operator(Operator&&) = default; }; +/** + * operator|() - For composing two operators without binding it to a + * particular generator. + */ +template> +Composed operator|(const Operator& left, + const Operator& right) { + return Composed(left.self(), right.self()); +} + +template> +Composed operator|(const Operator& left, + Operator&& right) { + return Composed(left.self(), std::move(right.self())); +} + +template> +Composed operator|(Operator&& left, + const Operator& right) { + return Composed(std::move(left.self()), right.self()); +} + +template> +Composed operator|(Operator&& left, + Operator&& right) { + return Composed(std::move(left.self()), std::move(right.self())); +} + /** * GenImpl - Core abstraction of a generator, an object which produces values by * passing them to a given handler lambda. All generator implementations must @@ -145,18 +172,60 @@ class GenImpl : public FBounded { return true; }); } - - template, - class ValueNext> - Chain operator+(const GenImpl& next) const { - static_assert( - std::is_same::value, - "Generators may ony be combined if Values are the exact same type."); - return Chain(*this, next); - } }; +template> +Chain operator+(const GenImpl& left, + const GenImpl& right) { + static_assert( + std::is_same::value, + "Generators may ony be combined if Values are the exact same type."); + return Chain(left.self(), right.self()); +} + +template> +Chain operator+(const GenImpl& left, + GenImpl&& right) { + static_assert( + std::is_same::value, + "Generators may ony be combined if Values are the exact same type."); + return Chain(left.self(), std::move(right.self())); +} + +template> +Chain operator+(GenImpl&& left, + const GenImpl& right) { + static_assert( + std::is_same::value, + "Generators may ony be combined if Values are the exact same type."); + return Chain(std::move(left.self()), right.self()); +} + +template> +Chain operator+(GenImpl&& left, + GenImpl&& right) { + static_assert( + std::is_same::value, + "Generators may ony be combined if Values are the exact same type."); + return Chain(std::move(left.self()), std::move(right.self())); +} + /** * operator|() which enables foreach-like usage: * gen | [](Value v) -> void {...}; @@ -193,7 +262,15 @@ template auto operator|(const GenImpl& gen, const Operator& op) -> decltype(op.self().compose(gen)) { - return op.self().compose(gen); + return op.self().compose(gen.self()); +} + +template +auto operator|(GenImpl&& gen, const Operator& op) -> +decltype(op.self().compose(std::move(gen.self()))) { + return op.self().compose(std::move(gen.self())); } namespace detail { @@ -377,10 +454,9 @@ class Chain : public GenImpl& first, - const GenImpl& second) - : first_(first.self()) - , second_(second.self()) {} + explicit Chain(First first, Second second) + : first_(std::move(first)) + , second_(std::move(second)) {} template bool apply(Handler&& handler) const { @@ -403,8 +479,8 @@ template class Yield : public GenImpl> { const Source source_; public: - explicit Yield(const Source& source) - : source_(source) { + explicit Yield(Source source) + : source_(std::move(source)) { } template @@ -460,8 +536,8 @@ class Map : public Operator> { const Source source_; const Predicate pred_; public: - explicit Generator(const Source& source, const Predicate& pred) - : source_(source), pred_(pred) {} + explicit Generator(Source source, const Predicate& pred) + : source_(std::move(source)), pred_(pred) {} template void foreach(Body&& body) const { @@ -478,6 +554,13 @@ class Map : public Operator> { } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), predicate_); + } + template> @@ -511,8 +594,8 @@ class Filter : public Operator> { const Source source_; const Predicate pred_; public: - explicit Generator(const Source& source, const Predicate& pred) - : source_(source), pred_(pred) {} + explicit Generator(Source source, const Predicate& pred) + : source_(std::move(source)), pred_(pred) {} template void foreach(Body&& body) const { @@ -534,6 +617,13 @@ class Filter : public Operator> { } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), predicate_); + } + template> @@ -567,8 +657,8 @@ class Until : public Operator> { const Source source_; const Predicate pred_; public: - explicit Generator(const Source& source, const Predicate& pred) - : source_(source), pred_(pred) {} + explicit Generator(Source source, const Predicate& pred) + : source_(std::move(source)), pred_(pred) {} template bool apply(Handler&& handler) const { @@ -579,6 +669,13 @@ class Until : public Operator> { } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), predicate_); + } + template> @@ -609,8 +706,8 @@ public: const Source source_; const size_t count_; public: - explicit Generator(const Source& source, size_t count) - : source_(source) , count_(count) {} + explicit Generator(Source source, size_t count) + : source_(std::move(source)) , count_(count) {} template bool apply(Handler&& handler) const { @@ -625,6 +722,13 @@ public: } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), count_); + } + template> @@ -655,8 +759,8 @@ public: const Source source_; const size_t count_; public: - explicit Generator(const Source& source, size_t count) - : source_(source) , count_(count) {} + explicit Generator(Source source, size_t count) + : source_(std::move(source)) , count_(count) {} template void foreach(Body&& body) const { @@ -690,6 +794,13 @@ public: } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), count_); + } + template> @@ -742,10 +853,12 @@ class Order : public Operator> { return std::move(vals); } public: - Generator(const Source& source, + Generator(Source source, const Selector& selector, const Comparer& comparer) - : source_(source) , selector_(selector) , comparer_(comparer) {} + : source_(std::move(source)), + selector_(selector), + comparer_(comparer) {} VectorType operator|(const Collect&) const { return asVector(); @@ -781,6 +894,13 @@ class Order : public Operator> { } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self()), selector_, comparer_); + } + template> @@ -807,9 +927,9 @@ class Composed : public Operator> { const Second second_; public: Composed() {} - Composed(const First& first, const Second& second) - : first_(first) - , second_(second) {} + Composed(First first, Second second) + : first_(std::move(first)) + , second_(std::move(second)) {} template> { SecondRet compose(const GenImpl& source) const { return second_.compose(first_.compose(source.self())); } + + template() + .compose(std::declval())), + class SecondRet = decltype(std::declval() + .compose(std::declval()))> + SecondRet compose(GenImpl&& source) const { + return second_.compose(first_.compose(std::move(source.self()))); + } }; /* @@ -1143,8 +1273,8 @@ public: public GenImpl> { const Source source_; public: - explicit Generator(const Source& source) - : source_(source) {} + explicit Generator(Source source) + : source_(std::move(source)) {} template bool apply(Handler&& handler) const { @@ -1161,6 +1291,13 @@ public: } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self())); + } + template> @@ -1190,8 +1327,8 @@ public: : public GenImpl> { const Source source_; public: - Generator(const Source& source) - : source_(source) {} + explicit Generator(Source source) + : source_(std::move(source)) {} template void foreach(Body&& body) const { @@ -1215,6 +1352,13 @@ public: } }; + template> + Gen compose(GenImpl&& source) const { + return Gen(std::move(source.self())); + } + template> @@ -1251,8 +1395,8 @@ class VirtualGen : public GenImpl> { class WrapperImpl : public WrapperBase { const Wrapped wrapped_; public: - WrapperImpl(const Wrapped& wrapped) - : wrapped_(wrapped) { + explicit WrapperImpl(Wrapped wrapped) + : wrapped_(std::move(wrapped)) { } virtual bool apply(const std::function& handler) const { @@ -1271,10 +1415,9 @@ class VirtualGen : public GenImpl> { std::unique_ptr wrapper_; public: - template - /* implicit */ VirtualGen(const GenImpl& source) - : wrapper_(new WrapperImpl(source.self())) + template + /* implicit */ VirtualGen(Self source) + : wrapper_(new WrapperImpl(std::move(source))) { } VirtualGen(VirtualGen&& source) -- 2.34.1