C++返回未知类型
C++ returning an unknown type
我正在创建一个Configuration类,它将保存我的用户应用程序的配置,并以字符串的形式从文件中读取。
class ConfigKey
{
public:
string KeyLabel; //Will be used to identify this key
string KeyValue; //The value
bool IsEditable; //For developing uses only, I'm saving a few default and non editable keys for specific apps here
};
class Configuration
{
public:
void AddKey(char* keyLabel, char* keyValue, bool isEditable);
private:
vector<ConfigKey> configKeys;
};
因此,当我启动应用程序时,我逐行读取配置文件,并将其添加到我的配置类中:
//Constructor
Configuration::Configuration()
{
//read from file, examples
AddKey("windowWidth", "1024", false);
AddKey("windowHeight", "768", false);
}
现在我想在其他地方检索这些值,以便在应用程序中使用,有没有办法将强制转换留给Configuration类?类似这样的东西:
//In the Configuration class
void* GetKey(char* keyLabel);
//And when I call it, I'd like to do something like this:
int windowAspectRatio = myApp.config.GetKey("windowWidth") / myApp.config.GetKey("windowHeight");
原因是,在使用配置值之前,代码中其他地方没有一堆字符串流来转换配置值。我会把configKey的类型也保存在configKey中,这样它就可以自动转换自己。
有什么建议吗?
编辑以澄清:
我想使用以下方法检索configKey:
//In the Configuration Class
public:
int GetKey(char* keyLabel)
{
//the value I saved in ConfigKey is a "string" type, but I'm converting it to Int before I return it
//loop through the vector, find the keyLabel
stringstream mySS(foundKey.KeyValue);
int returnValue = 0;
mySS >> returnValue; //converted the string to int
return returnValue; //returned an int
}
所以在代码的其他地方我可以调用:
int myWidth = myConfig.GetKey("windowWidth"); //It's already converted
但我可以有多个configKeys,它们要么是int,要么是float、bool或其他东西。我正在寻找一种方法,让GetKey(char*keyLabel)检查keyType,然后转换它,然后返回它。
或者任何关于更好解决方案的建议!
为此使用boost::variant
。
Boost变体允许您表示多个类型,条件是每个类型都是可复制的。
boost::get
将允许您从变体中获取类型。
用法示例:
using string_or_int = boost::variant<std::string, int>;
std::vector<string_or_int> data;
std::string& s = boost::get<std::string>(data[0]);
你可以使用这个概念制作一个模板来获得你想要的任何配置信息:
template<typename T> T get_config_data(const std::string& key) {
return boost::get<T>( some_variant );
}
用法:
std::string window_name = config.get_config_data<std::string>("WindowName");
int window_width = config.get_config_data<int>("WindowWidth");
int window_height = config.get_config_data<int>("WindowHeight");
你能使用模板吗?以下代码可能不完全正确,但至少应该帮助您评估这是否是一个适合您的解决方案:
//In the Configuration Class
public:
template< typename T >
T GetKey(char* keyLabel)
{
stringstream mySS( foundKey.KeyValue );
T returnValue;
mySS >> returnValue;
return returnValue;
}
// elsewhere
int myWidth = myConfig.GetKey< int >("windowWidth");
您可以覆盖ConfigKey类的转换运算符并返回它的实例。
class ConfigKey
{
public:
string KeyLabel; //Will be used to identify this key
string KeyValue; //The value
bool IsEditable; //For developing uses only, I'm saving a few default and non editable keys for specific apps here
operator int(); //Returns an integer representation of KeyValue
};
您可能必须显式执行转换,以防编译器抱怨:
//In the Configuration class
void* GetKey(char* keyLabel);
//And when I call it, I'd like to do something like this:
int windowAspectRatio = (int)myApp.config.GetKey("windowWidth") / (int)myApp.config.GetKey("windowHeight");
如果转换中出现错误(例如字符串不代表数字),则可以引发异常。此外,如果"windowHeight"的值不为0,也可以进行检查。
相关文章:
- OpenCV C++:当垫子类型未知时无法访问垫子元素?
- 参数数据类型未知的可变参数函数
- 矢量迭代器类型未知
- 模板函数的返回类型未知,使用 decltype 时代码重复
- 使用不同类型 /未知类型的类模板初始化向量
- 当类型未知时调用模板方法
- 当类类型未知时,指向非静态成员函数的函数指针
- 如何在数据类型未知的C++中创建模板类
- 组件 X 的超类型未知,其某些属性未经验证
- 当数据类型未知时如何声明数组
- 如何在对象类型未知时实现交换函数
- 类成员是类型未知的派生对象
- 模板中的返回类型未知
- 在运行时之前调用类型未知的模板化函数
- 当类类型未知时调用成员函数
- 在编译时使用类型未知的对象初始化
- 处理运行时类型未知的模板
- 模板类的返回类型未知
- 当编译时数据格式和类型未知时,C++将数据读入内存
- 属性类型未知的模板类中的默认构造函数