]> Dogcows Code - chaz/yoink/blob - src/moof/settings.hh
c8459cf1ee4b69f9fc2c88c00aaba6f401fb4dbc
[chaz/yoink] / src / moof / settings.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_SETTINGS_HH_
11 #define _MOOF_SETTINGS_HH_
12
13 /**
14 * \file settings.hh
15 * Load, store, save program settings.
16 */
17
18 #include <string>
19 #include <vector>
20
21 #include <boost/algorithm/string.hpp>
22
23 #include <moof/script.hh>
24
25
26 namespace moof {
27
28
29 class settings
30 {
31 public:
32
33 settings(int argc, char* argv[], const std::string& path);
34 ~settings();
35
36 void parse_args(int argc, char* argv[]);
37
38 void load_files(const std::string& path);
39 void load_files(const std::vector<std::string>& path);
40
41 /**
42 * Remove all settings.
43 */
44 void clear();
45
46 void save_as(const std::string& path);
47 void save() const;
48
49 /**
50 * Get a setting by name.
51 * \param key The name of the setting.
52 * \param value A reference to the variable to store the setting.
53 * \return True if the setting exists, false otherwise.
54 */
55 template <class T>
56 bool get(const std::string& key, T& value) const;
57
58 private:
59
60 mutable script script_;
61 std::string userfile_;
62 };
63
64 template <class T>
65 bool settings::get(const std::string& key, T& value) const
66 {
67 script::slot top = script_[-1];
68 script::slot globals = script_.globals();
69
70 std::vector<std::string> fields;
71 boost::split(fields, key, boost::is_any_of("."));
72
73 globals.push_copy();
74
75 std::vector<std::string>::iterator it;
76 for (it = fields.begin(); it != fields.end(); ++it)
77 {
78 if (top.is_table())
79 {
80 top.push_field(*it);
81 }
82 else
83 {
84 script_.clear_stack();
85 return false;
86 }
87 }
88
89 bool got = top.get(value);
90 script_.clear_stack();
91 return got;
92 }
93
94
95 } // namepsace moof
96
97 #endif // _MOOF_SETTINGS_HH_
98
This page took 0.032025 seconds and 3 git commands to generate.