* 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() :
+ globals_(script_.getGlobalTable()),
+ top_(script_[-1])
+ {
+ importLogScript(script_);
+ }
// 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 script_;
+ Script::Value globals_, top_;
};
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);
+ globals_.pushCopy();
- if (it != map_.end())
+ std::vector<std::string>::iterator it;
+ for (it = fields.begin(); it != fields.end(); ++it)
{
- SerializablePtr obj = (*it).second;
- return obj->getNumber(value);
- }
- else
- {
- return false;
+ if (top_.isTable())
+ {
+ top_.pushField(*it);
+ }
+ else
+ {
+ script_.clear();
+ return false;
+ }
}
+
+ bool got = top_.get(value);
+ script_.clear();
+ return got;
}