Promise
API reference
rili::Context and rili::TaskGuard usage example
#include <memory>
#include <rili/Context.hpp>
#include <rili/TaskGuard.hpp>
namespace working_but_ugly_solution {
struct Something {};
void async_makeSomething(std::function<void(Something const&)> const& callback) noexcept;
void doSomething(std::function<void(Something const&)> jobToBeDone) {
rili::Context& context = rili::Context::get();
context.reserve();
async_makeSomething(
[&context, jobToBeDone](Something const& something) {
context.schedule([something, jobToBeDone]() { jobToBeDone(something); });
context.release();
});
}
}
namespace much_better_solution {
struct SomethingElse {};
void async_makeSomethingElse(std::function<void(SomethingElse const&)> const& callback);
void doSomethingElse(std::function<void(SomethingElse const&)> jobToBeDone) {
std::shared_ptr<rili::TaskGuard> guard(std::make_shared<rili::TaskGuard>(
rili::Context::get()));
async_makeSomethingElse(
[guard, jobToBeDone](SomethingElse const& somethingElse) {
guard->context().schedule([somethingElse, jobToBeDone]() { jobToBeDone(somethingElse); });
});
}
}
rili::ComplexException usage example
#include <iostream>
#include <rili/ComplexException.hpp>
int operation1(int const& data);
int operation2(int const& data);
int complexOperation(int const& data) {
rili::ComplexException exception("complexOperation(int const&)", "Operation cannot be completed!");
int result = data;
try {
result += operation1(result);
} catch (...) {
exception.addException(std::current_exception());
try {
result += operation2(result);
} catch (...) {
exception.addException(std::current_exception());
}
}
if (exception.size() > 1) {
throw exception;
}
return result;
}
void use() {
try {
std::cout << complexOperation(5) << std::endl;
} catch (rili::ComplexException const& err) {
std::cerr << err.what() << std::endl;
for (auto const& e : err) {
try {
std::rethrow_exception(e);
} catch (std::exception const& exception) {
std::cout << exception.what() << std::endl;
} catch (...) {
std::cout << "unknown exception" << std::endl;
}
}
} catch (std::exception const& err) {
std::cerr << err.what() << std::endl;
}
}