gpt4 book ai didi

c++ - Boost/Property Tree 判断值类型

转载 作者:行者123 更新时间:2023-11-30 03:43:12 26 4
gpt4 key购买 nike

我想使用 Boost/Property Tree 作为与我的应用程序同步的一种方式。为此,我计划使用 Zeroc/ICE 进行状态同步(使用观察模式和双向连接)

但是,要以高效的方式执行此操作,我需要以某种方式指定应用程序的 I/O (显然)

这非常适合将值导入树(因为我可以使用 InputStream 转换为任何原始类型并捕获发生的错误)但是当我想要导出值时它会妨碍。

对于文档中公开的函数,我看不到任何方法来检索元素的实际类型

boost::property_tree::ptree Tree;

// Import
Ice::CommunicatorPtr communicator = current.adapter->getCommunicator();
Ice::InputStreamPtr in = Ice::createInputStream(communicator, item.data);

switch (item.type) {
case BOOLVAL:
double boolval;
in->read(boolval);
Tree.put(item.path, boolval);
break;
}

// Export
// This is not possible since I cannot retrieve or compare the type
Ice::CommunicatorPtr communicator = current.adapter->getCommunicator();
Ice::OutputStreamPtr out = Ice::createOutputStream(communicator);
auto data = Tree.get<TYPE>(path);
out->write(data);

最佳答案

属性树的概念提供了数据(键值对)的分层存储,其中值为文本

就是这样。

如果您的应用程序为该文本的内容分配意义,则元数据必须在您的程序中。库中没有变异叶存储。

即使底层序列化格式(例如 JSON) 支持它也是如此,因为文档清楚地说明了,例如:

  • JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.

现在怎么办?

看起来你想要一个反射式的框架,而Boost Property Tree没有提供。我建议使用其他库,但所有这些库要么假设进行侵入性更改,要么仍然需要您手动为程序数据的元数据建模。

关于c++ - Boost/Property Tree 判断值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36268753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com