[英]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;
};
因此,當我啟動應用程序時,我逐行讀取配置文件並添加到我的Config類中:
//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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.