From 36b2fada153accac6a6f31d95f2250ab4e937556 Mon Sep 17 00:00:00 2001 From: Martin Bond <28291143+martinbond7@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:17:27 +0100 Subject: [PATCH] Updated modules examples --- .../13_modules/cmake-alarm/CMakeLists.txt | 68 +++++++++++++++++ .../13_modules/cmake-alarm/CMakePresets.json | 74 +++++++++++++++++++ examples/13_modules/cmake-alarm/src/alarm.cpp | 26 +++++++ examples/13_modules/cmake-alarm/src/alarm.ixx | 20 +++++ .../13_modules/cmake-alarm/src/handler.cpp | 11 +++ .../13_modules/cmake-alarm/src/handler.ixx | 7 ++ examples/13_modules/cmake-alarm/src/main.cpp | 16 ++++ .../src-alarm-partitions/alarm-base.cpp | 2 +- .../src-alarm-partitions/alarm-extra.cpp | 2 +- .../src-alarm-partitions/alarm-extra.ixx | 2 +- .../src-alarm-partitions/alarm-helper.cpp | 2 +- .../src-alarm-partitions/alarm-helper.ixx | 2 +- examples/13_modules/src-greet/greet.cpp | 5 ++ examples/13_modules/src-greet/greet.ixx | 2 + examples/13_modules/src-greet/main.cpp | 9 +++ examples/13_modules/src-morning/evening.ixx | 15 ++++ examples/13_modules/src-morning/main.cpp | 11 +++ examples/13_modules/src-morning/morning.ixx | 15 ++++ examples/13_modules/src-templates/Modules.txt | 3 + examples/13_modules/src-templates/alarm.cpp | 27 +++++++ examples/13_modules/src-templates/alarm.ixx | 20 +++++ .../13_modules/src-templates/constraints.ixx | 14 ++++ examples/13_modules/src-templates/main.cpp | 17 +++++ .../13_modules/src-templates/templates.ixx | 8 ++ workspaces/host-clang/CMakeLists.txt | 15 ++++ workspaces/host-msvc/CMakeLists.txt | 17 ++++- 26 files changed, 404 insertions(+), 6 deletions(-) create mode 100644 examples/13_modules/cmake-alarm/CMakeLists.txt create mode 100644 examples/13_modules/cmake-alarm/CMakePresets.json create mode 100644 examples/13_modules/cmake-alarm/src/alarm.cpp create mode 100644 examples/13_modules/cmake-alarm/src/alarm.ixx create mode 100644 examples/13_modules/cmake-alarm/src/handler.cpp create mode 100644 examples/13_modules/cmake-alarm/src/handler.ixx create mode 100644 examples/13_modules/cmake-alarm/src/main.cpp create mode 100644 examples/13_modules/src-greet/greet.cpp create mode 100644 examples/13_modules/src-greet/greet.ixx create mode 100644 examples/13_modules/src-greet/main.cpp create mode 100644 examples/13_modules/src-morning/evening.ixx create mode 100644 examples/13_modules/src-morning/main.cpp create mode 100644 examples/13_modules/src-morning/morning.ixx create mode 100644 examples/13_modules/src-templates/Modules.txt create mode 100644 examples/13_modules/src-templates/alarm.cpp create mode 100644 examples/13_modules/src-templates/alarm.ixx create mode 100644 examples/13_modules/src-templates/constraints.ixx create mode 100644 examples/13_modules/src-templates/main.cpp create mode 100644 examples/13_modules/src-templates/templates.ixx diff --git a/examples/13_modules/cmake-alarm/CMakeLists.txt b/examples/13_modules/cmake-alarm/CMakeLists.txt new file mode 100644 index 0000000..fed8bc9 --- /dev/null +++ b/examples/13_modules/cmake-alarm/CMakeLists.txt @@ -0,0 +1,68 @@ +cmake_minimum_required (VERSION 3.28) + +project ("Application") + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 23) + +set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_C_STANDARD_REQUIRED ON) +# set(CMAKE_CXX_EXTENSIONS OFF) +# set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +add_compile_options( + /std:c++latest + /experimental:module + /DWIN32 + /D_WINDOWS + /W4 + /GR + /EHsc + $<$:/Od> + $<$:/O2> +) + +add_compile_definitions( + $<$:DEBUG> +) + +add_library(alarm) +target_sources(alarm + PUBLIC + FILE_SET CXX_MODULES FILES + ${CMAKE_SOURCE_DIR}/src/alarm.ixx +) + +target_include_directories(alarm PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include +) + +add_library(handler) +target_sources(handler + PUBLIC + FILE_SET CXX_MODULES FILES + ${CMAKE_SOURCE_DIR}/src/handler.ixx +) + +target_include_directories(handler PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include +) + +target_link_libraries(handler PRIVATE alarm) + +add_executable(Application + ${CMAKE_SOURCE_DIR}/src/alarm.cpp + ${CMAKE_SOURCE_DIR}/src/handler.cpp + ${CMAKE_SOURCE_DIR}/src/main.cpp +) + +target_include_directories(Application PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include +) + +target_link_libraries(Application PRIVATE handler) + diff --git a/examples/13_modules/cmake-alarm/CMakePresets.json b/examples/13_modules/cmake-alarm/CMakePresets.json new file mode 100644 index 0000000..db45ffe --- /dev/null +++ b/examples/13_modules/cmake-alarm/CMakePresets.json @@ -0,0 +1,74 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 21, + "patch": 0 + }, + "configurePresets": [ + { + "name": "base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/build/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "warnings": { + "uninitialized": true, + "dev": true, + "deprecated": true + }, + "vendor": { + "microsoft.com/VisualStudioSettings/CMake/1.0": { + "intelliSenseMode": "linux-gcc-arm" + } + } + }, + { + "name": "debug", + "displayName": "Debug", + "inherits": "base" + }, + { + "name": "release", + "displayName": "Release", + "inherits": "base", + "environment": { + "CMAKE_BUILD_TYPE": "Release" + } + } + ], + "buildPresets": [ + { + "name": "build-base", + "hidden": true, + "configurePreset": "debug", + "nativeToolOptions": [ + ] + }, + { + "name": "debug", + "inherits": "build-base" + }, + { + "name": "release", + "inherits": "build-base", + "configurePreset": "release" + }, + { + "name": "clang-tidy", + "inherits": "debug", + "targets": [ "clang-tidy" ] + }, + { + "name": "test", + "inherits": "debug", + "targets": [ "test" ], + "nativeToolOptions": [ + "ARGS=--output-on-failure" + ] + } + ] +} + diff --git a/examples/13_modules/cmake-alarm/src/alarm.cpp b/examples/13_modules/cmake-alarm/src/alarm.cpp new file mode 100644 index 0000000..7fad4d0 --- /dev/null +++ b/examples/13_modules/cmake-alarm/src/alarm.cpp @@ -0,0 +1,26 @@ +// alarm.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +module; + +#include + +module Alarms; + +namespace Alarms +{ + Alarm::Alarm(Type type, bool status) + : type{type}, status{status} + {} + + void Alarm::reset() { + status = false; + } + + std::ostream& operator<< (std::ostream& out, const Alarm& alarm) + { + out << "Alarm " << int(alarm.type) << ": " << (alarm.status ? "on" : "off"); + return out; + } +} diff --git a/examples/13_modules/cmake-alarm/src/alarm.ixx b/examples/13_modules/cmake-alarm/src/alarm.ixx new file mode 100644 index 0000000..9e6852d --- /dev/null +++ b/examples/13_modules/cmake-alarm/src/alarm.ixx @@ -0,0 +1,20 @@ +module; +#include + +export module Alarms; + +export namespace Alarms +{ + class Alarm + { + public: + enum class Type {warning, error, critical}; + Alarm() = default; + Alarm(Type type, bool status); + void reset(); + friend std::ostream& operator<< (std::ostream& out, const Alarm& alarm); + private: + Type type {}; + bool status {}; + }; +} diff --git a/examples/13_modules/cmake-alarm/src/handler.cpp b/examples/13_modules/cmake-alarm/src/handler.cpp new file mode 100644 index 0000000..1050a78 --- /dev/null +++ b/examples/13_modules/cmake-alarm/src/handler.cpp @@ -0,0 +1,11 @@ +// handler.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +module Handler; + +namespace Handler { + void cancel (Alarms::Alarm& alarm) { + alarm.reset(); + } +}; diff --git a/examples/13_modules/cmake-alarm/src/handler.ixx b/examples/13_modules/cmake-alarm/src/handler.ixx new file mode 100644 index 0000000..ff31559 --- /dev/null +++ b/examples/13_modules/cmake-alarm/src/handler.ixx @@ -0,0 +1,7 @@ +export module Handler; + +export import Alarms; + +export namespace Handler { + void cancel(typename Alarms::Alarm& alarm); +} diff --git a/examples/13_modules/cmake-alarm/src/main.cpp b/examples/13_modules/cmake-alarm/src/main.cpp new file mode 100644 index 0000000..0f2d9ae --- /dev/null +++ b/examples/13_modules/cmake-alarm/src/main.cpp @@ -0,0 +1,16 @@ +// main.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +#include +import Handler; + +using Alarms::Alarm; + +int main() +{ + Alarm a1 {Alarm::Type::critical, true}; + Handler::cancel(a1); + std::cout << a1 << '\n'; +} + diff --git a/examples/13_modules/src-alarm-partitions/alarm-base.cpp b/examples/13_modules/src-alarm-partitions/alarm-base.cpp index 9cb19da..1bf51bc 100644 --- a/examples/13_modules/src-alarm-partitions/alarm-base.cpp +++ b/examples/13_modules/src-alarm-partitions/alarm-base.cpp @@ -8,7 +8,7 @@ module; // module Alarm:Base; // g++ 12.1 implementation -module Alarm; +module Alarm:Base; namespace Alarms { diff --git a/examples/13_modules/src-alarm-partitions/alarm-extra.cpp b/examples/13_modules/src-alarm-partitions/alarm-extra.cpp index 158b089..9caec10 100644 --- a/examples/13_modules/src-alarm-partitions/alarm-extra.cpp +++ b/examples/13_modules/src-alarm-partitions/alarm-extra.cpp @@ -2,7 +2,7 @@ // See project README.md for disclaimer and additional information. // Feabhas Ltd -module Alarm; +module Alarm:Extra; import :Helper; namespace Alarms diff --git a/examples/13_modules/src-alarm-partitions/alarm-extra.ixx b/examples/13_modules/src-alarm-partitions/alarm-extra.ixx index 64bd5d2..e7245f5 100644 --- a/examples/13_modules/src-alarm-partitions/alarm-extra.ixx +++ b/examples/13_modules/src-alarm-partitions/alarm-extra.ixx @@ -3,5 +3,5 @@ import :Helper; export namespace Alarms { - extern const char* to_string(Alarms::Alarm::Type type); + const char* to_string(Alarms::Alarm::Type type); } diff --git a/examples/13_modules/src-alarm-partitions/alarm-helper.cpp b/examples/13_modules/src-alarm-partitions/alarm-helper.cpp index 2484657..a2513d7 100644 --- a/examples/13_modules/src-alarm-partitions/alarm-helper.cpp +++ b/examples/13_modules/src-alarm-partitions/alarm-helper.cpp @@ -2,7 +2,7 @@ // See project README.md for disclaimer and additional information. // Feabhas Ltd -module Alarm; +module Alarm:Helper; namespace Alarms { diff --git a/examples/13_modules/src-alarm-partitions/alarm-helper.ixx b/examples/13_modules/src-alarm-partitions/alarm-helper.ixx index afbef64..f08f3b1 100644 --- a/examples/13_modules/src-alarm-partitions/alarm-helper.ixx +++ b/examples/13_modules/src-alarm-partitions/alarm-helper.ixx @@ -3,6 +3,6 @@ import :Base; export namespace Alarms { - extern const char* type_names[]; + const char* type_names[]; } diff --git a/examples/13_modules/src-greet/greet.cpp b/examples/13_modules/src-greet/greet.cpp new file mode 100644 index 0000000..14d164f --- /dev/null +++ b/examples/13_modules/src-greet/greet.cpp @@ -0,0 +1,5 @@ +module Greet; + +const char* greeting() { + return "Hello module"; +} diff --git a/examples/13_modules/src-greet/greet.ixx b/examples/13_modules/src-greet/greet.ixx new file mode 100644 index 0000000..ee62777 --- /dev/null +++ b/examples/13_modules/src-greet/greet.ixx @@ -0,0 +1,2 @@ +export module Greet; +export const char* greeting(); \ No newline at end of file diff --git a/examples/13_modules/src-greet/main.cpp b/examples/13_modules/src-greet/main.cpp new file mode 100644 index 0000000..6afd762 --- /dev/null +++ b/examples/13_modules/src-greet/main.cpp @@ -0,0 +1,9 @@ + +#include + +import Greet; + +int main() +{ + std::cout << greeting() << '\n'; +} diff --git a/examples/13_modules/src-morning/evening.ixx b/examples/13_modules/src-morning/evening.ixx new file mode 100644 index 0000000..c6d4a56 --- /dev/null +++ b/examples/13_modules/src-morning/evening.ixx @@ -0,0 +1,15 @@ +export module Evening; + +export namespace evening{ + const char* greeting(); +} + +module :private; + +const char *message = "Good evening"; + +namespace evening { + const char* greeting() { + return message; + } +} \ No newline at end of file diff --git a/examples/13_modules/src-morning/main.cpp b/examples/13_modules/src-morning/main.cpp new file mode 100644 index 0000000..149f9db --- /dev/null +++ b/examples/13_modules/src-morning/main.cpp @@ -0,0 +1,11 @@ + +#include + +import Morning; +import Evening; + +int main() +{ + std::cout << morning::greeting() << '\n'; + std::cout << evening::greeting() << '\n'; +} diff --git a/examples/13_modules/src-morning/morning.ixx b/examples/13_modules/src-morning/morning.ixx new file mode 100644 index 0000000..e674d84 --- /dev/null +++ b/examples/13_modules/src-morning/morning.ixx @@ -0,0 +1,15 @@ +export module Morning; + +export namespace morning { + const char* greeting(); +} + +module :private; + +const char *message = "Good morning"; + +namespace morning { + const char* greeting() { + return message; + } +} \ No newline at end of file diff --git a/examples/13_modules/src-templates/Modules.txt b/examples/13_modules/src-templates/Modules.txt new file mode 100644 index 0000000..0cd3993 --- /dev/null +++ b/examples/13_modules/src-templates/Modules.txt @@ -0,0 +1,3 @@ +alarm.ixx +constraints.ixx +templates.ixx diff --git a/examples/13_modules/src-templates/alarm.cpp b/examples/13_modules/src-templates/alarm.cpp new file mode 100644 index 0000000..67d6227 --- /dev/null +++ b/examples/13_modules/src-templates/alarm.cpp @@ -0,0 +1,27 @@ +// alarm.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +module; + +#include + +module Alarms; + +namespace Alarms +{ + Alarm::Alarm(Type type, bool status) + : type{type}, status{status} + {} + + void Alarm::reset() + { + status = false; + } + + std::ostream& operator<< (std::ostream& out, const Alarm& alarm) + { + out << "Alarm " << int(alarm.type) << ": " << (alarm.status ? "on" : "off"); + return out; + } +} diff --git a/examples/13_modules/src-templates/alarm.ixx b/examples/13_modules/src-templates/alarm.ixx new file mode 100644 index 0000000..4842aba --- /dev/null +++ b/examples/13_modules/src-templates/alarm.ixx @@ -0,0 +1,20 @@ +module; +#include + +export module Alarms; + +export namespace Alarms +{ + class Alarm + { + public: + enum class Type {unknown, warning, error, critical}; + Alarm() = default; + Alarm(Type type, bool status); + void reset(); + friend std::ostream& operator<< (std::ostream& out, const Alarm& alarm); + private: + Type type {}; + bool status {}; + }; +} diff --git a/examples/13_modules/src-templates/constraints.ixx b/examples/13_modules/src-templates/constraints.ixx new file mode 100644 index 0000000..85402c7 --- /dev/null +++ b/examples/13_modules/src-templates/constraints.ixx @@ -0,0 +1,14 @@ +// handler.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd +module; + +#include + +export module Constraints; + +export template +concept Resetable = requires(T t) +{ + { t.reset() } -> std::same_as; +}; diff --git a/examples/13_modules/src-templates/main.cpp b/examples/13_modules/src-templates/main.cpp new file mode 100644 index 0000000..9fefbdd --- /dev/null +++ b/examples/13_modules/src-templates/main.cpp @@ -0,0 +1,17 @@ +// main.cpp +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +#include +import Alarms; +import Templates; + +using Alarms::Alarm; + +int main() +{ + Alarm a1 {Alarm::Type::critical, true}; + do_reset(a1); + std::cout << a1 << '\n'; +} + diff --git a/examples/13_modules/src-templates/templates.ixx b/examples/13_modules/src-templates/templates.ixx new file mode 100644 index 0000000..cb802af --- /dev/null +++ b/examples/13_modules/src-templates/templates.ixx @@ -0,0 +1,8 @@ +export module Templates; + +export import Constraints; + +export template +void do_reset(T& object) { + object.reset(); +} diff --git a/workspaces/host-clang/CMakeLists.txt b/workspaces/host-clang/CMakeLists.txt index 80905d8..36d14a3 100644 --- a/workspaces/host-clang/CMakeLists.txt +++ b/workspaces/host-clang/CMakeLists.txt @@ -52,6 +52,21 @@ add_compile_definitions( # src/handler.ixx # ) +# add_library(modules OBJECT + # src/alarm.ixx + # src/constraints.ixx + # src/templates.ixx +# ) + +# add_library(modules OBJECT +# src/morning.ixx +# src/evening.ixx +# ) + +# add_library(modules OBJECT +# ${CMAKE_SOURCE_DIR}/src/greet.ixx +# ) + if (TARGET modules) target_include_directories(modules PRIVATE ${CMAKE_SOURCE_DIR}/src diff --git a/workspaces/host-msvc/CMakeLists.txt b/workspaces/host-msvc/CMakeLists.txt index 80905d8..fe008bc 100644 --- a/workspaces/host-msvc/CMakeLists.txt +++ b/workspaces/host-msvc/CMakeLists.txt @@ -1,4 +1,4 @@ -# CMakeList.txt : CMake project for Application, include source and define +# CMakeList.txt : CMake project for Application, include source and define # project specific logic here. # cmake_minimum_required (VERSION 3.16) @@ -50,6 +50,21 @@ add_compile_definitions( # add_library(modules OBJECT # src/alarm.ixx # src/handler.ixx +#) + +# add_library(modules OBJECT +# src/alarm.ixx +# src/constraints.ixx +# src/templates.ixx +# ) + +# add_library(modules OBJECT +# src/morning.ixx +# src/evening.ixx +# ) + +# add_library(modules OBJECT +# ${CMAKE_SOURCE_DIR}/src/greet.ixx # ) if (TARGET modules)