-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigManager.cpp
71 lines (53 loc) · 2.19 KB
/
ConfigManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Created by Balajanovski on 2/12/2017.
//
#include "Util/RGB.h"
#include "ConfigManager.h"
#include <stdexcept>
#include <iostream>
#include <string>
#include "Util/RandomNumberGen.h"
ConfigManager& ConfigManager::instance() {
static ConfigManager instance;
return instance;
}
void ConfigManager::load_file(const std::string& config_path) {
m_config_path = config_path;
m_config = YAML::LoadFile(m_config_path);
m_width = m_config["width"].as<int>();
m_height = m_config["height"].as<int>();
m_cell_size = m_config["cell-size"].as<int>();
m_ant_number = m_config["ant-number"].as<int>();
m_thread_number = m_config["thread-number"].as<size_t>();
m_background_color = m_config["background-color"].as<RGB>();
m_machine_generated_colors = m_config["machine-generated-colors"].as<bool>();
if (!m_machine_generated_colors) {
auto unprocessed_custom_colors = m_config["ant-colors"];
for (int i = 0; i < unprocessed_custom_colors.size(); ++i)
{
auto unprocessed_color = (unprocessed_custom_colors[i])["color"];
if (unprocessed_color.IsMap()) {
auto color = unprocessed_color.as<RGB>();
m_tile_colors.push_back(color);
}
else if (unprocessed_color.IsDefined() && unprocessed_color.as<std::string>() == "random") {
auto color = RGB{static_cast<unsigned char>(RandomNumberGen::rng(0, 255)),
static_cast<unsigned char>(RandomNumberGen::rng(0, 255)),
static_cast<unsigned char>(RandomNumberGen::rng(0, 255))};
m_tile_colors.push_back(color);
}
}
}
m_config_loaded = true;
}
void ConfigManager::validate_field_access(const std::string& field) const {
if (!m_config_loaded) {
throw std::runtime_error("error: field " + field + " of ConfigManager accessed before file was loaded");
}
}
std::vector<RGB> ConfigManager::get_custom_colors() const {
if (m_machine_generated_colors) {
throw std::runtime_error("error: attempted access of custom colors when machine generated colors flag is enabled");
}
return m_tile_colors;
}