[英]C++ Variadic parameters
我正在設計一個方便的配置對象,該對象將從文件中加載配置值。 為了確保有合理的默認值,程序員可以為每種值聲明其類型和默認值。 這樣,可以檢查配置文件,並且可以立即發現任何錯誤。 例如,考慮以下配置文件httpd.conf:
port 8080
htdocs ROOT
prelude true
和main中的配置對象:
int main() {
Config conf("httpd.conf",
"port", Config::INT, 80,
"htdocs", Config::STRING, "default/",
"preload", Config::BOOLEAN, false);
上面的代碼將加載文件並驗證port實際上是整數,它將加載htdocs,並且將發現文件中的“ prelude”與配置中的任何注冊值都不匹配,並發出錯誤消息:
第3行:未定義的配置項目“前奏”
我可以使用舊的C可變參數實現上述功能,但這些參數不是類型安全的。 有什么辦法可以使用新的C ++可變參數嗎? 我所看到的例子都是單類型的。 在這里,我擁有三倍的價值。
我想設計一個易於在單個大調用中編寫的東西,但這是類型安全的。
在不使用可變參數模板或函數且避免類型省略的情況下,您可以執行以下操作:
#include <sstream>
#include <stdexcept>
class Configuration
{
public:
Configuration(const std::string& resource)
// Initialize the resources: Program options, environment variables, files
{}
/// Get a raw configuration value for a key.
/// Reurns true if the key exists
bool get_raw(const std::string key, std::string& result) const {
// Find the key in supplied resources and set the result string
// trimming leading and trailing spaces
return false;
}
template <typename T>
T get(const std::string key) const;
template <typename T>
T get(const std::string key, const T& default_value) const;
};
template <typename T>
T Configuration::get(const std::string key) const {
std::string str;
if( ! get_raw(key, str)) throw std::runtime_error("Invalid Key");
else {
T result;
std::istringstream is(str);
is.unsetf(std::ios_base::basefield);
is >> result;
if( ! is.eof() || is.fail()) throw std::runtime_error("Invalid Value");
return result;
}
}
template <typename T>
T Configuration::get(const std::string key, const T& default_value) const {
std::string str;
// There might be a dilemma - is a non existing key an error?
if( ! get_raw(key, str)) return default_value;
else if(str.empty()) return default_value;
else {
T result;
std::istringstream is(str);
is.unsetf(std::ios_base::basefield);
is >> result;
if( ! is.eof() || is.fail()) throw std::runtime_error("Invalid Value");
return result;
}
}
// Usage
struct HttpConfiguration : public Configuration
{
unsigned port;
std::string htdocs;
bool preload;
HttpConfiguration()
: Configuration("httpd.conf"),
port(get<unsigned>("port", 80)),
htdocs(get<std::string>("htdocs", "default/")),
preload(get<bool>("prelude", false)) // typo here
{}
};
注意:類配置可以是管理配置源的任何內容(請查看Boost,POCO等)。
如果您想使用可變參數模板,請入門:
template <class T>
struct Param {
using Type = T;
std::string name_;
T default_value_;
};
template <class T>
auto MakeParam(std::string name, T default_value) -> Param<T> {
return {name, default_value};
}
template <class T, class... Args>
auto ReadParams(Param<T> p, Args... args) -> void {
ReadParams(p);
ReadParams(args...);
}
template <class T>
auto ReadParams(Param<T> p) -> void {
// here you can read from file
cout << "param: '" << p.name_ << "' type: '"
<< typeid(typename Param<T>::Type).name() << "' defval: '"
<< p.default_value_ << "'" << endl;
}
int main() {
ReadParams(MakeParam("param1", 0), MakeParam("param2", true),
MakeParam("param3", "c-string"),
MakeParam("param4", std::string{"c++str"}));
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.