X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FSettings.hh;h=9b3e0cb27146bbc3a1a99c91a8c76f56dad3d8d0;hp=9c29007613f90d6b35a7c8500208581dae05a5a5;hb=71bd9dbaf1c1e3c55a9f63392a73865d8aeee7d4;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Settings.hh b/src/Moof/Settings.hh index 9c29007..9b3e0cb 100644 --- a/src/Moof/Settings.hh +++ b/src/Moof/Settings.hh @@ -34,68 +34,75 @@ * Load, store, save program settings. */ -#include #include +#include -#include -#include +#include + +#include +#include namespace Mf { -class Settings : public Singleton +class Settings { public: - Settings() {} - Settings(int argc, char* argv[]); + + Settings() : + mGlobals(mScript.getGlobalTable()), + mTop(mScript[-1]) + { + mScript.importBaseLibrary(); + importLogPrintFunction(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& filePaths, - bool precedence = false); + void loadFromFile(const std::string& filePath); + void loadFromFiles(const std::vector& filePaths); + + void clear(); // remove all settings template bool get(const std::string& key, T& value); - template - bool getNumber(const std::string& key, T& value); private: - std::map map_; + + Script mScript; + Script::Value mGlobals, mTop; }; template bool Settings::get(const std::string& key, T& value) { - std::map::const_iterator it = map_.find(key); + std::vector fields; + boost::split(fields, key, boost::is_any_of(".")); - if (it != map_.end()) - { - SerializablePtr obj = (*it).second; - return obj->get(value); - } - else - { - return false; - } -} + mGlobals.pushCopy(); -template -bool Settings::getNumber(const std::string& key, T& value) -{ - std::map::const_iterator it = map_.find(key); - - if (it != map_.end()) - { - SerializablePtr obj = (*it).second; - return obj->getNumber(value); - } - else + std::vector::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; }