Переглянути джерело

简化when_all/when_any的实现

tags/v2.9.7
tearshark 6 роки тому
джерело
коміт
83a631b6f8
3 змінених файлів з 19 додано та 75 видалено
  1. 7
    7
      .gitignore
  2. 11
    67
      librf/src/when.h
  3. 1
    1
      vs_proj/librf.vcxproj

+ 7
- 7
.gitignore Переглянути файл

################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################
/vs_proj/x64
/vs_proj/.vs
/vs_proj/librf.vcxproj.user
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################
/vs_proj/x64
/vs_proj/.vs
/vs_proj/librf.vcxproj.user

+ 11
- 67
librf/src/when.h Переглянути файл

#include "_awaker.h" #include "_awaker.h"
#include <optional> #include <optional>
#include <any>
#include <variant>
namespace resumef namespace resumef
{ {
template<class _Ty> template<class _Ty>
using remove_future_ot = typename remove_future<_Ty>::optional_type; using remove_future_ot = typename remove_future<_Ty>::optional_type;
template<class _Fty, class _Ty> template<class _Fty, class _Ty>
struct when_one_functor struct when_one_functor
{ {
inline future_vt operator ()() const inline future_vt operator ()() const
{ {
_val.get() = co_await _f;
_e->signal();
}
};
template<class _Fty>
struct when_one_functor<_Fty, void>
{
using value_type = ignore_type;
using future_type = _Fty;
when_impl_ptr _e;
mutable future_type _f;
when_one_functor(const detail::when_impl_ptr & e, future_type f, value_type &)
: _e(e)
, _f(std::move(f))
{}
when_one_functor(when_one_functor &&) = default;
inline future_vt operator ()() const
{
co_await _f;
_e->signal();
}
};
template<class _Fty>
struct when_one_functor<_Fty, ignore_type>
{
using value_type = ignore_type;
using future_type = _Fty;
when_impl_ptr _e;
mutable future_type _f;
when_one_functor(const detail::when_impl_ptr & e, future_type f, value_type &)
: _e(e)
, _f(std::move(f))
{}
when_one_functor(when_one_functor &&) = default;
inline future_vt operator ()() const
{
co_await _f;
_e->signal();
}
};
template<class _Fty>
struct when_one_functor<_Fty, std::optional<ignore_type> >
{
using value_type = std::optional<ignore_type>;
using future_type = _Fty;
when_impl_ptr _e;
mutable future_type _f;
mutable std::reference_wrapper<value_type> _val;
when_one_functor(const detail::when_impl_ptr & e, future_type f, value_type & v)
: _e(e)
, _f(std::move(f))
, _val(v)
{}
when_one_functor(when_one_functor &&) = default;
if constexpr(std::is_same_v<future_type, future_vt>)
{
co_await _f;
_val.get() = std::ignore;
}
else
{
_val.get() = co_await _f;
}
inline future_vt operator ()() const
{
co_await _f;
_val.get() = std::ignore; //让外面感知到optional已经赋值了
_e->signal(); _e->signal();
} }
}; };

+ 1
- 1
vs_proj/librf.vcxproj Переглянути файл

<AdditionalIncludeDirectories>..\librf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\librf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/await</AdditionalOptions> <AdditionalOptions>/await</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp17</LanguageStandard>
<MinimalRebuild /> <MinimalRebuild />
</ClCompile> </ClCompile>
<Link> <Link>

Завантаження…
Відмінити
Зберегти