You need to provide a default constructor for Ship
and a stream input operator:
struct Ship {
friend std::istream& operator>>(std::istream& s, Ship& e) {
/* read ship data from s */
return s;
}
};
Unfortunately, I'm not sure if this is an official feature of property_tree
as I can not find it in the documentation.
For more fine-grained access get
also takes a Translator
as it's second argument.
struct ShipTranslatorJSON {
// the type we return
typedef Ship external_type;
// the type expect as an argument
typedef std::string internal_type;
boost::optional< external_type > get_value(const internal_type&) {
return external_type();
}
// only required when you actually use put
// boost::optional< T > put_value(const T &);
};
// use as
xml.get<Ship>("sauce_pan", ShipTranslatorJSON());
This method also seems more "official", although nothing in the documentation actually explains what a Translator really is.