Skip to content

Commit

Permalink
Updated modules examples
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbond7 committed Jun 7, 2024
1 parent 84f7120 commit 36b2fad
Show file tree
Hide file tree
Showing 26 changed files with 404 additions and 6 deletions.
68 changes: 68 additions & 0 deletions examples/13_modules/cmake-alarm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<$<CONFIG:DEBUG>:/Od>
$<$<CONFIG:RELEASE>:/O2>
)

add_compile_definitions(
$<$<CONFIG:DEBUG>: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)

74 changes: 74 additions & 0 deletions examples/13_modules/cmake-alarm/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}

26 changes: 26 additions & 0 deletions examples/13_modules/cmake-alarm/src/alarm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// alarm.cpp
// See project README.md for disclaimer and additional information.
// Feabhas Ltd

module;

#include <iostream>

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;
}
}
20 changes: 20 additions & 0 deletions examples/13_modules/cmake-alarm/src/alarm.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module;
#include <iosfwd>

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 {};
};
}
11 changes: 11 additions & 0 deletions examples/13_modules/cmake-alarm/src/handler.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
};
7 changes: 7 additions & 0 deletions examples/13_modules/cmake-alarm/src/handler.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export module Handler;

export import Alarms;

export namespace Handler {
void cancel(typename Alarms::Alarm& alarm);
}
16 changes: 16 additions & 0 deletions examples/13_modules/cmake-alarm/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// main.cpp
// See project README.md for disclaimer and additional information.
// Feabhas Ltd

#include <iostream>
import Handler;

using Alarms::Alarm;

int main()
{
Alarm a1 {Alarm::Type::critical, true};
Handler::cancel(a1);
std::cout << a1 << '\n';
}

2 changes: 1 addition & 1 deletion examples/13_modules/src-alarm-partitions/alarm-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module;

// module Alarm:Base;
// g++ 12.1 implementation
module Alarm;
module Alarm:Base;

namespace Alarms
{
Expand Down
2 changes: 1 addition & 1 deletion examples/13_modules/src-alarm-partitions/alarm-extra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See project README.md for disclaimer and additional information.
// Feabhas Ltd

module Alarm;
module Alarm:Extra;
import :Helper;

namespace Alarms
Expand Down
2 changes: 1 addition & 1 deletion examples/13_modules/src-alarm-partitions/alarm-extra.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/13_modules/src-alarm-partitions/alarm-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See project README.md for disclaimer and additional information.
// Feabhas Ltd

module Alarm;
module Alarm:Helper;

namespace Alarms
{
Expand Down
2 changes: 1 addition & 1 deletion examples/13_modules/src-alarm-partitions/alarm-helper.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import :Base;

export namespace Alarms
{
extern const char* type_names[];
const char* type_names[];
}

5 changes: 5 additions & 0 deletions examples/13_modules/src-greet/greet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Greet;

const char* greeting() {
return "Hello module";
}
2 changes: 2 additions & 0 deletions examples/13_modules/src-greet/greet.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export module Greet;
export const char* greeting();
9 changes: 9 additions & 0 deletions examples/13_modules/src-greet/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include <iostream>

import Greet;

int main()
{
std::cout << greeting() << '\n';
}
15 changes: 15 additions & 0 deletions examples/13_modules/src-morning/evening.ixx
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions examples/13_modules/src-morning/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <iostream>

import Morning;
import Evening;

int main()
{
std::cout << morning::greeting() << '\n';
std::cout << evening::greeting() << '\n';
}
15 changes: 15 additions & 0 deletions examples/13_modules/src-morning/morning.ixx
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions examples/13_modules/src-templates/Modules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alarm.ixx
constraints.ixx
templates.ixx
27 changes: 27 additions & 0 deletions examples/13_modules/src-templates/alarm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// alarm.cpp
// See project README.md for disclaimer and additional information.
// Feabhas Ltd

module;

#include <iostream>

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;
}
}
20 changes: 20 additions & 0 deletions examples/13_modules/src-templates/alarm.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module;
#include <iosfwd>

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 {};
};
}
Loading

0 comments on commit 36b2fad

Please sign in to comment.