Boost logo

Boost Users :

Subject: Re: [Boost-users] [karma] Losing attribute?
From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2010-02-25 03:36:19


On Wed, 24 Feb 2010 09:25:55 -0600, "Hartmut Kaiser"
<hartmut.kaiser_at_[hidden]> wrote:
> Sebastian,
>
>> I've got a Karma generator that should emit a vector of records,
>> writing
>> out the index of every entry at the start. My code looks like this
>> (note
>> that GEOPOINT is a fusion-adapted struct of two doubles):
>>
>
> Could you please provide a minimal, but still self-contained example I
> could
> run? That would help me to give you a proper answer to your question.
From
> what I can see your code should work just fine.
>


This is the test program I'm using, minus a special float formatter.



#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
#include <vector>

namespace ph = boost::phoenix;
namespace km = boost::spirit::karma;

struct GEOPOINT
{
        double x, y;
};
BOOST_FUSION_ADAPT_STRUCT(
        GEOPOINT,
        (double, x)
        (double, y)
        )

typedef std::vector<GEOPOINT> Contour;
typedef std::vector<Contour> Poly;

template <typename Iterator>
class ZoneWriter : public km::grammar<Iterator, Poly()>
{
public:
        ZoneWriter() : base_type(start)
        {
                using namespace km;
                point = L';' << double_ << L';' << double_;

                contour = L";SUBPOLYGON " << lit(_r1 - 1) << L" POINTS " <<
                        lit(ph::size(_val)) << L" MASK XOR" << *point;

                polygon = L"POLYGON COUNT " << lit(ph::size(_val)) <<
                        *(contour(_a)[++_a]);

                start = polygon;
        }

private:
        km::rule<Iterator, GEOPOINT()> point;
        km::rule<Iterator, Contour(int)> contour;
        km::rule<Iterator, km::locals<int>, Poly()> polygon;
        km::rule<Iterator, Poly()> start;
};

int main()
{
        Poly poly(5);
        for(int i = 0; i < 5; ++i) {
                Contour &c = poly[i];
                c.resize(10);
                for(int j = 0; j < 10; ++j) {
                        c[j].x = i + j / 4.25129;
                        c[j].y = -j + i / 1.24951;
                }
        }
        typedef std::ostreambuf_iterator<wchar_t> osbi;
        km::generate(osbi(std::wcout), ZoneWriter<osbi>(), poly);
        std::wcout << L'\n';
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net