我正在尝试将boost :: any类型存储到boost属性树中。这是一些可运行的例子:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <sys/types.h>
#include <boost/any.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
int main()
{
boost::property_tree::ptree pTree_Root;
pTree_Root.put("sigRoot.property1", "value1");
pTree_Root.put("sigRoot.property2", "value2");
pTree_Root.put("sigRoot.property3", "value3");
std::vector<std::string> vecString;
for(int i = 0; i <= 5; i++) {
vecString.push_back("somestring");
}
boost::any anyVar = vecString;
pTree_Root.put("sigRoot.property4", anyVar);
std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, pTree_Root);
std::string jsonString = ss.str();
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\n'), jsonString.end());
std::cout << jsonString << std::endl;
return 0;
}
这不起作用,可能出于同样的原因,boost :: any不可序列化。但我发现我被允许查询anyVar.type()
,但我不知道可以用它做什么。有可能以某种方式使用auto
或其他东西将数据添加到属性树而不在此处明确编码固定数量的已知类型吗?也欢迎使用C ++ 11 / C ++ 0x方法。
这是我得到的错误:
In file included from /usr/include/boost/property_tree/ptree.hpp:17:0,
from pTreeTest.cpp:8:
/usr/include/boost/property_tree/stream_translator.hpp: In static member function ‘static void boost::property_tree::customize_stream<Ch, Traits, E, Enabler>::insert(std::basic_ostream<_Ch, _Tr>&, const E&) [with Ch = char, Traits = std::char_traits<char>, E = boost::any, Enabler = void]’:
/usr/include/boost/property_tree/stream_translator.hpp:199:13: instantiated from ‘boost::optional<std::basic_string<Ch, Traits, Alloc> > boost::property_tree::stream_translator<Ch, Traits, Alloc, E>::put_value(const E&) [with Ch = char, Traits = std::char_traits<char>, Alloc = std::allocator<char>, E = boost::any]’
/usr/include/boost/property_tree/detail/ptree_implementation.hpp:795:54: instantiated from ‘void boost::property_tree::basic_ptree<Key, Data, KeyCompare>::put_value(const Type&, Translator) [with Type = boost::any, Translator = boost::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, boost::any>, Key = std::basic_string<char>, Data = std::basic_string<char>, KeyCompare = std::less<std::basic_string<char> >]’
/usr/include/boost/property_tree/detail/ptree_implementation.hpp:817:13: instantiated from ‘boost::property_tree::basic_ptree<K, D, C>& boost::property_tree::basic_ptree<Key, Data, KeyCompare>::put(const path_type&, const Type&, Translator) [with Type = boost::any, Translator = boost::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, boost::any>, Key = std::basic_string<char>, Data = std::basic_string<char>, KeyCompare = std::less<std::basic_string<char> >, boost::property_tree::basic_ptree<Key, Data, KeyCompare>::path_type = boost::property_tree::string_path<std::basic_string<char>, boost::property_tree::id_translator<std::basic_string<char> > >]’
/usr/include/boost/property_tree/detail/ptree_implementation.hpp:832:72: instantiated from ‘boost::property_tree::basic_ptree<K, D, C>& boost::property_tree::basic_ptree<Key, Data, KeyCompare>::put(const path_type&, const Type&) [with Type = boost::any, Key = std::basic_string<char>, Data = std::basic_string<char>, KeyCompare = std::less<std::basic_string<char> >, boost::property_tree::basic_ptree<Key, Data, KeyCompare>::path_type = boost::property_tree::string_path<std::basic_string<char>, boost::property_tree::id_translator<std::basic_string<char> > >]’
pTreeTest.cpp:26:47: instantiated from here
/usr/include/boost/property_tree/stream_translator.hpp:33:13: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
/usr/include/c++/4.6/ostream:581:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = boost::any]’
答案 0 :(得分:5)
没有。 boost::any
没有operator <<
。您无法在any
中使用property_tree
。 anyVar.type()
返回std::typeinfo
,此类提供有关类型的runtime
信息。
template<typename Type, typename Translator>
self_type & put(const path_type & path, const Type & value, Translator tr);
将给定路径上的节点值设置为提供的值,并转换为树的数据类型。如果该节点不存在,则创建该节点,包括其所有缺少的父节点。
您可以创建Translator
并将其传递给函数库,因为ptree
确实basic_ptree<std::string, std::string>
您的翻译人员应将any
转换为string
。
翻译的简单例子
struct SimpleTranslator
{
public:
boost::optional<std::string> put_value(const boost::any& value)
{
if (value.type() == typeid(std::vector<std::string>))
{
std::stringstream ss;
std::vector<std::string> vec = boost::any_cast<std::vector<std::string>>(value);
std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(ss));
return ss.str();
}
return boost::optional<std::string>();
}
};
http://liveworkspace.org/code/275820c1becfb63deda4e4eed8524833