我找到这个代码了,下面的代码我删除了大部分内容,只保留讲解需要用到的内容
先说一下 PARAM_SET_GET 这个宏定义
这个宏会创建一个成员变量,然后创建一个 set 函数和一个 get 函数#define PARAM_SET_GET(type, name, qualifier, setqualifier, getqualifier)\
qualifier: type m_##name;\
getqualifier: inline type get##name() const {return m_##name;}\
setqualifier: inline void set##name(type name) {m_##name=name;}
ScanMatcher 这个类使用了 PARAM_SET_GET 这个宏,创建了 laserMaxRange 这个成员变量,还有 set 函数 和 get 函数class ScanMatcher{
protected:
PARAM_SET_GET(double, laserMaxRange, protected, public, public)
};
这个类,宏展开后是这样的,当然我调整了一下格式,更易于阅读class ScanMatcher{
protected:
protected: double m_laserMaxRange;
public: inline double getlaserMaxRange() const {return m_laserMaxRange;}
public: inline void setlaserMaxRange(double laserMaxRange) {m_laserMaxRange=laserMaxRange;}
};
下面这个宏的作用是,导出作为成员变量 member 的 name 函数,就是包装一下#define MEMBER_PARAM_SET_GET(member, type, name, qualifier, setqualifier, getqualifier)\
getqualifier: inline type get##name() const {return member.get##name();}\
setqualifier: inline void set##name(type name) { member.set##name(name);}
在类 GridSlamProcessor 中用到了 MEMBER_PARAM_SET_GET 这个宏class GridSlamProcessor{
public:
/**the scanmatcher algorithm*/
ScanMatcher m_matcher;
//accessor methods
/**the maxrange of the laser to consider */
MEMBER_PARAM_SET_GET(m_matcher, double, laserMaxRange, protected, public, public);
protected:
/**Copy constructor*/
GridSlamProcessor(const GridSlamProcessor& gsp);
};
这个类,宏展开后是这样的,同样调整了格式,更易于阅读class GridSlamProcessor{
public:
ScanMatcher m_matcher;
public: inline double getlaserMaxRange() const {return m_matcher.getlaserMaxRange();}
public: inline void setlaserMaxRange(double laserMaxRange) { m_matcher.setlaserMaxRange(laserMaxRange);};
protected:
GridSlamProcessor(const GridSlamProcessor& gsp);
};
下面是完整的代码#define PARAM_SET_GET(type, name, qualifier, setqualifier, getqualifier)\
qualifier: type m_##name;\
getqualifier: inline type get##name() const {return m_##name;}\
setqualifier: inline void set##name(type name) {m_##name=name;}
class ScanMatcher{
protected:
PARAM_SET_GET(double, laserMaxRange, protected, public, public)
};
#define MEMBER_PARAM_SET_GET(member, type, name, qualifier, setqualifier, getqualifier)\
getqualifier: inline type get##name() const {return member.get##name();}\
setqualifier: inline void set##name(type name) { member.set##name(name);}
class GridSlamProcessor{
public:
/**the scanmatcher algorithm*/
ScanMatcher m_matcher;
//accessor methods
/**the maxrange of the laser to consider */
MEMBER_PARAM_SET_GET(m_matcher, double, laserMaxRange, protected, public, public);
protected:
/**Copy constructor*/
GridSlamProcessor(const GridSlamProcessor& gsp);
};
通过 g++ 预处理后是这样的$ g++ -E main.cpp
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
class ScanMatcher{
protected:
protected: double m_laserMaxRange;public: inline double getlaserMaxRange() const {return m_laserMaxRange;}public: inline void setlaserMaxRange(double laserMaxRange) {m_laserMaxRange=laserMaxRange;}
};
class GridSlamProcessor{
public:
ScanMatcher m_matcher;
public: inline double getlaserMaxRange() const {return m_matcher.getlaserMaxRange();}public: inline void setlaserMaxRange(double laserMaxRange) { m_matcher.setlaserMaxRange(laserMaxRange);};
protected:
GridSlamProcessor(const GridSlamProcessor& gsp);
};
调整一下格式,使之更易于阅读class ScanMatcher{
protected:
protected: double m_laserMaxRange;
public: inline double getlaserMaxRange() const {return m_laserMaxRange;}
public: inline void setlaserMaxRange(double laserMaxRange) {m_laserMaxRange=laserMaxRange;}
};
class GridSlamProcessor{
public:
ScanMatcher m_matcher;
public: inline double getlaserMaxRange() const {return m_matcher.getlaserMaxRange();}
public: inline void setlaserMaxRange(double laserMaxRange) { m_matcher.setlaserMaxRange(laserMaxRange);};
protected:
GridSlamProcessor(const GridSlamProcessor& gsp);
};
|