我知道标题没有意义,找不到更好的东西。
我需要为SQlite表提供一个C ++接口,我可以在其中存储键/值/类型配置设置,例如
Key | Value | Type
PATH | /path/to/ | STRING
HAS_FEATURE | Y | BOOLEAN
REFRESH_RATE| 60 | INTEGER
为了简单和灵活的目的,datamodel将值作为字符串托管,但提供了一列来保留原始数据类型。
这就是我想象客户端调用这样的c ++接口的方式。
Configuration c;
int refreshRate = c.get<int>("REFRESH_RATE");
// Next line throws since type won't match
std::string refreshRate = c.get<std::string>("REFRESH_RATE");
这就是我想象的实现方式(我知道代码不会按原样编译,将其视为伪c ++,我更多地质疑设计而不是语法)
class Parameter
{
public:
enum KnownTypes
{
STRING = 0,
BOOLEAN,
INTEGER,
DOUBLE,
...
}
std::string key;
std::string value;
KnownTypes type;
}
class Configuration
{
public:
template<class RETURNTYPE>
RETURNTYPE get(std::string& key)
{
// get parameter(eg. get cached value or from db...)
const Parameter& parameter = retrieveFromDbOrCache(key);
return <parameter.type, RETURNTYPE>getImpl(parameter);
}
private:
template<int ENUMTYPE, class RETURNTYPE>
RETURNTYPE getImpl(const Parameter& parameter)
{
throw "Tthe requested return type does not match with the actual parameter's type"; // shall never happen
}
template<Parameter::KnownTypes::STRING, std::string>
std::string getImpl(const Parameter& parameter)
{
return parameter.value;
}
template<Parameter::KnownTypes::BOOLEAN, bool>
std::string getImpl(const Parameter& parameter)
{
return parameter.value == "Y";
}
template<Parameter::KnownTypes::INTEGER, int>
int getImpl(const Parameter& parameter)
{
return lexical_cast<int>(parameter.value)
}
// and so on, specialize once per known type
};
这是一个很好的实施吗?有关如何改进它的任何建议吗?
我知道我可以直接为每个返回类型设置公共get
,但我会在每个模板专门化中重复一些代码(类型一致性检查以及参数检索)
答案 0 :(得分:2)
如果您尝试实施它,您的方法将会失败!问题是:
return <parameter.type, RETURNTYPE>getImpl(parameter);
或使用正确的C ++语法:
return getImpl<parameter.type, RETURNTYPE>(parameter);
模板参数需要编译时间常量,parameter.type
不是!所以你必须尝试这样的事情:
switch(parameter.type)
{
case STRING:
return getImpl<STRING, RETURNTYPE>(parameter);
//...
}
看起来你没有获得任何东西,是吗?
你可以尝试反过来,专门研究getter本身:
public:
template<class RETURNTYPE>
RETURNTYPE get(std::string const& key);
template<>
std::string get<std::string>(std::string const& key)
{
return getImpl<STRING>(key);
}
template<>
int get<int>(std::string const& key)
{
return lexical_cast<int>(getImpl<STRING>(key));
}
private:
template<KnownTypes Type>
std::string getImpl(std::string const& key)
{
Parameter parameter = ...;
if(parameter.type != Type)
throw ...;
return parameter.value;
}
或没有模板(参考Nim的评论......):
public:
int getInt(std::string const& key)
{
return lexical_cast<int>(getImpl(STRING, key));
}
private:
inline std::string getImpl(KnownTypes type, std::string const& key)
{
Parameter parameter = ...;
if(parameter.type != type)
throw ...;
return parameter.value;
}
您可能注意到的一个变化:我修复了参数的常量......
附注:类范围内不允许使用上述模板特化(上面是为了简洁而写的)。在您的真实代码中,您必须将专业化移出课堂:
struct S { template<typename T> void f(T t); };
template<> void S::f<int>(int t) { }
答案 1 :(得分:1)
除了接受的答案之外,我想添加一个演示版,但在验证类型的正确性方面没有什么区别,而无需在所有模板专业版上进行样板编写。同样纠正不允许的类范围中的显式模板特化。
class Parameter {
public:
enum KnownTypes { STRING = 0, BOOLEAN, INTEGER, DOUBLE };
std::string key;
std::string value;
KnownTypes type;
};
class Configuration {
public:
template <class RETURNTYPE>
RETURNTYPE get(std::string const& key) {
// get parameter(eg. get cached value or from db...)
std::map<std::string, Parameter> map{
{"int", Parameter{"int", "100", Parameter::KnownTypes::INTEGER}},
{"string", Parameter{"string", "string_value", Parameter::KnownTypes::STRING}},
{"throwMe", Parameter{"throwMe", "throw", Parameter::KnownTypes::DOUBLE}},
{"bool", Parameter{"bool", "Y", Parameter::KnownTypes::BOOLEAN}}};
const Parameter& parameter = map.at(key);
bool isMatchingType = false;
switch (parameter.type) {
case Parameter::STRING:
isMatchingType = std::is_same<RETURNTYPE, std::string>::value;
break;
case Parameter::BOOLEAN:
isMatchingType = std::is_same<RETURNTYPE, bool>::value;
break;
case Parameter::INTEGER:
isMatchingType = std::is_same<RETURNTYPE, int>::value;
break;
case Parameter::DOUBLE:
isMatchingType = std::is_same<RETURNTYPE, double>::value;
break;
};
if (!isMatchingType)
throw "Tthe requested return type does not match with the actual parameter's type";
return getImpl<RETURNTYPE>(parameter);
}
private:
template <class RETURNTYPE>
RETURNTYPE getImpl(const Parameter& parameter);
};
template <>
std::string Configuration::getImpl<std::string>(const Parameter& parameter) {
return parameter.value;
}
template <>
bool Configuration::getImpl<bool>(const Parameter& parameter) {
return parameter.value == "Y";
}
template <>
int Configuration::getImpl<int>(const Parameter& parameter) {
return std::stoi(parameter.value);
}
int main() {
Configuration conf;
cerr << conf.get<int>("int") << endl;
cerr << conf.get<bool>("bool") << endl;
cerr << conf.get<string>("string") << endl;
cerr << conf.get<string>("throwMe") << endl;
return 0;
}