C++返回未知类型

C++ returning an unknown type

本文关键字:类型 未知 返回 C++      更新时间:2023-10-16

我正在创建一个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,要么是floatbool或其他东西。我正在寻找一种方法,让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,也可以进行检查。