Files

192 lines
5.5 KiB
C++

/**
* @file include/ActivMiddleware/CxxYZ/future.h
*
* @brief Header file for easing C++11 (or later) <future> features across compilers.
*/
#if (!defined ACTIV_CXXYZ_FUTURE_H)
#define ACTIV_CXXYZ_FUTURE_H
#include "ActivMiddleware/ActivMiddleware.h"
#include "ActivMiddleware/CxxYZ/chrono.h"
// For Boost::native_value, given Boost::future uses their scoped-enum emulation.
#include ACTIV_BOOST_INCLUDE(core/scoped_enum.hpp)
#if (defined ACTIV_BOOST_NO_CXX11_HDR_FUTURE) || (defined ACTIV_CXXYZ_CHRONO_USE_BOOST)
#include ACTIV_BOOST_INCLUDE(thread/future.hpp)
#define ACTIV_CXXYZ_FUTURE_NAMESPACE Boost
#else
#include <future>
#define ACTIV_CXXYZ_FUTURE_NAMESPACE std
#endif
//! Activ namespace.
namespace Activ
{
//! CxxYZ namespace.
namespace CxxYZ
{
using ACTIV_CXXYZ_FUTURE_NAMESPACE::future;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::future_errc;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::future_error;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::future_status;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::packaged_task;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::promise;
using ACTIV_CXXYZ_FUTURE_NAMESPACE::future_category;
} // namespace CxxYZ
} // namespace Activ
#undef ACTIV_CXXYZ_FUTURE_NAMESPACE
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_BOOST_NO_VARIADIC_TEMPLATES)
#include "ActivMiddleware/CxxYZ/exception.h"
#include "ActivMiddleware/CxxYZ/functional.h"
#include "ActivMiddleware/CxxYZ/utility.h"
#include ACTIV_BOOST_INCLUDE(function_types/result_type.hpp)
#include ACTIV_BOOST_INCLUDE(preprocessor/arithmetic/inc.hpp)
#include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma_if.hpp)
#include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat_from_to.hpp)
#define ACTIV_CXXYZ_FUTURE_PARAMETERIZED_PACKAGED_TASK_MAX_ARITY 5
//! Activ namespace.
namespace Activ
{
/**
* @brief std::packaged_task-like class. Boost::packaged_task without variadic templates doesn't support parameters.
*
* @tparam T function type being wrapped, of the form "RETURN_TYPE (PARAMETER1_TYPE, PARAMETER2_TYPE, ...)".
*
* Note the variadic nature of the call operators do not account for references; when calling, wrap with
* CxxYZ::ref() or CxxYZ::cref() as appropriate.
*/
template<typename T>
class ParameterizedPackagedTask
{
typedef typename Boost::function_types::result_type<T>::type ResultType;
public:
/**
* @brief Constructor.
*
* @tparam F a callable type compatible with T.
*
* @param fn callable entity compatible with T. A copy is taken.
*/
template<typename F>
ParameterizedPackagedTask(const F &fn) :
m_function(fn)
{
}
//! Move constructor.
ParameterizedPackagedTask(ACTIV_BOOST_RV_REF(ParameterizedPackagedTask) rhs) :
m_function(CxxYZ::move(rhs.m_function)),
m_promise(CxxYZ::move(rhs.m_promise))
{
}
//! Move assignment operator.
ParameterizedPackagedTask &operator=(ACTIV_BOOST_RV_REF(ParameterizedPackagedTask) rhs)
{
m_function = CxxYZ::move(rhs.m_function);
m_promise = CxxYZ::move(rhs.m_promise);
return *this;
}
//! Returns the future associated with the result of the task.
CxxYZ::future<ResultType> get_future()
{
return m_promise.get_future();
}
/**
* @brief Invoke the task and store the result in the corresponding future (nullary version).
*
* If the task returns normally, the return value is stored as the asynchronous result.
* Otherwise the exception thrown is stored.
*/
void operator()()
{
try
{
m_promise.set_value(m_function());
}
ACTIV_CATCH_ALL()
{
m_promise.set_exception(CxxYZ::current_exception());
}
}
#define ACTIV_PACKAGED_TASK_TEMPLATE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) typename A##n
#define ACTIV_PACKAGED_TASK_SIGNATURE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) A##n a##n
#define ACTIV_PACKAGED_TASK_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) a##n
#define ACTIV_PACKAGED_TASK_CALL_OPERATOR(z, n, data) \
template<ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_PACKAGED_TASK_TEMPLATE_PARAM, )> \
void operator()(ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_PACKAGED_TASK_SIGNATURE_PARAM, )) \
{ \
try \
{ \
m_promise.set_value(m_function( \
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_PACKAGED_TASK_PARAM, ) \
)); \
} \
ACTIV_CATCH_ALL() \
{ \
m_promise.set_exception(CxxYZ::current_exception()); \
} \
}
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_CXXYZ_FUTURE_PARAMETERIZED_PACKAGED_TASK_MAX_ARITY, ACTIV_PACKAGED_TASK_CALL_OPERATOR, )
#undef ACTIV_PACKAGED_TASK_CALL_OPERATOR
#undef ACTIV_PACKAGED_TASK_PARAM
#undef ACTIV_PACKAGED_TASK_SIGNATURE_PARAM
#undef ACTIV_PACKAGED_TASK_TEMPLATE_PARAM
private:
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ParameterizedPackagedTask)
CxxYZ::function<T> m_function;
CxxYZ::promise<ResultType> m_promise;
};
} // namespace Activ
// ---------------------------------------------------------------------------------------------------------------------------------
#else
//! Activ namespace.
namespace Activ
{
//! Alias for CxxYZ::packaged_task.
template<typename... Ts>
using ParameterizedPackagedTask = CxxYZ::packaged_task<Ts...>;
} // namespace Activ
#endif
#endif // (!defined ACTIV_CXXYZ_FUTURE_H)