c++ - 在yaml-cpp中为yaml变量返回bool的任何方法?
问题描述
我有一个用于禁用特定代码路径的配置文件。我刚刚在 yaml 文件中添加了一个 bool 选项,并且很难弄清楚 yaml-cpp 如何处理这些选项。该文档比首选文档要轻一些,而且我没有看到任何Node
适合我用例的内容。我可以手动解析返回为true
and的字符串false
,但这似乎是框架应该支持的,因为在规范中有多种写作true
风格false
。有没有办法从 yaml-cpp 中获取布尔值?
IsScalar
是我能找到的最接近的。
void LoadConfig(string file)
{
Node config = LoadFile(file);
string targetDirectory;
bool compile;
if (config["TargetDirectory"])
targetDirectory = config["TargetDirectory"].Scalar();
if (config["Compile"])
compile = Config["Compile"].IsScalar();
}
解决方案
你想要模板as()
方法:
config["Compile"].as<bool>()
或者使用默认值在一行而不是三行中完成所有操作的更简洁的方法(这也解决了您潜在的未初始化变量错误):
bool compile = config["Compile"].as<bool>(false);
推荐阅读
- r - 从数据表 R 下载数据时更改文件名
- c# - 初始化 XAML 预览器时找不到 System.Runtime 程序集
- php - 我如何注释我的属性,它是值对象,以便 API 平台生成其字段用于招摇文档?
- google-analytics - ReferenceError:未定义数据层
- javascript - 仅使用javascript触发单击事件时如何在列表项上切换css类
- javascript - Safari 10 上的 Ionic 3 远程内容加载问题
- python - 按值对包含字典列表的字典进行排序
- sql-server - 使用 Powershell 从 SQL 输出大型 XML 文件
- excel - 在列表底部/第一个空白单元格处停止“=组合”代码
- mule - 如何在 dataweave 2.0 中为 XML 属性组合映射和过滤器?