我想在我的项目中使用 boost ptree,但由于 ptree.hpp 导致包含大约另外 1000 个头文件,这会大大增加编译时间(例如从 1 秒到 7 秒),并且因为它需要在 20 多个不同的 cpp 文件中,所以这不是可接受的(预编译的头文件并没有太大的改进)。所以我正在考虑将 boost ptree 封装在我自己的类中,比如
// 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;
};
但是,我未能以一种很好的方式实现迭代器。理想情况下,我希望将 a作为私有成员变量,然后可以使用我自己的成员函数boost::property_tree::ptree::iterator
对其进行迭代,但正如我从如何转发声明内部类中了解的那样?这通常是不可能的。在此类中实现迭代器的任何优雅方法?m_tree