@@ -11,18 +11,35 @@ using std::runtime_error;
1111using std::istream;
1212using std::fstream;
1313
14+ /* *
15+ * Initialize a ConfigValue with the raw string it represents.
16+ * @param value The raw value.
17+ */
1418ConfigValue::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+ */
1826ConfigValue::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+ */
2234ConfigValue::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+ */
2643ConfigValue::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+ */
4264ConfigValue::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+ */
5884Config::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+ */
6292Config::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+ */
66100Config::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+ */
71109void 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+ */
84128const 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+ */
93144const 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+ */
108164const Config& getGraphicsConfig () {
109165 return getConfigFile (" resources/graphics.conf" );
110166}
0 commit comments