Skip to content

Commit

Permalink
cpp20 update
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed Apr 29, 2024
1 parent 8d02547 commit 4a559af
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 212 deletions.
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"MACORO_FETCH_AUTO": "ON",
"MACORO_CPP_VER": "20",
"MACORO_CPP_VER": "14",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
Expand Down
4 changes: 2 additions & 2 deletions cmake/macoroFindDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if((MACORO_FETCH_AUTO OR MACORO_FETCH_OPTIONAL) AND MACORO_BUILD)
include("${CMAKE_CURRENT_LIST_DIR}/../thirdparty/getOptionalLite.cmake")
endif()

if(MACORO_OPTIONAL_LITE)
if(MACORO_OPTIONAL_LITE_V)
FIND_OPTIONAL(REQUIRED)
endif()

Expand Down Expand Up @@ -60,7 +60,7 @@ if((MACORO_FETCH_AUTO OR MACORO_FETCH_OPTIONAL) AND MACORO_BUILD)

include("${CMAKE_CURRENT_LIST_DIR}/../thirdparty/getVariantLite.cmake")
endif()
if(MACORO_VARIANT_LITE)
if(MACORO_VARIANT_LITE_V)
FIND_VARIANT(REQUIRED)
endif()

Expand Down
209 changes: 0 additions & 209 deletions tests/when_all_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,214 +80,5 @@ namespace macoro
}

}


#ifdef MACORO_CPP_20
#define MACORO_MAKE_blocking2_20 1
#endif


namespace detail
{
struct blocking2_task;


struct blocking2_promise
{
int* mVal = nullptr;

blocking2_task get_return_object() noexcept;

using reference_type = int&;
void return_value(reference_type v) noexcept
{
mVal = std::addressof(v);
}

reference_type value()
{
if (this->exception)
std::rethrow_exception(this->exception);
return static_cast<reference_type>(*mVal);
}

struct final_awaiter
{
final_awaiter() noexcept{};
final_awaiter(const final_awaiter&) noexcept {};
final_awaiter(final_awaiter&&) noexcept {};

bool await_ready() noexcept { return false; }
#ifdef MACORO_CPP_20
template<typename P>
void await_suspend(std::coroutine_handle<P> h) noexcept { h.promise().set(); }
#endif
template<typename P>
void await_suspend(coroutine_handle<P> h) noexcept { h.promise().set(); }
void await_resume() noexcept {}
};

std::exception_ptr exception;
std::mutex mutex;
std::condition_variable cv;
bool is_set = false;

void wait()
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this] { return is_set; });
}

void set()
{
assert(is_set == false);
std::lock_guard<std::mutex> lock(this->mutex);
this->is_set = true;
this->cv.notify_all();
}


suspend_always initial_suspend() noexcept { return{}; }

final_awaiter final_suspend() noexcept(true) {
return { };
}

void unhandled_exception() noexcept
{
exception = std::current_exception();
}
};

//static_assert(std::is_nothrow_invocable<blocking2_promise::final_suspend, blocking2_promise&>::value, "");
struct blocking2_task
{
using promise_type = blocking2_promise;
coroutine_handle<promise_type> handle;
blocking2_task(coroutine_handle<promise_type> h)
: handle(h)
{}

blocking2_task() = delete;
blocking2_task(blocking2_task&& h) noexcept :handle(std::exchange(h.handle, std::nullptr_t{})) {}
blocking2_task& operator=(blocking2_task&& h) noexcept { handle = std::exchange(h.handle, std::nullptr_t{}); }

~blocking2_task()
{
if (handle)
handle.destroy();
}

void start()
{
handle.resume();
}

decltype(auto) get()
{
handle.promise().wait();
return handle.promise().value();
}
};

blocking2_task blocking2_promise::get_return_object() noexcept
{
return { coroutine_handle<blocking2_promise>::from_promise(*this, coroutine_handle_type::std) };
}


// template<
// typename Awaitable
// >
// blocking2_task
// make_blocking2_task(Awaitable&& awaitable)
// {
//#if MACORO_MAKE_blocking2_20
// //auto promise = co_await typename blocking2_promise<ResultType>::get_promise;
// //auto awaiter = promise->yield_value(co_await std::forward<Awaitable>(awaitable));
// //co_await awaiter;
// co_return co_await static_cast<Awaitable&&>(awaitable);
//#else
// MC_BEGIN(blocking2_task<ResultType>, &awaitable);
// MC_RETURN_AWAIT(static_cast<Awaitable&&>(awaitable));
// MC_END();
//
//#endif
// }

// template<
// typename Awaitable,
// typename ResultType = typename awaitable_traits<Awaitable&&>::await_result>
// enable_if_t<std::is_void<ResultType>::value,
// blocking2_task<ResultType>
// >
// make_blocking2_task(Awaitable&& awaitable)
// {
//#if MACORO_MAKE_blocking2_20
// co_await std::forward<Awaitable>(awaitable);
//#else
// MC_BEGIN(blocking2_task<ResultType>, &awaitable);
// MC_AWAIT(static_cast<Awaitable&&>(awaitable));
// MC_END();
//#endif
// }
}


//template<typename Awaitable>
//typename awaitable_traits<Awaitable&&>::await_result
// sync_wait2(Awaitable&& awaitable)
//{
// auto task = detail::make_blocking2_task<Awaitable&&>(std::forward<Awaitable>(awaitable));
// task.start();
// return task.get();
//}


//struct sync_wait2_t
//{
//};

//inline sync_wait2_t sync_wait2()
//{
// return {};
//}


//template<typename awaitable>
//decltype(auto) operator|(awaitable&& a, sync_wait2_t)
//{
// return sync_wait2(std::forward<awaitable>(a));
//}


void when_all_basic2_tests()
{

auto f = []() -> task<int> {
MC_BEGIN(task<int>);
MC_RETURN(42);
MC_END();
};

auto g = []() -> task<bool> {
MC_BEGIN(task<bool>);
MC_RETURN(true);
MC_END();
};

using F = decltype(f());
using Awaitable = F;
using ResultType = int;

auto task = [&]() -> detail::blocking2_task {
auto i = co_await f();
co_return i;
}();
task.start();
auto i = task.get();

}

}

0 comments on commit 4a559af

Please sign in to comment.