/*] Copyright (c) 2009-2010, Charles McGarvey [************************** **] All rights reserved. * * vi:ts=4 sw=4 tw=75 * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * **************************************************************************/ #ifndef _MOOF_SETTINGS_HH_ #define _MOOF_SETTINGS_HH_ /** * @file Settings.hh * Load, store, save program settings. */ #include #include #include #include #include namespace Mf { class Settings { public: Settings(int argc, char* argv[], const std::string& path) { mScript.importBaseLibrary(); importLogFunctions(mScript); parseArgs(argc, argv); loadFromFiles(path); } ~Settings(); void parseArgs(int argc, char* argv[]); void loadFromFiles(const std::string& path); void loadFromFiles(const std::vector& path); void clear(); // remove all settings void saveAs(const std::string& path); void save() const; template bool get(const std::string& key, T& value) const; private: mutable Script mScript; std::string mUserFile; }; template bool Settings::get(const std::string& key, T& value) const { Script::Slot top = mScript[-1]; Script::Slot globals = mScript.globals(); std::vector fields; boost::split(fields, key, boost::is_any_of(".")); globals.pushCopy(); std::vector::iterator it; for (it = fields.begin(); it != fields.end(); ++it) { if (top.isTable()) { top.pushField(*it); } else { mScript.clearStack(); return false; } } bool got = top.get(value); mScript.clearStack(); return got; } } // namepsace Mf #endif // _MOOF_SETTINGS_HH_