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

來吧,朋友!

為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>
            久久久九九九九| 国产精品jizz在线观看美国 | 日韩午夜激情电影| 欧美激情第3页| 一本色道久久88综合亚洲精品ⅰ | 久久综合九色九九| 久久夜色精品一区| 亚洲高清不卡| 99re国产精品| 国产欧美日韩精品a在线观看| 久久国产精品色婷婷| 久久久久综合网| 99精品福利视频| 99在线热播精品免费| 国产日韩欧美制服另类| 免费看成人av| 欧美精品在欧美一区二区少妇| 中文亚洲字幕| 欧美一区二区三区播放老司机 | 国产精品试看| 免费精品99久久国产综合精品| 欧美成人精品不卡视频在线观看| 99国产一区| 午夜免费日韩视频| 亚洲精品久久久蜜桃| 亚洲一区二区三区精品在线| 好吊日精品视频| 日韩一区二区精品在线观看| 国产欧美日韩视频| 亚洲国产天堂网精品网站| 欧美小视频在线观看| 美女免费视频一区| 国产精品久在线观看| 欧美.www| 国产精品自在在线| 亚洲精品123区| 韩国三级电影一区二区| 在线亚洲精品福利网址导航| 亚洲国产成人高清精品| 亚洲午夜精品视频| avtt综合网| 久久一区二区三区国产精品| 欧美一区永久视频免费观看| 欧美护士18xxxxhd| 久久久久免费观看| 国产精品日韩| 亚洲免费观看在线视频| 亚洲狠狠丁香婷婷综合久久久| 亚洲免费在线看| 亚洲视频一二区| 欧美+日本+国产+在线a∨观看| 久久久综合激的五月天| 国产精品嫩草99a| 亚洲免费观看高清完整版在线观看| 亚洲第一页中文字幕| 欧美一级播放| 久久国产精品黑丝| 国产精品男人爽免费视频1| 亚洲美女区一区| 亚洲美女黄网| 欧美成人第一页| 亚洲高清一二三区| 91久久国产综合久久91精品网站| 久久精品国产精品| 久久久久久亚洲综合影院红桃| 国产精品视频精品视频| 亚洲私人黄色宅男| 欧美一级午夜免费电影| 国产精品亚洲片夜色在线| 亚洲视频在线观看视频| 欧美亚洲日本一区| 国产麻豆午夜三级精品| 午夜精品影院| 久久久国产成人精品| 精品9999| 欧美高清在线一区二区| 亚洲精品护士| 亚洲自拍啪啪| 国产日韩欧美精品一区| 欧美永久精品| 欧美99在线视频观看| 日韩亚洲综合在线| 欧美午夜a级限制福利片| 亚洲图片欧洲图片av| 久久精品国产第一区二区三区最新章节 | 另类酷文…触手系列精品集v1小说| 国产综合视频| 欧美gay视频激情| 亚洲精选视频在线| 欧美一区午夜视频在线观看| 一区精品久久| 欧美日韩性生活视频| 午夜精品成人在线| 欧美xart系列在线观看| 亚洲视频每日更新| 国产一区观看| 欧美精品观看| 欧美一区二区视频在线观看2020| 久久精品国产99| 亚洲欧洲一区二区三区| 国产精品久久久久久久久免费樱桃 | 蜜臀va亚洲va欧美va天堂| 亚洲精品一区在线| 久久精品国产一区二区三区免费看 | 亚洲精品国产精品久久清纯直播| 欧美无砖砖区免费| 久久久久久网| 亚洲视频在线观看三级| 免费在线观看日韩欧美| 亚洲在线免费视频| 亚洲国产成人午夜在线一区| 国产精品国产成人国产三级| 久久婷婷综合激情| 午夜国产精品视频| 亚洲精品乱码久久久久久按摩观| 久久色在线观看| 亚洲欧美日韩网| 99国内精品久久| 亚洲第一精品影视| 国产午夜精品一区二区三区视频 | 欧美日本中文| 久久伊人精品天天| 午夜在线一区二区| 夜夜夜精品看看| 亚洲国产视频a| 狂野欧美一区| 久久福利影视| 香港成人在线视频| 亚洲午夜免费福利视频| 亚洲精品裸体| 亚洲国产精品成人va在线观看| 国产日韩欧美91| 国产精品乱码妇女bbbb| 欧美三级午夜理伦三级中文幕 | 欧美成人精品一区二区三区| 欧美一区亚洲一区| 亚洲欧美日韩视频二区| 亚洲一区久久| 亚洲曰本av电影| 亚洲一级在线| 亚洲午夜一区| 亚洲欧美在线网| 亚洲欧美日韩精品久久久| 亚洲一区二区精品在线| 亚洲午夜久久久久久久久电影院| 亚洲精品少妇| 中文精品视频| 亚洲影院色无极综合| 亚洲免费视频在线观看| 性伦欧美刺激片在线观看| 午夜久久久久久| 久久9热精品视频| 久久精品首页| 鲁大师影院一区二区三区| 久久这里只精品最新地址| 久久综合五月| 欧美精品一区二区在线观看| 欧美久久精品午夜青青大伊人| 欧美另类亚洲| 国产精品免费一区二区三区观看 | 免费一级欧美片在线观看| 欧美a级一区| 欧美日韩影院| 国产亚洲一区二区三区在线观看| 在线成人激情| 99伊人成综合| 欧美在线观看视频| 欧美va天堂在线| 亚洲精品男同| 亚洲欧美视频在线观看| 久久五月天婷婷| 欧美女同在线视频| 国产欧美日韩91| 最近中文字幕mv在线一区二区三区四区 | 蜜臀久久99精品久久久久久9| 欧美xxx成人| 国产精品一区二区女厕厕| 激情视频一区二区| 99综合精品| 久久午夜视频| 一区二区日本视频| 久久久亚洲国产天美传媒修理工 | 欧美视频一区二区三区四区| 国产婷婷97碰碰久久人人蜜臀| 亚洲国产高清在线| 中文网丁香综合网| 蜜臀av一级做a爰片久久| 99在线精品视频| 久久一综合视频| 国产农村妇女精品一二区| 亚洲区一区二| 久久久久九九视频| 夜夜嗨av一区二区三区免费区| 久久精品日韩| 国产精品国色综合久久| 亚洲国产高清视频| 久久久久久久网| 亚洲天天影视| 欧美另类综合| 91久久国产综合久久|