C++ 对象间通信框架 V2.0 ××××××× 之(五)

类定义:

=======================================================================

 1 // MemberFuncPointer.h: interface for the CMemberFuncPointer class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_)
 6 #define AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 // 成员函数指针最大长度
13 #define MEMBER_FUNC_POINTER_MAX_SIZE 32
14 class CMemberFuncPointer  
15 {
16 public:
17     void Reset();
18     void Set(const void*pFunc,int p_size);
19     void Get(void*pFunc);
20     CMemberFuncPointer();
21     CMemberFuncPointer(const char pointerdata[],int p_size);
22     CMemberFuncPointer(void* pointerdata,int p_size);
23     virtual ~CMemberFuncPointer();
24     
25     bool operator==(const CMemberFuncPointer &mfp) const;
26     bool operator!=(const CMemberFuncPointer &mfp) const;
27 
28     CMemberFuncPointer& operator=(const CMemberFuncPointer &mfp);
29 
30 private:
31     int m_size;
32     char m_pointer_data[MEMBER_FUNC_POINTER_MAX_SIZE];
33 };
34 
35 #endif // !defined(AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_)
MemberFuncPointer.h
  1 // MemberFuncPointer.cpp: implementation of the CMemberFuncPointer class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "OverlapRoutine.h"
  7 #include "MemberFuncPointer.h"
  8 
  9 #ifdef _DEBUG
 10 #undef THIS_FILE
 11 static char THIS_FILE[]=__FILE__;
 12 #define new DEBUG_NEW
 13 #endif
 14 
 15 //////////////////////////////////////////////////////////////////////
 16 // Construction/Destruction
 17 //////////////////////////////////////////////////////////////////////
 18 
 19 CMemberFuncPointer::CMemberFuncPointer()
 20 {
 21     this->m_size = 0;
 22 }
 23 
 24 CMemberFuncPointer::CMemberFuncPointer(const char pointerdata[],int p_size)
 25 {
 26     if(p_size<=MEMBER_FUNC_POINTER_MAX_SIZE)
 27     {
 28         this->m_size = p_size;
 29         memcpy(this->m_pointer_data,pointerdata,p_size); 
 30     }
 31     else
 32     {
 33         this->m_size = 0;
 34     }
 35 }
 36 
 37 CMemberFuncPointer::CMemberFuncPointer(void* pointerdata,int p_size)
 38 {
 39     if(p_size<=MEMBER_FUNC_POINTER_MAX_SIZE)
 40     {
 41         this->m_size = p_size;
 42         memcpy(this->m_pointer_data,pointerdata,p_size); 
 43     }
 44     else
 45     {
 46         this->m_size = 0;
 47     }
 48 }
 49 
 50 CMemberFuncPointer::~CMemberFuncPointer()
 51 {
 52 
 53 }
 54 
 55 bool CMemberFuncPointer::operator==(const CMemberFuncPointer &mfp) const
 56 {
 57     if(this->m_size==mfp.m_size) //mfp.m_size 可以访问私有成员
 58     {
 59         if(0==memcmp(this->m_pointer_data,mfp.m_pointer_data,this->m_size))
 60         {
 61             return true;
 62         }
 63     }
 64 
 65     return false;
 66 }
 67 
 68 bool CMemberFuncPointer::operator!=(const CMemberFuncPointer &mfp) const
 69 {
 70     if(this->m_size==mfp.m_size) //mfp.m_size 可以访问私有成员
 71     {
 72         if(0==memcmp(this->m_pointer_data,mfp.m_pointer_data,this->m_size))
 73         {
 74             return false;
 75         }
 76     }
 77 
 78     return true;
 79 }
 80 
 81 void CMemberFuncPointer::Get(void *pFunc)
 82 {
 83     memcpy(pFunc,this->m_pointer_data,this->m_size);
 84 }
 85 
 86 void CMemberFuncPointer::Set(const void *pFunc,int p_size)
 87 {
 88     memcpy(this->m_pointer_data,pFunc,p_size);
 89     this->m_size = p_size;
 90 }
 91 
 92 void CMemberFuncPointer::Reset()
 93 {
 94     this->m_size = 0;
 95 }
 96 
 97 CMemberFuncPointer& CMemberFuncPointer::operator =(const CMemberFuncPointer &mfp)
 98 {
 99     if(this != &mfp)
100     {
101         memcpy(this->m_pointer_data,mfp.m_pointer_data,mfp.m_size); //mfp.m_pointer_data 可以访问私有成员
102         this->m_size = mfp.m_size;  //mfp.m_size 可以访问私有成员
103     }
104 
105     return *this;
106 }
CMemberFuncPointer.cpp
 1 // _ss_slot_item.h: interface for the C_ss_slot_item class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX__SS_SLOT_ITEM_H__9F9FE84E_5DDA_480C_88CA_9B8AAB534B07__INCLUDED_)
 6 #define AFX__SS_SLOT_ITEM_H__9F9FE84E_5DDA_480C_88CA_9B8AAB534B07__INCLUDED_
 7 
 8 #include "ss_type_def.h"
 9 #include <Afxtempl.h>
10 
11 #if _MSC_VER > 1000
12 #pragma once
13 #endif // _MSC_VER > 1000
14 
15 /*
16   信号接收对象类: 包括信号接收对象指针,槽接口对象列表
17 */
18 
19 class C_ss_slot_item  
20 {
21 public:
22     int DisConnectSS(CMemberFuncPointer slot_F);
23     void Set_SLOTFUNC(SS_STAIC_SLOTFUNC slotfunc);
24     int Signal(void *pSrc, void *pDes);
25     int Add(CMemberFuncPointer &slot_F);
26     void* Get_slotSelf();
27     void Set_slotSelf(void *slot_pSelf);
28     C_ss_slot_item();
29     virtual ~C_ss_slot_item();
30 
31 private:
32     void* m_slot_pSelf;
33     CArray<CMemberFuncPointer*,CMemberFuncPointer*>*m_pMFPArray;
34     SS_STAIC_SLOTFUNC m_slot_pFunc;
35     CMemberFuncPointer m_curFuncPointer;
36 };
37 
38 #endif // !defined(AFX__SS_SLOT_ITEM_H__9F9FE84E_5DDA_480C_88CA_9B8AAB534B07__INCLUDED_)
_ss_slot_item.h
  1 // _ss_slot_item.cpp: implementation of the C_ss_slot_item class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "OverlapRoutine.h"
  7 #include "_ss_slot_item.h"
  8 #include <Afxtempl.h>
  9 
 10 #ifdef _DEBUG
 11 #undef THIS_FILE
 12 static char THIS_FILE[]=__FILE__;
 13 #define new DEBUG_NEW
 14 #endif
 15 
 16 //////////////////////////////////////////////////////////////////////
 17 // Construction/Destruction
 18 //////////////////////////////////////////////////////////////////////
 19 
 20 C_ss_slot_item::C_ss_slot_item()
 21 {
 22     this->m_slot_pSelf = NULL;
 23     this->m_pMFPArray = new CArray<CMemberFuncPointer*,CMemberFuncPointer*>;
 24     this->m_slot_pFunc = NULL;
 25 }
 26 
 27 C_ss_slot_item::~C_ss_slot_item()
 28 {
 29 
 30 }
 31 
 32 void C_ss_slot_item::Set_slotSelf(void *slot_pSelf)
 33 {
 34     this->m_slot_pSelf = (void*)slot_pSelf;
 35 }
 36 
 37 void* C_ss_slot_item::Get_slotSelf()
 38 {
 39     return this->m_slot_pSelf;
 40 }
 41 
 42 int C_ss_slot_item::Add(CMemberFuncPointer &slot_F)
 43 {
 44     int len = this->m_pMFPArray->GetSize();
 45     CMemberFuncPointer *pTemp = NULL;
 46     for(int i=0;i<len;i++)
 47     {
 48         pTemp = this->m_pMFPArray->GetAt(i);
 49         if(*pTemp == slot_F)
 50         {
 51             return 0;
 52         }
 53     }
 54     pTemp = new CMemberFuncPointer;
 55     *pTemp = slot_F;
 56     this->m_pMFPArray->Add(pTemp);
 57     return 1;
 58 }
 59 
 60 int C_ss_slot_item::Signal(void *pSrc, void *pDes)
 61 {
 62     int res = 0;
 63     if(this->m_slot_pFunc!=NULL)
 64     {
 65         int len = this->m_pMFPArray->GetSize();
 66         for(int i=0;i<len;i++)
 67         {
 68             CMemberFuncPointer tempFP = *(this->m_pMFPArray->GetAt(i));
 69             if(this->m_curFuncPointer != tempFP)
 70             {
 71                 this->m_curFuncPointer = tempFP;
 72                 this->m_slot_pFunc(this->m_slot_pSelf,tempFP,pSrc,pDes);
 73                 res+=1;
 74             }  //防止嵌套调用死循环
 75             this->m_curFuncPointer.Reset();
 76         }
 77     }
 78     return res;
 79 }
 80 
 81 void C_ss_slot_item::Set_SLOTFUNC(SS_STAIC_SLOTFUNC slotfunc)
 82 {
 83     this->m_slot_pFunc = slotfunc;
 84 }
 85 
 86 int C_ss_slot_item::DisConnectSS(CMemberFuncPointer slot_F)
 87 {
 88     int len =  this->m_pMFPArray->GetSize();
 89     CMemberFuncPointer *pTemp;
 90     for(int i=0;i<len;i++)
 91     {
 92         pTemp = this->m_pMFPArray->GetAt(i);
 93         if(*pTemp==slot_F)
 94         {
 95             delete pTemp;
 96             this->m_pMFPArray->RemoveAt(i);
 97             return 1;
 98         }
 99     }
100     return 0;
101 }
_ss_slot_item.cpp
 1 // _ss_signal_item.h: interface for the C_ss_signal_item class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX__SS_SIGNAL_ITEM_H__CA28EC16_931C_4ACA_8897_0A81B3DA967B__INCLUDED_)
 6 #define AFX__SS_SIGNAL_ITEM_H__CA28EC16_931C_4ACA_8897_0A81B3DA967B__INCLUDED_
 7 
 8 #include "_ss_signal_func.h"
 9 #include <Afxtempl.h>
10 
11 #if _MSC_VER > 1000
12 #pragma once
13 #endif // _MSC_VER > 1000
14 /*
15   信号发起方对象类: 包含发起方对象指针,信号接口对象列表
16 */
17 class C_ss_signal_item
18 {
19 public:
20     int DisConnectSS(CMemberFuncPointer signal_F, void *slot_pSelf, CMemberFuncPointer slot_F);
21     int Signal(CMemberFuncPointer signal_F,void *pSrc, void *pDes);
22     void Set_signalSelf(void *signal_pSelf);
23     int Add(CMemberFuncPointer &signal_F, void *slot_pSelf, CMemberFuncPointer& slot_F,SS_STAIC_SLOTFUNC slot_pFunc);
24     void* Get_signalSelf();
25     C_ss_signal_item();
26     virtual ~C_ss_signal_item();
27 private:
28     void* m_signal_pSelf;
29     CArray<C_ss_signal_func*,C_ss_signal_func*>*m_pSFArray;
30     SS_STAIC_SLOTFUNC m_slotfunc;
31 };
32 
33 #endif // !defined(AFX__SS_SIGNAL_ITEM_H__CA28EC16_931C_4ACA_8897_0A81B3DA967B__INCLUDED_)
_ss_signal_item.h
 1 // _ss_signal_item.cpp: implementation of the C_ss_signal_item class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #include "stdafx.h"
 6 #include "OverlapRoutine.h"
 7 #include "_ss_signal_item.h"
 8 
 9 #ifdef _DEBUG
10 #undef THIS_FILE
11 static char THIS_FILE[]=__FILE__;
12 #define new DEBUG_NEW
13 #endif
14 
15 //////////////////////////////////////////////////////////////////////
16 // Construction/Destruction
17 //////////////////////////////////////////////////////////////////////
18 C_ss_signal_item::C_ss_signal_item()
19 {
20     this->m_signal_pSelf = NULL;
21     this->m_pSFArray = new CArray<C_ss_signal_func*,C_ss_signal_func*>;
22     this->m_slotfunc = NULL;
23 }
24 
25 C_ss_signal_item::~C_ss_signal_item()
26 {
27 
28 }
29 
30 void* C_ss_signal_item::Get_signalSelf()
31 {
32     return this->m_signal_pSelf;
33 }
34 
35 int C_ss_signal_item::Add(CMemberFuncPointer &signal_F, void *slot_pSelf,CMemberFuncPointer& slot_F,SS_STAIC_SLOTFUNC slot_pFunc)
36 {
37     int len = this->m_pSFArray->GetSize();
38     C_ss_signal_func* pTemp=NULL;
39     for(int i=0;i<len;i++)
40     {
41         pTemp = this->m_pSFArray->GetAt(i);
42         if(pTemp->Get_signal_F()==signal_F)
43         {
44             return pTemp->Add(slot_pSelf,slot_F,slot_pFunc);
45         }
46     }
47 
48     pTemp = new C_ss_signal_func;
49     pTemp->Set_signal_F(signal_F);
50     
51     if(1==pTemp->Add(slot_pSelf,slot_F,slot_pFunc))
52     {
53         this->m_pSFArray->Add(pTemp);
54         return 1;
55     }
56     
57     delete pTemp;
58     return -1;
59 }
60 
61 void C_ss_signal_item::Set_signalSelf(void *signal_pSelf)
62 {    
63     this->m_signal_pSelf = (void*)signal_pSelf;
64 }
65 
66 int C_ss_signal_item::Signal(CMemberFuncPointer signal_F, void *pSrc, void *pDes)
67 {
68     int res = 0;
69     int len = this->m_pSFArray->GetSize();
70     C_ss_signal_func *p_signal_func;
71     for(int i =0;i<len;i++)
72     {
73         p_signal_func = this->m_pSFArray->GetAt(i);
74         if(p_signal_func->Get_signal_F()==signal_F)
75         {
76             res = p_signal_func->Signal(pSrc,pDes);
77             return res;
78         }
79     }
80     return res;
81 }
82 
83 int C_ss_signal_item::DisConnectSS(CMemberFuncPointer signal_F, void *slot_pSelf, CMemberFuncPointer slot_F)
84 {
85     int len = this->m_pSFArray->GetSize();
86     C_ss_signal_func *pTemp = NULL;
87     for(int i=0;i<len;i++)
88     {
89         pTemp = this->m_pSFArray->GetAt(i);
90         if(pTemp->Get_signal_F()==signal_F)
91         {
92             return pTemp->DisConnectSS(slot_pSelf,slot_F);
93         }
94     }
95     return 0;
96 }
_ss_signal_item.cpp
 1 // _ss_signal_func.h: interface for the C_ss_signal_func class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX__SS_SIGNAL_FUNC_H__59FB419F_4520_420E_BE2A_D9E6248FD1A1__INCLUDED_)
 6 #define AFX__SS_SIGNAL_FUNC_H__59FB419F_4520_420E_BE2A_D9E6248FD1A1__INCLUDED_
 7 
 8 #include "_ss_slot_item.h"
 9 #include "MemberFuncPointer.h"    // Added by ClassView
10 #include <Afxtempl.h>
11 
12 #if _MSC_VER > 1000
13 #pragma once
14 #endif // _MSC_VER > 1000
15 
16 /*
17   信号接口对象类: 包括信号接口对象地址,信号接收对象列表
18 */
19 
20 class C_ss_signal_func  
21 {
22 public:
23     int DisConnectSS(void *slot_pSelf, CMemberFuncPointer slot_F);
24     int Signal(void *pSrc, void *pDes);
25     void Set_signal_F(CMemberFuncPointer &signal_F);
26     int Add(void *slot_pSelf,CMemberFuncPointer &slot_F,SS_STAIC_SLOTFUNC slot_pFunc);
27     CMemberFuncPointer& Get_signal_F();
28     C_ss_signal_func();
29     virtual ~C_ss_signal_func();
30 private:
31     CMemberFuncPointer m_signal_F;
32     CArray<C_ss_slot_item*,C_ss_slot_item*>*m_pSlotArray;
33 };
34 
35 #endif // !defined(AFX__SS_SIGNAL_FUNC_H__59FB419F_4520_420E_BE2A_D9E6248FD1A1__INCLUDED_)
_ss_signal_func.h
 1 // _ss_signal_func.cpp: implementation of the C_ss_signal_func class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #include "stdafx.h"
 6 #include "OverlapRoutine.h"
 7 #include "_ss_signal_func.h"
 8 
 9 #ifdef _DEBUG
10 #undef THIS_FILE
11 static char THIS_FILE[]=__FILE__;
12 #define new DEBUG_NEW
13 #endif
14 
15 //////////////////////////////////////////////////////////////////////
16 // Construction/Destruction
17 //////////////////////////////////////////////////////////////////////
18 
19 C_ss_signal_func::C_ss_signal_func()
20 {
21     this->m_pSlotArray = new CArray<C_ss_slot_item*,C_ss_slot_item*>;
22 }
23 
24 C_ss_signal_func::~C_ss_signal_func()
25 {
26 
27 }
28 
29 CMemberFuncPointer& C_ss_signal_func::Get_signal_F()
30 {
31     return this->m_signal_F;
32 }
33 
34 int C_ss_signal_func::Add(void *slot_pSelf,CMemberFuncPointer &slot_F,SS_STAIC_SLOTFUNC slot_pFunc)
35 {
36     int len = this->m_pSlotArray->GetSize();
37     C_ss_slot_item *pTemp=NULL;
38     for(int i=0;i<len;i++)
39     {
40         pTemp = this->m_pSlotArray->GetAt(i);
41         if(pTemp->Get_slotSelf()==slot_pSelf)
42         {
43             return pTemp->Add(slot_F);
44         }
45     }
46 
47     pTemp = new C_ss_slot_item;
48     pTemp->Set_slotSelf(slot_pSelf);
49     pTemp->Set_SLOTFUNC(slot_pFunc);
50     if(1==pTemp->Add(slot_F))
51     {
52         this->m_pSlotArray->Add(pTemp);
53         return 1;
54     }
55 
56     delete pTemp;
57     return -1;
58 }
59 
60 void C_ss_signal_func::Set_signal_F(CMemberFuncPointer &signal_F)
61 {
62     this->m_signal_F = signal_F;
63 }
64 
65 int C_ss_signal_func::Signal(void *pSrc, void *pDes)
66 {
67     int  res = 0;
68     int len = this->m_pSlotArray->GetSize();
69     C_ss_slot_item *p_slot_item;
70     for(int i=0;i<len;i++)
71     {
72         p_slot_item = this->m_pSlotArray->GetAt(i);
73         res += p_slot_item->Signal(pSrc,pDes);
74     }
75     return res;
76 }
77 
78 int C_ss_signal_func::DisConnectSS(void *slot_pSelf, CMemberFuncPointer slot_F)
79 {
80     int len = this->m_pSlotArray->GetSize();
81     C_ss_slot_item *pTemp = NULL;
82     for(int i=0;i<len;i++)
83     {
84         pTemp = this->m_pSlotArray->GetAt(i);
85         if(pTemp->Get_slotSelf()==slot_pSelf)
86         {
87             return pTemp->DisConnectSS(slot_F);
88         }
89     }
90     return 0;
91 }
_ss_signal_func.cpp

 

posted on 2016-03-10 11:28  雁北  阅读(338)  评论(0编辑  收藏  举报

导航