spectrum2/include/transport/Config.h

145 lines
5.3 KiB
C
Raw Permalink Normal View History

/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/assign.hpp>
2011-02-13 22:52:46 +01:00
#include <boost/bind.hpp>
#include <boost/signalslib.hpp>
2011-02-13 22:52:46 +01:00
namespace Transport {
template <class myType>
const myType &safeAs(const boost::program_options::variable_value &var, const myType &def) {
try {
return var.as<myType>();
}
catch(...) {
return def;
}
}
}
#define CONFIG_HAS_KEY(PTR, KEY) (*PTR).hasKey(KEY)
2011-02-13 22:47:28 +01:00
#define CONFIG_STRING(PTR, KEY) (*PTR)[KEY].as<std::string>()
#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as<int>()
#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as<bool>()
#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as<std::list<std::string> >()
#define CONFIG_VECTOR(PTR, KEY) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<std::vector<std::string> >() : std::vector<std::string>())
#define CONFIG_STRING_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? Transport::safeAs<std::string>((*PTR)[KEY], DEF) : DEF)
#define CONFIG_BOOL_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? Transport::safeAs<bool>((*PTR)[KEY], DEF) : DEF)
#define CONFIG_LIST_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? Transport::safeAs<std::list<std::string> >((*PTR)[KEY], DEF) : DEF)
2013-03-07 12:45:47 +01:00
#define CONFIG_INT_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? Transport::safeAs<int>((*PTR)[KEY], DEF) : DEF)
namespace Transport {
2011-02-13 22:47:28 +01:00
2011-03-17 20:31:00 +01:00
/// Represents variable:value pairs.
2011-02-13 22:47:28 +01:00
typedef boost::program_options::variables_map Variables;
2011-02-15 15:48:52 +01:00
/// Represents config file.
/// It's used to load config file and allows others parts of libtransport to be configured
/// properly. Config files are text files which use "ini" format. Variables are divided into multiple
/// sections. Every class is configurable with some variables which change its behavior. Check particular
/// class documentation to get a list of all relevant variables for that class.
2011-02-13 22:47:28 +01:00
class Config {
public:
typedef std::map<std::string, boost::program_options::variable_value> SectionValuesCont;
typedef std::map<std::string, boost::program_options::variable_value> UnregisteredCont;
2011-02-15 15:48:52 +01:00
/// Constructor.
Config(int argc = 0, char **argv = NULL) : m_argc(argc), m_argv(argv) {}
2011-02-15 15:48:52 +01:00
/// Destructor
2011-02-13 22:47:28 +01:00
virtual ~Config() {}
2011-02-15 15:48:52 +01:00
2011-06-06 10:48:27 +02:00
/// Loads data from config file.
/// You can pass your extra options which will be recognized by
2011-02-15 15:48:52 +01:00
/// the parser using opts parameter.
/// \param configfile path to config file
/// \param opts extra options which will be recognized by a parser
bool load(const std::string &configfile, boost::program_options::options_description &opts, const std::string &jid = "");
2011-08-21 13:41:23 +02:00
bool load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &jid = "");
2011-08-21 13:41:23 +02:00
bool load(std::istream &ifs);
2011-02-15 15:48:52 +01:00
2011-06-06 10:48:27 +02:00
/// Loads data from config file.
/// This function loads only config variables needed by libtransport.
2011-02-15 15:48:52 +01:00
/// \see load(const std::string &, boost::program_options::options_description &)
/// \param configfile path to config file
bool load(const std::string &configfile, const std::string &jid = "");
2011-02-13 22:47:28 +01:00
2011-07-09 17:07:44 +02:00
bool reload();
bool hasKey(const std::string &key) {
2012-09-12 13:04:39 +02:00
return (m_variables.find(key) != m_variables.end() || m_unregistered.find(key) != m_unregistered.end()
|| m_backendConfig.find(key) != m_backendConfig.end());
}
2011-06-06 10:48:27 +02:00
/// Returns value of variable defined by key.
/// For variables in sections you can use "section.variable" key format.
2011-02-15 15:48:52 +01:00
/// \param key config variable name
2011-02-13 22:47:28 +01:00
const boost::program_options::variable_value &operator[] (const std::string &key) {
if (m_variables.find(key) != m_variables.end()) {
return m_variables[key];
}
2012-09-12 13:04:39 +02:00
if (m_backendConfig.find(key) != m_backendConfig.end()) {
return m_backendConfig[key];
}
return m_unregistered[key];
2011-02-13 22:47:28 +01:00
}
2011-02-13 22:52:46 +01:00
SectionValuesCont getSectionValues(const std::string& sectionName);
std::string getCommandLineArgs() const;
2011-06-06 10:48:27 +02:00
/// Returns path to config file from which data were loaded.
2011-05-13 10:54:03 +02:00
const std::string &getConfigFile() { return m_file; }
2011-02-15 15:48:52 +01:00
/// This signal is emitted when config is loaded/reloaded.
2011-02-13 22:52:46 +01:00
boost::signal<void ()> onConfigReloaded;
2012-09-12 13:04:39 +02:00
void updateBackendConfig(const std::string &backendConfig);
boost::signal<void ()> onBackendConfigUpdated;
2012-09-12 13:04:39 +02:00
static Config *createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port);
2011-02-13 22:47:28 +01:00
private:
int m_argc;
char **m_argv;
2011-02-13 22:47:28 +01:00
Variables m_variables;
2012-09-12 13:04:39 +02:00
Variables m_backendConfig;
std::map<std::string, boost::program_options::variable_value> m_unregistered;
2011-05-13 10:54:03 +02:00
std::string m_file;
2012-09-12 08:15:10 +02:00
std::string m_jid;
2011-02-13 22:47:28 +01:00
};
}