Skip to content

Commit fa12b11

Browse files
committed
refactor: extract config base class
1 parent 68d7ad7 commit fa12b11

File tree

6 files changed

+41
-75
lines changed

6 files changed

+41
-75
lines changed

source/cli/Config/Config.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
#include "Config.h"
22

3+
bool Config::load()
4+
{
5+
auto config_opt = json::open(_target_path);
6+
if (!config_opt) {
7+
std::cerr << "Failed to open config file: " << _target_path << std::endl;
8+
return false;
9+
}
10+
11+
auto& config = *config_opt;
12+
if (!config.is_object()) {
13+
std::cerr << "Json is not an object: " << config << std::endl;
14+
return false;
15+
}
16+
17+
if (!parse(config)) {
18+
std::cerr << "Failed to parse control: " << config << std::endl;
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
bool Config::save(const json::value& root)
26+
{
27+
std::filesystem::create_directories(_target_path.parent_path());
28+
std::ofstream ofs(_target_path, std::ios::out);
29+
if (!ofs.is_open()) {
30+
std::cerr << "Failed to open config file: " << _target_path << std::endl;
31+
return false;
32+
}
33+
ofs << root;
34+
ofs.close();
35+
return true;
36+
}
37+
338
bool Config::dump()
439
{
540
return save(to_json());

source/cli/Config/Config.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <filesystem>
4+
#include <iostream>
35
#include <string>
46

57
#include "../meojson/json.hpp"
@@ -11,9 +13,9 @@ class Config
1113

1214
public:
1315
virtual bool parse(const json::value& config_json) = 0;
14-
virtual bool load() = 0;
16+
virtual bool load();
1517
virtual json::value to_json() = 0;
16-
virtual bool save(const json::value& root) = 0;
18+
virtual bool save(const json::value& root);
1719
bool dump();
1820

1921
public:
@@ -24,4 +26,5 @@ class Config
2426

2527
protected:
2628
std::string _target = "default";
29+
std::filesystem::path _target_path = ".";
2730
};

source/cli/Config/ControlConfig.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,13 @@ bool ControlConfig::parse(const json::value& config_opt)
1414
return true;
1515
}
1616

17-
bool ControlConfig::load()
18-
{
19-
auto config_opt = json::open(_target_path);
20-
if (!config_opt) {
21-
std::cerr << "Failed to open control config file: " << _target_path << std::endl;
22-
return false;
23-
}
24-
25-
auto& config = *config_opt;
26-
if (!config.is_object()) {
27-
std::cerr << "Json is not an object: " << config << std::endl;
28-
return false;
29-
}
30-
31-
if (!parse(config)) {
32-
std::cerr << "Failed to parse control: " << config << std::endl;
33-
return false;
34-
}
35-
36-
return true;
37-
}
38-
3917
json::value ControlConfig::to_json()
4018
{
4119
json::value root;
4220
root = { { "server", _config_server }, { "screencap", _config_screencap }, { "touch", _config_touch } };
4321
return root;
4422
}
4523

46-
bool ControlConfig::save(const json::value& root)
47-
{
48-
std::filesystem::create_directories(config_dir);
49-
std::ofstream ofs(_target_path, std::ios::out);
50-
if (!ofs.is_open()) {
51-
std::cerr << "Failed to open control config file: " << _target_path << std::endl;
52-
return false;
53-
}
54-
ofs << root;
55-
ofs.close();
56-
return true;
57-
}
58-
5924
void ControlConfig::set_config_server(int server)
6025
{
6126
_config_server = server;

source/cli/Config/ControlConfig.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class ControlConfig : public Config
1818

1919
public:
2020
bool parse(const json::value& config_json) override;
21-
bool load() override;
2221
json::value to_json() override;
23-
bool save(const json::value& root) override;
2422

2523
public:
2624
void set_config_server(int server);

source/cli/Config/DeviceConfig.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DeviceConfig::DeviceConfig()
44
{
55
build_target_path();
66
}
7+
78
bool DeviceConfig::parse(const json::value& config_opt)
89
{
910
_config_adb = config_opt.get("adb", _config_adb);
@@ -12,27 +13,6 @@ bool DeviceConfig::parse(const json::value& config_opt)
1213

1314
return true;
1415
}
15-
bool DeviceConfig::load()
16-
{
17-
auto config_opt = json::open(_target_path);
18-
if (!config_opt) {
19-
std::cerr << "Failed to open device config file: " << _target_path << std::endl;
20-
return false;
21-
}
22-
23-
auto& config = *config_opt;
24-
if (!config.is_object()) {
25-
std::cerr << "Json is not an object: " << config << std::endl;
26-
return false;
27-
}
28-
29-
if (!parse(config)) {
30-
std::cerr << "Failed to parse control: " << config << std::endl;
31-
return false;
32-
}
33-
34-
return true;
35-
}
3616

3717
json::value DeviceConfig::to_json()
3818
{
@@ -43,19 +23,6 @@ json::value DeviceConfig::to_json()
4323
return root;
4424
}
4525

46-
bool DeviceConfig::save(const json::value& root)
47-
{
48-
std::filesystem::create_directories(config_dir);
49-
std::ofstream ofs(_target_path, std::ios::out);
50-
if (!ofs.is_open()) {
51-
std::cerr << "Failed to open device config file: " << _target_path << std::endl;
52-
return false;
53-
}
54-
ofs << root;
55-
ofs.close();
56-
return true;
57-
}
58-
5926
void DeviceConfig::set_config_device_name(std::string name)
6027
{
6128
_config_device_name = name;

source/cli/Config/DeviceConfig.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class DeviceConfig : public Config
1818

1919
public:
2020
bool parse(const json::value& config_opt) override;
21-
bool load() override;
2221
json::value to_json() override;
23-
bool save(const json::value& root) override;
2422

2523
public:
2624
void set_config_device_name(std::string name);

0 commit comments

Comments
 (0)