Boost::property_tree: Using std::vector<> in XML parser to store multiple values in one key

狂风中的少年 提交于 2019-12-06 04:58:05
sehe
  1. As the older answer also points out¹ you must satisfy the requirements for boost::property_tree::detail::is_translator, so you need the internal_type / external_type typedefs.

    typedef T internal_type;
    typedef T external_type;
    
  2. Next up, the loop is wrong, you need check the result of the value extraction:

    while (ss >> temp_value)
        values.push_back(temp_value);
    
  3. It's bad practice to put your own types inside the boost namespace. Only the specialization of translator_between<> needs to be there.

  4. You can simplify and generalize a lot of the code

All in a working demo:

Live On Coliru

#include <boost/optional.hpp>
#include <boost/property_tree/ptree.hpp>
#include <vector>
#include <list>

namespace mylib { namespace xml_translators {

    template<typename T> struct container
    {
        // types
        typedef T internal_type;
        typedef T external_type;

        boost::optional<T> get_value(const std::string& str) const
        {
            if (str.empty())
                return boost::none;

            T values;
            std::stringstream ss(str);

            typename T::value_type temp_value;
            while (ss >> temp_value)
                values.insert(values.end(), temp_value);

            return boost::make_optional(values);
        }

        boost::optional<std::string> put_value(const T& b) {
            std::stringstream ss;
            size_t i = 0;
            for (auto v : b)
                ss << (i++?" ":"") << v;
            return ss.str();
        }
    };

} }

namespace boost { namespace property_tree {
    template<typename ch, typename traits, typename alloc, typename T>
        struct translator_between<std::basic_string<ch, traits, alloc>, std::vector<T> > {
            typedef mylib::xml_translators::container<std::vector<T> > type;
        };

    template<typename ch, typename traits, typename alloc, typename T>
        struct translator_between<std::basic_string<ch, traits, alloc>, std::list<T> > {
            typedef mylib::xml_translators::container<std::list<T> > type;
        };
} }

#include <sstream>
#include <iostream>
#include <boost/property_tree/xml_parser.hpp>

int main()
{
    std::stringstream ss;
    using boost::property_tree::ptree;

    {
        ptree pt;
        pt.add("base", std::vector<double> { 1, 6, 3 });

        write_xml(ss, pt, boost::property_tree::xml_writer_settings<std::string>(' ', 2));
    }

    {
        ptree pt;
        read_xml(ss, pt);

        std::cout << "As string: '" << pt.get("base", "") << "'\n";
        auto roundtrip = pt.get<std::list<int> >("base");
        for (auto i : roundtrip)
            std::cout << i << std::endl;
    }
}

Prints

As string: '1 6 3'
1
6
3

¹ Boost property_tree: multiple values per key, see also the identity translator http://www.boost.org/doc/libs/1_64_0/doc/html/boost/property_tree/id_translator.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!