Skip to content

Commit

Permalink
allow disable exception handling for use with MCU
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Dec 21, 2024
1 parent 2bd8707 commit 007e855
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions include/ucoro/awaitable.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
//
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#pragma once

#include <concepts>
#include <any>
#include <cassert>
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <atomic>
#ifdef DISABLE_EXCEPTION
#include <optional>
#else
#include <variant>
#endif

#if defined(__has_include)

Expand All @@ -30,14 +40,7 @@ namespace std

#endif

#include <any>
#include <cassert>
#include <cstdlib>
#include <functional>
#include <memory>
#include <type_traits>
#include <atomic>
#include <cassert>


#if defined(DEBUG) || defined(_DEBUG)
#if defined(ENABLE_DEBUG_CORO_LEAK)
Expand Down Expand Up @@ -225,44 +228,58 @@ namespace ucoro

void unhandled_exception() noexcept
{
#ifndef DISABLE_EXCEPTION
value_.template emplace<std::exception_ptr>(std::current_exception());
#endif
}

T get_value() const
{
#ifndef DISABLE_EXCEPTION
if (std::holds_alternative<std::exception_ptr>(value_))
{
std::rethrow_exception(std::get<std::exception_ptr>(value_));
}

return std::get<T>(value_);
#else
return value_.value();
#endif
}

#ifndef DISABLE_EXCEPTION
std::variant<std::exception_ptr, T> value_{nullptr};
#else
std::optional<T> value_;
#endif
};

//////////////////////////////////////////////////////////////////////////
// 存储协程 promise 的返回值 void 的特化实现
template<>
struct awaitable_promise_value<void>
{
#ifndef DISABLE_EXCEPTION
std::exception_ptr exception_{nullptr};

#endif
constexpr void return_void() noexcept
{
}

void unhandled_exception() noexcept
{
#ifndef DISABLE_EXCEPTION
exception_ = std::current_exception();
#endif
}

void get_value() const
{
#ifndef DISABLE_EXCEPTION
if (exception_)
{
std::rethrow_exception(exception_);
}
#endif
}
};

Expand Down

0 comments on commit 007e855

Please sign in to comment.