2

我正在尝试将 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]’
4

1 回答 1

5

boost::any没有operator <<。_ 你不能anyproperty_tree. anyVar.type()返回std::typeinfo,此类提供runtime有关类型的信息。

template<typename Type, typename Translator> 
  self_type & put(const path_type & path, const Type & value, Translator tr);

将给定路径上的节点值设置为提供的值,转换为树的数据类型。如果节点不存在,则创建它,包括它所有缺失的父节点。

您可以创建Translator并将其传递给函数 put,因为您的翻译ptree器确实basic_ptree<std::string, std::string>应该将您转换anystring.

翻译器的简单示例

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

于 2012-09-13T09:25:40.210 回答