I would like to generate output like this

common, int, optional
video, double, optional

by karma with the structure

struct attr_struct
 {
   std::vector<std::string> name;
   std::vector<std::string> type;
   std::vector<std::string> use;
 };

So I defined some rules for it

#include<iostream>
  #include<string>
  #include<vector>

  #include<boost/fusion/adapted/struct.hpp>

  #include<boost/spirit/include/karma.hpp>

  namespace karma = boost::spirit::karma;
  namespace karma = boost::spirit::karma;

  struct attr_struct
  {
    std::vector<std::string> name;
    std::vector<std::string> type;
    std::vector<std::string> use;
  };

  BOOST_FUSION_ADAPT_STRUCT
  (
     attr_struct,
     (std::vector<std::string>, name)
     (std::vector<std::string>, type)
     (std::vector<std::string>, use)
  );

  struct attr_grammar
  : karma::grammar<std::back_insert_iterator<std::string>, attr_struct()>
  {
    attr_grammar() : attr_grammar::base_type(query)
    {                       
      key   =  karma::delimit(", ") [karma::string << karma::string 
               << karma::string] << karma::eol;
      query =  key; //this one work
      //query =  key % karma::eol; //oops, can't compile #1
      //query = +key; //this one can't compile either #2
      //can't compile either #3
      //query = +(karma::string << ", " << karma::string 
      //        << ", " << karma::string << karma::eol);        
      //* and % also can't make the job done
    }

    private :
      typedef std::back_insert_iterator<std::string> sink_type;   
      karma::rule<sink_type, attr_struct()> key;
      karma::rule<sink_type, attr_struct()> query;   
  };

It can't work, what should I do?

Thank you very much

Edited 4 Years Ago by stereomatching: n/a

This one work, but I hope previous example could work like I expect too

#include<iostream>
  #include<string>
  #include<vector>

  #include<boost/fusion/adapted/struct.hpp>

  #include<boost/spirit/include/karma.hpp>

  namespace karma = boost::spirit::karma;
  namespace karma = boost::spirit::karma;

  struct attr_struct
  {
     std::string name;
     std::string type;
     std::string use;
  };

  BOOST_FUSION_ADAPT_STRUCT
  (
    attr_struct,
    (std::string, name)
    (std::string, type)
    (std::string, use)
  );

  struct attr_struct_vec
  {
    std::vector<attr_struct> attr_vec;
  };

  BOOST_FUSION_ADAPT_STRUCT
  (
    attr_struct_vec,
    (std::vector<attr_struct>, attr_vec)  
  );

  struct attr_grammar
  : karma::grammar<std::back_insert_iterator<std::string>, std::vector<attr_struct>()>
  {
    attr_grammar() : attr_grammar::base_type(query)
    {                        
      key = karma::string << ", "<< karma::string <<", " << karma::string << karma::eol;
      query = *key;
    }

    private :
      typedef std::back_insert_iterator<std::string> sink_type;    
      karma::rule<sink_type, attr_struct()> key;
      karma::rule<sink_type, std::vector<attr_struct>() > query;
  };

  int main()
  {  
    attr_struct data;
    std::vector<attr_struct> attr;
    data.name = "common"; data.type = "int"; data.use = "optional";    
    attr.push_back(data);
    data.name = "video"; data.type = "doule"; data.use = "optional";
    attr.push_back(data);

    std::string temp;
    karma::generate(std::back_inserter(temp), attr_grammar(), attr);
    std::cout<<temp<<std::endl;

    std::cin.get();
    return 0;
  }

How could I make spirit karma work with the last example?
Thanks

sorry, these are redundancy codes

struct attr_struct_vec
{
std::vector<attr_struct> attr_vec;
};
 
BOOST_FUSION_ADAPT_STRUCT
(
attr_struct_vec,
(std::vector<attr_struct>, attr_vec)
);
This article has been dead for over six months. Start a new discussion instead.