How do I merge/update a boost::property_tree::ptree?

后端 未结 2 2039
死守一世寂寞
死守一世寂寞 2021-02-01 07:07

I have read through the documentation for boost::property_tree and have not found a way to update or merge a ptree with another ptree. How do I do this?

Given the code b

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 07:25

    Boost.Property tree does not support this, yet: boost.org/doc/libs/1_48_0/doc/html/property_tree/appendices.html. Look at the future work section.

    Mathematical relations: ptree difference, union, intersection.

    An update is simply a difference followed by a union. a = (a - b) + b.

    The general solution would require recursively traversing update ptree and putting each leaf.

    However a good enough solution can be built with put_child. This may do all you need, without the complexity of a general solution.

    void merge( ptree& pt, const ptree& updates )
    {
       BOOST_FOREACH( auto& update, updates )
       {
          pt.put_child( update.first, update.second );
       }
    }
    

    The good enough solution has two limitations, by coincidence they are the same limitations the ini_parser has.

    • the tree can only be two layers (e.g. "first.number", but not "first.again.number" )
    • values can only be stored in leaf nodes.

提交回复
热议问题