continuable/doc/tutorial-transforming-continuables.dox
2022-01-20 08:41:32 +01:00

85 lines
3.2 KiB
Plaintext

/*
Copyright(c) 2015 - 2022 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.
*/
namespace cti {
/** \page tutorial-transforming-continuables Transforming continuables
\brief Explains the conversion into other types such as `std::future`.
\tableofcontents
\section tutorial-transforming-continuables-transforms Transforms in general
Sometimes it's required to change a \ref continuable_base object by its whole.
Thus the library offers the ability to apply a transformation to any
\ref continuable_base through using \link continuable_base::apply apply \endlink.
A transformation is a callable object that accepts a \ref continuable_base
and returns an arbitrary object
The library provides several transforms already as part of the
\ref cti::transforms namespace.
\section tutorial-transforming-continuables-wait Synchronous wait
The library is capable of converting every asynchronous control flow
into a synchronous one through \ref transforms::wait, \ref transforms::wait_for
and \ref transforms::wait_until.
\code{.cpp}
std::string response = http_request("github.com")
.apply(cti::transforms::wait());
std::string response = http_request("github.com")
.apply(cti::transforms::wait_for(std::chrono::seconds(5)));
std::string response = http_request("github.com")
.apply(cti::transforms::wait_until(...));
\endcode
The current thread will be blocked until the result has arrived
\section tutorial-transforming-continuables-future Conversion into std::future
The library is capable of converting (*futurizing*) every continuable into a
fitting `std::future` through the \ref transforms::to_future transform:
\code{.cpp}
std::future<std::string> future = http_request("github.com")
.then([](std::string response) {
// Do sth...
return http_request("travis-ci.org") || http_request("atom.io");
})
.apply(cti::transforms::to_future());
// ^^^^^^^^
\endcode
Multiple arguments which can't be handled by `std::future` itself are
converted into `std::tuple`, see \ref transforms::to_future for details.
\code{.cpp}
std::future<std::tuple<std::string, std::string>> future =
(http_request("travis-ci.org") && http_request("atom.io"))
.apply(cti::transforms::to_future());
\endcode
*/
}