[CPP] 유용한 매크로 & 함수

//-----------------------------------------------------------------------------
// Name: LOG_A
// Desc: 로그 출력 유틸
//-----------------------------------------------------------------------------
namespace NS_UTIL
{
 inline void LOG_A(char* pcString, ...)
 {
#ifdef _DEBUG
  char    buff[255];
  va_list marker;
  va_start(marker, pcString);
  vsprintf(buff, pcString, marker);
  va_end(marker);
  //WCHAR wstrDst[256];
  //ConvertAnsiStringToWide(wstrDst, buff, 256);
  //char cInfo[80];
  //char* pcFileName = strrchr(__FILE__, '\');
  //sprintf(cInfo, "Log(FN: %s, FUN: %s, L:%d \n",pcFileName, __FUNCTION__, __LINE__);
  //OutputDebugString( cInfo);
  OutputDebugString(buff);
  //OutputDebugString( "\n" );
#endif
 }
 //ex LOG_A_("if(!pElement)");
#define LOG_A_(msg) NS_UTIL::LOG_A("\n F: %s, L:%d, FUN: %s ,\nMsg: %s\n",__FILE__, __LINE__, __FUNCTION__, msg);
}
//-----------------------------------------------------------------------------
// Name: cSingleton
// Desc: 싱글톤

//-----------------------------------------------------------------------------
/* ex)
class MyClass : public Singleton<MyClass>
MyClass::GetInstance() / ReleaseInstance()
*/
template < typename T >
class cSingleton
{
 friend T;
public:
 cSingleton() {};
 virtual void Initialize() = 0;
 static T* GetInstance()
 {
  if (_Instance == 0)
  {
   _Instance = new T();
   _Instance->Initialize();
  }
  return _Instance;
 }
 static void ReleaseInstance()
 {
  if (_Instance != 0)
  {
   delete _Instance;
  }
  _Instance = 0;
 }
protected:
 static T*  _Instance;
};
template < typename T > T*
cSingleton< T >::_Instance = 0;
#endif


//-----------------------------------------------------------------------------
// Name: 
// Desc: 클라이언트 화면 중앙 정렬

//-----------------------------------------------------------------------------
if (!IsFullScreen)
{
 RECT rect = { 0, 0, SURFACE_WIDTH, SURFACE_HEIGHT };
 long iX, iY, iWidth, iHeight;
 ::GetWindowRect(g_hWnd, &rect);
 iWidth = rect.right - rect.left;
 iHeight = rect.bottom - rect.top;
 iX = LONG((GetSystemMetrics(SM_CXSCREEN) - iWidth) / 2);
 iY = LONG((GetSystemMetrics(SM_CYSCREEN) - iHeight) / 2);
 ::MoveWindow(g_hWnd, iX, iY, iWidth, iHeight, TRUE);
 //::SetCursorPos(GetSystemMetrics(SM_CXSCREEN)/2, GetSystemMetrics(SM_CYSCREEN)/2);
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: LEAK MEMORY CHECK

//-----------------------------------------------------------------------------
#ifdef __MEMORY_LEAK_CHECK__
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
// Detected memory leaks
char szTemp[12] = { NULL, };
long lLeakNum = 0;
ifstream ifStrema;
ifStrema.open("\\MemoryNum.txt");
if (ifStrema.is_open() == true)
{
 ifStrema.seekg(0, SEEK_END);
 int iSize = ifStrema.tellg();    ifStrema.seekg(0, SEEK_SET);
 ifStrema.read((char*)szTemp, iSize);
 lLeakNum = atol(szTemp);
 ifStrema.close();
}
if (lLeakNum > 0)
_CrtSetBreakAlloc(lLeakNum);
#endif //__MEMORY_LEAK_CHECK__

#define SAFE_DELETE(x) { if(x!=NULL) delete x; x=NULL; }


//-----------------------------------------------------------------------------
// Name: OnBasicException
// Desc: Exception

//-----------------------------------------------------------------------------
#define _Error(code)  g_BasicException(code, NULL, __FILE__, __LINE__)

#define _ErrorStr(sz_error) g_BasicException(INVALID_EXCEPTION_CODE, sz_error, __FILE__, __LINE__)
void OnBasicException(EXCEPTION_CODE code, char * sz_error, char * file, unsigned long line)
{
 const int WHERE_BUF_SIZE = 100;

 char * app_title;

 switch (code)
 {
 case INVALID_EXCEPTION_CODE:
  app_title = sz_error;
  break;

 case FILE_OPEN:
  app_title = "File open failed";
  break;

 case FILE_LOAD:
  app_title = "File load failed";

 case FAILED_JOB:
  app_title = "Job failed";
  break;

 case NULL_REF:
  app_title = "Null referenced";
  break;

 case MEM_ALLOC:
  app_title = "Not enough memory";
  break;
 }

 //
 // error가 발생한 source file name, line을 출력한다.
 //
 char where_error[WHERE_BUF_SIZE];
 char temp[6]; // max: 99,999

 strcpy(where_error, "File: ");
 strcat(where_error, file);
 strcat(where_error, "\n");
 strcat(where_error, "Line: ");
 sprintf(temp, "%d", line);
 strcat(where_error, temp);

 MessageBox(NULL, where_error, app_title, MB_OK | MB_ICONERROR);

 abort();

}

댓글

이 블로그의 인기 게시물

[유니티] 오류 사례 "Moving file failed", "Temp"

[유니티 사례] 비주얼 스튜디오(Visual Studio) 실행 오류

[유니티] 구글 지도 출력 샘플