C ++返回一个未知类型

时间:2013-07-18 22:31:58

标签: c++ string type-conversion stringstream

我正在创建一个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;
};

因此,当我启动应用程序时,我逐行阅读配置文件并添加到我的Config类:

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

现在我想在其他地方检索这些值以便在app中使用,有没有办法让我可以为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,然后转换它,然后返回它。

或者有关更好解决方案的任何建议!

3 个答案:

答案 0 :(得分:3)

请使用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");

答案 1 :(得分:1)

你能使用模板吗?以下代码可能不完全正确,但至少应该帮助您评估这是否适合您:

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

答案 2 :(得分:0)

您可以覆盖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。