mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 01:06:44 +08:00
76 lines
2.9 KiB
Plaintext
76 lines
2.9 KiB
Plaintext
/*
|
|
Copyright(c) 2015 - 2019 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
|
|
or \link continuable_base::operator | its operator | \endlink.
|
|
|
|
A transformation accepts a \ref continuable_base and returns
|
|
an arbitrary object.
|
|
|
|
To create a transformation use the \ref make_transform function:
|
|
|
|
\code{.cpp}
|
|
auto transform = cti::make_transform([] (auto&& continuable) {
|
|
// Change the continuable
|
|
return std::forward<decltype(continuable)>(continuable);
|
|
});
|
|
\endcode
|
|
|
|
The library provides several transforms already as part of the
|
|
\ref cti::transforms namespace.
|
|
|
|
\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::futurize 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::futurize());
|
|
// ^^^^^^^^
|
|
\endcode
|
|
|
|
Multiple arguments which can't be handled by `std::future` itself are
|
|
converted into `std::tuple`, see \ref transforms::futurize 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::futurize());
|
|
\endcode
|
|
*/
|
|
}
|