The simple implementation of event mechanism in C + + and the features to be discarded _c language

Source: Internet
Author: User
Tags constant eventbase
The event model is a widely used good thing, but there is no ready-made in the C + + standard library, and other implementations are complex or not elegant, such as the need to use macros. Now that VC11 can be used under XP, then it's time to pick up the advanced facilities provided by C++11 to combine a lightweight implementation.

to achieve simplicity, some features need to be discarded
1, does not support the determination of whether the function has been bound (because Std::function does not provide a comparison method, the implementation of their own functions, the code has become more)
2, require the user to receive the callback function ID returned to remove the event binding (cause Ibid.)
3, the event does not return a value, does not support the callback function precedence, conditional callback, such as advanced characteristics of events (such as returning all processing results of the maximum minimum value; Only callback the event handler that matches the specified parameters)
4, the event parameters theoretically unlimited, actually limited, generally support 0~10 parameters (VC11 has not supported variable length template parameters, GCC has. But can be simulated by default template parameters and skewness, so theoretically unrestricted)
5, not thread-safe

Note: 3, 52 can provide flexible support by introducing a policy model, as the standard library and Loki do, to achieve a complete event mechanism.

The simplest implementation
Copy Code code as follows:

#include <map>
#include <functional>
using namespace Std;
Template<class Param1, Class param2>
Class Event
{
typedef void Handlert (PARAM1, Param2);
int M_handlerid;
Public
Event (): M_handlerid (0) {}
Template<class funct> int AddHandler (funct func)
{
M_handlers.emplace (M_handlerid, Forward<funct> (func));
return m_handlerid++;
}
void RemoveHandler (int handlerid)
{
M_handlers.erase (Handlerid);
}
void operator () (Param1 arg1, Param2 arg2)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2);
}
Private
Map<int, function};

AddHandler the callback function to the Std::function, lets the standard library handle various overloads, and returns an identifier to unregister the binding. Try it, work well:
Copy Code code as follows:

void F1 (int, int)
{
Puts ("F1 ()");
}
struct F2
{
void f (int, int)
{
Puts ("F2 ()");
}
void operator () (int, int)
{
Puts ("F3 ()");
}
};
int _tmain (int argc, _tchar* argv[])
{
Event<int, int> E;
int id = e.addhandler (F1);
E.removehandler (ID);
Using namespace std::p laceholders;
F2 F2;
E.addhandler (Bind (&f2::f, F2, _1, _2));
E.addhandler (Bind (F2, _1, _2));
E.addhandler ([] (int, int) {
Puts ("F4 ()");
});
E (1, 2);
return 0;
}

Although there is a small drawback here, for an imitation function, if you want to use its pointer or reference is not directly binding, you need to do this:
Copy Code code as follows:

E.addhandler (ref (F2));
E.addhandler (ref (*PF2)); PF2 is a pointer to a F2 but the use of a mock function object pointer is not much, and it's not bad enough to knock a few

Characters, not to mention in the case of a lambda expression?
Improved
1, some people do not like bind, use up trouble, put in AddHandler inside:
Copy Code code as follows:

Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2));
return m_handlerid++;
}

2, the number of extended parameters. No variable length template parameters, to modify:
Copy Code code as follows:

struct Nulltype {};
Template<class P1 = Private::nulltype, class P2 = private::nulltype>
Class Event
{
Public
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2));
return m_handlerid++;
}
void operator () (P1 arg1, P2 arg2)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2);
}
};
Template<>
Class Event<private::nulltype, private::nulltype>
{
Public
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj));
return m_handlerid++;
}
void operator () ()
{
for (const auto& i:m_handlers)
I.second ();
}
};
Template<class p1>
Class Event<p1, private::nulltype>
{
Public
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1));
return m_handlerid++;
}
void operator () (P1 arg1)
{
for (const auto& i:m_handlers)
I.second (ARG1);
}
};

0~2 parameters are now supported. Notice that there are common code in each template, extracted it into the base class, and all you have to do is open the text generator.
Complete Code
Code download
Copy Code code as follows:

View Code
#pragma once
#include <map>
#include <functional>
Namespace Utility
{
Namespace Private
{
struct Nulltype {};
Template<class handlert>
Class Eventbase
{
Public
Eventbase (): M_handlerid (0) {}
Template<class funct> int AddHandler (funct func)
{
M_handlers.emplace (M_handlerid, Std::forward<funct> (func));
return m_handlerid++;
}
void RemoveHandler (int handlerid)
{
M_handlers.erase (Handlerid);
}
Protected
int M_handlerid;
Std::map<int, std::function};
}
Template<class P1 = Private::nulltype, class P2 = Private::nulltype, class P3 = Private::nulltype, class P4 = Private:: Nulltype, class P5 = Private::nulltype, class P6 = Private::nulltype, class P7 = Private::nulltype, class P8 = Private::nu Lltype, class P9 = Private::nulltype, class P10 = private::nulltype>
Class Event
: Public Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5, _6, _7, _8, _9, _10));
return m_handlerid++;
}
void operator (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 Arg6, P7 Arg7, P8 Arg8, P9 arg9, P10 arg10)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, ARG10);
}
};
Template<>
Class Event<private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private::NullType, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, private::nulltype>
: Public private::eventbase<void () >
{
Public
Using Private::eventbase<void () >::addHandler;
Template<class OBJT, class funct> int addHandler (objt const obj, funct func)
{
M_handlers.emplace (M_handlerid, Std::bind (func, obj));
return m_handlerid++;
}
void operator () ()
{
for (const auto& i:m_handlers)
I.second ();
}
};
Template<class p1>
Class Event<p1, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private::NullType, Private::nulltype, Private::nulltype, Private::nulltype, private::nulltype>
: Public private::eventbase<void (P1) >
{
Public
Using Private::eventbase<void (P1) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1));
return m_handlerid++;
}
void operator () (P1 arg1)
{
for (const auto& i:m_handlers)
I.second (ARG1);
}
};
Template<class P1, Class p2>
Class Event<p1, P2, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private::NullType, Private::nulltype, Private::nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2) >
{
Public
Using Private::eventbase<void (P1, P2) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2));
return m_handlerid++;
}
void operator () (P1 arg1, P2 arg2)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2);
}
};
Template<class P1, Class P2, class p3>
Class Event<p1, P2, P3, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private:: Nulltype, Private::nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3) >
{
Public
Using Private::eventbase<void (P1, P2, P3) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3));
return m_handlerid++;
}
void operator () (P1 arg1, P2 arg2, P3 arg3)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, ARG3);
}
};
Template<class P1, Class P2, Class P3, class p4>
Class Event<p1, P2, P3, P4, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private:: Nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4));
return m_handlerid++;
}
void operator () (P1 arg1, P2 arg2, P3 arg3, P4 arg4)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, ARG4);
}
};
Template<class P1, Class P2, Class P3, Class P4, class p5>
Class Event<p1, P2, P3, P4, P5, Private::nulltype, Private::nulltype, Private::nulltype, Private::nulltype, Private:: Nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4, P5) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5));
return m_handlerid++;
}
void operator () (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, ARG5);
}
};
Template<class P1, Class P2, Class P3, Class P4, Class P5, class p6>
Class Event<p1, P2, P3, P4, P5, P6, Private::nulltype, Private::nulltype, Private::nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4, P5, P6) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5, P6) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5, _6));
return m_handlerid++;
}
void operator (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 Arg6)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, Arg5, ARG6);
}
};
Template<class P1, Class P2, Class P3, Class P4, Class P5, Class P6, class p7>
Class Event<p1, P2, P3, P4, P5, P6, P7, Private::nulltype, Private::nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5, _6, _7));
return m_handlerid++;
}
void operator (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 Arg6, P7 Arg7)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
}
};
Template<class P1, Class P2, Class P3, Class P4, Class P5, Class P6, Class P7, class p8>
Class Event<p1, P2, P3, P4, P5, P6, P7, P8, Private::nulltype, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5, _6, _7, _8));
return m_handlerid++;
}
void operator (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 Arg6, P7 Arg7, P8 Arg8)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8);
}
};
Template<class P1, Class P2, Class P3, Class P4, Class P5, Class P6, Class P7, Class P8, class p9>
Class Event<p1, P2, P3, P4, P5, P6, P7, P8, P9, private::nulltype>
: Public Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8, P9) >
{
Public
Using Private::eventbase<void (P1, P2, P3, P4, P5, P6, P7, P8, P9) >::addHandler;
Template<class OBJT, class funct> int addHandler (OBJT obj, funct func)
{
Using namespace std::p laceholders;
M_handlers.emplace (M_handlerid, Std::bind (func, std::forward<objt> (obj), _1, _2, _3, _4, _5, _6, _7, _8, _9));
return m_handlerid++;
}
void operator (P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 Arg6, P7 Arg7, P8 Arg8, P9 arg9)
{
for (const auto& i:m_handlers)
I.second (Arg1, arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, ARG9);
}
};
}//Namespace Utility

Test Code
Various binding methods
Copy Code code as follows:

View Code
#include "Event.h"
using namespace Std;
void F1 (int, int)
{
Puts ("F1 ()");
}
struct F2
{
F2 () {puts ("F2 construct");}
F2 (const F2 &) {puts ("F2 copy");}
F2 (F2 &&) {puts ("F2 move");}
f2& operator= (const F2 &) {puts ("F2 copy Assign"); return *this;}
f2& operator= (F2 &&) {puts ("F2 move Assign");
void f (int, int)
{
Puts ("F2 ()");
}
void FC (int, int) const
{
Puts ("f2c ()");
}
};
struct F3
{
F3 () {puts ("F3 construct");}
F3 (const F3 &) {puts ("F3 copy");}
F3 (F3 &&) {puts ("F3 move");}
f3& operator= (const F3 &) {puts ("F3 copy Assign"); return *this;}
f3& operator= (F3 &&) {puts ("F3 move Assign");
void operator () (int, int) const
{
Puts ("F3 ()");
}
};
int _tmain (int argc, _tchar* argv[])
{
Utility::event<int, int> E;
General functions
E.addhandler (F1);
int id = e.addhandler (&AMP;F1);
E.removehandler (ID); Removing an event handler function
member functions
Using namespace std::p laceholders;
F2 F2;
Const F2 *PF2 = &f2;
E.addhandler (Bind (&f2::f, &AMP;F2, _1, _2)); Std::bind
E.addhandler (&AMP;F2, &f2::f);
E.addhandler (PF2, &AMP;F2::FC); Constant pointer
Puts ("----AddHandler (F2, &f2::f)----");
E.addhandler (F2, &f2::f); Object Copy Construction
Puts ("----AddHandler (F2 (), &f2::f)----");
E.addhandler (F2 (), &f2::f); Object Transfer constructs
Puts ("--------");
Imitation function
F3 f3;
Const F3 *PF3 = &f3;
Puts ("----AddHandler (F3)----");
E.addhandler (F3); Object Copy Construction
Puts ("----AddHandler (F3 ())----");
E.addhandler (F3 ()); Object Transfer constructs
Puts ("--------");
E.addhandler (Ref (F3)); Referencing an imitation function object
E.addhandler (ref (*PF3)); Referencing an imitation function constant object
Puts ("--------");
Lambda expression
E.addhandler ([] (int, int) {
Puts ("F4 ()");
});
Firing events
E (1, 2);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.