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