Skip to content

Commit abcebd8

Browse files
committed
Added documentation to Config functions
1 parent 4e405c6 commit abcebd8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

include/Config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include "Util.h"
1010

11+
/**
12+
* A configuration value stored in a configuration file.
13+
* These can auto-convert to several common types that are used.
14+
*/
1115
class ConfigValue {
1216
private:
1317
std::string value;
@@ -25,6 +29,14 @@ class ConfigValue {
2529
operator ScreenCoordinate() const;
2630
};
2731

32+
/**
33+
* Configuration storage. Reads in configuration files in the format:
34+
*
35+
* #Comment
36+
* name=value
37+
*
38+
* Particular configuration values can be accessed by name and automatically to convert to common types.
39+
*/
2840
class Config {
2941
private:
3042
std::map<std::string, ConfigValue> values;

src/Config.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,35 @@ using std::runtime_error;
1111
using std::istream;
1212
using std::fstream;
1313

14+
/**
15+
* Initialize a ConfigValue with the raw string it represents.
16+
* @param value The raw value.
17+
*/
1418
ConfigValue::ConfigValue(const string& value) : value(value) {
1519

1620
}
1721

22+
/**
23+
* Convert the ConfigValue into a float.
24+
* @return The text of the ConfigValue, interpreted as a float.
25+
*/
1826
ConfigValue::operator float() const {
1927
return fromString<float>(value);
2028
}
2129

30+
/**
31+
* Convert the ConfigValue into a string.
32+
* @return The text of the ConfigValue.
33+
*/
2234
ConfigValue::operator string() const {
2335
return value;
2436
}
2537

38+
/**
39+
* Convert the ConfigValue into a game Coordinate.
40+
* @throws runtime_error If the value cannot be converted.
41+
* @return The text of the ConfigValue, interpreted as a game Coordinate.
42+
*/
2643
ConfigValue::operator Coordinate() const {
2744
stringstream stream(value);
2845
Coordinate ret;
@@ -39,6 +56,11 @@ ConfigValue::operator Coordinate() const {
3956
return ret;
4057
}
4158

59+
/**
60+
* Convert the ConfigValue into a ScreenCoordinate.
61+
* @throws runtime_error If the value cannot be converted.
62+
* @return The text of the ConfigValue, interpreted as a ScreenCoordinate.
63+
*/
4264
ConfigValue::operator ScreenCoordinate() const {
4365
stringstream stream(value);
4466
ScreenCoordinate ret;
@@ -55,19 +77,35 @@ ConfigValue::operator ScreenCoordinate() const {
5577
return ret;
5678
}
5779

80+
/**
81+
* Initialize a Config file, reading from an input stream.
82+
* @param source The stream to read config values from.
83+
*/
5884
Config::Config(istream& source) {
5985
init(source);
6086
}
6187

88+
/**
89+
* Initialize a Config file, reading from a file stream.
90+
* @param source The file stream to read config values from.
91+
*/
6292
Config::Config(fstream&& source) {
6393
init(source);
6494
}
6595

96+
/**
97+
* Initialize a Config file, reading from a given file.
98+
* @param filename The path to the file to read.
99+
*/
66100
Config::Config(const string& filename) {
67101
fstream source(filename, fstream::in);
68102
init(source);
69103
}
70104

105+
/**
106+
* Read in configuration values from a stream.
107+
* @param source The place to read configuration values from.
108+
*/
71109
void Config::init(istream& source) {
72110
string line;
73111
while(source) {
@@ -81,6 +119,12 @@ void Config::init(istream& source) {
81119
}
82120
}
83121

122+
/**
123+
* Retrieve a configuration value.
124+
* @throws runtime_error If no configuration value with that name exists.
125+
* @param name The name to retrieve from the configuration file.
126+
* @return The configuration value stored at the given name.
127+
*/
84128
const ConfigValue& Config::operator[](const string& name) const {
85129
auto it = values.find(name);
86130
if(it == values.end()) {
@@ -90,6 +134,13 @@ const ConfigValue& Config::operator[](const string& name) const {
90134
}
91135
}
92136

137+
/**
138+
* Load a configuration file at a given path.
139+
* These configuration files are cached, and only will be loaded once.
140+
* @throws runtime_error If the file cannot be loaded.
141+
* @param filename The path to the file to read in.
142+
* @return The configuration stored at the file.
143+
*/
93144
const Config& getConfigFile(const std::string& filename) {
94145
static std::map<std::string, Config> configs;
95146

@@ -105,6 +156,11 @@ const Config& getConfigFile(const std::string& filename) {
105156
}
106157
}
107158

159+
/**
160+
* Load the graphics configuration file.
161+
* @throws runtime_error If the graphics configuration file is missing.
162+
* @return The graphics configuration.
163+
*/
108164
const Config& getGraphicsConfig() {
109165
return getConfigFile("resources/graphics.conf");
110166
}

0 commit comments

Comments
 (0)