Skip to content

Commit 1466af0

Browse files
committed
minimize Config's public surface
1 parent 27390cd commit 1466af0

2 files changed

Lines changed: 40 additions & 47 deletions

File tree

src/config.cpp

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using namespace std;
2626

2727
constexpr const char* GPHOX_PTX_PATH_ENV = "CSGOptiX__optixpath";
2828

29-
namespace
29+
namespace
3030
{
3131

3232
struct EventModeInfo
@@ -56,8 +56,6 @@ auto FindEventMode(std::string_view name)
5656
return std::ranges::find(EventModeInfos, name, &EventModeInfo::name);
5757
}
5858

59-
} // namespace
60-
6159
bool FileExists(const std::string& path)
6260
{
6361
if (path.empty())
@@ -66,24 +64,55 @@ bool FileExists(const std::string& path)
6664
return std::filesystem::exists(path, ec) && !ec;
6765
}
6866

69-
std::filesystem::path Config::DefaultOutputDir()
67+
std::string ValidEventModes()
7068
{
71-
return std::filesystem::current_path();
69+
std::string names;
70+
for (const auto& info : EventModeInfos)
71+
{
72+
if (!names.empty())
73+
names += ", ";
74+
names += info.name;
75+
}
76+
return names;
77+
}
78+
79+
std::string_view EventModeName(EventMode mode)
80+
{
81+
const auto it = FindEventMode(mode);
82+
if (it != EventModeInfos.end())
83+
return it->name;
84+
85+
return "Minimal";
86+
}
87+
88+
EventMode ReadEventMode(const nlohmann::json& event)
89+
{
90+
std::string name = event["mode"].get<std::string>();
91+
92+
const auto it = FindEventMode(name);
93+
if (it != EventModeInfos.end())
94+
return it->mode;
95+
96+
throw std::invalid_argument{
97+
"Invalid event.mode \"" + std::string{name} + "\". Expected one of: " + ValidEventModes()
98+
};
7299
}
73100

74101
std::filesystem::path ReadOutputDir(const nlohmann::json& event)
75102
{
76103
if (event.contains("output_dir"))
77104
return event["output_dir"].get<std::string>();
78105

79-
return Config::DefaultOutputDir();
106+
return std::filesystem::current_path();
80107
}
81108

109+
} // namespace
110+
82111
Config::Config(std::string config_name) :
83112
name{config_name},
84113
event_mode{EventMode::Minimal},
85114
maxslot{0},
86-
output_dir{DefaultOutputDir()},
115+
output_dir{std::filesystem::current_path()},
87116
torch{}
88117
{
89118
ReadConfig(Locate(name + ".json"));
@@ -155,37 +184,6 @@ std::string Config::Locate(std::string filename) const
155184
return filepath;
156185
}
157186

158-
EventMode Config::ParseEventMode(std::string_view name)
159-
{
160-
const auto it = FindEventMode(name);
161-
if (it != EventModeInfos.end())
162-
return it->mode;
163-
164-
throw std::invalid_argument(
165-
"Invalid event.mode \"" + std::string{name} + "\". Expected one of: " + ValidEventModes());
166-
}
167-
168-
std::string Config::ValidEventModes()
169-
{
170-
std::string names;
171-
for (const auto& info : EventModeInfos)
172-
{
173-
if (!names.empty())
174-
names += ", ";
175-
names += info.name;
176-
}
177-
return names;
178-
}
179-
180-
std::string_view Config::EventModeName(EventMode mode)
181-
{
182-
const auto it = FindEventMode(mode);
183-
if (it != EventModeInfos.end())
184-
return it->name;
185-
186-
return "Minimal";
187-
}
188-
189187
/**
190188
* Expects a valid filepath.
191189
*/
@@ -223,8 +221,8 @@ void Config::ReadConfig(std::string filepath)
223221

224222
nlohmann::json event_ = json["event"];
225223

226-
event_mode = ParseEventMode(event_["mode"].get<std::string>());
227-
maxslot = event_["maxslot"].get<int>();
224+
event_mode = ReadEventMode(event_);
225+
maxslot = event_["maxslot"].get<int>();
228226
output_dir = ReadOutputDir(event_);
229227
}
230228
catch (nlohmann::json::exception& e)
@@ -246,4 +244,5 @@ void Config::Apply() const
246244
SEventConfig::SetMaxSlot(maxslot);
247245
SEventConfig::SetOutFold(output_dir_str.c_str());
248246
}
247+
249248
} // namespace gphox

src/config.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <filesystem>
44
#include <string>
5-
#include <string_view>
65
#include <vector>
76

87
#include "sysrap/srng.h"
@@ -35,12 +34,6 @@ class Config
3534
public:
3635
Config(std::string config_name = "dev");
3736

38-
void Apply() const;
39-
40-
static std::filesystem::path DefaultOutputDir();
41-
static EventMode ParseEventMode(std::string_view name);
42-
static std::string ValidEventModes();
43-
static std::string_view EventModeName(EventMode mode);
4437
static std::string PtxPath(const std::string& ptx_name = "CSGOptiX7.ptx");
4538

4639
/// A unique name associated with this Config
@@ -60,6 +53,7 @@ class Config
6053
private:
6154
std::string Locate(std::string filename) const;
6255
void ReadConfig(std::string filepath);
56+
void Apply() const;
6357
};
6458

6559
} // namespace gphox

0 commit comments

Comments
 (0)