Skip to content

Commit

Permalink
create object at startup (mock is not being registered)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgsogo committed Mar 1, 2023
1 parent c236928 commit 01f49da
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
10 changes: 10 additions & 0 deletions include/HAL/TargetSystemRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ namespace qssc::hal::registry {
class TargetSystemRegistry : public qssc::plugin::registry::PluginRegistry<TargetSystemInfo> {
using PluginRegistry = qssc::plugin::registry::PluginRegistry<TargetSystemInfo>;
public:
template<typename ConcreteTargetSystem>
struct InitRegistry {
template<typename... Args>
InitRegistry(llvm::StringRef name, Args &&... args) {
registered = TargetSystemRegistry::registerPlugin<ConcreteTargetSystem>(name, std::forward<Args>(args)...);
}

bool registered = false;
};

template<typename ConcreteTargetSystem>
static bool registerPlugin(llvm::StringRef name, llvm::StringRef description,
const TargetSystemInfo::PluginFactoryFunction &pluginFactory) {
Expand Down
11 changes: 11 additions & 0 deletions include/Plugin/PluginRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ namespace qssc::plugin::registry {
struct PluginRegistry {

public:
struct InitRegistry {
template<typename... Args>
InitRegistry(llvm::StringRef name, Args &&... args) {
registered = PluginRegistry::registerPlugin(name, std::forward<Args>(args)...);
}

bool registered = false;
};

PluginRegistry(const PluginRegistry&) = delete;

template<typename... Args>
static bool registerPlugin(llvm::StringRef name, Args &&... args) {
auto &pluginRegistry = instance();
Expand Down
26 changes: 13 additions & 13 deletions mock_target/MockTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ static llvm::cl::OptionCategory
"Compiler target");

int qssc::targets::mock::init() {
registry::TargetSystemRegistry::registerPlugin<MockSystem>(
"mock", "Mock system for testing the targetting infrastructure.",
[](llvm::Optional<llvm::StringRef> configurationPath)
-> llvm::Expected<std::unique_ptr<hal::TargetSystem>> {
if (!configurationPath)
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Configuration file must be specified.\n");

auto config = std::make_unique<MockConfig>(*configurationPath);
return std::make_unique<MockSystem>(std::move(config));
});
return 0;
const registry::TargetSystemRegistry::InitRegistry<MockSystem> reg(
"mock", "Mock system for testing the targetting infrastructure.",
[](llvm::Optional<llvm::StringRef> configurationPath)
-> llvm::Expected<std::unique_ptr<hal::TargetSystem>> {
if (!configurationPath)
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Configuration file must be specified.\n");

auto config = std::make_unique<MockConfig>(*configurationPath);
return std::make_unique<MockSystem>(std::move(config));
});
return reg.registered ? 0 : -1;
}

MockConfig::MockConfig(llvm::StringRef configurationPath)
Expand Down
3 changes: 2 additions & 1 deletion mock_target/Target.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#ifndef HAL_TARGETS_MOCK_TARGET_H
#define HAL_TARGETS_MOCK_TARGET_H

#include "MockTarget.h"

namespace qssc::targets::mock {

int init();
[[maybe_unused]] int registrar = init();

} // namespace qssc::targets::mock
Expand Down

0 comments on commit 01f49da

Please sign in to comment.