'setter'에 해당되는 글 1건

  1. 2017.01.06 Getter Setter Macro in C/C++

Getter/Setter Macro입니다.
코드에 하나씩 (1) 선언 (2) Get (2) Set Method를 작성할 필요가 없게 되며, 필요시 구분하여 사용하여 시간을 절약할 수 있습니다.

감사합니다.

The follow codes are the example macro of getter/setter. This code can auto-generate (1) defintion (2) getter method (2) setter method once at all and you can save you time :)

Thank you very much.

1) Make a Header File as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef COMM_MACRO_H
#define COMM_MACRO_H
 
#ifdef  __cplusplus
extern "C" {
#endif
 
#define GETTER(T,member) const T& Get##member() const { return member; }
#define SETTER(T, member) void Set##member(const T & value) { member =value; } // std::string, BAR
#define GSETTER(T, member) private: T member;  SETTER(T,member) public: GETTER(T,member)
#define PGSETTER(T, member) private: T member;  public: SETTER(T,member) GETTER(T,member) private:
 
#ifdef  __cplusplus
}
#endif
 
#endif  /* COMM_MACRO_H */

2) Sample Code

2.1) Sample Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef APPCONF_H
#define APPCONF_H
 
#include <string>
#include "COMM/UTIL/COMM_MACRO.h"
 
using namespace std;
 
class AppConf {
public:
    GSETTER(int, EIF_LISTENING_PORT);
    GSETTER(std::string, EIF_IP);
    GSETTER(bool, IsActiveNode);
};
#endif  /* APPCONF_H */

2.2) Sample Code

1
2
3
4
5
6
7
8
9
// Setting Values
SetEIF_IP("192.168.1.50");
SetEIF_LISTENING_PORT(8881);
SetIsActiveNode(false);
 
// Getting Values
string EIFIP=GetEIF_IP();
int PORT=GetEIF_LISTENING_PORT();
bool IsActive = GetIsActiveNode();


Posted by kkckc
,