BOOST_PP_SEQ_FOR_EACH boost宏

  • 结构体预定义,可定义同类型不通结构,在宏中展开

😊💕😁👍🙌🤦‍♀️🤦‍♂️🤷‍♀️🤷‍♂️😜💖😢🎶😎🤞✌👏💋🌹🎉🎂🤳🐱‍👤🐱‍🏍✨😃🎁❤💋🌹💖🎶🙌😒😘😉😍😘🙌👀👏🎟🎏🧨🎇🎨🎠🎊🎉🎊

struct ExperimentMapping {
   tsl::robin_map<std::string, std::string> unitmap;
   FileWatchInfo watch_info;
   void insert(const string &key,const ml::utilities::StringTokenizer &tok) {
       if (tok.Size() >= 2) {
         unitmap.insert(make_pair(key, tok[1].String()));
       }
   }
};

  • 定义宏

#define RANKER_FILE_SEQ                                      \
((experiment_mapping, GetExperimentMapping, ExperimentMapping, false, 100, "\t "))

  • 定义类

struct ExperimentMapping {
   tsl::robin_map<std::string, std::string> unitmap;
   FileWatchInfo watch_info;
   void insert(const string &key,const ml::utilities::StringTokenizer &tok) {
       if (tok.Size() >= 2) {
         unitmap.insert(make_pair(key, tok[1].String()));
       }
   }
};
class DataCenter{
public:
    DataCenter() = default;
    ~DataCenter();
public:
#define ExpandInterfaceFunMacro(r, data, elem)                      \
    std::shared_ptr<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem)>  \
       BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 1, elem) ();
BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
#undef ExpandInterfaceFunMacro
private:
   typedef ml::utilities::DynamicWatcher ObjectHandlerList;
   template <typename T, bool IsRealTime>
   std::shared_ptr GetFromObject(const std::string &key){
      return std::shared_ptr(object_void, reinterpret_cast<T *>(object_void.get()));
   }
   int ProcessFileWrapper(const std::string &key, const std::string &jvalue,
     const ml::utilities::WatchMeta &meta,ObjectHandlerList &handler_list){
         return 0UL;
   }
#define ExpandInterfaceFunMacro(r, data, elem)                       \
   static std::shared_ptr<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem)>     \
       BOOST_PP_CAT(Process,BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem))(const std::string \
       &filename,const json11::Json &json);       
BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
#undef ExpandInterfaceFunMacro
};

  • 定义接口

#define ExpandInterfaceFunMacro(r, data, elem)                         \
   std::shared_ptr<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem)>  \
   DataCenter::BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 1, elem)() {   \
    return GetFromObject<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem), \
      BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 3, elem)>(         \
      BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 0, elem)));\
   }
BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
#undef ExpandInterfaceFunMacro

#define ExpandInterfaceFunMacro(r, data, elem)                         \
  std::shared_ptr<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem)>   \
  DataCenter::BOOST_PP_CAT(Process, BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem))(    \
   const std::string &filename, const json11::Json &json) {              \
     return ProcessTimeFile<BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 2, elem)>(filename, json, \
       BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 4, elem), \
       BOOST_PP_TUPLE_ELEM(BOOST_PP_TUPLE_SIZE(elem), 5, elem)); \
  }
BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
#undef ExpandInterfaceFunMacro

  • 上述宏展开实际得到下面两个函数:

// BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
std::shared_ptr DataCenter::GetExperimentMapping(){
  return GetFromObject<ExperimentMapping, false>(experiment_config);
}

// BOOST_PP_SEQ_FOR_EACH(ExpandInterfaceFunMacro, _, RANKER_FILE_SEQ);
std::shared_ptr DataCenter::ProcessExperimentMapping(const std::string &filename,const json11::Json &json) {
  return ProcessTimeFile(filename, json, 100, “\t”);
}

  • 总结
    • BOOST_PP_SEQ_FOR_EACH 宏,用于将一个序列中参数依次按照指定宏进行展开。

      • macro, 一个以格式macro(r, data, elem)定义的三元宏。该宏被BOOST_PP_SEQ_FOR_EACH 按照 seq 中每个元素进行展开。展开该宏,需要用到下一个 BOOST_PP_FOR 的重复项、备用数据data和当前元素。

      • data ,备用数据,用于传给 macro

      • seq,用于供macro按照哪个序列进行展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值