简体   繁体   English

C ++返回未知类型

[英]C++ returning an unknown type

I'm making a Configuration class that will hold my user application's configuration, and it reads from a file as strings. 我正在创建一个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;
};

So when I start the app, I read the config file line by line and add to my Config class: 因此,当我启动应用程序时,我逐行读取配置文件并添加到我的Config类中:

//Constructor
Configuration::Configuration()
{
    //read from file, examples
    AddKey("windowWidth", "1024", false);
    AddKey("windowHeight", "768", false);
}

Now I want to retrieve these values somewhere else for use in the app, is there a way I can leave the cast for the Configuration class? 现在,我想在其他地方检索这些值以在应用程序中使用,有什么办法可以将转换保留给Configuration类? Something like this: 像这样:

//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");

The reason is so I don't have a bunch of stringstreams elsewhere in the code converting the config values before I can use them. 原因是我在使用配置值之前在转换配置值的代码中没有其他字符串流。 I would save the configKey's type as well in the ConfigKey so it can auto-convert itself. 我还将configKey的类型也保存在ConfigKey中,以便它可以自动转换。

Any advice or suggestions? 有什么建议吗?

Edit for clarification: 编辑以澄清:

I want to retrieve a configKey using this method: 我想使用此方法检索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
    }

So elsewhere in the code I can call: 所以我可以在代码的其他地方调用:

int myWidth = myConfig.GetKey("windowWidth"); //It's already converted

But I can have multiple configKeys that are either int , float , bool or even something else. 但是我可以有多个configKeys,它们可以是intfloatbool或什至其他东西。 I'm looking for a way to have the GetKey(char* keyLabel) to check for the keyType, then convert it, then return it. 我正在寻找一种方法,让GetKey(char * keyLabel)检查keyType,然后将其转换,然后返回。

Or any advice on a better solution! 或任何关于更好解决方案的建议!

Use boost::variant for this. 为此使用boost::variant

Boost variant allows you to represent multiple types with the condition that each type is copyable. Boost变体允许您在每种类型都可复制的条件下表示多种类型。

boost::get will allow you to get a type out of the variant. boost::get将允许您从变量中获取类型。

Usage example: 用法示例:

using string_or_int = boost::variant<std::string, int>;

std::vector<string_or_int> data;

std::string& s = boost::get<std::string>(data[0]);

You can make a template using this concept to get whatever configuration information you want: 您可以使用此概念制作模板,以获取所需的任何配置信息:

template<typename T> T get_config_data(const std::string& key) {
    return boost::get<T>( some_variant );
}

Usage: 用法:

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");

Can you use templates? 可以使用模板吗? The following code may not be totally correct, but should at least help you evaluate whether this is a solution that will work for you: 以下代码可能并不完全正确,但是至少应该可以帮助您评估这是否是适合您的解决方案:

//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");

You can override the conversion operator for ConfigKey class and return an instance of it. 您可以重写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
};

You might have to perform the conversion explicitly in case the compiler complains: 如果编译器抱怨,您可能必须显式执行转换:

//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");

In case of an error in the conversion (eg the string does not represent a number) you can throw an exception. 如果转换出错(例如,字符串不代表数字),则可以引发异常。 Also, would be nice to check if "windowHeight" does not have the value of 0. 此外,很高兴检查“ windowHeight”的值是否为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM