출처 : http://www.viper.pe.kr
C++ 유용한 매크로
#format wiki
#language ko
##pragma section-numbers off
##acl All:
[[TableOfContents]]
== SAFE_ 매크로 ==
Direct3D 에서 제공하는 유용한 매크로로써 DirectX SDK 의 Framework 에서 찾음
{{{#!cplusplus numbers=off
//-----------------------------------------------------------------------------
// Miscellaneous helper functions
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
}}}
== 조건문 매크로 ==
조건문을 좀 더 명시적으로 나타내기 위하여 만든 매크로.
{{{#!cplusplus numbers=off
// 조건문 매크로
#define IS_TRUE(c) (c)
#define IS_FALSE(c) (!(c))
#define IS_EXIST(c) (c)
#define IS_NOT_EXIST(c) (!(c))
#define IS_VALID(c) (c)
#define IS_INVALID(c) (!(c))
#define IS_SUCCEEDED(c) (c)
#define IS_FAILED(c) (!(c))
// 포인터 매크로
#define IS_NULL(p) (!(p))
#define IS_NOT_NULL(p) (p)
#define IS_ZERO(n) (!(n))
#define IS_NOT_ZERO(n) (n)
// 범위 검사 매크로
#define IS_WITHIN(min,max,expr) (((min)<=(expr))&&((max)>=(expr)))
#define IS_WITHOUT(min,max,expr) (((min)>(expr))||((max)<(expr)))
}}}
== STL 매크로 ==
STL 을 사용할 때 자주 사용하는 코드를 매크로로 만듬.
{{{#!cplusplus numbers=off
// EMPTY 매크로
#define IS_EMPTY(c) ((c).empty())
#define IS_NOT_EMPTY(c) (!((c).empty()))
#define IS_EMPTY_PTR(p) ((p)->empty())
#define IS_NOT_EMPTY_PTR(p) (!((p)->empty()))
#define WHILE_NOT_EMPTY(c) while(!((c).empty()))
// FOUND 매크로
#define IS_FOUND(i,c) (i != (c).end())
#define IS_NOT_FOUND(i,c) (i == (c).end())
#define IS_FOUND_PTR(i,p) (i != (p)->end())
#define IS_NOT_FOUND_PTR(i,p) (i == (p)->end())
// FOREACH 매크로
#define FOREACH_MAP(i,c) for( i = (c).begin(); i != (c).end(); ++i )
#define FOREACH_MAP_PTR(i,c) for( i = (c)->begin(); i != (c)->end(); ++i )
#define FOREACH_VECTOR(i,c) for( i = 0; i < (c).size(); ++i )
#define FOREACH_VECTOR_PTR(i,c) for( i = 0; i < (c)->size(); ++i )
// CLEANUP 매크로
#define CLEANUP_MAP(i,c) for( i = (c).begin(); i != (c).end(); ++i ) \
delete (*i).second; \
(c).clear();
#define CLEANUP_MAP_PTR(i,c) for( i = (c)->begin(); i != (c)->end(); ++i ) \
delete (*i).second; \
(c)->clear();
#define CLEANUP_VECTOR(i,c) for( i = 0; i < (c).size(); ++i ) \
delete c[i]; \
(c).clear();
#define CLEANUP_VECTOR_PTR(i,c) for( i = 0; i < (c)->size(); ++i ) \
delete c->at(i); \
(c)->clear();
}}}
== Breakpoint 매크로 ==
Visual Studio 에서 강제로 Breakpoint 를 설정해야 할 경우 유용하게 사용할 수 있다. Win32 API 를 사용할 경우에는 {{{DebugBreak()}}} 함수를 사용할 수도 있지만 VC++ 만 사용한다면 intrin.h 헤더 파일을 포함한 후 {{{__debugbreak()}}} 함수를 사용하면 된다. 하지만 지원 안되는 버전의 VC++ (Visual Studio 2003 이하) 일 경우 다음과 같이 간단히 선언 후 사용할 수 있다.
{{{#!cplusplus numbers=off
#if (_MSC_VER >= 1400)
// Microsoft Visual C++ .NET 2005 이상
#include <intrin.h>
#else
// Microsoft Visual C++ .NET 2003 이하
#define __debugbreak() {__asm int 3}
#endif
}}}
* 응용 코드
{{{#!cplusplus numbers=off
#ifdef _WINDOWS
#define __debugbreak() { DebugBreak(); } // Windows 모드
#else
#define __debugbreak() {__asm int 3} // Console 모드
#endif // _WINDOWS
}}}
== 컴파일러 TODO 매크로 ==
* 컴파일러에 !ToDo 기능을 추가하는 방법 http://www.gpgstudy.com/forum/viewtopic.php?t=126
=== _TODO.h 파일 ===
{{{#!cplusplus numbers=off
// _TODO.h : TODO / FIXME / NOTE 정의
//
//------------------------------------------------------------------------------
// TODO / FIXME / NOTE macros
//------------------------------------------------------------------------------
#define _QUOTE(x) # x
#define QUOTE(x) _QUOTE(x)
#define __FILE__LINE__ __FILE__ "(" QUOTE(__LINE__) "): "
#define NOTE( x ) message( x )
#define FILE_LINE message( __FILE__LINE__ )
#define TODO( x ) message( __FILE__LINE__"[TODO]: " #x "\n" )
#define FIXME( x ) message( __FILE__LINE__"[FIXME]: " #x "\n" )
}}}
=== 매크로 사용 예 ===
* 소스 코드
{{{#!cplusplus numbers=off
// main.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의
//
#include "_TODO.h"
// main() 함수 구현
int main()
{
#pragma TODO( TODO 메시지 1...! )
#pragma FIXME( FIXME 메시지 2...! )
#pragma FILE_LINE
#pragma NOTE( "NOTE 메시지 3...!\n\
------------------------------------------------------------")
return 0;
}
}}}
* 컴파일 결과
{{{
------ 빌드 시작: 프로젝트: ExMacro, 구성: Debug Win32 ------
컴파일하고 있습니다.
main.cpp
d:\exmacro\main.cpp(9): [TODO]: TODO 메시지 1...!
d:\exmacro\main.cpp(10): [FIXME]: FIXME 메시지 2...!
d:\exmacro\main.cpp(12):
NOTE 메시지 3...!
------------------------------------------------------------
링크하고 있습니다.
빌드 시간 0:00
빌드 로그가 "file://d:\ExMacro\Debug\BuildLog.htm"에 저장되었습니다.
ExMacro - 0 오류, 0 경고
----------------------완료----------------------
빌드: 성공 1, 실패 0, 생략 0
}}}
== ASSERT(), VERIFY(), and TRACE() 매크로 ==
ASSERT() 와 VERIFY(), TRACE() 매크로는 MFC 에서 사용하는 매크로로써 디버깅에 매우 유용하다. 하지만 MFC 을 사용하지 않는 프로그램에서 사용할 수 없다. 그러나 이 매크로들을 사용할 수 있는 방법을 아래의 글에서 찾을 수 있다.
* Using ASSERT(), VERIFY(), and TRACE() in non-MFC Applications - http://www.gamedev.net/reference/articles/article1846.asp
* 다운로드 - DOWNLOAD:Sources/DebugMacro4NonMFC.zip
=== debug.h 파일 ===
{{{#!cplusplus numbers=off
// file debug.h
#ifndef __DEBUG_H__
#define __DEBUG_H__
#ifdef _DEBUG
void _trace(char *fmt, ...);
#define ASSERT(x) {if(!(x)) _asm{int 0x03}}
#define VERIFY(x) {if(!(x)) _asm{int 0x03}}
#else
#define ASSERT(x)
#define VERIFY(x) x
#endif
#ifdef _DEBUG
#define TRACE _trace
#else
inline void _trace(LPCTSTR fmt, ...) {}
#define TRACE 1 ? (void)0 : _trace
#endif
#endif // __DEBUG_H__
}}}
=== debug.cpp 파일 ===
{{{#!cplusplus numbers=off
// file debug.cpp
#ifdef _DEBUG
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
void _trace(char *fmt, ...)
{
char out[1024];
va_list body;
va_start(body, fmt);
vsprintf(out, fmt, body);
va_end(body);
OutputDebugString(out);
}
#endif
}}}
----
CategoryCpp
다음검색