Files
        @ 7102c90a4eb0
    
        
              Branch filter: 
        
    Location: libtransport.git/include/transport/config.h
        
            
            7102c90a4eb0
            3.7 KiB
            text/plain
        
        
    
    Merge branch 'master' of github.com:hanzz/libtransport
    | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | /**
 * 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>
#include <boost/bind.hpp>
#include <boost/signal.hpp>
#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)[KEY].as<std::vector<std::string> >()
#define CONFIG_HAS_KEY(PTR, KEY) (*PTR).hasKey(KEY)
namespace Transport {
/// Represents variable:value pairs.
typedef boost::program_options::variables_map Variables;
/// 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.
class Config {
	public:
		/// Constructor.
		Config() {}
		/// Destructor
		virtual ~Config() {}
		/// Loads data from config file.
		
		/// You can pass your extra options which will be recognized by
		/// 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 = "");
		bool load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &jid = "");
		bool load(std::istream &ifs);
		/// Loads data from config file.
		
		/// This function loads only config variables needed by libtransport.
		/// \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 = "");
		bool reload();
		bool hasKey(const std::string &key) {
			return m_variables.find(key) != m_variables.end();
		}
		/// Returns value of variable defined by key.
		
		/// For variables in sections you can use "section.variable" key format.
		/// \param key config variable name
		const boost::program_options::variable_value &operator[] (const std::string &key) {
			return m_variables[key];
		}
		/// Returns path to config file from which data were loaded.
		const std::string &getConfigFile() { return m_file; }
		const std::map<std::string, std::string> &getUnregistered() {
			return m_unregistered;
		}
		/// This signal is emitted when config is loaded/reloaded.
		boost::signal<void ()> onConfigReloaded;
	
	private:
		Variables m_variables;
		std::map<std::string, std::string> m_unregistered;
		std::string m_file;
};
}
 |