Ticket #3674: boost-property_tree-examples-custom_data_type.cpp,1.41,0.patch
File boost-property_tree-examples-custom_data_type.cpp,1.41,0.patch, 3.6 KB (added by , 13 years ago) |
---|
-
custom_data_type.cpp
old new 14 14 // container (instead of std::string that standard ptree has). 15 15 16 16 #include <boost/property_tree/ptree.hpp> 17 #include <boost/property_tree/info_parser.hpp>18 #include <boost/property_tree/ini_parser.hpp>19 #include <boost/property_tree/json_parser.hpp>20 #include <boost/property_tree/xml_parser.hpp>21 17 #include <boost/any.hpp> 22 18 #include <list> 23 19 #include <string> 24 20 #include <iostream> 25 21 26 22 // Custom translator that works with boost::any instead of std::string 27 struct my_translator 23 template <class Ext, class Int = boost::any> 24 struct variant_translator 28 25 { 26 typedef Ext external_type; 27 typedef Int internal_type; 29 28 30 // Custom extractor - converts data from boost::any to T 31 template<class Ptree, class T> 32 bool get_value(const Ptree &pt, T &value) const 29 external_type 30 get_value(const internal_type &value) const 33 31 { 34 value = boost::any_cast<T>(pt.data()); 35 return true; // Success 32 return boost::any_cast<external_type>(value); 36 33 } 37 38 // Custom inserter - converts data from T to boost::any 39 template<class Ptree, class T> 40 bool put_value(Ptree &pt, const T &value) const 34 internal_type 35 put_value(const external_type &value) const 41 36 { 42 pt.data() = value; 43 return true; 37 return value; 44 38 } 45 46 39 }; 47 40 48 41 int main() … … 51 44 using namespace boost::property_tree; 52 45 53 46 // Property_tree with boost::any as data type 54 // Key comparison: std::less<std::string>55 47 // Key type: std::string 56 // Path type: path57 48 // Data type: boost::any 58 // Translator type: my_translator59 typedef basic_ptree<std:: less<std::string>, std::string, path, boost::any, my_translator> my_ptree;49 // Key comparison: default (std::less<std::string>) 50 typedef basic_ptree<std::string, boost::any> my_ptree; 60 51 my_ptree pt; 61 52 62 53 // Put/get int value 63 pt.put("int value", 3); 64 int int_value = pt.get<int>("int value"); 54 typedef variant_translator<int> int_tran; 55 pt.put("int value", 3, int_tran()); 56 int int_value = pt.get<int>("int value", int_tran()); 65 57 std::cout << "Int value: " << int_value << "\n"; 66 58 67 59 // Put/get string value 68 pt.put<std::string>("string value", "foo bar"); 69 std::string string_value = pt.get<std::string>("string value"); 60 typedef variant_translator<std::string> string_tran; 61 pt.put<std::string>("string value", "foo bar", string_tran()); 62 std::string string_value = pt.get<std::string>( 63 "string value" 64 , string_tran() 65 ); 70 66 std::cout << "String value: " << string_value << "\n"; 71 67 72 68 // Put/get list<int> value 69 typedef std::list<int> intlist; 70 typedef variant_translator<intlist> intlist_tran; 73 71 int list_data[] = { 1, 2, 3, 4, 5 }; 74 pt.put<std::list<int> >("list value", std::list<int>(list_data, list_data + sizeof(list_data) / sizeof(*list_data))); 75 std::list<int> list_value = pt.get<std::list<int> >("list value"); 72 pt.put<intlist>( 73 "list value" 74 , intlist( 75 list_data 76 , list_data + sizeof(list_data) / sizeof(*list_data) 77 ) 78 , intlist_tran() 79 ); 80 intlist list_value = pt.get<intlist>( 81 "list value" 82 , intlist_tran() 83 ); 76 84 std::cout << "List value: "; 77 for ( std::list<int>::iterator it = list_value.begin(); it != list_value.end(); ++it)85 for (intlist::iterator it = list_value.begin(); it != list_value.end(); ++it) 78 86 std::cout << *it << ' '; 79 87 std::cout << '\n'; 80 88 }