青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

來吧,朋友!

為C++瘋狂

Generic Observer Pattern and Events in C++

Introduction

One of the interesting features I found in C# is a ?Events and Delegates? concept. The idea is good but not new in Object Oriented Programming, it is one of the most frequently used concepts in programming, sometimes referred to as ?Observer? or ?Document/View? design pattern. Classical formulation of it could be found in ?Design Patterns, Elements of Reusable Object Oriented Software? by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (The Gang of Four).

This concept is used when you want some information stored in one object, called ?model? (subject) to be watched by others, called ?views? (observers). Each time when information is changed in the ?model?, ?views? attached to the model should receive notification and update there states accordingly to the changed ?model?.

Classical implementation described in ?Design Patterns?:

As it is seen from the class diagram, concrete models should be derived from Subject class and views from Observer. Any time the state of Subject is changed, it calls notify method which notifies all observers attached to the Subject.

Collapse Copy Code
void Subject::notify()
{
for(int i=0; i<observes.size(); i++)
observers[i]->update();
}

In many applications, this straightforward implementation is good enough, but things are getting ugly when you have different kinds of changes in the ?subject? and you want to pass different types of parameters to the ?views?.

One of the examples for complex ?Model?/?View? relations is a GUI control attached to its processing function. Each time the control?s state is changed, process function is called with parameters indicating new state of the control.

These kinds of problems are solved in C# by the introduction of ?Events and Delegates? concept. The resolution of the problem is easier in C#, because all classes are inherited from the same ?object? class.

At the beginning, I thought why we do not have this nice ?Events and Delegates? thing in standard C++, but then I came to the conclusion that C++ does not need it.

C++ language is powerful enough to express ?Events? and ?Delegates? concept in terms of already existing primitives. Proposed design makes it possible to "connect" different methods with different number of parameters belonging to unrelated classes to the ?model?.

The keys for this solution are C++ templates (generic types) and pointes to member functions.

Using Code

Suppose we have a class MySubject that has internal information connected to different views, it produces three different types of events called int_event, double_event and triple_event with different types and numbers of parameters.

Collapse Copy Code
class MySubject
{
public:
CppEvent1<bool,int> int_event;
CppEvent2<bool,double,int> double_event;
CppEvent3<bool,double,int,const char*> triple_event;
void submit_int()
{
int_event.notify(1);
}
void submit_double()
{
double_event.notify(10.5,100);
}
void submit_triple()
{
triple_event.notify(10.5,100,"Oh ye");
}
};

Views represented by MyListener1 and MyListener2 are unrelated. The only requirement is for callback (delegate) methods to have parameters signature similar to corresponding CppEvent.

Collapse Copy Code
class MyListener1
{
public:
bool update_int(int p)
{
Console::WriteLine(S"int update listener 1");
return true;
}
bool update_double(double p,int p1)
{
Console::WriteLine(S"double update listener 1");
return true;
}
bool update_triple(double p,int p1,const char* str)
{
Console::WriteLine(S"triple update listener 1");
return true;
}
};
class MyListener2
{
public:
bool fun(int p)
{
Console::WriteLine(S"int update listener 2");
return true;
}
};

The final step is to create viewers MyListener1 and MyListener2 and connect their member functions to corresponding events in MySubject model.

Collapse Copy Code
int main(void)
{
// create listeners (viewers)
    MyListener1* listener1 = new MyListener1;
MyListener2* listener2 = new MyListener2;
// create model
    MySubject subject;
// connect different viewers to different events of the model
    CppEventHandler h1 = subject.int_event.attach(listener1,
&MyListener1::update_int);
CppEventHandler h2 = subject.int_event.attach(listener2,
&MyListener2::fun);
CppEventHandler h3 = subject.double_event.attach(listener1,
&MyListener1::update_double);
CppEventHandler h4 = subject.triple_event.attach(listener1,
&MyListener1::update_triple);
// generate events
    subject.submit_int();
subject.submit_double();
subject.submit_triple();
// detach handlers
    subject.int_event.detach(h1);
subject.int_event.detach(h2);
subject.double_event.detach(h3);
subject.triple_event.detach(h4);
return 0;
}

Resulting output is:

Collapse Copy Code
> int update listener 1
> int update listener 2
> double update listener 1
> triple update listener 1

Implementation

First of all, if we want to attach different types of event handles (member functions with same types of parameters from different classes) to the same event, we should provide common base for them. We use templates to make it generic for any combination of parameter types in ?delegate? or call back method. There are different event types for every number of arguments in callback function.

Collapse Copy Code
// Event handler base for delegate with 1 parameter
template <typename ReturnT,typename ParamT>
class EventHandlerBase1
{
public:
virtual ReturnT notify(ParamT param) = 0;
};

Specific type of member function pointer within a pointer to the object is stored in the derived class.

Collapse Copy Code
template <typename ListenerT,typename ReturnT,typename ParamT>
class EventHandler1 : public EventHandlerBase1<ReturnT,ParamT>
{
typedef ReturnT (ListenerT::*PtrMember)(ParamT);
ListenerT* m_object;
PtrMember m_member;
public:
EventHandler1(ListenerT* object, PtrMember member)
: m_object(object), m_member(member)
{}
ReturnT notify(ParamT param)
{
return (m_object->*m_member)(param);
}
};

Event class stores map of event handlers and notifies all of them when notify method is called. Detach method is used to release handler from the map.

Collapse Copy Code
template <typename ReturnT,typename ParamT>
class CppEvent1
{
typedef std::map<int,EventHandlerBase1<ReturnT,ParamT> *> HandlersMap;
HandlersMap m_handlers;
int m_count;
public:
CppEvent1()
: m_count(0) {}
template <typename ListenerT>
CppEventHandler attach(ListenerT* object,ReturnT (ListenerT::*member)(ParamT))
{
typedef ReturnT (ListenerT::*PtrMember)(ParamT);
m_handlers[m_count] = (new EventHandler1<ListenerT,
ReturnT,ParamT>(object,member));
m_count++;
return m_count-1;
}
bool detach(CppEventHandler id)
{
HandlersMap::iterator it = m_handlers.find(id);
if(it == m_handlers.end())
return false;
delete it->second;
m_handlers.erase(it);
return true;
}
ReturnT notify(ParamT param)
{
HandlersMap::iterator it = m_handlers.begin();
for(; it != m_handlers.end(); it++)
{
it->second->notify(param);
}
return true;
}
};

Comments

This implementation is quite similar to those in the article ?Emulating C# delegates in Standard C++?. I found out it after I already wrote the article. Actually, the fact that we have a similar way to deal with the problem means that it?s a very intuitive solution for this kind of problem in C++. An advantage of the current implementation is that it supports different number of arguments, so any member function of any class could be a callback (delegate). Probably to have this thing as a part of standard library is a good thing, but even if it?s not a part of the standard, you can use it as it is. This implementation is restricted to events up to 3 parameters, it can be easily extended to other numbers by just rewriting it with different number of parameters (see code for details).

License

posted on 2009-07-22 14:51 yanghaibao 閱讀(510) 評論(0)  編輯 收藏 引用

導航

<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章檔案

收藏夾

Good blogs

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一区二区av在线| 久久天天综合| 国产精品一区二区三区观看| 亚洲美洲欧洲综合国产一区| 亚洲区国产区| 欧美日韩一二三区| 亚洲色在线视频| 亚洲综合精品四区| 国产在线欧美日韩| 老鸭窝91久久精品色噜噜导演| 久久这里有精品视频| 亚洲精品激情| 亚洲天堂久久| 国产自产精品| 亚洲人成在线播放网站岛国| 欧美日韩精品综合| 久久精品国产综合| 免费观看欧美在线视频的网站| 99在线热播精品免费| 一区二区三区欧美视频| 国产精一区二区三区| 免费日韩成人| 国产精品久久久久久久久免费| 久久亚洲一区二区| 欧美金8天国| 久久爱另类一区二区小说| 久久国产精品网站| 99re66热这里只有精品3直播 | 亚洲精品国产拍免费91在线| 日韩午夜精品视频| 国产一区二区三区四区| 亚洲国产精品t66y| 国产日韩精品视频一区| 亚洲国产福利在线| 国产视频一区在线| 亚洲伦理中文字幕| 影音先锋久久资源网| 亚洲一区二区三区涩| 亚洲激情第一页| 欧美一区综合| 亚洲在线观看视频| 欧美成人综合| 久久久久成人网| 国产精品毛片高清在线完整版| 欧美黄色免费| 韩日欧美一区二区| 亚洲欧美成人网| 99re66热这里只有精品3直播 | 亚洲人成77777在线观看网| 国产麻豆视频精品| 一本色道久久综合狠狠躁篇怎么玩| 狠狠干成人综合网| 亚洲免费在线观看| av成人免费| 欧美91福利在线观看| 久久婷婷国产综合精品青草| 国产美女精品视频免费观看| 亚洲欧洲精品一区二区精品久久久| 激情久久久久久久| 亚洲综合第一页| 亚洲综合色激情五月| 欧美国产成人在线| 欧美激情亚洲国产| 亚洲国产网站| 免费亚洲电影在线| 欧美激情精品久久久六区热门 | 欧美1区2区3区| 黄色成人av| 久久九九久久九九| 久久亚洲私人国产精品va| 国内伊人久久久久久网站视频 | 亚洲亚洲精品三区日韩精品在线视频 | 欧美高清免费| 亚洲人在线视频| 夜夜躁日日躁狠狠久久88av| 欧美激情视频在线免费观看 欧美视频免费一 | 好吊妞这里只有精品| 欧美一区亚洲| 嫩草成人www欧美| 在线观看久久av| 欧美肥婆bbw| 一区二区欧美日韩视频| 亚洲欧美日韩国产一区二区| 国产伦精品一区二区三区免费 | 亚洲精品一区二区在线观看| 在线视频你懂得一区| 国产精品一区二区女厕厕| 欧美一区二区三区四区视频| 蜜臀91精品一区二区三区| 亚洲美女免费精品视频在线观看| 欧美人在线视频| 一区二区黄色| 久久综合色8888| 日韩视频在线一区二区三区| 欧美性jizz18性欧美| 亚洲欧美日韩成人高清在线一区| 快射av在线播放一区| 亚洲免费观看在线视频| 国产精品麻豆欧美日韩ww| 久久精品国亚洲| 日韩视频免费观看高清完整版| 欧美一区二区三区四区夜夜大片 | 国产精品一区二区男女羞羞无遮挡| 香蕉成人伊视频在线观看| 亚洲福利国产| 先锋影音国产一区| 亚洲精品乱码久久久久久按摩观| 国产精品久久久久久户外露出 | 亚洲一区二区三区在线| 欧美v国产在线一区二区三区| 亚洲午夜精品福利| 激情六月婷婷综合| 欧美三级电影网| 久久综合久久88| 欧美一区二区三区四区夜夜大片| 亚洲国产一区二区三区a毛片| 欧美在线播放| 亚洲手机成人高清视频| 亚洲国产另类久久精品| 国产精品热久久久久夜色精品三区 | 亚洲欧美日韩系列| 亚洲日本中文| 嫩草国产精品入口| 久久久精品动漫| 亚洲自拍偷拍色片视频| 亚洲精品一区在线观看香蕉| 国模精品一区二区三区| 国产精品国产三级国产 | 亚洲最新合集| 亚洲第一福利视频| 免费观看久久久4p| 久久深夜福利| 久久精品国产免费观看| 午夜精品福利一区二区蜜股av| 日韩一区二区福利| 亚洲人久久久| 亚洲啪啪91| 在线看片欧美| 亚洲第一精品电影| 亚洲第一精品久久忘忧草社区| 国内成+人亚洲+欧美+综合在线| 国产精品羞羞答答xxdd| 欧美三区免费完整视频在线观看| 欧美激情黄色片| 欧美久久婷婷综合色| 欧美绝品在线观看成人午夜影视| 欧美成人日韩| 欧美精品一区二区三区在线播放 | 久久综合999| 久久综合色播五月| 久色婷婷小香蕉久久| 欧美成年人视频网站| 欧美激情精品久久久久久| 欧美激情一区二区三区四区| 欧美 亚欧 日韩视频在线| 欧美激情一区二区三区四区 | 中文在线一区| 亚洲制服丝袜在线| 欧美一级二区| 久久久久9999亚洲精品| 欧美成人精品1314www| 欧美国产在线电影| 国产精品成人免费| 国产精品综合| 亚洲国产99精品国自产| 99ri日韩精品视频| 亚洲欧美日韩精品久久亚洲区| 欧美诱惑福利视频| 免费看成人av| 日韩一级免费| 久久精品中文| 欧美紧缚bdsm在线视频| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ入口 | 久久国产99| 欧美韩日精品| 国产精品婷婷午夜在线观看| 激情婷婷久久| 一区二区三区国产| 久久久亚洲高清| 91久久精品日日躁夜夜躁欧美| 亚洲午夜精品17c| 麻豆av一区二区三区| 国产精品毛片大码女人| 亚洲高清一区二| 亚洲欧美在线免费| 欧美激情1区2区3区| 亚洲视屏一区| 欧美成人a视频| 国模吧视频一区| 亚洲欧美国产va在线影院| 女人色偷偷aa久久天堂| 亚洲午夜久久久久久久久电影网| 久久久久国产精品一区三寸| 国产精品扒开腿爽爽爽视频| 亚洲高清三级视频| 欧美一级视频精品观看| 亚洲国产清纯| 久久精品在线| 国产日韩一区二区三区| 在线视频欧美日韩|