Using pragma once from now on, working on a dynamic library class
[ail.git] / ail / configuration.hpp
blobfcf0ab4d445fad37da371bd49f1c973809fbbeb9
1 #pragma once
3 #include <string>
4 #include <vector>
5 #include <map>
6 #include <iostream>
8 #include <ail/file.hpp>
9 #include <ail/string.hpp>
10 #include <ail/exception.hpp>
12 namespace ail
14 class configuration
17 public:
19 configuration();
20 configuration(std::string const & file_name);
22 bool load(std::string const & new_file_name);
24 std::string string(std::string const & variable_name);
25 std::string string(std::string const & variable_name, std::string const & default_value);
27 template <typename number_type>
28 bool read_number(std::string const & variable_name, number_type & output)
30 std::map<std::string, std::string>::iterator search = values.find(variable_name);
31 if(search == values.end())
33 return false;
36 try
38 output = ail::string_to_number<number_type>(search->second);
40 catch(std::exception const &)
42 std::cout << "Unable to find string value \"" << variable_name << "\" in \"" << file_name << "\"" << std::endl;
43 throw exception("Failed to parse numeric value for a variable");
46 return true;
49 template <typename number_type>
50 number_type number(std::string const & variable_name)
52 number_type output;
53 bool success = read_number<number_type>(variable_name, output);
54 if(success == false)
56 std::cout << "Unable to find string value \"" << variable_name << "\" in \"" << file_name << "\"" << std::endl;
57 throw exception("Missing value for a required variable");
59 return output;
62 template <typename number_type>
63 number_type number(std::string const & variable_name, number_type default_value)
65 number_type output;
66 bool success;
68 try
70 success = read_number<number_type>(variable_name, output);
72 catch(std::exception const &)
74 return default_value;
77 if(success == false)
79 return default_value;
82 return output;
85 private:
87 bool read_string(std::string const & variable_name, std::string & output);
89 std::string file_name;
90 std::map<std::string, std::string> values;