zoukankan      html  css  js  c++  java
  • 管理全局对象

    //manage exit ==>delete process object

    class chExitMethodList;
    typedef void (*msgExitMethod)();
    class ETLLIB_DLL_DECLARRE chExitMethodWrap
    {
    friend class chExitMethodList;
    public:
    void onExit();
    public:
    chExitMethodWrap(chExitMethodList* pOwner, msgExitMethod method);
    ~chExitMethodWrap();
    private:
    msgExitMethod m_pMethod;
    chExitMethodList* m_pOwnerList;
    };

    class ETLLIB_DLL_DECLARRE chExitMethodList : public chObjList_stack< chExitMethodWrap* >
    {
    public:
    BOOL AddExitMethod(chExitMethodWrap* pWrap);
    void RemoveExitMethod(chExitMethodWrap* pWrap);
    void ExecuteOnExit();
    };

    //////////////////////////////////////////////////////////////////////////
    // class chExitMethodWrap
    chExitMethodWrap::chExitMethodWrap(chExitMethodList* pOwner, msgExitMethod method)
    {
    m_pOwnerList = pOwner;
    m_pMethod = method;
    pOwner->AddExitMethod(this);
    }

    chExitMethodWrap::~chExitMethodWrap()
    {
    if(m_pOwnerList != NULL)
    {
    m_pOwnerList->RemoveExitMethod(this);
    onExit();
    }
    }

    void chExitMethodWrap::onExit()
    {
    m_pMethod();
    m_pMethod = NULL_METHOD;
    m_pOwnerList = NULL;
    }

    //////////////////////////////////////////////////////////////////////////
    // class chExitMethodList
    BOOL chExitMethodList::AddExitMethod(chExitMethodWrap* pWrap)
    {
    chASSERT(pWrap != NULL_METHOD && !chExitMethodList::has_value(pWrap));
    chExitMethodList::push_front(pWrap);
    return true;
    }

    void chExitMethodList::RemoveExitMethod(chExitMethodWrap* pWrap)
    {
    chExitMethodList::erase_value(pWrap);
    }

    void chExitMethodList::ExecuteOnExit()
    {
    while(!empty())
    {
    chExitMethodWrap* pWrap = front();
    pop_front();
    pWrap->onExit();
    }
    }

    #define DECLARE_PROCESS_OBJECT(type)
    public: static type& getInstance();
    public: static bool hasInstance();
    public: static void releaseInstance();

    #define IMPLEMENT_PROCESS_OBJECT(type)
    static type* s_##type = NULL;
    static bool b_##type##freed = false;
    type& type::getInstance() { static bool bConstructing = false; chASSERT(!b_##type##freed && !bConstructing); if(s_##type == NULL && !b_##type##freed) { bConstructing = true; s_##type = new type(); bConstructing = false; static chExitMethodWrap wrap(&g_uCSystemMessage, type::releaseInstance); } return *s_##type; }
    bool type::hasInstance() { return s_##type != NULL; }
    void type::releaseInstance() { if(s_##type != NULL) { delete s_##type; s_##type = NULL; b_##type##freed = true;} }

    //note    g_uCSystemMessage ==> public chExitMethodList

  • 相关阅读:
    第一周例行报告psp
    作业要求 2018091-2 博客作业
    20181011-1第一周例行报告
    20180912-2第一周博客作业
    Elasticsearch
    centos7 安装Hadoop-2.6.0-cdh5.16.1.tar.gz
    centos7安装hadoop2.7.7
    centos7安装jdk8
    专业知识4
    专业知识3
  • 原文地址:https://www.cnblogs.com/hqu-ye/p/4535008.html
Copyright © 2011-2022 走看看