Skip to content

Commit

Permalink
add Qt Demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Oct 2, 2024
1 parent cad5784 commit c55a899
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0091 NEW) # required for /MT /MD flag with msvc
project(µcoro)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

Expand Down
7 changes: 6 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ if(Boost_FOUND)
add_subdirectory(test2)
endif(Boost_FOUND)

add_subdirectory(test3)
add_subdirectory(test3)

find_package(Qt6 6.6 COMPONENTS Core)
if(Qt6_FOUND)
add_subdirectory(testqt)
endif(Qt6_FOUND)
10 changes: 10 additions & 0 deletions tests/testqt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

qt_standard_project_setup()

qt_add_executable(testqt testqt.cpp)

set_property(TARGET testqt PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set_target_properties(testqt PROPERTIES FOLDER "ucoro_tests")
target_link_libraries(testqt PRIVATE ucoro Qt6::Core)

add_test(NAME testqt COMMAND testqt)
50 changes: 50 additions & 0 deletions tests/testqt/testqt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@


#include <iostream>
#include "ucoro/awaitable.hpp"
#include <QtCore>
#include <QTimer>
#include <QCoreApplication>

ucoro::awaitable<int> coro_compute_int(int value)
{
auto ret = co_await executor_awaitable<int>([value](auto handle) {
QTimer::singleShot(0, [value, handle = std::move(handle)]() mutable {
std::this_thread::sleep_for(std::chrono::seconds(0));
std::cout << value << " value\n";
handle(value * 100);
});
});

co_return (value + ret);
}

ucoro::awaitable<void> coro_compute_exec(int value)
{
auto ret = co_await coro_compute_int(value);
std::cout << "return: " << ret << std::endl;
co_return;
}

ucoro::awaitable<void> coro_compute()
{
auto x = co_await ucoro::local_storage;
std::cout << "local storage: " << std::any_cast<std::string>(x) << std::endl;

for (auto i = 0; i < 100; i++)
{
co_await coro_compute_exec(i);
}
}

int main(int argc, char **argv)
{
std::string str = "hello";

QCoreApplication app(argc, argv);

coro_start(coro_compute(), str);

app.exec();
return 0;
}

0 comments on commit c55a899

Please sign in to comment.