开发者

Nested boost (preprocessor) sequence

Why can't i call BOOST_PP_SEQ_FOR_EACH from inside a macro like this:

#define MAP_KEY_TYPES (int)(double)(std::string)
#define MAP_VAL_TYPES (int)(double)(std::string)(std::vector<int>)

#define DECLARE_MAP_VARIANTS(r, K, V) \
    void toJson(        Json::Value &j, const std::map< K, V >& v);\
    void fromJson(const Json::Value &j,       std::map< K, V >& v);

#define DECLARE_MAP_VARIANTS_KEY(r, data, K) \
    BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, K, MAP_VAL_TYPES)

    BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS_KEY, _, MAP_KEY_TYPES)

When I run the preprocesson on this code i get the following:

BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, int, (int)(double)(std::string)(std::vector<int>))
BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, double, (int)(double)(std::string)(std::vector<int>))
BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, std::string, (int)(double)(std::string)(std::vector<int>))

What I would like to see is:

void toJson(        Json::Value &j, const std::map< int,         int >& v);
void fromJson(const Json::Value &j,       std::map< int,         int >& v);
void toJson(        Json::Value &j, const std::map< double,      int >& v);
void fromJson(const Json::Value &j,       std::map< double,      int >& v);
void toJson(        Json::Value &j, const std::map< std::string, int >& v);
void fromJson(const Json::Value &j,       std::map< std::string, int >& v);

void toJson(        Json::Value &j, const std::map< int,         double >& v);
void fromJson(const Json::Value &j,       std::map< int,         double >& v);
void toJson(        Json::Value &j, const std::map< double,      double >& v);
void fromJson(const Json::Value &j,       std::map< double,      double >& v);
void toJson(        Json::Value &j, const std::map< std::string, double >& v);
void fromJson(const Json::Value &j,       std::map< std::string, double >& v);

void toJson(        Json::Value &j, const std::map< int,         std::string >& v);
void fromJson(const Json::Value &j,       std::map< int,         std::string >& v);
void toJson(        Json::Value &j, const std::map< double,      std::string >& v);
void fromJson(const Json::Value &j,       std::map< double,      std::string >& v);
void toJson(        Json::Value &j, const std::map< std::string, std::string >& v);
void fromJson(const Json::Value &j,       std::map< std::string, std::string >& v);

void toJson(        Json::Value &j, const std::map< int,         std::vector<std::string> >& v);
void fromJson(const Json::Value &j,       std::map< int,         std::vector<std::string> >& v);
void toJson(        Json::Value &j, const std::map< double,      std::vector<std::string> >& v);
void fromJson(const Json::Value &j,       std::map< double,      std::vector<std::string> >& v);
void toJson(        Json::Value &j, const std::map< std::string, std::vector<std::string> >& v);
void开发者_JAVA百科 fromJson(const Json::Value &j,       std::map< std::string, std::vector<std::string> >& v);

How to achieve this?


Unfortunately, I suppose this cannot be done only with BOOST_PP_SEQ_FOR_EACH. If BOOST_PP_SEQ_FOR_EACH_PRODUCT is allowed instead, probably the following macro will meet the purpose:

#define DECLARE_MAP_VARIANTS_(r, KV) \
    DECLARE_MAP_VARIANTS(r, BOOST_PP_SEQ_ELEM(0, KV), BOOST_PP_SEQ_ELEM(1, KV))

BOOST_PP_SEQ_FOR_EACH_PRODUCT(
    DECLARE_MAP_VARIANTS_, (MAP_KEY_TYPES)(MAP_VAL_TYPES) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜