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

來吧,朋友!

為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 閱讀(499) 評論(0)  編輯 收藏 引用

導航

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章檔案

收藏夾

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一区二区老牛| 久久亚洲欧美国产精品乐播| 亚洲精品在线看| 久久夜色精品国产噜噜av| 99在线精品免费视频九九视| 国产精品男人爽免费视频1| 久久久国产亚洲精品| 亚洲精品乱码久久久久久黑人| 欧美伊久线香蕉线新在线| 亚洲国产精品精华液2区45 | 国产香蕉久久精品综合网| 欧美丰满少妇xxxbbb| 亚洲性av在线| 一区二区国产日产| 亚洲精品一区二区三区樱花 | 久久se精品一区二区| 亚洲美女视频在线观看| 亚洲影音一区| 一本色道久久88精品综合| 久久都是精品| 日韩视频中文字幕| 久久久福利视频| 国产日韩综合一区二区性色av| 亚洲免费观看高清完整版在线观看| 欧美一区二视频在线免费观看| 亚洲大片精品永久免费| 久久午夜av| 一区二区三区欧美视频| 免费成人网www| 免费亚洲网站| 久久精品人人做人人爽| 欧美日韩中文在线观看| 亚洲精品乱码久久久久久按摩观 | 亚洲人成啪啪网站| 久久久综合精品| 美女图片一区二区| 国产日韩欧美日韩| 先锋a资源在线看亚洲| 欧美在线日韩在线| 亚洲特级毛片| 国产精品久久中文| 香蕉成人久久| 亚洲欧美三级伦理| 亚洲欧美一区二区三区在线 | 在线看国产日韩| 亚洲国产美国国产综合一区二区| 在线精品亚洲| 蜜臀91精品一区二区三区| 欧美二区在线| 亚洲精品一区在线观看香蕉| 蜜乳av另类精品一区二区| 亚洲国产精品成人综合| 欧美激情日韩| 欧美精品色综合| 国产精品亚洲аv天堂网| 国产亚洲成av人片在线观看桃| 亚洲直播在线一区| 亚洲图片欧美日产| 国产免费一区二区三区香蕉精| 欧美有码视频| 久久久噜噜噜久久中文字幕色伊伊| 国内偷自视频区视频综合| 91久久极品少妇xxxxⅹ软件| 中日韩在线视频| 亚洲亚洲精品在线观看| 国产精品香蕉在线观看| 久久九九精品99国产精品| 久久久精品视频成人| 亚洲大片在线| 亚洲毛片在线观看| 亚洲一二三四久久| 久久久久久久网| 久久久亚洲人| 亚洲狼人综合| 亚洲图片欧美日产| 亚洲高清av在线| 夜夜嗨av一区二区三区网页 | 亚洲经典视频在线观看| 午夜精彩视频在线观看不卡| 国模精品娜娜一二三区| 亚洲电影欧美电影有声小说| 欧美日韩免费观看中文| 国产一区二区黄色| 亚洲第一天堂av| 国产精品亚洲精品| 亚洲电影成人| 国产日韩免费| 亚洲免费成人| 亚洲电影网站| 午夜精品久久久久久久99黑人| 亚洲三级免费电影| 午夜精品区一区二区三| 国产精品剧情在线亚洲| 欧美成人精品在线观看| 久久精品国产亚洲5555| 国产欧美日韩| 日韩视频在线免费| 欧美激情精品久久久久久蜜臀| 国产精品久久91| 亚洲午夜在线观看| 久久久精品免费视频| 欧美一二三视频| 欧美美女bbbb| 91久久综合| 性欧美8khd高清极品| 亚洲视频免费看| 欧美成人性网| 一本色道久久综合一区| 久久久美女艺术照精彩视频福利播放| 亚洲午夜小视频| 欧美日韩国产色站一区二区三区| 亚洲二区视频在线| 欧美中在线观看| 亚洲欧美综合一区| 午夜视频一区在线观看| 一本色道久久综合亚洲二区三区| 免费毛片一区二区三区久久久| 久久久久久久久蜜桃| 国产亚洲精品v| 欧美一级视频免费在线观看| 韩国一区二区在线观看| 亚洲欧美一区二区激情| 亚洲综合首页| 国产精品电影网站| 亚洲图片激情小说| 性色av一区二区怡红| 国产精品午夜电影| 久久疯狂做爰流白浆xx| 久久综合狠狠综合久久综青草| 国户精品久久久久久久久久久不卡| 欧美一区二区视频97| 久久视频一区二区| 欧美电影资源| 日韩一区二区久久| 欧美激情视频一区二区三区不卡| 欧美韩日一区| 9色精品在线| 国产精品jizz在线观看美国| 久久久国产视频91| 国产在线精品二区| 久久久久久久综合狠狠综合| 欧美国产日韩一区二区| 亚洲日韩中文字幕在线播放| 欧美日韩理论| 午夜精品视频网站| 欧美激情aaaa| 午夜久久久久久| 亚洲风情亚aⅴ在线发布| 欧美精品videossex性护士| 亚洲图片自拍偷拍| 噜噜噜在线观看免费视频日韩| 欧美日韩高清区| 亚洲欧美久久| 一本到12不卡视频在线dvd| 国产精品豆花视频| 欧美在线日韩精品| 亚洲国产欧美精品| 性色av一区二区怡红| 91久久久在线| 国产亚洲欧美一区二区三区| 欧美精品精品一区| 午夜精品久久久| 欧美国产视频在线| 欧美怡红院视频| 亚洲精品一区久久久久久| 国产精品综合色区在线观看| 欧美成人免费全部观看天天性色| 亚洲欧美激情在线视频| 亚洲精品欧美日韩专区| 暖暖成人免费视频| 欧美在线视频一区二区| 夜夜爽99久久国产综合精品女不卡| 国产视频一区在线观看一区免费| 欧美精品一区二区三区很污很色的| 午夜精品国产精品大乳美女| 亚洲精品免费一区二区三区| 久久久.com| 亚洲欧美日韩国产中文| 亚洲精品视频免费在线观看| 激情懂色av一区av二区av| 久久九九精品| 一本一本久久a久久精品综合妖精| 欧美成人国产一区二区| 久久久国产一区二区三区| 亚洲一区二区高清视频|