宏
- 结构体预定义,可定义同类型不通结构,在宏中展开
😊💕😁👍🙌🤦♀️🤦♂️🤷♀️🤷♂️😜💖😢🎶😎🤞✌👏💋🌹🎉🎂🤳🐱👤🐱🏍✨😃🎁❤💋🌹💖🎶🙌😒😘😉😍😘🙌👀👏🎟🎏🧨🎇🎨🎠🎊🎉🎊
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按照哪个序列进行展开
-
-