mirror of
https://github.com/Naios/continuable.git
synced 2026-02-07 18:26:40 +08:00
use own trait to box continuables
This commit is contained in:
parent
9c4a664ce0
commit
005ccf4e49
@ -135,6 +135,32 @@ namespace detail
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pack a continuable into the continuable returning functional type.
|
||||||
|
template<typename _CTy>
|
||||||
|
static auto box_continuable(_CTy&& continuable)
|
||||||
|
-> typename std::enable_if<is_continuable<typename std::decay<_CTy>::type>::value,
|
||||||
|
std::function<typename std::decay<_CTy>::type(_ATy...)>>::type
|
||||||
|
{
|
||||||
|
// Trick C++11 lambda capture rules for non copyable but moveable continuables.
|
||||||
|
std::shared_ptr<typename std::decay<_CTy>::type> shared_continuable =
|
||||||
|
std::make_shared<typename std::decay<_CTy>::type>(std::forward<_CTy>(continuable));
|
||||||
|
|
||||||
|
// Create a fake function which returns the value on invoke.
|
||||||
|
return [shared_continuable](_ATy...)
|
||||||
|
{
|
||||||
|
return std::move(*shared_continuable);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do nothing if already a non continuable type
|
||||||
|
template<typename _CTy>
|
||||||
|
static auto box_continuable(_CTy&& continuable)
|
||||||
|
-> typename std::enable_if<!is_continuable<typename std::decay<_CTy>::type>::value,
|
||||||
|
typename std::decay<_CTy>::type>::type
|
||||||
|
{
|
||||||
|
return continuable;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// Deleted copy construct
|
/// Deleted copy construct
|
||||||
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
||||||
@ -190,8 +216,7 @@ namespace detail
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then implementation of eval functionals.
|
/// Waits for this continuable and invokes the given callback.
|
||||||
// Enable if the given type isn't a continuable.
|
|
||||||
template<typename _CTy>
|
template<typename _CTy>
|
||||||
auto then(_CTy&& functional)
|
auto then(_CTy&& functional)
|
||||||
-> typename std::enable_if<!is_continuable<typename std::decay<_CTy>::type>::value,
|
-> typename std::enable_if<!is_continuable<typename std::decay<_CTy>::type>::value,
|
||||||
@ -214,7 +239,7 @@ namespace detail
|
|||||||
}, std::move(*this));
|
}, std::move(*this));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The continuable itself
|
/// Waits for this continuable and continues with the given one.
|
||||||
template<typename _CTy>
|
template<typename _CTy>
|
||||||
auto then(_CTy&& continuable)
|
auto then(_CTy&& continuable)
|
||||||
-> typename std::enable_if<is_continuable<typename std::decay<_CTy>::type>::value,
|
-> typename std::enable_if<is_continuable<typename std::decay<_CTy>::type>::value,
|
||||||
@ -223,15 +248,7 @@ namespace detail
|
|||||||
static_assert(std::is_rvalue_reference<_CTy&&>::value,
|
static_assert(std::is_rvalue_reference<_CTy&&>::value,
|
||||||
"Given continuable must be passed as r-value!");
|
"Given continuable must be passed as r-value!");
|
||||||
|
|
||||||
// Trick C++11 lambda capture rules for non copyable but moveable continuables.
|
return then(box_continuable(std::forward<_CTy>(continuable)));
|
||||||
std::shared_ptr<typename std::decay<_CTy>::type> shared_continuable =
|
|
||||||
std::make_shared<typename std::decay<_CTy>::type>(std::forward<_CTy>(continuable));
|
|
||||||
|
|
||||||
// Create a fake function which returns the value on invoke.
|
|
||||||
return then([shared_continuable](_ATy...)
|
|
||||||
{
|
|
||||||
return std::move(*shared_continuable);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Placeholder
|
/// Placeholder
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user