Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost preprocessor question
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-05 15:29:44


AMDG

Michiel Helvensteijn wrote:
> I'm looking for a way to write certain kinds of classes without duplication
> or unnecessary verbosity. Because there will be many of them in a single
> file and I want them to be readable.
>
> I'm trying to decide if I should use the preprocessor or if I should write a
> script to do the translation before compilation. I would like something
> like this (how I think it might look):
>
> CLASS(Foo, FROM(Bar))
> CONSTRUCT()
> CONSTRUCT(PAR(int, a, A), PAR(bool, b, B))
> int A;
> bool B;
> END_CLASS
>
> to automatically become something like this:
>
> class Foo : public Bar, public shared_from_this<Foo> {
> private: Foo() {}
> public: static shared_ptr<Foo> construct() {
> return shared_ptr<Foo>(new Foo());
> }
> private: Foo(int a, bool b) : A(a), B(b) {}
> public: static shared_ptr<Foo> construct(int a, bool b) {
> return shared_ptr<Foo>(new Foo(a, b));
> }
> int A;
> bool B;
> };
>
> Is such a thing possible? You see, there are a few things happening there:
>
> * Variable length list, reproduced more than once in different ways.
>

Preprocessor sequences would work.

> * Foo is remembered to use in the constructor name.
>

Not possible.

> What would the original code look like if we could make this work?

CLASS(Foo, FROM(Bar))
    CONSTRUCT(Foo, BOOST_PP_EMPTY_SEQ)
    CONSTRUCT(Foo, (int, a, A)(bool, b, B))
    int A;
    bool B;
END_CLASS

is possible.

> And is
> there something in the boost preprocessor library that can help me?
>

The following at least might be useful
http://www.boost.org/libs/preprocessor/doc/ref/seq_enum.html
http://www.boost.org/libs/preprocessor/doc/ref/tuple_elem.html

In Christ,
Steven Watanabe


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