How to forward a boost :: ptree :: iterator ad

I would like to use boost ptree in my project, but since ptree.hpp calls about 1000 header files to be included, it increases compilation time (for example, from 1 to 7 seconds) and, as necessary, in more than 20 different files cpp is unacceptable (precompiled headers do nothing better). So I'm thinking of encapsulating boost ptree in my class, something like

// myptree.h #include <boost/property_tree/ptree_fwd.hpp> class myptree { private: boost::property_tree::ptree *m_tree; public: ... // adding new (single value) members to the the tree void put(const std::string&, double); void put(const std::string&, int); void put(const std::string&, const std::string&); // returning (single value) members of the tree double get_double(const std::string&) const; int get_int(const std::string&) const; std::string get_str(const std::string&) const; // working with subtrees void push_back(const std::string&, const myptree&); myptree get_child(const std::string&) const; // import/export void read_from_json(const std::string&); void write_to_json(const std::string&) const; }; 

However, I cannot implement an iterator in a beautiful way. Ideally, I would like to have boost::property_tree::ptree::iterator as a private member variable, which could then be repeated through m_tree using my own member functions, but as I understand from How to forward an inner class declaration? this is generally impossible. Any elegant ways to implement an iterator in this class?

+4
source share
3 answers

Your problem is a good candidate for Pimpl's Hippodrome , as well as a compiler firewall, also known as Handle-Body. See also article. The solution you offer is very reminiscent of this idiom.

To hide the it ptree from your customers, check out the any_iterator technique in this article .

You can find any_iterator options here and here .

+4
source

I do not have a good answer to your real question, but precompiled headers should be a significant improvement in this case. Are you sure this was actually used and the headers have not yet been read from each compilation unit? It can be a little difficult to optimize. (ie avoid the "automatic" option)

+3
source

Thank you for your responses. As for the precompiled headers, I checked that they are actually used ( g++ -H shows initially 1460 header files and only about 30 when using precompiled headers), and the compilation time is reduced from 7 to 5.5 s, which is still not very good compared to 1s when using the above encapsulated class.

Now, when I tried to use any_iterator (which seems to have also become a part of boost), I realized that he also added a few hundred other header files, but simply including them did not increase compile time. So I tried the same with the ptree header and included ptree.hpp instead of ptree_fwd.hpp and increased the compilation time by only a few (from 1.1 to 1.8 s). So it seems like heavy compilation of fine time only occurs when ptree templates are created? It might also explain why precompiled headers didn't help much? Being lazy, and since my main concern was compilation time, I could just stick with something like:

 // myptree.h #include <boost/property_tree/ptree.hpp> class myptree { private: boost::property_tree::ptree m_tree; boost::property_tree::ptree::iterator m_it; ... }; 
0
source

All Articles