C++中建立对象间消息连接的系统方法
:pointerToCBO(NULL), pointerToCBSF(pCBSF)
{
eventName = strdup(ename);
}
EventRecord::~EventRecord(void)
{
if(eventName) delete eventName;
}
void EventRecord::operator = (const EventRecord& er)
{
if(er.eventName)
eventName = strdup(er.eventName);
else
eventName = NULL;
pointerToCBO = er.pointerToCBO;
pointerToCBF = er.pointerToCBF;
}
int EventRecord::operator == (char *ename) const
{
if((eventName == NULL)||ename == NULL)
return eventName == ename;
else
return strcmp(eventName,ename) == 0;
}
int EventRecord::operator == (const EventRecord& er) const
{
return (er == eventName) /*er和eventname不能交换位置*/
&&(pointerToCBO == er.pointerToCBO)
&&(pointerToCBO ?
(pointerToCBF == er.pointerToCBF):
(pointerToCBSF == er.pointerToCBSF));
}
void EventRecord::Flush(void)
{
if(eventName){
delete eventName;
eventName = NULL;
}
pointerToCBO = NULL;
pointerToCBF = NULL;
}
int EventRecord::IsEmpty(void) const
{
if(eventName == NULL)
return 1;
else
return 0;
}
file://Callback类的实现
CallBack::CallBack(void)
{
file://按初始尺寸为回调表分配内存空间
callBackList = new EventRecord[CALLBACKLIST_INIT_SIZE];
if(!callBackList){
cerr<<"CallBack: memory allocation error."<<endl;
exit(1);
}
size = CALLBACKLIST_INIT_SIZE;
lastpos = 0;
curpos = 0;
}
CallBack::CallBack(const CallBack& cb): curpos(cb.curpos),lastpos(cb.lastpos),size(cb.size)
{
callBackList = new EventRecord[size];
if(!callBackList){
cerr<<"CallBack: memory allocation error."<<endl;
exit(1);
}
file://一一复制各条事件记录
for(int i = 0; i < size; i++) callBackList[i] = cb.callBackList[i];
}
void CallBack::operator = (const CallBack& cb)
{
curpos = cb.curpos;
lastpos = cb.lastpos;
size = cb.size;
delete [] callBackList;//删除旧的回调表
callBackList = new EventRecord[size];//重新分配内存空间
if(!callBackList){
cerr<<"CallBack: memory allocation error."<<endl;
exit(1);
}
file://一一复制各条事件记录
for(int i = 0; i < size; i++) callBackList[i] = cb.callBackList[i];
}
CallBack::~CallBack(void)
{
delete [] callBackList;
}
void CallBack::AddCallBack(char *event, CallBackFunction pCBF, CallBack *pCBO)
{
file://如事件名为空,退出
if( (event == NULL)?1:(strlen(event) == 0)) return;
file://寻找因删除事件记录而产生的第一个空闲位置,并填写新事件记录
for(int start=0;start<lastpos;start++)
if(callBackList[start].IsEmpty()){
callBackList[start] = EventRecord(event,pCBO,pCBF);
break;
}
if(start < lastpos) return; file://确实存在空闲位置
file://没有空闲位置,在回调表后追加新记录
if(lastpos == size) file://回调表已满,需“伸长”
{
EventRecord *tempList = callBackList;//暂存旧回调表指针
file://以一定的步长“伸长”回调表
callBackList = new EventRecord[size + CALLBACKLIST_INCREMENT];
if(!callBackList){
cerr<<"CallBack: memory allocation error."<<endl;
exit(1);
}
file://复制旧回调表中的记录
for(int i = 0; i < size; i++) callBackList[i] = tempList[i];
delete [] tempList;//删除旧回调表
size += CALLBACKLIST_INCREMENT;//记下新回调表的尺寸
}
file://构造新的事件记录并将其填入回调表中
callBackList[lastpos] = EventRecord(event,pCBO,pCBF);
lastpos++;
}
void CallBack::AddCallBack(char *event,CallBackStaticFunction pCBSF)
{
if( (event == NULL)?1:(strlen(event) == 0)) return;
for(int start=0;start<lastpos;start++)
if(callBackList[start].IsEmpty()){
callBackList[start] = EventRecord(event,pCBSF);
break;
}
if(start < lastpos) return; file://a hole is found
if(lastpos == size) file://event list is insufficient
{
EventRecord *tempList = callBackList;
callBackList = new EventRecord[size + CALLBACKLIST_INCREMENT];
if(!callBackList){
cerr<<"CallBack: memory allocation error."<<endl;
exit(1);
}
for(int i = 0; i < size; i++) callBackList[i] = tempList[i];
delete [] tempList;
size += CALLBACKLIST_INCREMENT;
}
callBackList[lastpos] = EventRecord(event,pCBSF);
lastpos++;
}
file://删除注册在指定事件上的成员函数
void CallBack::RemoveCallBack(char *event, CallBackFunction pCBF, CallBack *pCBO)
{
if( (event == NULL)?1:(strlen(event) == 0)) return;
EventRecord er(event,pCBO,pCBF);
for(int i = 0; i < lastpos; i++)
if(callBackList[i] == er) callBackList[i].Flush();
}
file://删除注册在指定事件上的静态成员函数或普通函数
void CallBack::RemoveCallBack(char *event,CallBackStaticFunction pCBSF)
{
if( (event == NULL)?1:(strlen(event) == 0)) return;
EventRecord er(event,pCBSF);
for(int i = 0; i < lastpos; i++)
if(callBackList[i] == er) callBackList[i].Flush();
}
file://删除注册在指定事件上的所有回调函数
void CallBack::RemoveCallBack(char *even

