BOOST_PP_REPEAT with array
Asked Answered
U

1

4

I have struct like :

struct E1
{
    typedef boost::tuple<  
     boost::optional< N::type_A >, // N - namespace
     boost::optional< N::type_B >,
     ...................
     boost::optional< N::type_X > //arbitrary number of, maximal is 7
     > data_type;


   // for access by name 
   boost::optional<N::type_A> const&  type_A() const { return boost::get<0>(data); }
   boost::optional<N::type_B> const&  type_B() const { return boost::get<1>(data); }
   .....................................
   boost::optional<N::type_X> const&  type_X() const { return boost::get<2>(data); }

   data_type data;
};

Q: how I may create this structure using BOOST preprocessors? For me known only type_A, type_B, ..., type_X name of types.

It's need me, because I must create a lot of structures like that, only changing type_A, type_B, ... types.

In common case, can I use boost preprocessor array or set?

Underfeed answered 26/8, 2013 at 9:58 Comment(0)
G
7

You could do it like this:

#define TYPES (type_A)(type_B)(type_X)


#define GENERATE_TUPLE(maR, maNamespace, maIndex, maType) \
  BOOST_PP_COMMA_IF(maIndex) boost::optional<maNamespace :: maType>


#define GENERATE_GETTER(maR, maNamespace, maIndex, maType) \
  boost::optional<maNamespace :: maType> const& maType () const { return boost::get<maIndex>(data); }


struct E1
{
  typedef boost::tuple<
    BOOST_PP_SEQ_FOR_EACH_I(GENERATE_TUPLE, N, TYPES)
  > data_type;

  BOOST_PP_SEQ_FOR_EACH_I(GENERATE_GETTER, N, TYPES)

  data_type data;
};

The N argument corresponds to the maNamespace parameter. You can of course use this argument in any other way (it's just passed through verbatim), such as hardcoding N into the macros (and passing a dummy in the argument), or encoding even the identifier data in there, etc.

Gelignite answered 26/8, 2013 at 10:16 Comment(1)
Note: let's not forget to #undef the macros once we are done with them; there is no need to pollute the global namespace for our clients :)Houlberg

© 2022 - 2024 — McMap. All rights reserved.