mirror of
https://github.com/Naios/continuable.git
synced 2026-02-07 18:26:40 +08:00
remove state
This commit is contained in:
parent
5fee7e0574
commit
e13e71ce99
@ -38,15 +38,9 @@ void debug(std::string const& m)
|
|||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
// ContinuableState forward declaration.
|
// Continuable forward declaration.
|
||||||
/// The internal state of the continuable
|
template<typename...>
|
||||||
/// which is used to save certain internal types.
|
class Continuable;
|
||||||
template<typename... Args>
|
|
||||||
class ContinuableState;
|
|
||||||
|
|
||||||
// ContinuableImpl forward declaration.
|
|
||||||
template<typename _STy, typename _CTy>
|
|
||||||
class _ContinuableImpl;
|
|
||||||
|
|
||||||
// convert_void_to_continuable forward declaration.
|
// convert_void_to_continuable forward declaration.
|
||||||
/// Corrects void return types from functional types which should be
|
/// Corrects void return types from functional types which should be
|
||||||
@ -67,23 +61,10 @@ namespace detail
|
|||||||
struct is_continuable
|
struct is_continuable
|
||||||
: public std::false_type { };
|
: public std::false_type { };
|
||||||
|
|
||||||
template<typename _STy, typename _CTy>
|
template<typename... Args>
|
||||||
struct is_continuable<_ContinuableImpl<_STy, _CTy>>
|
struct is_continuable<Continuable<Args...>>
|
||||||
: public std::true_type { };
|
: public std::true_type { };
|
||||||
|
|
||||||
template<typename _WeakContextType>
|
|
||||||
class ContinuableState<_WeakContextType>
|
|
||||||
{
|
|
||||||
template<typename, typename>
|
|
||||||
friend class _ContinuableImpl;
|
|
||||||
|
|
||||||
typedef std::weak_ptr<_WeakContextType> weak_reference_t;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct WeakContext { };
|
|
||||||
|
|
||||||
typedef ContinuableState<WeakContext> DefaultContinuableState;
|
|
||||||
|
|
||||||
// MSVC 12 has issues to detect the parameter pack otherwise.
|
// MSVC 12 has issues to detect the parameter pack otherwise.
|
||||||
template<typename _NextRTy, typename... _NextATy>
|
template<typename _NextRTy, typename... _NextATy>
|
||||||
struct unary_chainer<_NextRTy, fu::identity<_NextATy...>>
|
struct unary_chainer<_NextRTy, fu::identity<_NextATy...>>
|
||||||
@ -138,19 +119,17 @@ namespace detail
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... _STy, typename... _ATy>
|
template<typename... _ATy>
|
||||||
class _ContinuableImpl<ContinuableState<_STy...>, std::function<void(_ATy...)>>
|
class Continuable
|
||||||
{
|
{
|
||||||
// Make all instances of _ContinuableImpl to a friend.
|
// Make all templates of Continuable to a friend.
|
||||||
template<typename, typename>
|
template<typename...>
|
||||||
friend class _ContinuableImpl;
|
friend class Continuable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef Callback<_ATy...> CallbackFunction;
|
typedef Callback<_ATy...> CallbackFunction;
|
||||||
typedef std::function<void(Callback<_ATy...>&&)> ForwardFunction;
|
typedef std::function<void(Callback<_ATy...>&&)> ForwardFunction;
|
||||||
|
|
||||||
typedef typename ContinuableState<_STy...>::weak_reference_t WeakReference;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Functional which expects a callback that is inserted from the Continuable
|
/// Functional which expects a callback that is inserted from the Continuable
|
||||||
/// to chain everything together
|
/// to chain everything together
|
||||||
@ -158,8 +137,6 @@ namespace detail
|
|||||||
|
|
||||||
bool _released;
|
bool _released;
|
||||||
|
|
||||||
WeakReference _reference;
|
|
||||||
|
|
||||||
template <typename _CTy>
|
template <typename _CTy>
|
||||||
void invoke(_CTy&& callback)
|
void invoke(_CTy&& callback)
|
||||||
{
|
{
|
||||||
@ -201,11 +178,10 @@ namespace detail
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/// Deleted copy construct
|
/// Deleted copy construct
|
||||||
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
Continuable(Continuable const&) = delete;
|
||||||
|
|
||||||
/// Move construct
|
/// Move construct
|
||||||
template<typename _RSTy>
|
Continuable(Continuable&& right)
|
||||||
_ContinuableImpl(_ContinuableImpl<_RSTy, Callback<_ATy...>>&& right)
|
|
||||||
: _released(right._released), _callback_insert(std::move(right._callback_insert))
|
: _released(right._released), _callback_insert(std::move(right._callback_insert))
|
||||||
{
|
{
|
||||||
right._released = true;
|
right._released = true;
|
||||||
@ -213,18 +189,18 @@ namespace detail
|
|||||||
|
|
||||||
// Construct through a ForwardFunction
|
// Construct through a ForwardFunction
|
||||||
template<typename _FTy>
|
template<typename _FTy>
|
||||||
_ContinuableImpl(_FTy&& callback_insert)
|
Continuable(_FTy&& callback_insert)
|
||||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(false) { }
|
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(false) { }
|
||||||
|
|
||||||
template<typename _RSTy, typename _RCTy, typename _FTy>
|
template<typename... _RATy, typename _FTy>
|
||||||
_ContinuableImpl(_FTy&& callback_insert, _ContinuableImpl<_RSTy, _RCTy>&& right)
|
Continuable(_FTy&& callback_insert, Continuable<_RATy...>&& right)
|
||||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released)
|
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released)
|
||||||
{
|
{
|
||||||
right._released = true;
|
right._released = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destructor which calls the dispatch chain if needed.
|
/// Destructor which calls the dispatch chain if needed.
|
||||||
~_ContinuableImpl()
|
~Continuable()
|
||||||
{
|
{
|
||||||
// Dispatch everything.
|
// Dispatch everything.
|
||||||
if (!_released)
|
if (!_released)
|
||||||
@ -238,12 +214,10 @@ namespace detail
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Deleted copy assign
|
/// Deleted copy assign
|
||||||
template<typename _RState, typename _RCTy>
|
Continuable& operator= (Continuable const&) = delete;
|
||||||
_ContinuableImpl& operator= (_ContinuableImpl const&) = delete;
|
|
||||||
|
|
||||||
/// Move construct assign
|
/// Move construct assign
|
||||||
template<typename _RSTy>
|
Continuable& operator= (Continuable&& right)
|
||||||
_ContinuableImpl& operator= (_ContinuableImpl<_RSTy, Callback<_ATy...>>&& right)
|
|
||||||
{
|
{
|
||||||
_released = right._released;
|
_released = right._released;
|
||||||
right._released = true;
|
right._released = true;
|
||||||
@ -288,7 +262,7 @@ namespace detail
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename... _CTy>
|
template<typename... _CTy>
|
||||||
_ContinuableImpl& _wrap_all(_CTy&&...)
|
Continuable& _wrap_all(_CTy&&...)
|
||||||
{
|
{
|
||||||
typedef multiple_all_chainer<_CTy...> type;
|
typedef multiple_all_chainer<_CTy...> type;
|
||||||
|
|
||||||
@ -298,14 +272,14 @@ namespace detail
|
|||||||
/// Placeholder
|
/// Placeholder
|
||||||
template<typename... _CTy>
|
template<typename... _CTy>
|
||||||
auto all(_CTy&&... functionals)
|
auto all(_CTy&&... functionals)
|
||||||
-> _ContinuableImpl&
|
-> Continuable&
|
||||||
{
|
{
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Placeholder
|
/// Placeholder
|
||||||
template<typename... _CTy>
|
template<typename... _CTy>
|
||||||
_ContinuableImpl& some(size_t const count, _CTy&&...)
|
Continuable& some(size_t const count, _CTy&&...)
|
||||||
{
|
{
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -313,7 +287,7 @@ namespace detail
|
|||||||
/// Placeholder
|
/// Placeholder
|
||||||
template<typename... _CTy>
|
template<typename... _CTy>
|
||||||
auto any(_CTy&&... functionals)
|
auto any(_CTy&&... functionals)
|
||||||
-> _ContinuableImpl& // FIXME gcc build &-> decltype(some(1, std::declval<_CTy>()...))
|
-> Continuable& // FIXME gcc build &-> decltype(some(1, std::declval<_CTy>()...))
|
||||||
{
|
{
|
||||||
// Equivalent to invoke `some` with count 1.
|
// Equivalent to invoke `some` with count 1.
|
||||||
return some(1, std::forward<_CTy>(functionals)...);
|
return some(1, std::forward<_CTy>(functionals)...);
|
||||||
@ -321,14 +295,14 @@ namespace detail
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
/// Validates the Continuable
|
/// Validates the Continuable
|
||||||
inline _ContinuableImpl& Validate()
|
inline Continuable& Validate()
|
||||||
{
|
{
|
||||||
_released = false;
|
_released = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invalidates the Continuable
|
/// Invalidates the Continuable
|
||||||
inline _ContinuableImpl& Invalidate()
|
inline Continuable& Invalidate()
|
||||||
{
|
{
|
||||||
_released = true;
|
_released = true;
|
||||||
return *this;
|
return *this;
|
||||||
@ -339,7 +313,7 @@ namespace detail
|
|||||||
template<>
|
template<>
|
||||||
struct convert_void_to_continuable<void>
|
struct convert_void_to_continuable<void>
|
||||||
{
|
{
|
||||||
typedef _ContinuableImpl<DefaultContinuableState, Callback<>> type;
|
typedef Continuable<> type;
|
||||||
|
|
||||||
template<typename Fn, typename... Args>
|
template<typename Fn, typename... Args>
|
||||||
static type invoke(Fn functional, Args&&... args)
|
static type invoke(Fn functional, Args&&... args)
|
||||||
@ -355,10 +329,10 @@ namespace detail
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename _State, typename _CTy>
|
template<typename... _CTy>
|
||||||
struct convert_void_to_continuable<_ContinuableImpl<_State, _CTy>>
|
struct convert_void_to_continuable<Continuable<_CTy...>>
|
||||||
{
|
{
|
||||||
typedef _ContinuableImpl<_State, _CTy> type;
|
typedef Continuable<_CTy...> type;
|
||||||
|
|
||||||
template<typename Fn, typename... Args>
|
template<typename Fn, typename... Args>
|
||||||
static type invoke(Fn functional, Args&&... args)
|
static type invoke(Fn functional, Args&&... args)
|
||||||
@ -367,17 +341,10 @@ namespace detail
|
|||||||
return functional(std::forward<Args>(args)...);
|
return functional(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
/// A continuable provides useful methods to react on the result of callbacks
|
/// A continuable provides useful methods to react on the result of callbacks
|
||||||
/// and allows to chain multiple callback calls to a chain.
|
/// and allows to chain multiple callback calls to a chain.
|
||||||
template<typename... Args>
|
|
||||||
using Continuable = detail::_ContinuableImpl<
|
|
||||||
detail::DefaultContinuableState,
|
|
||||||
Callback<Args...>>;
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
template<typename _RTy, typename... _ATy>
|
template<typename _RTy, typename... _ATy>
|
||||||
struct ContinuableFactory;
|
struct ContinuableFactory;
|
||||||
|
|
||||||
@ -399,6 +366,9 @@ namespace detail
|
|||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
using Continuable = detail::Continuable<Args...>;
|
||||||
|
|
||||||
/// Wraps a functional object which expects a r-value callback as argument into a continuable.
|
/// Wraps a functional object which expects a r-value callback as argument into a continuable.
|
||||||
/// The callable is invoked when the continuable shall continue.
|
/// The callable is invoked when the continuable shall continue.
|
||||||
/// For example:
|
/// For example:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user