本文介绍了Boost/Property Tree 确定值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to use Boost/Property Tree as a sort of synchronization with my application.To do this I'd planned to use Zeroc/ICE for state-synchronization (using an Observe pattern and bidirectional connections).

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

However, to do this in an efficient way I need to somehow specify the I/O of the application (obviously).

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

This works nicely for importing values into the tree (since I can use InputStream to convert to any primitive type and catch occuring errors) but it hampers when I want to export values.

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

With the functions exposed in the documentation I do not see any way to retrieve the actual type of an element

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);

推荐答案

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

The concept of a Property Tree provides hierarchical storage of data (key-value pairs) where the value is text.

就是这样.

如果您的应用程序为该文本的内容指定含义,则元数据必须在您的程序中.库中没有变体叶存储.

If your application assigns meaning to the contents of that text, the meta-data will have to be in your program. There is no variant leaf storage in the library.

即使底层序列化格式(例如 JSON)确实支持它,这也是正确的,正如文档中清楚说明的那样,例如:

This is also true even if the underlying serialization format (such as JSON) does support it, as the documentation clearly spells out, e.g.:

  • JSON 值被映射到包含该值的节点.但是,所有类型信息都丢失了;数字以及文字null"、true"和false"都简单地映射到它们的字符串形式.

现在怎么办?

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

What now?

It looks like you want to have a reflection-style framework, and Boost Property Tree doesn't provide it. I'd suggest other libraries but all of them either assume intrusive changes or still require you to model the meta-data for the program data manually.

这篇关于Boost/Property Tree 确定值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:04