/*] 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 namespace moof { class settings { public: settings(int argc, char* argv[], const std::string& path); ~settings(); void parse_args(int argc, char* argv[]); void load_files(const std::string& path); void load_files(const std::vector& path); /** * Remove all settings. */ void clear(); void save_as(const std::string& path); void save() const; /** * Get a setting by name. * \param key The name of the setting. * \param value A reference to the variable to store the setting. * \return True if the setting exists, false otherwise. */ template bool get(const std::string& key, T& value) const; private: mutable script script_; std::string mUserFile; }; template bool settings::get(const std::string& key, T& value) const { script::slot top = script_[-1]; script::slot globals = script_.globals(); std::vector fields; boost::split(fields, key, boost::is_any_of(".")); globals.push_copy(); std::vector::iterator it; for (it = fields.begin(); it != fields.end(); ++it) { if (top.is_table()) { top.push_field(*it); } else { script_.clear_stack(); return false; } } bool got = top.get(value); script_.clear_stack(); return got; } } // namepsace moof #endif // _MOOF_SETTINGS_HH_