c++ - Boost property_tree: multiple values per key, on a template class -


on topic, boost property_tree: multiple values per key, method proposed read multiples keys of boost tree. paste modified version below

template<int dim> struct my_vector {   std::vector<double> a_vector; }; 

the translator be:

template<int dim> struct my_vector_translator {   typedef std::string    internal_type;   typedef my_vector external_type;   // my_vector string   boost::optional<external_type> get_value(const internal_type& str)   {     if (!str.empty())     {       std::vector val;       val.resize(dim)       std::stringstream s(str);       double temp;       (int i=0, < dim; ++i){           s >> temp;           val.a_vector[i] = temp;       return boost::optional<external_type>(val);     }     else       return boost::optional<external_type>(boost::none);   }  }; 

and now, how should register translator ?

namespace boost { namespace property_tree  {   template<typename ch, typename traits, typename alloc>    struct translator_between<std::basic_string< ch, traits, alloc >, my_vector<it want here of course, ?> >   {     typedef my_vector_translator<it want here of course, ?> type;   }; } } 

after include previous code, can use property_tree as:

  pt.get<my_vector ??and here put size ?>("box.x"); 

and somehow feel class my_vector useless. couldn't work directly std::vector ?

i like:

pt.get<std::vector<double>, 3>("mesh.a_line_where_i_know_i_have_3_doubles_to_read"); 

thank you.

it seems me size template argument my_vector class template.

so

pt.get<my_vector<3> >("box.x"); 

should good

registering:

a partial specialization can introduce template arguments, add size_t n list:

namespace boost { namespace property_tree {     template<typename ch, typename traits, typename alloc, size_t n>          struct translator_between<std::basic_string<ch, traits, alloc>, my_vector<n> >         {             typedef my_vector_translator<n> type;         }; } } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -