/* /~` _ _ _|_. _ _ |_ | _ \_,(_)| | | || ||_|(_||_)|(/_ https://github.com/Naios/continuable v4.0.0 Copyright(c) 2015 - 2018 Denis Blank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **/ #ifndef CONTINUABLE_DETAIL_CONNECTION_SEQ_HPP_INCLUDED #define CONTINUABLE_DETAIL_CONNECTION_SEQ_HPP_INCLUDED #include #include #include #include #include #include #include #include #include #include #include namespace cti { namespace detail { namespace connection { namespace seq { /// Connects the left and the right continuable to a sequence /// /// \note This is implemented in an eager way because we would not gain /// any profit from chaining sequences lazily. template auto sequential_connect(Left&& left, Right&& right) { left.freeze(right.is_frozen()); right.freeze(); return std::forward(left).then( [right = std::forward(right)](auto&&... args) mutable { return std::move(right).then( [previous = std::make_tuple(std::forward(args)...)]( auto&&... args) mutable { return std::tuple_cat( std::move(previous), std::make_tuple(std::forward(args)...)); }); }); } template struct sequential_dispatch_data { Callback callback; Box box; }; template class sequential_dispatch_visitor : public std::enable_shared_from_this>, public util::non_movable { Data data_; public: explicit sequential_dispatch_visitor(Data&& data) : data_(std::move(data)) { } virtual ~sequential_dispatch_visitor() = default; /// Returns the pack that should be traversed auto& head() { return data_.box; } template >::value>* = nullptr> bool operator()(async_traverse_visit_tag, Box&& box) { if (base::attorney::is_ready(box.peek())) { // The result can be resolved directly traits::unpack( [&](auto&&... args) mutable { box.assign(std::forward(args)...); }, base::attorney::query(box.fetch())); return true; } else { return false; } } template void operator()(async_traverse_detach_tag, Box&& box, N&& next) { box.fetch() .then([box = std::addressof(box), next = std::forward(next)](auto&&... args) mutable { // Assign the result to the target box->assign(std::forward(args)...); // Continue the asynchronous sequential traversal next(); }) .fail([me = this->shared_from_this()](exception_t exception) { // Abort the traversal when an error occurred std::move(me->data_.callback)(exception_arg_t{}, std::move(exception)); }) .done(); } template void operator()(async_traverse_complete_tag, T&& /*pack*/) { return aggregated::finalize_data(std::move(data_.callback), std::move(data_.box)); } }; } // namespace seq struct connection_strategy_seq_tag {}; template <> struct is_connection_strategy // ... : std::true_type {}; /// Finalizes the seq logic of a given connection template <> struct connection_finalizer { /// Finalizes the all logic of a given connection template static auto finalize(Connection&& connection, util::ownership ownership) { auto result = aggregated::box_continuables(std::forward(connection)); auto signature = aggregated::hint_of_data(); return base::attorney::create_from( [result = std::move(result)](auto&& callback) mutable { // The data from which the visitor is constructed in-place using data_t = seq::sequential_dispatch_data, std::decay_t>; // The visitor type using visitor_t = seq::sequential_dispatch_visitor; traverse_pack_async(async_traverse_in_place_tag{}, data_t{std::forward(callback), std::move(result)}); }, signature, std::move(ownership)); } }; } // namespace connection /// Specialization for a connection annotation template <> struct annotation_trait : connection::connection_annotation_trait< connection::connection_strategy_seq_tag> {}; } // namespace detail } // namespace cti #endif // CONTINUABLE_DETAIL_CONNECTION_SEQ_HPP_INCLUDED