Changeset - b7f06ac35251
[Not reviewed]
0 2 0
Jan Kaluza - 13 years ago 2012-10-03 16:40:11
hanzz.k@gmail.com
CONFIG_*_DEFAULTED now uses safeAs and should be used everywhere instead of CONFIG_* macros
2 files changed with 34 insertions and 3 deletions:
0 comments (0 inline, 0 general)
include/transport/config.h
Show inline comments
 
@@ -19,34 +19,48 @@
 
 */
 

	
 
#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>
 

	
 
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)
 
#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) ? (*PTR)[KEY].as<std::string>() : DEF)
 
#define CONFIG_BOOL_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<bool>() : DEF)
 
#define CONFIG_LIST_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<std::list<std::string> >() : DEF)
 
#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)
 

	
 

	
 
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
src/tests/config.cpp
Show inline comments
 
@@ -19,24 +19,26 @@
 
#include "basictest.h"
 

	
 
#include "transport/util.h"
 

	
 
using namespace Transport;
 

	
 
class ConfigTest : public CPPUNIT_NS :: TestFixture{
 
	CPPUNIT_TEST_SUITE(ConfigTest);
 
	CPPUNIT_TEST(setStringTwice);
 
	CPPUNIT_TEST(updateBackendConfig);
 
	CPPUNIT_TEST(unregisteredList);
 
	CPPUNIT_TEST(unregisteredString);
 
	CPPUNIT_TEST(unregisteredListAsString);
 
	CPPUNIT_TEST(unregisteredStringAsList);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
		void setUp (void) {
 
		}
 

	
 
		void tearDown (void) {
 

	
 
		}
 

	
 
	void setStringTwice() {
 
		char *argv[3] = {"binary", "--service.jids=localhost", NULL};
 
@@ -60,15 +62,30 @@ class ConfigTest : public CPPUNIT_NS :: TestFixture{
 
		std::istringstream ifs("service.irc_server = irc.freenode.org\nservice.irc_server=localhost\n");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(2, (int) CONFIG_LIST(&cfg, "service.irc_server").size());
 
	}
 

	
 
	void unregisteredString() {
 
		Config cfg;
 
		std::istringstream ifs("service.irc_server = irc.freenode.org");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(std::string("irc.freenode.org"), CONFIG_STRING(&cfg, "service.irc_server"));
 
	}
 

	
 
	void unregisteredListAsString() {
 
		Config cfg;
 
		std::istringstream ifs("service.irc_server = irc.freenode.orgn\nservice.irc_server = irc2.freenode.org");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), CONFIG_STRING_DEFAULTED(&cfg, "service.irc_server", ""));
 
	}
 

	
 
	void unregisteredStringAsList() {
 
		Config cfg;
 
		std::istringstream ifs("service.irc_server = irc.freenode.org");
 
		cfg.load(ifs);
 
		std::list<std::string> list;
 
		CPPUNIT_ASSERT_EQUAL(0, (int) CONFIG_LIST_DEFAULTED(&cfg, "service.irc_server", list).size());
 
	}
 

	
 
};
 

	
 
CPPUNIT_TEST_SUITE_REGISTRATION (ConfigTest);
0 comments (0 inline, 0 general)