Skip to content

Commit 8ac3741

Browse files
committed
Add modularity API
1 parent 11ba955 commit 8ac3741

File tree

11 files changed

+267
-0
lines changed

11 files changed

+267
-0
lines changed

src/global/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
set(MODULE global)
22
set(MODULE_URI Global)
33
set(MODULE_SRC
4+
modularity/ioc.h
5+
modularity/modulesioc.h
6+
modularity/imoduleexportinterface.h
7+
modularity/imodulesetup.h
48
)
59

610
include(${PROJECT_SOURCE_DIR}/build/module.cmake)
11+
12+
add_subdirectory(test)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#define MODULE_EXPORT_INTERFACE \
6+
public \
7+
scratchcpp::modularity::IModuleExportInterface
8+
9+
namespace scratchcpp::modularity
10+
{
11+
12+
class IModuleExportInterface
13+
{
14+
public:
15+
virtual ~IModuleExportInterface() { }
16+
};
17+
18+
} // namespace scratchcpp::modularity
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <string>
6+
7+
namespace scratchcpp::modularity
8+
{
9+
10+
class IModuleSetup
11+
{
12+
public:
13+
virtual ~IModuleSetup() { }
14+
15+
virtual std::string moduleName() const = 0;
16+
17+
virtual void registerExports() { }
18+
virtual void initSettings() { }
19+
20+
virtual void onPreInit() { }
21+
virtual void onInit() { }
22+
virtual void onDeinit() { }
23+
virtual void onDestroy() { }
24+
25+
virtual void onStartApp() { }
26+
};
27+
28+
} // namespace scratchcpp::modularity

src/global/modularity/ioc.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include "modulesioc.h"
6+
7+
#define INJECT(Interface, getter) \
8+
private: \
9+
static inline std::shared_ptr<Interface> _##getter = nullptr; \
10+
\
11+
public: \
12+
static std::shared_ptr<Interface> getter() \
13+
{ \
14+
if (!_##getter) { \
15+
_##getter = modularity::ioc()->resolve<Interface>(); \
16+
} \
17+
return _##getter; \
18+
} \
19+
static void set##getter(std::shared_ptr<Interface> impl) \
20+
{ \
21+
_##getter = impl; \
22+
}
23+
24+
namespace scratchcpp::modularity
25+
{
26+
27+
inline ModulesIoC *ioc()
28+
{
29+
return ModulesIoC::instance();
30+
}
31+
32+
} // namespace scratchcpp::modularity

src/global/modularity/modulesioc.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <unordered_set>
6+
#include <vector>
7+
#include <memory>
8+
#include <cassert>
9+
#include "imoduleexportinterface.h"
10+
11+
namespace scratchcpp::modularity
12+
{
13+
14+
class ModulesIoC
15+
{
16+
public:
17+
static ModulesIoC *instance()
18+
{
19+
static ModulesIoC instance;
20+
return &instance;
21+
}
22+
23+
template<class I>
24+
void registerExport(std::shared_ptr<I> instance)
25+
{
26+
// Remove old exports of this class
27+
std::vector<std::shared_ptr<IModuleExportInterface>> toRemove;
28+
29+
for (auto ex : m_exports) {
30+
std::shared_ptr<I> exCast = std::dynamic_pointer_cast<I>(ex);
31+
32+
if (exCast)
33+
toRemove.push_back(exCast);
34+
}
35+
36+
for (auto ex : toRemove)
37+
m_exports.erase(ex);
38+
39+
// Register the export
40+
auto iface = std::dynamic_pointer_cast<IModuleExportInterface>(instance);
41+
assert(m_exports.find(iface) == m_exports.end());
42+
43+
if (!iface) {
44+
assert(false);
45+
return;
46+
}
47+
48+
m_exports.insert(iface);
49+
}
50+
51+
template<class I>
52+
std::shared_ptr<I> resolve() const
53+
{
54+
for (auto ex : m_exports) {
55+
std::shared_ptr<I> exCast = std::dynamic_pointer_cast<I>(ex);
56+
if (exCast)
57+
return exCast;
58+
}
59+
60+
return nullptr;
61+
}
62+
63+
private:
64+
std::unordered_set<std::shared_ptr<IModuleExportInterface>> m_exports;
65+
};
66+
67+
} // namespace scratchcpp::modularity

src/global/test/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(MODULE_TEST_SRC
2+
modularity.cpp
3+
fakeexport.h
4+
fakedependency.h
5+
mocks/moduleexportinterfacemock.h
6+
mocks/modulesetupmock.h
7+
)
8+
9+
include(${PROJECT_SOURCE_DIR}/build/module_test.cmake)

src/global/test/fakedependency.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <modularity/imoduleexportinterface.h>
4+
5+
namespace scratchcpp
6+
{
7+
8+
class FakeDependency : MODULE_EXPORT_INTERFACE
9+
{ };
10+
11+
} // namespace scratchcpp

src/global/test/fakeexport.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <modularity/imoduleexportinterface.h>
4+
#include <modularity/ioc.h>
5+
6+
#include "fakedependency.h"
7+
8+
namespace scratchcpp
9+
{
10+
11+
class FakeExport : MODULE_EXPORT_INTERFACE
12+
{
13+
INJECT(FakeDependency, dep);
14+
};
15+
16+
} // namespace scratchcpp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <modularity/imoduleexportinterface.h>
4+
5+
namespace scratchcpp::modularity
6+
{
7+
8+
class ModuleExportInterfaceMock : public IModuleExportInterface
9+
{ };
10+
11+
} // namespace scratchcpp::modularity
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <modularity/imodulesetup.h>
4+
#include <gmock/gmock.h>
5+
6+
namespace scratchcpp::modularity
7+
{
8+
9+
class ModuleSetupMock : public IModuleSetup
10+
{
11+
public:
12+
MOCK_METHOD(std::string, moduleName, (), (const, override));
13+
14+
MOCK_METHOD(void, registerExports, (), (override));
15+
MOCK_METHOD(void, initSettings, (), (override));
16+
17+
MOCK_METHOD(void, onPreInit, (), (override));
18+
MOCK_METHOD(void, onInit, (), (override));
19+
MOCK_METHOD(void, onDeinit, (), (override));
20+
MOCK_METHOD(void, onDestroy, (), (override));
21+
22+
MOCK_METHOD(void, onStartApp, (), (override));
23+
};
24+
25+
} // namespace scratchcpp::modularity

0 commit comments

Comments
 (0)