]> Dogcows Code - chaz/yoink/blob - src/Moof/Settings.hh
script API improvements
[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/Log.hh>
26 #include <Moof/Script.hh>
27
28
29 namespace Mf {
30
31
32 class Settings
33 {
34 public:
35
36 Settings(int argc, char* argv[], const std::string& path)
37 {
38 mScript.importBaseLibrary();
39 importLogFunctions(mScript);
40
41 parseArgs(argc, argv);
42 loadFromFiles(path);
43 }
44
45 ~Settings();
46
47 void parseArgs(int argc, char* argv[]);
48
49 void loadFromFiles(const std::string& path);
50 void loadFromFiles(const std::vector<std::string>& path);
51
52 void clear(); // remove all settings
53
54 void saveAs(const std::string& path);
55 void save() const;
56
57 template <class T>
58 bool get(const std::string& key, T& value) const;
59
60 private:
61
62 mutable Script mScript;
63
64 std::string mUserFile;
65 };
66
67
68 template <class T>
69 bool Settings::get(const std::string& key, T& value) const
70 {
71 Script::Slot top = mScript[-1];
72 Script::Slot globals = mScript.globals();
73
74 std::vector<std::string> fields;
75 boost::split(fields, key, boost::is_any_of("."));
76
77 globals.pushCopy();
78
79 std::vector<std::string>::iterator it;
80 for (it = fields.begin(); it != fields.end(); ++it)
81 {
82 if (top.isTable())
83 {
84 top.pushField(*it);
85 }
86 else
87 {
88 mScript.clearStack();
89 return false;
90 }
91 }
92
93 bool got = top.get(value);
94 mScript.clearStack();
95 return got;
96 }
97
98
99 } // namepsace Mf
100
101 #endif // _MOOF_SETTINGS_HH_
102
This page took 0.038043 seconds and 4 git commands to generate.