I need to define a Configuration object. I am using boost::property_tree
for the impl, but I do not want to expose Impl details in my interface, or have to #include
boost files in my interface.
The problem is that I would like to use the template method of ptree for getting values,
template <typename T>
T get(const std::string key)
But this is impossible (?)
// Non compilable code (!)
// Interface
class Config {
public:
template <typename T>
T get(const std::string& key);
}
// Impl
#include "boost/property_tree.h"
class ConfigImpl : public Config {
public:
template <typename T>
T get(const std::string& key) {
return m_ptree.get(key);
}
private:
boost::ptree m_ptree;
}
One option is to limit the types I can "get", for instance:
// Interface
class Config {
public:
virtual int get(const std::string& key) = 0;
virtual const char* get(const std::string& key) = 0;
}
// Impl
#include "boost/property_tree.h"
class ConfigImpl : public Config {
public:
virtual int get(const std::string& key) { return m_ptree.get<int>(key) };
virtual const char* get(const std::string& key) { return m_ptree.get<const char*>(key); }
private:
boost::ptree m_ptree;
}
But this is pretty ugly and not scalable.
Any better options out there?