Tutorial » Transforming continuables

Explains the conversion into other types such as std::future.

Transforms in general

Sometimes it's required to change a continuable_base object by its whole. Thus the library offers the ability to apply a transformation to any continuable_base through using apply or its operator |.

A transformation accepts a continuable_base and returns an arbitrary object.

To create a transformation use the make_transform function:

auto transform = cti::make_transform([] (auto&& continuable) {
  // Change the continuable
  return std::forward<decltype(continuable)>(continuable);
});

The library provides several transforms already as part of the cti::transforms namespace.

Conversion into std::future

The library is capable of converting (futurizing) every continuable into a fitting std::future through the transforms::futurize transform:

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());
// ^^^^^^^^

Multiple arguments which can't be handled by std::future itself are converted into std::tuple, see transforms::futurize for details.

std::future<std::tuple<std::string, std::string>> future =
  (http_request("travis-ci.org") && http_request("atom.io"))
    .apply(cti::transforms::futurize());