8. BOOST_PP 를 이용한 해결
• 전처리기 기본
• 매크로
– object-like macro
• #define 식별자 치환 - 목록
– function-like macro
• #define 식별자 (a0,a1,…,an-1) 치환 - 목록
• 매크로 인수
– , ( )
9. • 매크로 인수
– FOO( std::pair<int,long> ) // 인수 2 개
– FOO({int x=1,y=2;return x+y;}) // 인수 2 개
– FOO( (std::pair<int,long>) ) // 인수 1 개
– FOO(({int x=1,y=2;return x+y;})) // 인수 1 개
9
, 를 전달할 때는 BOOST_PP_COMMA 를
사용한다 .
28. 28
#define POST_STATE_MAX_ARG 2
class KSystemStateEvent
{
public:
template<typename T>
void _Push( const char* pszInEvent_, T inValue_ )
{
std::cout << pszInEvent_ << ":" << inValue_ << std::endl;
}//_Push()
#define _POST_STATE_EVENT_TEMPLATE(z, n, unused)
template<BOOST_PP_ENUM_PARAMS(n, typename T)>
void PostStateEvent( const char* szInEvent_,
BOOST_PP_ENUM_PARAMS( n, T ) );
BOOST_PP_REPEAT_FROM_TO( 1,
BOOST_PP_ADD(POST_STATE_MAX_ARG,1),
_POST_STATE_EVENT_TEMPLATE, ~ )
#undef _POST_STATE_EVENT_TEMPLATE
};//class KSystemStateEvent
선언과 body 를 별도로 구현 .
먼전 선언 부분을 간단하게 구
현한다 .
29. 29
#ifndef BOOST_PP_IS_ITERATING
1) 반복을 호출하는 부분 작성
#else // #ifndef BOOST_PP_IS_ITERATING
2) 되풀이 되는 패턴을 제공하는 부분 작성
#endif // #ifndef BOOST_PP_IS_ITERATING
KSystemState.hpp 파일 작성
30. 30
#ifndef _KSYSTEMSTATE_INL
#define _KSYSTEMSTATE_INL
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/iteration/iterate.hpp>
#define _POST_ARG(z, n, unused) BOOST_PP_CAT(T,n) BOOST_PP_CAT(In,n)
#define _POST_PUSH(z, n, unused) _Push( pszInEvent, BOOST_PP_CAT(In,n) );
// generate specializations
#define BOOST_PP_ITERATION_LIMITS (1, POST_STATE_MAX_ARG)
#define BOOST_PP_FILENAME_1 "KSystemState.hpp" // this file
#include BOOST_PP_ITERATE()
#undef BOOST_PP_FILENAME_1
#undef BOOST_PP_ITERATION_LIMITS
#undef _POST_PUSH
#undef _POST_ARG
#endif // #ifndef _KSYSTEMSTATE_INL
1) 반복을 호출하는
부분
각각의 n 에 대해 자기자신
KSystemState.hpp 를
include 한다 .
31. 31
#define n BOOST_PP_ITERATION()
template<BOOST_PP_ENUM_PARAMS(n, typename T)>
void KSystemStateEvent::PostStateEvent( const char* pszInEvent
, BOOST_PP_ENUM( n, _POST_ARG, ~) )
{
BOOST_PP_REPEAT( n, _POST_PUSH, ~ )
}
#undef n
2) 되풀이 되는 패턴을 제공하는
부분
각각의 n 에 대해 (n 이 1, 2 일 때 ) 확장된다 .
32. 32
…
#define POST_STATE_MAX_ARG 4
class KSystemStateEvent
{
…
};//class KSystemStateEvent
#include "KSystemState.hpp"
KSystemStateEvent g_systemState;
void main()
{
g_systemState.PostStateEvent( "Input", 0 );
g_systemState.PostStateEvent( "Output", 1, "dummy", 1.2f, 0xff );
//Input:0
//Output:1
//Output:dummy
//Output:1.2
//Output:255
// 계속하려면아무키나누르십시오 . . .
}//main()
이제 자유롭게 아규먼트의 개
수만 정의하면 됩니다 ^^;