管理全局对象

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

时间: 2024-08-09 10:43:41

管理全局对象的相关文章

HTML5实战与剖析之历史管理(history对象)

HTML5新添加了对历史的管理,更新了history对象让管理历史状态更加方便了.在现代Web应用中,用户可以通过"前进"和"后退"按钮进行历史页面的切换.这让一些不在新页面中打开的新页面前进后退自如,提高了用户体验. 通过haschange事件,可以知道URL的参数什么时候发生了变化,也就是什么时候该有所反应.通过状态管理的API,能够在不加载新页面的情况下改变浏览器的URL.所以需要使用history.pushState()方法.history.pushStat

JavaScript中两种类型的全局对象/函数

这里所说的JavaScript指浏览器环境中的包括宿主环境在内的. 第一种是ECMAScript Global Object,第二种是宿主环境(Host)下的全局对象/函数. 一.核心JavaScript内置对象,即ECMAScript实现提供的不依赖于宿主环境的对象 这些对象在程序执行之前就已经(实例化)存在了.ECMAScript称为The Global Object,分为以下几种 1, 值属性的全局对象(Value Properties of the Global Object).有NaN

javascript如何列出全局对象的非原生属性

研究一个网站前端技术的时候,了解它的全局的对象是一个好的入口, 一般来说,常见的库就会用外观模式,最后暴露一个对象给用户调用, 比如jQuery,requirejs,angular,react均是用这种方式. 如果没有用cmd/amd模块化或类似webpack工具打包的话,会给全局对象window添加一个属性,如angular: 如React 同时,为了避免全局污染,也要关注全局变量的个数和详情. How 可以通过ES5的新增api(Object.keys)看浏览器全局变量列表: Object.

javascript如何列出全局对象的非原生属性。

Why 研究一个网站前端技术的时候,了解它的全局的对象是一个好的入口,一般来说,常见的库就会用外观模式,最后暴露一个对象给用户调用,比如jQuery,requirejs,angular,react均是用这种方式. 如果没有用cmd/amd模块化或类似webpack工具打包的话,会给全局对象window添加一个属性,如angular: 如React 同时,为了避免全局污染,也要关注全局变量的个数和详情. How 可以通过ES5的新增api(Object.keys)看浏览器全局变量列表:Object

Nodejs随笔(三):全局对象之global

首先,进入node REPL: [email protected]:~$ node > 查看global对象,发现其他全局对象(包括Buffer.require对象)以及全局方法都包含在global对象接口中: > console.log(global); { global: [Circular], process: { title: 'node', version: 'v0.12.1', moduleLoadList: [ 'Binding contextify', 'Binding nat

JS全局对象的属性

全局对象是最顶层的对象,在浏览器环境指的是window对象.在ES5中,全局对象的属性与全局变量是等价的. var str = "hello"; function test(){ alert(window.str); } window.str = "hello"; function test(){ alert(str); } 以上两段代码输出结果都是hello. 这种规定被视为是JS语言的一大问题,因为很容易不知不觉就创建了全局变量.在ES6中,var命令和func

全局对象的构造函数会在main 函数之前执行

#include <iostream> using namespace std; class A { public: A() { cout << "Generator A" << endl; } } a = A(); int main() { cout << "Hello World" << endl; } 全局对象的构造会在main函数之前执行.

ASP.NET——Application全局对象

Application是应用全局对象,被全体共享.无论通过哪个页面操作Application,另一个页面都可以读取Application信息. 由于Application是共享的,操作之前先Lock,操作完成后UnLock. 在一个页面设置数据: Application.Lock(); Application.Set("address", "上海"); Application.UnLock(); 在另一个页面取数据: string s = (string)Appli

使用Executor管理Thread对象详解

java SE5的java.util.concurrent包中的执行器(Executor)是管理Thread对象的优选方法.使用Executor管理Thread对象可以简化并发编程. Executor是在客户端和任务执行之间提供了一个间接层,与客户端直接执行任务不同,我们将使用Executor来执行任务.Executor允许你管理异步任务的执行,而无须显示地管理线程的生命周期. 线程对象知道如何运行具体的任务,它暴露了要执行的单一方法.ExecutorService(具有生命周期的Executo