Skip to content

Commit e7985ac

Browse files
committed
feature: example module
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
1 parent dcf68c2 commit e7985ac

File tree

5 files changed

+133
-79
lines changed

5 files changed

+133
-79
lines changed

src/modules/CMakeLists.txt

+71-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,82 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7+
function(add_jam_module NAME)
8+
set(MODULE_NAME ${NAME})
9+
10+
set(MODULE "${MODULE_NAME}_module")
11+
12+
# Parse named arguments
13+
cmake_parse_arguments(
14+
# Prefix for parsed argument variables
15+
MODULE
16+
# List of flags (boolean arguments without values)
17+
""
18+
# List of named arguments with a single value
19+
""
20+
# List of named arguments with multiple values
21+
"SOURCE;INCLUDE_DIRS;LIBRARIES;DEFINITIONS"
22+
# Input arguments
23+
${ARGN}
24+
)
25+
26+
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/module.cpp)
27+
message(FATAL_ERROR "Not found `module.cpp` file (main file of module)")
28+
endif ()
29+
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${MODULE_NAME}.hpp" OR NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${MODULE_NAME}.cpp")
30+
message(FATAL_ERROR "Not found `${MODULE_NAME}.hpp` nor `${MODULE_NAME}.cpp` file (class of module)")
31+
endif ()
32+
33+
# Create a shared module library
34+
add_library(${MODULE} MODULE # or SHARED
35+
module.cpp
36+
${MODULE_NAME}.cpp
37+
${MODULE_SOURCE}
38+
)
39+
40+
# Set exported symbols visibility
41+
set_target_properties(${MODULE} PROPERTIES
42+
CXX_VISIBILITY_PRESET hidden
43+
VISIBILITY_INLINES_HIDDEN ON
44+
)
45+
46+
# Set include directories
47+
if (MODULE_INCLUDE_DIRS)
48+
target_include_directories(${MODULE} PRIVATE
49+
${MODULE_INCLUDE_DIRS}
50+
)
51+
endif ()
52+
53+
# Set definitions specified for module
54+
if (MODULE_DEFINITIONS)
55+
target_compile_definitions(${MODULE} PRIVATE
56+
${MODULE_DEFINITIONS}
57+
)
58+
endif ()
59+
60+
# Link with libs
61+
if (MODULE_LIBRARIES)
62+
target_link_libraries(${MODULE}
63+
${MODULE_LIBRARIES}
64+
)
65+
endif ()
66+
67+
# Set C++ standard
68+
target_compile_features(${MODULE} PRIVATE
69+
cxx_std_20
70+
)
71+
72+
endfunction()
73+
74+
# -------------- Core-part of module subsystem --------------
75+
776
add_library(modules
877
module_loader.cpp
9-
)
78+
)
1079

1180
target_link_libraries(modules
1281
qtils::qtils
13-
)
82+
)
1483

1584
# -------------- Modules --------------
1685

src/modules/example/CMakeLists.txt

+9-28
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7-
# Create a shared module library
8-
add_library(example_module MODULE # or SHARED
7+
add_jam_module(example
8+
SOURCE
99
example.cpp
10-
)
11-
12-
# Set C++ standard
13-
target_compile_features(example_module PRIVATE
14-
cxx_std_20
15-
)
16-
17-
# Set include directories
18-
target_include_directories(example_module PRIVATE
19-
${CMAKE_CURRENT_SOURCE_DIR}
20-
)
21-
22-
# Set exported symbols visibility
23-
set_target_properties(example_module PROPERTIES
24-
CXX_VISIBILITY_PRESET hidden
25-
VISIBILITY_INLINES_HIDDEN ON
26-
)
27-
28-
# Ensure exported symbols are explicitly marked
29-
target_compile_definitions(example_module PRIVATE
30-
EXAMPLE_EXPORTS
31-
)
32-
33-
# Link with libs
34-
target_link_libraries(example_module
10+
INCLUDE_DIRS
11+
${CMAKE_SOURCE_DIR}/src
12+
DEFINITIONS
13+
SOME_FLAG=1
14+
LIBRARIES
3515
qtils::qtils
36-
)
16+
soralog::soralog
17+
)

src/modules/example/example.cpp

+9-28
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,15 @@
66

77
#include "modules/example/example.hpp"
88

9-
MODULE_C_API const uint64_t JAM_MAGIC = jam::modules::JAM_MAGIC;
9+
namespace jam::modules {
10+
std::shared_ptr<ExampleModule> ExampleModule::instance;
1011

11-
std::shared_ptr<jam::modules::ExampleModule> module_instance;
12+
ExampleModule::ExampleModule(
13+
qtils::StrictSharedPtr<ExampleModuleLoader> loader,
14+
qtils::StrictSharedPtr<log::LoggingSystem> logging_system)
15+
: loader_(loader),
16+
logger_(logging_system->getLogger("ExampleModule", "example_module"))
1217

13-
MODULE_C_API const char *loader_id() {
14-
return "ExampleLoader";
15-
}
18+
{}
1619

17-
MODULE_C_API const char *get_module_name_and_version() {
18-
return "ExampleModule v1.0";
19-
}
20-
21-
std::string path_;
22-
std::unique_ptr<void, decltype(&dlclose)> handle_{};
23-
std::string loader_id_;
24-
25-
MODULE_API std::weak_ptr<jam::modules::ExampleModule> query_module_instance() {
26-
static bool _ = ({
27-
module_instance = jam::modules::ExampleModule::create_shared(
28-
path_, // path
29-
std::move(handle_), // handler
30-
loader_id_ // loader
31-
);
32-
true;
33-
});
34-
return module_instance;
35-
}
36-
37-
MODULE_API void release_module_instance() {
38-
module_instance = nullptr;
39-
};
20+
} // namespace jam::modules

src/modules/example/example.hpp

+13-21
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,29 @@
66

77
#pragma once
88

9-
#include <modules/module.hpp>
10-
11-
/// @brief Magic word to check if module is JAM module actually
12-
MODULE_C_API const uint64_t JAM_MAGIC;
13-
14-
/// @brief Identifier of suitable module loader
15-
MODULE_C_API const char *loader_id();
16-
17-
/// @brief Returns module name and version
18-
/// @return A string containing module name and version
19-
MODULE_C_API const char *get_module_name_and_version();
9+
#include <metrics/impl/session_impl.hpp>
10+
#include <modules/module_loader.hpp>
11+
#include <qtils/strict_sptr.hpp>
2012

2113
namespace jam::modules {
2214
class ExampleModule;
15+
class ExampleModuleLoader;
2316
}
2417

25-
MODULE_API std::weak_ptr<jam::modules::ExampleModule> query_module_instance();
26-
27-
MODULE_API void release_module_instance();
28-
2918
class BlockTree;
30-
class ExampleModuleLoader;
3119

3220
namespace jam::modules {
3321

34-
class ExampleModule : public Module, Singleton<ExampleModule> { // implement by module
35-
using Module::Module;
36-
virtual ~ExampleModule() = default;
22+
class ExampleModule : public Singleton<ExampleModule> {
23+
public:
24+
static std::shared_ptr<ExampleModule> instance;
3725
CREATE_SHARED_METHOD(ExampleModule);
38-
// virtual void initialize(std::shared_ptr<BlockTree> tree,
39-
// std::weak_ptr<ExampleModuleLoader> loader) = 0;
26+
27+
ExampleModule(qtils::StrictSharedPtr<ExampleModuleLoader> loader,
28+
qtils::StrictSharedPtr<log::LoggingSystem> logging_system);
29+
30+
qtils::StrictSharedPtr<ExampleModuleLoader> loader_;
31+
log::Logger logger_;
4032
};
4133

4234
} // namespace jam::modules

src/modules/example/module.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <modules/example/example.hpp>
8+
9+
#define MODULE_C_API extern "C" __attribute__((visibility("default")))
10+
#define MODULE_API __attribute__((visibility("default")))
11+
12+
MODULE_C_API const char *loader_id() {
13+
return "ExampleLoader";
14+
}
15+
16+
MODULE_C_API const char *module_info() {
17+
return "ExampleModule v1.0";
18+
}
19+
20+
MODULE_API std::weak_ptr<jam::modules::ExampleModule> query_module_instance(
21+
std::shared_ptr<jam::modules::ExampleModuleLoader> loader,
22+
std::shared_ptr<jam::log::LoggingSystem> block_tree) {
23+
return jam::modules::ExampleModule::instance
24+
? jam::modules::ExampleModule::instance
25+
: (jam::modules::ExampleModule::instance = jam::modules::ExampleModule::create_shared(
26+
loader, block_tree));
27+
}
28+
29+
MODULE_API void release_module_instance() {
30+
jam::modules::ExampleModule::instance.reset();
31+
}

0 commit comments

Comments
 (0)