X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/8cb54d14b2c312911cc049247c702cffb48131cc..504e8ecc4d4245655dafaae23e56e64260b746ad:/include/xbt/future.hpp diff --git a/include/xbt/future.hpp b/include/xbt/future.hpp deleted file mode 100644 index 0c850ee413..0000000000 --- a/include/xbt/future.hpp +++ /dev/null @@ -1,163 +0,0 @@ -/* Copyright (c) 2015-2021. The SimGrid Team. - * All rights reserved. */ - -/* This program is free software; you can redistribute it and/or modify it - * under the terms of the license (GNU LGPL) which comes with this package. */ - -#ifndef XBT_FUTURE_HPP -#define XBT_FUTURE_HPP - -#include - -#include -#include -#include -#include // std::future_error -#include -#include -#include -#include - -namespace simgrid { -namespace xbt { - -/** A value or an exception (or nothing) - * - * This is similar to `optional>`` but it with a Future/Promise - * like API. - * - * Also the name is not so great. - **/ -template -class Result { -public: - bool is_valid() const - { - return value_.which() > 0; - } - void set_exception(std::exception_ptr e) - { - value_ = std::move(e); - } - void set_value(T&& value) - { - value_ = std::move(value); - } - void set_value(T const& value) - { - value_ = value; - } - - /** Extract the value from the future - * - * After this, the value is invalid. - **/ - T get() - { - switch (value_.which()) { - case 1: { - T value = std::move(boost::get(value_)); - value_ = boost::blank(); - return value; - } - case 2: { - std::exception_ptr exception = std::move(boost::get(value_)); - value_ = boost::blank(); - std::rethrow_exception(std::move(exception)); - break; - } - default: - throw std::future_error(std::future_errc::no_state); - } - } - -private: - boost::variant value_; -}; - -template<> -class Result : public Result -{ -public: - void set_value() - { - Result::set_value(nullptr); - } - void get() - { - Result::get(); - } -}; - -template -class Result : public Result> -{ -public: - void set_value(T& value) - { - Result>::set_value(std::ref(value)); - } - T& get() - { - return Result>::get(); - } -}; - -/** Execute some code and set a promise or result accordingly - * - * Roughly this does: - * - *
- *  promise.set_value(code());
- *  
- * - * but it takes care of exceptions and works with `void`. - * - * We might need this when working with generic code because - * the trivial implementation does not work with `void` (before C++1z). - * - * @param code What we want to do - * @param promise Where to want to store the result - */ -template auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value(code())) -{ - try { - promise.set_value(std::forward(code)()); - } catch (...) { - promise.set_exception(std::current_exception()); - } -} - -template auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value()) -{ - try { - std::forward(code)(); - promise.set_value(); - } catch (...) { - promise.set_exception(std::current_exception()); - } -} - -/** Set a promise/result from a future/result - * - * Roughly this does: - * - *
promise.set_value(future);
- * - * but it takes care of exceptions and works with `void`. - * - * We might need this when working with generic code because - * the trivial implementation does not work with `void` (before C++1z). - * - * @param promise output (a valid future or a result) - * @param future input (a ready/waitable future or a valid result) - */ -template inline void set_promise(P& promise, F&& future) -{ - fulfill_promise(promise, [&future] { return std::forward(future).get(); }); -} - -} -} - -#endif