mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 08:46:44 +08:00
* The logic requires now that all continuables yield the same types and amount of result objects.
179 lines
5.1 KiB
C++
179 lines
5.1 KiB
C++
/*
|
|
Copyright(c) 2015 - 2018 Denis Blank <denis.blank at outlook dot com>
|
|
|
|
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.
|
|
**/
|
|
|
|
#include <string>
|
|
#include <system_error>
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
#include <continuable/continuable.hpp>
|
|
|
|
/*
|
|
static cti::continuable<std::string> http_request(std::string url) {
|
|
return [url = std::move(url)](cti::promise<std::string> promise) {
|
|
if (false) {
|
|
promise.set_value("");
|
|
std::forward<decltype(promise)>(promise)("");
|
|
}
|
|
promise.set_exception(std::error_condition{});
|
|
};
|
|
}
|
|
|
|
static auto http_request2(std::string url) {
|
|
return cti::make_continuable<std::string>(
|
|
// ...
|
|
[url = std::move(url)](auto&& promise) {
|
|
if (false) {
|
|
promise.set_value("");
|
|
std::forward<decltype(promise)>(promise)("");
|
|
}
|
|
promise.set_exception(std::error_condition{});
|
|
});
|
|
}
|
|
|
|
static cti::continuable<std::string> http_request3(std::string url) {
|
|
return [url = std::move(url)](auto&& promise) {
|
|
if (false) {
|
|
promise.set_value("");
|
|
std::forward<decltype(promise)>(promise)("");
|
|
}
|
|
promise.set_exception(std::error_condition{});
|
|
};
|
|
}
|
|
|
|
struct my_callable {
|
|
void operator()(std::string) && {
|
|
// ...
|
|
}
|
|
void operator()(cti::dispatch_error_tag, cti::error_type) && {
|
|
// ...
|
|
}
|
|
};
|
|
|
|
void old() {
|
|
http_request("github.com").next(my_callable{});
|
|
|
|
http_request("github.com") | [](std::string) {
|
|
// ...
|
|
return 0;
|
|
} | [] {
|
|
// ...
|
|
};
|
|
|
|
http_request2("github.com")
|
|
.then([](std::string) {
|
|
// ...
|
|
return 0;
|
|
})
|
|
.then([](int) {
|
|
// ...
|
|
})
|
|
.fail([](std::error_condition) {
|
|
// ...
|
|
});
|
|
|
|
(http_request("github.com") && http_request3("github.com"))
|
|
.then([](std::string, std::string) {
|
|
// ...
|
|
})
|
|
.fail([](std::error_condition) {
|
|
// ...
|
|
})
|
|
.apply([](auto&& me) {
|
|
// ...
|
|
return std::forward<decltype(me)>(me);
|
|
});
|
|
|
|
(http_request("github.com") || http_request("github.com"))
|
|
.then([](std::string) {
|
|
// ...
|
|
})
|
|
.fail([](std::error_condition) {
|
|
// ...
|
|
});
|
|
|
|
(http_request("github.com") >> http_request("github.com"))
|
|
.then([](std::string, std::string) {
|
|
// ...
|
|
})
|
|
.fail([](std::error_condition) {
|
|
// ...
|
|
});
|
|
}
|
|
*/
|
|
|
|
int main(int, char**) {
|
|
using namespace cti::detail;
|
|
|
|
/*cti::when_seq(
|
|
cti::make_ready_continuable(0, 1), 2, //< See this plain value
|
|
std::vector<cti::continuable<int>>{cti::make_ready_continuable(3),
|
|
cti::make_ready_continuable(4)},
|
|
std::make_tuple(std::make_tuple(cti::make_ready_continuable(5))))
|
|
.then([](int r0, int r1, int r2, std::vector<int> r34,
|
|
std::tuple<std::tuple<int>> r5) {
|
|
// ...
|
|
util::unused(r0, r1, r2, r34, r5);
|
|
});
|
|
|
|
std::vector<cti::continuable<int>> v{cti::make_ready_continuable(8),
|
|
cti::make_ready_continuable(9)};
|
|
|
|
cti::when_seq(v.begin(), v.end()).then([](auto o2) {
|
|
// ...
|
|
util::unused(o2);
|
|
});
|
|
|
|
cti::when_seq(cti::make_ready_continuable()) // ...
|
|
.then([] {
|
|
// ...
|
|
});
|
|
|
|
cti::when_seq() // ...
|
|
.then([] {
|
|
// ...
|
|
});
|
|
|
|
cti::when_seq(cti::make_exceptional_continuable<void>(std::error_condition{}))
|
|
.fail([](auto) {
|
|
// ...
|
|
});
|
|
|
|
cti::when_all(
|
|
cti::make_ready_continuable(0, 1), 2, //< See this plain value
|
|
std::vector<cti::continuable<int>>{cti::make_ready_continuable(3),
|
|
cti::make_ready_continuable(4)},
|
|
std::make_tuple(std::make_tuple(cti::make_ready_continuable(5))))
|
|
.then([](int r0, int r1, int r2, std::vector<int> r34,
|
|
std::tuple<std::tuple<int>> r5) {
|
|
// ...
|
|
util::unused(r0, r1, r2, r34, r5);
|
|
});*/
|
|
|
|
composition::apply_composition(composition::composition_strategy_any_tag{},
|
|
cti::make_ready_continuable(22),
|
|
cti::make_ready_continuable(44))
|
|
.then([](int) {
|
|
|
|
});
|
|
}
|