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 roman.neuhauser+boost.org@…, 13 years ago)
  • custom_data_type.cpp

    old new  
    1414// container (instead of std::string that standard ptree has).
    1515
    1616#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>
    2117#include <boost/any.hpp>
    2218#include <list>
    2319#include <string>
    2420#include <iostream>
    2521
    2622// Custom translator that works with boost::any instead of std::string
    27 struct my_translator
     23template <class Ext, class Int = boost::any>
     24struct variant_translator
    2825{
     26    typedef Ext external_type;
     27    typedef Int internal_type;
    2928
    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
    3331    {
    34         value = boost::any_cast<T>(pt.data());
    35         return true;    // Success
     32        return boost::any_cast<external_type>(value);
    3633    }
    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
    4136    {
    42         pt.data() = value;
    43         return true;
     37        return value;
    4438    }
    45 
    4639};
    4740
    4841int main()
     
    5144    using namespace boost::property_tree;
    5245   
    5346    // Property_tree with boost::any as data type
    54     // Key comparison:  std::less<std::string>
    5547    // Key type:        std::string
    56     // Path type:       path
    5748    // Data type:       boost::any
    58     // Translator type: my_translator
    59     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;
    6051    my_ptree pt;
    6152
    6253    // 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());
    6557    std::cout << "Int value: " << int_value << "\n";
    6658
    6759    // 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    );
    7066    std::cout << "String value: " << string_value << "\n";
    7167
    7268    // Put/get list<int> value
     69    typedef std::list<int> intlist;
     70    typedef variant_translator<intlist> intlist_tran;
    7371    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    );
    7684    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)
    7886        std::cout << *it << ' ';
    7987    std::cout << '\n';
    8088}