From 8deaf1cd3d20d19fa2acf608eb7fede2eb77663e Mon Sep 17 00:00:00 2001 From: Indy Ray Date: Sat, 19 Mar 2022 04:32:46 +0000 Subject: [PATCH 1/4] Initial cmake build support. --- CMakeLists.txt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6ea8b51 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.15) + +project(SquidTasks) + +add_library(${PROJECT_NAME} INTERFACE) + +# There doesn't seem to be a coroutines feature in CMAKE_CXX_KNOWN_FEATURES +target_compile_features(${PROJECT_NAME} INTERFACE "cxx_std_20") +target_compile_options(${PROJECT_NAME} INTERFACE "-fcoroutines") + +target_include_directories(${PROJECT_NAME} INTERFACE include) + +target_sources(${PROJECT_NAME} INTERFACE + include/Task.h + include/TaskManager.h + include/TasksConfig.h + include/TokenList.h + include/TaskFSM.h + include/FunctionGuard.h + ) From 9b573c34223c4584f9010b1a42c802ed8cb99c3d Mon Sep 17 00:00:00 2001 From: Indy Ray Date: Mon, 21 Mar 2022 12:15:45 -0700 Subject: [PATCH 2/4] CMake:Only use -fcoroutines for Clang builds. --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ea8b51..26a39ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,9 @@ add_library(${PROJECT_NAME} INTERFACE) # There doesn't seem to be a coroutines feature in CMAKE_CXX_KNOWN_FEATURES target_compile_features(${PROJECT_NAME} INTERFACE "cxx_std_20") -target_compile_options(${PROJECT_NAME} INTERFACE "-fcoroutines") +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(${PROJECT_NAME} INTERFACE "-fcoroutines") +endif() target_include_directories(${PROJECT_NAME} INTERFACE include) From 1d6bcbb0669c4cdf1ae851dc0044ada5658d3abd Mon Sep 17 00:00:00 2001 From: Indy Ray Date: Tue, 22 Mar 2022 15:05:32 -0700 Subject: [PATCH 3/4] Additional cmake work, adding MSVC option arguments, and adding build samples. --- CMakeLists.txt | 30 +++++++++++++++++++++++--- samples/Sample_Template/CMakeLists.txt | 10 +++++++++ samples/Sample_Tests/CMakeLists.txt | 10 +++++++++ samples/Sample_TextGame/CMakeLists.txt | 12 +++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 samples/Sample_Template/CMakeLists.txt create mode 100644 samples/Sample_Tests/CMakeLists.txt create mode 100644 samples/Sample_TextGame/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 26a39ae..cc48558 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,9 +5,20 @@ project(SquidTasks) add_library(${PROJECT_NAME} INTERFACE) # There doesn't seem to be a coroutines feature in CMAKE_CXX_KNOWN_FEATURES -target_compile_features(${PROJECT_NAME} INTERFACE "cxx_std_20") -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(${PROJECT_NAME} INTERFACE "-fcoroutines") + +# Some errors in Sample_TextGame with std_20 +#target_compile_features(${PROJECT_NAME} INTERFACE "cxx_std_20") + +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(${PROJECT_NAME} INTERFACE "-fcoroutines-ts") +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.10) + # Seems like we should use /await:strict here, but seems to fail. + #target_compile_options(${PROJECT_NAME} INTERFACE "/await:strict") + target_compile_options(${PROJECT_NAME} INTERFACE "/await") + else() + target_compile_options(${PROJECT_NAME} INTERFACE "/await") + endif() endif() target_include_directories(${PROJECT_NAME} INTERFACE include) @@ -19,4 +30,17 @@ target_sources(${PROJECT_NAME} INTERFACE include/TokenList.h include/TaskFSM.h include/FunctionGuard.h + # Private + include/Private/TaskPrivate.h + include/Private/TaskFSMPrivate.h + include/Private/TasksCommonPrivate.h + include/Private/tl/optional.hpp ) + +option(SQUIDTASKS_BUILD_SAMPLES "Enable building with samples." OFF) + +if(SQUIDTASKS_BUILD_SAMPLES) + add_subdirectory(samples/Sample_Tests) + add_subdirectory(samples/Sample_Template) + add_subdirectory(samples/Sample_TextGame) +endif() \ No newline at end of file diff --git a/samples/Sample_Template/CMakeLists.txt b/samples/Sample_Template/CMakeLists.txt new file mode 100644 index 0000000..6e6f0f0 --- /dev/null +++ b/samples/Sample_Template/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(Sample_Template) + +target_link_libraries(Sample_Template SquidTasks) + +target_include_directories(Sample_Template PRIVATE ../Common) + +target_sources(Sample_Template PRIVATE + ../Common/TimeSystem.h + Main.cpp +) \ No newline at end of file diff --git a/samples/Sample_Tests/CMakeLists.txt b/samples/Sample_Tests/CMakeLists.txt new file mode 100644 index 0000000..899439f --- /dev/null +++ b/samples/Sample_Tests/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(Sample_Tests) + +target_link_libraries(Sample_Tests SquidTasks) + +target_include_directories(Sample_Tests PRIVATE ../Common) + +target_sources(Sample_Tests PRIVATE + ../Common/TimeSystem.h + Main.cpp +) \ No newline at end of file diff --git a/samples/Sample_TextGame/CMakeLists.txt b/samples/Sample_TextGame/CMakeLists.txt new file mode 100644 index 0000000..6d3ffad --- /dev/null +++ b/samples/Sample_TextGame/CMakeLists.txt @@ -0,0 +1,12 @@ +add_executable(Sample_TextGame) + +target_link_libraries(Sample_TextGame SquidTasks) + +target_include_directories(Sample_TextGame PRIVATE ../Common) + +target_sources(Sample_TextGame PRIVATE + ../Common/TimeSystem.h + TextGame.h + TextInput.h + Main.cpp +) \ No newline at end of file From 0ca2b0921111e1f86a8fdc02fd7421663085783b Mon Sep 17 00:00:00 2001 From: Indy Ray Date: Tue, 22 Mar 2022 15:07:24 -0700 Subject: [PATCH 4/4] Factoring out forward decls into TaskTypes.h to minimize header dependiencies. --- CMakeLists.txt | 1 + include/FunctionGuard.h | 3 ++ include/Private/TasksCommonPrivate.h | 12 -------- include/Task.h | 38 +++++--------------------- include/TaskTypes.h | 41 ++++++++++++++++++++++++++++ include/TasksConfig.h | 15 ++++++++-- include/TokenList.h | 3 ++ 7 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 include/TaskTypes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cc48558..5b35c31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ endif() target_include_directories(${PROJECT_NAME} INTERFACE include) target_sources(${PROJECT_NAME} INTERFACE + include/TaskTypes.h include/Task.h include/TaskManager.h include/TasksConfig.h diff --git a/include/FunctionGuard.h b/include/FunctionGuard.h index 6b15f69..b22640b 100644 --- a/include/FunctionGuard.h +++ b/include/FunctionGuard.h @@ -51,6 +51,9 @@ //--- User configuration header ---// #include "TasksConfig.h" +//--- C++17/C++20 Compatibility ---// +#include "Private/TasksCommonPrivate.h" + NAMESPACE_SQUID_BEGIN template > diff --git a/include/Private/TasksCommonPrivate.h b/include/Private/TasksCommonPrivate.h index 71daa6e..c14f267 100644 --- a/include/Private/TasksCommonPrivate.h +++ b/include/Private/TasksCommonPrivate.h @@ -3,18 +3,6 @@ //--- User configuration header ---// #include "../TasksConfig.h" -// Namespace macros (enabled/disabled via SQUID_ENABLE_NAMESPACE) -#if SQUID_ENABLE_NAMESPACE -#define NAMESPACE_SQUID_BEGIN namespace Squid { -#define NAMESPACE_SQUID_END } -#define NAMESPACE_SQUID Squid -#else -#define NAMESPACE_SQUID_BEGIN -#define NAMESPACE_SQUID_END -#define NAMESPACE_SQUID -namespace Squid {} // Convenience to allow 'using namespace Squid' even when namespace is disabled -#endif - // Exception macros (to support environments with exceptions disabled) #if SQUID_USE_EXCEPTIONS && (defined(__cpp_exceptions) || defined(__EXCEPTIONS)) #include diff --git a/include/Task.h b/include/Task.h index ce6e214..1fd5f33 100644 --- a/include/Task.h +++ b/include/Task.h @@ -18,6 +18,9 @@ //--- User configuration header ---// #include "TasksConfig.h" +//--- C++17/C++20 Compatibility ---// +#include "Private/TasksCommonPrivate.h" + //--- Debug Macros ---// #if SQUID_ENABLE_TASK_DEBUG /// @ingroup Tasks @@ -45,41 +48,14 @@ NAMESPACE_SQUID_BEGIN +#include "TaskTypes.h" + /// @addtogroup Tasks /// @{ -//--- Task Reference Type ---// -enum class eTaskRef /// Whether a handle references a task using a strong or weak reference -{ - Strong, ///< Handle will keep the task alive (so long as there exists a valid Resumable handle) - Weak, ///< Handle will not the task alive -}; - -//--- Task Resumable Type ---// -enum class eTaskResumable /// Whether a handle can be resumed (all live tasks have exactly one resumable handle and 0+ non-resumable handles) -{ - Yes, ///< Handle is resumable - No, ///< Handle is not resumable -}; - -//--- Task Status ---// -enum class eTaskStatus /// Status of a task (whether it is currently suspended or done) -{ - Suspended, ///< Task is currently suspended - Done, ///< Task has terminated and coroutine frame has been destroyed -}; - //--- tTaskCancelFn ---// using tTaskCancelFn = std::function; ///< CancelIf/StopIf condition function type -// Forward declarations -template -class Task; /// Templated handle type (defaults to ) -template -using TaskHandle = Task; ///< Non-resumable handle that holds a strong reference to a task -using WeakTask = Task; ///< Resumable handle that holds a weak reference to a task (always void return type) -using WeakTaskHandle = Task; ///< Non-resumable handle that holds a weak reference to a task (always void return type) - /// @} end of addtogroup Tasks /// @addtogroup Awaiters @@ -199,7 +175,7 @@ struct GetStopContext /// @tparam tRet Return type of the underlying coroutine (can be void if the coroutine does not co_return a value) /// @tparam RefType Whether this handle holds a strong or weak reference to the underlying coroutine /// @tparam Resumable Whether this handle can be used to resume the underlying coroutine -template +template class Task { public: @@ -594,7 +570,7 @@ class Task /// Here is an example Squid::GetTime() function implementation from within the ```main.cpp``` file of a sample project: /// /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp -/// NAMESPACE_SQUID_BEGIN +/// /// tTaskTime GetTime() /// { /// return (tTaskTime)TimeSystem::GetTime(); diff --git a/include/TaskTypes.h b/include/TaskTypes.h new file mode 100644 index 0000000..ee05a18 --- /dev/null +++ b/include/TaskTypes.h @@ -0,0 +1,41 @@ +#pragma once + +//--- User configuration header ---// +#include "TasksConfig.h" + +NAMESPACE_SQUID_BEGIN +/// @addtogroup Tasks +/// @{ + +//--- Task Reference Type ---// +enum class eTaskRef /// Whether a handle references a task using a strong or weak reference +{ + Strong, ///< Handle will keep the task alive (so long as there exists a valid Resumable handle) + Weak, ///< Handle will not the task alive +}; + +//--- Task Resumable Type ---// +enum class eTaskResumable /// Whether a handle can be resumed (all live tasks have exactly one resumable handle and 0+ non-resumable handles) +{ + Yes, ///< Handle is resumable + No, ///< Handle is not resumable +}; + +//--- Task Status ---// +enum class eTaskStatus /// Status of a task (whether it is currently suspended or done) +{ + Suspended, ///< Task is currently suspended + Done, ///< Task has terminated and coroutine frame has been destroyed +}; + +// Forward declarations +template +class Task; /// Templated handle type (defaults to ) +template +using TaskHandle = Task; ///< Non-resumable handle that holds a strong reference to a task +using WeakTask = Task; ///< Resumable handle that holds a weak reference to a task (always void return type) +using WeakTaskHandle = Task; ///< Non-resumable handle that holds a weak reference to a task (always void return type) + +/// @} end of addtogroup Tasks + +NAMESPACE_SQUID_END diff --git a/include/TasksConfig.h b/include/TasksConfig.h index befd1cc..ac04920 100644 --- a/include/TasksConfig.h +++ b/include/TasksConfig.h @@ -44,5 +44,16 @@ /// @} end of addtogroup Config -//--- C++17/C++20 Compatibility ---// -#include "Private/TasksCommonPrivate.h" +// Namespace macros (enabled/disabled via SQUID_ENABLE_NAMESPACE) +#if SQUID_ENABLE_NAMESPACE +#define NAMESPACE_SQUID_BEGIN namespace Squid { +#define NAMESPACE_SQUID_END } +#define NAMESPACE_SQUID Squid +#else +#define NAMESPACE_SQUID_BEGIN +#define NAMESPACE_SQUID_END +#define NAMESPACE_SQUID +namespace Squid {} // Convenience to allow 'using namespace Squid' even when namespace is disabled +#endif + + diff --git a/include/TokenList.h b/include/TokenList.h index d71b15c..3c59883 100644 --- a/include/TokenList.h +++ b/include/TokenList.h @@ -66,6 +66,9 @@ //--- User configuration header ---// #include "TasksConfig.h" +//--- C++17/C++20 Compatibility ---// +#include "Private/TasksCommonPrivate.h" + NAMESPACE_SQUID_BEGIN template