Boost logo

Boost Users :

From: Rush Manbert (rush_at_[hidden])
Date: 2005-10-13 14:46:17


Paul Mensonides wrote:

>>That's pretty cool. I'm going to impose on your good nature
>>to ask a follow up question here. In the real code, each CODE
>>macro contains more entries, like so:
>>#define CODE_TABLE \
>> CODE(FooCodeA, "FooCodeA short description",
>>"SummarySelectorFooCodeA", "DetailSelectorFooCodeA") \
>> CODE(FooCodeB, "FooCodeB short description",
>>"SummarySelectorFooCodeB", "DetailSelectorFooCodeB")
>>
>>And the structure is defined as:
>>typedef struct
>>{
>> CodesT theCode;
>> const char *pTheCodeAsString;
>> const char *pShortDescription;
>> const char *pSummarySelector;
>> const char *pDetailSelector;
>>} CodeLookupTableT;
>>
>>I have done no programming with the preprocessor lib, so I
>>only have a vague idea how to extend what you did to cover
>>this case. (I think CODE needs to define either an array or a
>>list or a sequence or a tuple, but that's as far as I have
>>gotten...) Would
>>you be willing to give it a go?
>>
>>
>
>Certainly. You just need a have a data structure of data structures. You don't
>really need CODE at all.
>
>--------------------
>foo.h
>--------------------
>#include <boost/preprocessor/punctuation/comma_if.hpp>
>#include <boost/preprocessor/seq/for_each_i.hpp>
>#include <boost/preprocessor/tuple/elem.hpp>
>
>#define CODE_TABLE \
> ( (FooCodeA, "description", "summary", "detail") ) \
> ( (FooCodeB, "description", "summary", "detail") ) \
> /**/
>
>#define ENUM(r, _, i, tuple) \
> BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(4, 0, tuple) \
> /**/
>
>class foo
>{
>public:
> typedef enum { BOOST_PP_SEQ_FOR_EACH_I(ENUM, ~, CODE_TABLE) };
>// ...
>};
>
>#undef ENUM
>
>--------------------
>foo.cpp
>--------------------
>#include "foo.h"
>
>#include <boost/preprocessor/punctuation/comma_if.hpp>
>#include <boost/preprocessor/seq/for_each_i.hpp>
>#include <boost/preprocessor/stringize.hpp>
>
>#define ENTRY(r, _, i, tuple) \
> BOOST_PP_COMMA_IF(i) ENTRY_II tuple \
> /**/
>#define ENTRY_II(code, desc, summary, detail) \
> { code, BOOST_PP_STRINGIZE(code), desc, summary, detail } \
> /**/
>
>foo::CodeTableLookupT foo::m_lookupTable[]
> = { BOOST_PP_SEQ_FOR_EACH_I(ENTRY, ~, CODE_TABLE) }
>
>#undef ENTRY
>#undef ENTRY_II
>
>Note that I'm using an extra ENTRY_II macro here to avoid five invocations of
>BOOST_PP_TUPLE_ELEM.
>
>
<snipped>

That. Is. So. Cool! No inner macros to redefine, and you can wrap the
enum declaration in a macro that takes CODE_TABLE (or whatever the
developer calls it) as a parameter. Same for the array definition. This
is a big improvement on the original version. Thank you for the great help.

I was unfamiliar with Chaos, but the code snippets looked interesting. I
found the project on SourceForge. Will there be a release sometime?

Thanks again,
Rush


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