Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sample coro #2126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

cmake_policy(SET CMP0025 NEW)
Expand Down
88 changes: 87 additions & 1 deletion src/mavsdk/plugins/action/include/plugins/action/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <array>
#include <cmath>
#include <coroutine>
#include <exception>
#include <functional>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -131,7 +133,91 @@ class Action : public PluginBase {
*
* @return Result of request.
*/
Result arm() const;
[[nodiscard]] Result arm() const;


/*struct ResultCoro {
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;

struct promise_type {
ResultCoro get_return_object() {
return ResultCoro {
handle_type::from_promise(*this)
};
}
std::suspend_never initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() { exception_ = std::current_exception(); }
void return_void() {}
std::suspend_always yield_value(Result value) {
value_ = value;
return {};
}

Result value_{};
std::exception_ptr exception_;
};

ResultCoro(handle_type h) : h_(h) {}
ResultCoro(const ResultCoro &) = delete;
~ResultCoro() { h_.destroy(); }

handle_type h_;
operator handle_type() const { return h_; }
};*/

struct task
{
struct promise_type
{
task get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_value(Result& result)
{
_result = result;
}
void unhandled_exception() {}

Result _result;
};
};

auto arm_coro_await()
{
struct awaiter : public std::suspend_always
{
awaiter(Action* plugin)
: _plugin(plugin)
{}

bool await_ready() { return true; }
void await_suspend(std::coroutine_handle<> handle)
{
// use your third party lib call directly here.
_plugin->arm_async([handle, this](Result result)
{
_result = result;
// call the handle to resume the coroutine
handle();
});
}
void await_resume() {}

Action* _plugin;
Result _result{};
};
return awaiter{this};
}

task
arm_coro()
{
auto awaiter = arm_coro_await();
co_await awaiter;
co_return awaiter._result;
}

/**
* @brief Send command to disarm the drone.
Expand Down
Loading