Loading...
Searching...
No Matches
future.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_PARALLEL_FUTURE_HH
#define DUNE_COMMON_PARALLEL_FUTURE_HH
#include <memory>
#include <type_traits>
#include <dune/common/exceptions.hh>
namespace Dune{
class InvalidFutureException : public InvalidStateException
{};
// forward declaration
template<class T>
class PseudoFuture;
template<class T>
class Future{
// Future interface:
class FutureBase{
public:
virtual ~FutureBase() = default;
virtual void wait() = 0;
virtual bool ready() const = 0;
virtual bool valid() const = 0;
virtual T get() = 0;
};
// model class
template<class F>
class FutureModel
: public FutureBase
{
F _future;
public:
FutureModel(F&& f)
: _future(std::forward<F>(f))
{}
virtual void wait() override
{
_future.wait();
}
virtual bool ready() const override
{
return _future.ready();
}
virtual bool valid() const override
{
return _future.valid();
}
virtual T get() override{
return (T)_future.get();
}
};
std::unique_ptr<FutureBase> _future;
public:
template<class F>
Future(F&& f)
: _future(std::make_unique<FutureModel<F>>(std::forward<F>(f)))
{}
template<class U, std::enable_if_t<std::is_same<U,T>::value && !std::is_same<T,void>::value>>
Future(U&& data)
: _future(std::make_unique<FutureModel<PseudoFuture<T>>>(PseudoFuture<T>(std::forward<U>(data))))
{}
Future() = default;
void wait(){
_future->wait();
}
T get() {
return _future->get();
}
bool ready() const {
return _future->ready();
}
bool valid() const {
if(_future)
return _future->valid();
return false;
}
};
template<class T>
class PseudoFuture{
bool valid_;
T data_;
public:
PseudoFuture() :
valid_(false)
{}
template<class U>
PseudoFuture(U&& u) :
valid_(true),
data_(std::forward<U>(u))
{}
void wait() {
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
}
bool ready() const {
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
return true;
}
T get() {
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
valid_ = false;
return std::forward<T>(data_);
}
bool valid() const {
return valid_;
}
};
template<>
class PseudoFuture<void>{
bool valid_;
public:
PseudoFuture(bool valid = false) :
valid_(valid)
{}
void wait(){
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
}
bool ready() const{
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
return true;
}
void get(){
if(!valid_)
DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
valid_ = false;
}
bool valid() const{
return valid_;
}
};
}
#endif // DUNE_COMMON_PARALLEL_FUTURE_HH
exceptions.hh
A few common exception classes.
DUNE_THROW
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::InvalidStateException
Default exception if a function was called while the object is not in a valid state for that function...
Definition exceptions.hh:375
Dune::InvalidFutureException
This exception is thrown when ready(), wait() or get() is called on an invalid future....
Definition future.hh:20
Dune::PseudoFuture
A wrapper-class for a object which is ready immediately.
Definition future.hh:124
Dune::PseudoFuture::ready
bool ready() const
Definition future.hh:143
Dune::PseudoFuture::get
T get()
Definition future.hh:149
Dune::PseudoFuture::PseudoFuture
PseudoFuture(U &&u)
Definition future.hh:133
Dune::PseudoFuture::PseudoFuture
PseudoFuture()
Definition future.hh:128
Dune::PseudoFuture::wait
void wait()
Definition future.hh:138
Dune::PseudoFuture::valid
bool valid() const
Definition future.hh:156
Dune::Future::ready
bool ready() const
Definition future.hh:105
Dune::Future::wait
void wait()
wait until the future is ready
Definition future.hh:89
Dune::Future::Future
Future(U &&data)
Definition future.hh:80
Dune::Future::get
T get()
Waits until the future is ready and returns the resulting value.
Definition future.hh:97
Dune::Future::Future
Future()=default
Dune::Future::Future
Future(F &&f)
Definition future.hh:75
Dune::PseudoFuture< void >::ready
bool ready() const
Definition future.hh:173
Dune::PseudoFuture< void >::valid
bool valid() const
Definition future.hh:185
Dune::PseudoFuture< void >::get
void get()
Definition future.hh:179
Dune::PseudoFuture< void >::PseudoFuture
PseudoFuture(bool valid=false)
Definition future.hh:165
Dune::PseudoFuture< void >::wait
void wait()
Definition future.hh:169