How to access data of nested objects in boost :: spirit :: karma?

In ItemList

containing lists of objects Item

, how do I access the objects Item

in the generator?

The following code example compiles on VC9 (with boost include and link setup). I don't know how to set it up list_generator::item

.

#include <boost/config/warning_disable.hpp>

#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>

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

#include <iostream>
#include <string>
#include <list>

namespace karma = boost::spirit::karma;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;


class Item
{
public:
    typedef std::vector<int> Values;

    Item(const std::string  & i, const Values & v) : m_id(i), m_values(v) {}
    std::string getId() const { return m_id; }
    const Values & getValues() const { return m_values; }

private:
    std::string m_id;
    Values m_values;
};

class ItemList
{
public:
    typedef std::map<std::string, Item> Items;

    ItemList() {}
    ItemList(const Items & s, const Items & o) : m_some(s), m_other(o) {}
    const Items getSome() const { return m_some; }
    const Items getOther() const { return m_other; }

private:
    Items m_some;;
    Items m_other;
};

template <typename Iterator>
struct list_generator : karma::grammar<Iterator, ItemList()>
{
    list_generator(const ItemList & i) : list_generator::base_type(start)
{
    using karma::int_;
    using karma::_1;
    using karma::lit;

    // Convert maps into lists containing only the values
    typedef std::vector<Item> Cells;
    const Cells some  = boost::copy_range<Cells>(i.getSome() | boost::adaptors::map_values);
    const Cells other = boost::copy_range<Cells>(i.getOther() | boost::adaptors::map_values);

    item =
        lit("<item>")
        << lit("<id>")      /*<< lit[_1 = ??.getId()]*/ << lit("</id>") // Item ID
        << lit("<values>")  /*<< (int_ % ';')[_1 = ??.getValues()]*/ << lit("</values>") // List of Item values
        << lit("</item>");

    start =
        lit("<some>")     << (*item)[_1 = some] << lit("</some>")
        << lit("<other>")  << (*item)[_1 = other] << lit("</other>");
}

karma::rule<Iterator, Item()> item;
karma::rule<Iterator, ItemList()> start;
};

int main()
{
    const Item::Values values = boost::assign::list_of(1)(2)(3);
    const Item a("a", values);
    const Item b("b", values);

    ItemList::Items some, other;
    some.insert(std::make_pair(a.getId(), a));
    other.insert(std::make_pair(b.getId(), b));
    const ItemList items(some, other);

    typedef std::back_insert_iterator<std::string> Iter;
    typedef list_generator<Iter> Generator;

    Generator grammar(items);

    std::string generated;
    Iter sink(generated);
    if (!karma::generate(sink, grammar))
    {
        std::cout << "Generating failed\n";
    }
    else
    {
        std::cout << "Generated: " << generated << "\n";
    }

    return 0;
}

      

Output:

Generated: <some><item><id></id><values></values></item></some><other><item><id></id><values></values></item></other>

      

+2


source to share


1 answer


You must use karma::_val

. For example you can write some binders (simple example)



<< lit("<id>") 
<< karma::string[_1 = phoenix::bind(&Item::getId, karma::_val)]
<< lit("</id>") // Item ID
<< lit("<values>")
<< (int_ % ';')[_1 = phoenix::bind(&Item::getValues, karma::_val)]
<< lit("</values>") // List of Item values

      

+3


source







All Articles