Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is boost property_tree::empty_ptree?

I'm using boots's property_tree library. I'm looking for a way to get a child node from a ptree object, but return an empty ptree if failed. I came across a nice example in property_tree/examples/empty_ptree_trick.cpp:

void process_settings(const std::string &filename)
{
    ptree pt;
    read_info(filename, pt);    
    const ptree &settings = pt.get_child("settings", empty_ptree<ptree>());
    std::cout << "\n    Processing " << filename << std::endl;
    std::cout << "        Setting 1 is " << settings.get("setting1", 0) << std::endl;
    std::cout << "        Setting 2 is " << settings.get("setting2", 0.0) << std::endl;
    std::cout << "        Setting 3 is " << settings.get("setting3", "default") <<     std::endl;
}

which does exactly what I need. The problem is that the compiler complains that empty_ptree() function is not a member of boost:property_tree. Any ideas where empty_ptree() is?

I'm using boost 1.44 on VS2010.

like image 637
bavaza Avatar asked Feb 15 '11 12:02

bavaza


2 Answers

I have just blown a full day trying to answer that question!

This was my solution. Firstly I used pointers, and not references as you have to initialize them immediately. Then I just caught the exception and added a new ptree.

using namespace boost::property_tree;

ptree r_pt;
ptree *c_pt;

read_xml( "file.xml" , r_pt);

try {
    c_pt = &(r_pt.get_child( "example" ));
}
catch (ptree_bad_path) {
    c_pt = &(r_pt.put_child( "example", ptree() ));
}

std::cout << "Setting 1 is " << c_pt.get("setting1", 0) << std::endl;

From what I could pick up they expect us to use the boost::optional type. But I'm just a beginner..

EDIT I just found the implementation of empty_ptree<>.

template<class Ptree>
    inline const Ptree &empty_ptree()
    {
        static Ptree pt;
        return pt;
    }

I think you can just add this to your code and use it as described in the empty_ptree_trick.cpp, but I am sticking with my solution for now untill I find out how its actually supposed to be done.

like image 179
expelledboy Avatar answered Dec 31 '22 08:12

expelledboy


void process_settings(const std::string &filename)
{
    ptree pt;
    read_info(filename, pt);    
    const ptree &settings = pt.get_child("settings", ptree());
    std::cout << "\n    Processing " << filename << std::endl;
    std::cout << "        Setting 1 is " << settings.get("setting1", 0) << std::endl;
    std::cout << "        Setting 2 is " << settings.get("setting2", 0.0) << std::endl;
    std::cout << "        Setting 3 is " << settings.get("setting3", "default") <<     std::endl;
}

Note, that will prevent throwing an instance of 'boost::wrapexceptboost::property_tree::ptree_bad_path'

like image 37
Edward Kigwana Avatar answered Dec 31 '22 09:12

Edward Kigwana