* Load, store, save program settings.
*/
-#include <map>
#include <string>
+#include <vector>
-#include <Moof/Serializable.hh>
+#include <boost/algorithm/string.hpp>
+
+#include <Moof/Log.hh>
+#include <Moof/Script.hh>
namespace Mf {
class Settings
{
public:
- Settings() {}
- Settings(int argc, char* argv[]);
+
+ Settings() :
+ mGlobals(mScript.getGlobalTable()),
+ mTop(mScript[-1])
+ {
+ importLogScript(mScript);
+ }
// get global instance
static Settings& getInstance();
void parseArgs(int argc, char* argv[]);
- void loadFromFile(const std::string& filePath, bool precedence = false);
- void loadFromFiles(const std::vector<std::string>& filePaths,
- bool precedence = false);
+ void loadFromFile(const std::string& filePath);
+ void loadFromFiles(const std::vector<std::string>& filePaths);
template <typename T>
bool get(const std::string& key, T& value);
- template <typename T>
- bool getNumber(const std::string& key, T& value);
private:
- std::map<std::string,SerializablePtr> map_;
+
+ Script mScript;
+ Script::Value mGlobals, mTop;
};
template <typename T>
bool Settings::get(const std::string& key, T& value)
{
- std::map<std::string,SerializablePtr>::const_iterator it = map_.find(key);
+ std::vector<std::string> fields;
+ boost::split(fields, key, boost::is_any_of("."));
- if (it != map_.end())
- {
- SerializablePtr obj = (*it).second;
- return obj->get(value);
- }
- else
- {
- return false;
- }
-}
-
-template <typename T>
-bool Settings::getNumber(const std::string& key, T& value)
-{
- std::map<std::string,SerializablePtr>::const_iterator it = map_.find(key);
+ mGlobals.pushCopy();
- if (it != map_.end())
- {
- SerializablePtr obj = (*it).second;
- return obj->getNumber(value);
- }
- else
+ std::vector<std::string>::iterator it;
+ for (it = fields.begin(); it != fields.end(); ++it)
{
- return false;
+ if (mTop.isTable())
+ {
+ mTop.pushField(*it);
+ }
+ else
+ {
+ mScript.clear();
+ return false;
+ }
}
+
+ bool got = mTop.get(value);
+ mScript.clear();
+ return got;
}