c++ - Boost/Property Tree 确定值类型

标签 c++ boost rpc boost-propertytree ice

我想使用 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/

相关文章:

c++ - 无法将 'int' 转换为 'const char *'

c++ - Boost C++ 序列化一个 char *

c++ - 使用 boost 库收听 udp 广播

sockets - Socket 和 RPC 有什么区别?

c++ - 多类继承设置问题

c++ - 树程序哪里出错了?

c++ - 我可以从 boost 的 weak_ptr 获得原始指针吗?

powershell - 如何在 PowerShell session 中正确退出远程作业

wcf - RPC vs REST vs WCF?

c++ - 在 C++11 中生成一个随机字符串?