管理全局对象

//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

转载于:https://www.cnblogs.com/hqu-ye/p/4535008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值