Skip to content

Commit

Permalink
feat: Config Mgr
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 19, 2023
1 parent 0102c83 commit 7d906be
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ add_executable(MAABH3_CLI
Config/DeviceConfig.cpp
Config/TasksConfig.h
Config/TasksConfig.cpp
Config/ConfigMgr.h
Config/ConfigMgr.cpp
)
target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit)

Expand Down
57 changes: 57 additions & 0 deletions source/cli/Config/ConfigMgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "ConfigMgr.h"

ConfigMgr& ConfigMgr::get_instance()
{
static ConfigMgr instance;
return instance;
}

bool ConfigMgr::init()
{
if (!_control.load()) {
std::cerr << "Failed to load control config" << std::endl;
return false;
}
if (!_device.load()) {
std::cerr << "Failed to load device config" << std::endl;
return false;
}
if (!_tasks.load()) {
std::cerr << "Failed to load tasks config" << std::endl;
return false;
}
return true;
}

bool ConfigMgr::set_config_target(ConfigOption option, std::string target)
{
switch (option) {
case ConfigOption_Control:
_control.set_target(target);
break;
case ConfigOption_Device:
_device.set_target(target);
break;
case ConfigOption_Tasks:
_tasks.set_target(target);
break;
default:
return false;
}
return true;
}

ControlConfig& ConfigMgr::get_control_config()
{
return _control;
}

DeviceConfig& ConfigMgr::get_device_config()
{
return _device;
}

TasksConfig& ConfigMgr::get_tasks_config()
{
return _tasks;
}
38 changes: 38 additions & 0 deletions source/cli/Config/ConfigMgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <iostream>

#include "ControlConfig.h"
#include "DeviceConfig.h"
#include "TasksConfig.h"

enum ConfigOption
{
ConfigOption_Control = 0,
ConfigOption_Device = 1,
ConfigOption_Tasks = 2
};

class ConfigMgr
{
public:
static ConfigMgr& get_instance();

public:
bool init();
bool set_config_target(ConfigOption option, std::string target);

public:
ControlConfig& get_control_config();
DeviceConfig& get_device_config();
TasksConfig& get_tasks_config();

private:
ConfigMgr() = default;
~ConfigMgr() = default;

private:
ControlConfig _control;
DeviceConfig _device;
TasksConfig _tasks;
};

0 comments on commit 7d906be

Please sign in to comment.