Advertisement
Guest User

TRUE C++ CODE

a guest
Apr 24th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. // Singleton.h
  2.  
  3. #ifdef _MSC_VER
  4. #   if defined(_M_X64) || defined(_M_AMD64)
  5. #      define BREAKPOINT() do {__asm {int 03h}} while(0)
  6. #   else
  7. #      pragma intrinsic(__debugbreak)
  8. #      define BREAKPOINT() __debugbreak()
  9. #   endif
  10. #endif
  11.  
  12. template <class T>
  13. class CSingleton
  14. {
  15. public:
  16.    inline CSingleton()
  17.    {
  18.       if(ms_bInstanceCreated)
  19.           BREAKPOINT();
  20.  
  21.       ms_pInstance = ((T *)this);
  22.       ms_bInstanceCreated = true;
  23.    }
  24.    inline ~CSingleton()
  25.    {
  26.       ms_pInstance = nullptr;
  27.       ms_bInstanceCreated = false;
  28.    }
  29.  
  30. private:
  31.    static T *   ms_pInstance;
  32.    static bool  ms_bInstanceCreated;
  33. };
  34.  
  35. template <class T> T *  CSingleton<T>::ms_pInstance        = nullptr;
  36. template <class T> bool CSingleton<T>::ms_bInstanceCreated = false;
  37.  
  38. // YourLib.h
  39.  
  40. class CAppException
  41. {
  42. public:
  43.    inline CAppException(const char *pError)
  44.    {
  45.       if(pError)
  46.          std::strncpy(m_szError, pError, MAX_ERROR_LENGTH);
  47.       else
  48.          std::strcpy(m_szError, "<???>");
  49.    }
  50.    inline ~CAppException() = default;
  51.  
  52.    inline const char * GetError() const
  53.    {
  54.        return m_szError;
  55.    }
  56.  
  57. private:
  58.    CAppException() = delete;
  59.  
  60.    enum { MAX_ERROR_LENGTH = 2048 };
  61.    char m_szError[MAX_ERROR_LENGTH];
  62. };
  63.  
  64. class CApp : public CSingleton<CApp>
  65. {
  66. public:
  67.    ~CApp() = default;
  68.  
  69.    virtual void OnInit() {}
  70.    virtual void OnRelease() {}
  71.  
  72.    virtual void OnFrame() = 0;
  73.    virtual void OnError(CAppException &rEx) = 0;
  74.  
  75.    inline void Quit(int nCode)
  76.    {
  77.       m_bQuitting = true;
  78.       m_nQuitCode = nCode;
  79.    }
  80.  
  81. protected:
  82.    static int Run();
  83.  
  84. private:
  85.    CApp();
  86.  
  87.    bool m_bRunning;
  88.    bool m_bQuitting;
  89.    int  m_nQuitCode;
  90. };
  91.  
  92. // YourLib.cpp
  93.  
  94. CApp::CApp() : m_bQuitting(false), m_nQuitCode(0)
  95. {}
  96.  
  97. int CApp::Run()
  98. {
  99.    if(m_bRunning)
  100.       return 0;
  101.    m_bRunning = true;
  102.  
  103.    try
  104.    {
  105.       OnInit();
  106.  
  107.       m_bQuitting = false;
  108.       if(!m_bQuitting)
  109.          OnFrame();
  110.    }
  111.    catch(CAppException &rEx)
  112.    {
  113.       OnError(rEx);
  114.    }
  115.  
  116.    OnRelease();
  117.  
  118.    m_bRunning = false;
  119.    return m_nQuitCode;
  120. }
  121.  
  122. int main()
  123. {
  124.    CApp *pApp = CApp::GetInstance();
  125.  
  126.    if(pApp)
  127.       return pApp->Run();
  128.    else
  129.       return 0;
  130. }
  131.  
  132. // ProgrammersCode.cpp
  133.  
  134. class CProgrammersApp : public CApp
  135. {
  136. public:
  137.    CProgrammersApp();
  138.    ~CProgrammersApp();
  139.  
  140.    virtual void OnFrame() override;
  141.    virtual void OnError(CAppException &rEx) override;
  142. };
  143.  
  144. static CProgrammersApp s_ProgrammersApp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement