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

來吧,朋友!

為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>
            亚洲缚视频在线观看| 欧美日韩一区二区三区在线看| 亚洲天堂激情| 99国产精品99久久久久久| 一区二区欧美激情| 午夜精品久久久久久久99水蜜桃 | 久久国产精品99国产精| 亚洲激情婷婷| 久久视频精品在线| 男女视频一区二区| 欧美日韩国产高清| 国产精品日本一区二区| 国产亚洲欧美日韩一区二区| 麻豆成人小视频| 亚洲全黄一级网站| 在线一区二区视频| 久久久综合精品| 亚洲日本中文字幕免费在线不卡| 久久久av网站| 亚洲激情在线播放| 亚洲欧美日韩国产综合在线| 久久综合五月天婷婷伊人| 欧美日韩一区二区三区| 国内精品嫩模av私拍在线观看| 亚洲精品永久免费| 久久国产精品久久久| 亚洲第一精品在线| 久久aⅴ国产紧身牛仔裤| 欧美精品亚洲| 亚洲桃花岛网站| 久久尤物电影视频在线观看| 国产精品久久久爽爽爽麻豆色哟哟| 在线观看视频欧美| 欧美亚洲免费电影| 亚洲人午夜精品免费| 久久婷婷国产综合尤物精品| 国产精品国产亚洲精品看不卡15 | 久久亚洲春色中文字幕| 欧美视频不卡| 亚洲福利免费| 91久久线看在观草草青青| 亚洲激情不卡| 久久色中文字幕| 国产精品久久久久久久久果冻传媒 | 国产精品国产三级国产普通话99| 亚洲精品美女在线| 久久久噜噜噜久久| 午夜国产精品视频| 欧美一级专区| 国产精品久久国产愉拍| 亚洲欧洲精品一区| 免费高清在线一区| 久久国产手机看片| 99热精品在线| 欧美日本视频在线| 亚洲日韩视频| 91久久极品少妇xxxxⅹ软件| 久久综合综合久久综合| 国产精品久久久爽爽爽麻豆色哟哟| 欧美精品一区在线发布| 国产日产欧产精品推荐色| 亚洲男女自偷自拍| 99国产精品自拍| 欧美日韩1区2区3区| 一区二区三区www| av成人福利| 国产一区视频在线观看免费| 亚洲午夜精品一区二区三区他趣| 99re6这里只有精品| 欧美日韩一视频区二区| 亚洲综合日韩在线| 亚洲男女自偷自拍图片另类| 国产一区二区电影在线观看| 猛男gaygay欧美视频| 久久综合色天天久久综合图片| 在线不卡视频| 亚洲破处大片| 国产精品国产精品| 久久爱www久久做| 久久久www成人免费毛片麻豆| 亚洲二区精品| 亚洲人成人一区二区三区| 亚洲国产精品999| 欧美三级电影精品| 91久久精品美女| 亚洲高清在线观看一区| 欧美韩国一区| 午夜国产精品视频免费体验区| 亚洲专区免费| 在线欧美日韩精品| 亚洲高清资源综合久久精品| 亚洲美女av黄| 亚洲一区二区三区乱码aⅴ| 国产一区二区日韩精品欧美精品 | 亚洲国产裸拍裸体视频在线观看乱了中文 | 久久影视精品| 亚洲欧美中日韩| 免费国产自线拍一欧美视频| 亚洲一区二区三区涩| 欧美亚洲三级| 欧美不卡高清| 久久久久久久999精品视频| 欧美区二区三区| 免费看亚洲片| 国产日本精品| 91久久国产综合久久| 狠狠狠色丁香婷婷综合久久五月| 日韩亚洲不卡在线| 亚洲国内欧美| 精品粉嫩aⅴ一区二区三区四区| 亚洲黄色在线观看| 在线观看成人一级片| 99国产精品久久久久老师| 伊甸园精品99久久久久久| 久久五月婷婷丁香社区| 国产精品久久久久久一区二区三区 | 中文亚洲字幕| 最新日韩欧美| 欧美影院一区| 亚洲欧美国产精品va在线观看| 免费观看30秒视频久久| 久久久亚洲人| 中文久久精品| 国产精品99久久久久久久女警 | 免费成人黄色av| 久色婷婷小香蕉久久| 国产欧美日韩麻豆91| 一个色综合av| 久久婷婷色综合| 国产乱人伦精品一区二区 | 久久亚洲电影| 欧美91大片| 亚洲第一精品久久忘忧草社区| 欧美中文字幕在线播放| 久久精品一本久久99精品| 国产欧美91| 欧美亚洲在线| 久久久久在线| 亚洲国产精品第一区二区三区| 久久青草福利网站| 欧美国产1区2区| 亚洲免费久久| 欧美视频在线观看一区二区| 在线视频欧美一区| 午夜精彩国产免费不卡不顿大片| 国语自产精品视频在线看一大j8| 一本色道久久综合狠狠躁篇的优点| 欧美激情综合亚洲一二区| 亚洲久色影视| 欧美亚洲综合另类| 激情婷婷久久| 欧美电影美腿模特1979在线看 | 一本色道**综合亚洲精品蜜桃冫| 亚洲午夜av在线| 国产欧美一区二区色老头| 欧美日韩亚洲网| 亚洲一区二区四区| 久久久综合网| 亚洲免费大片| 国产精品日韩精品欧美在线| 欧美综合国产| 亚洲精品日韩综合观看成人91| 亚洲欧美国产精品专区久久| 欧美jizzhd精品欧美巨大免费| 欧美大成色www永久网站婷| 91久久久久久久久久久久久| 一本色道久久加勒比精品| 国产精品日韩精品欧美精品| 久久久久久久一区二区三区| 亚洲精品一区二区三区在线观看| 午夜宅男欧美| 久久久久久久波多野高潮日日| 亚洲福利av| 欧美尤物巨大精品爽| 亚洲电影一级黄| 国产精品久久久久高潮| 快she精品国产999| 亚洲无线视频| 亚洲第一色中文字幕| 欧美在线视频免费观看| 99国产精品视频免费观看一公开| 国产日韩精品一区观看| 欧美日韩一区在线| 久久久久久夜精品精品免费| 一本色道**综合亚洲精品蜜桃冫 | 亚洲欧洲日韩在线| 国产精品久久久久7777婷婷| 久久久久久69| 亚洲线精品一区二区三区八戒| 欧美成人精品福利| 久久国产精品电影| 亚洲综合二区| 99国产精品国产精品久久 | 欧美成人精品不卡视频在线观看| 在线一区观看| 亚洲国产老妈| 另类图片国产| 久久精品日韩| 亚洲欧美综合网| 亚洲一区免费视频|