C++ 11 Events
Aaron Nielsen
Rob Alvies
@BloocoInc
Weeks and Microseconds (both?)
Complexity Management
Navigation

Targeting

Guidance
Host Interface

Actuators
Propulsion

Fuse

Health and
Status
Built-...
Pub Sub Mitigation
Navigation

Targeting

Guidance
Host Interface

Actuators

Event Manager

Fuse

Propulsion

Health and
...
Goals
● Lightweight
● No Dependencies
● Fast Runtime Performance
● Leverage the Compiler
● Decoupled, Flexible Design
● Pr...
EventDemo: General Applications
● When you need to extend an existing eventdriven system with C++ components (i.e.
Node.js...
Entities
● Event Manager (reactor pattern)
● Event Listener
● Active Object (AO from ACE term)
● EventBasedAO
● Concrete E...
Entities Static UML
EventMgr

EventListener

AO

Simple subscribe/notify of concrete events
EventBasedAO

Everything concr...
Coke

Consumer
AO

Event
Manager

subscribe(coke)

Create

notify(coke)

onEvent(coke)

consume(coke)

Consumer
Adapter
Example (single threaded)
void test1()
{
// Initialization
EventMgr &events = EventMgr::instance();
ConsumerAO consumer;
/...
Event Notification
void EventMgr::notify( unsigned int id, void *pEvent )
{
// Create an event that wraps the opaque point...
Command Pattern
class Command
{
public:
Command(){}
virtual ~Command(){}
// Command interface
virtual void execute() = 0;
...
What about Event Subscription?
Trying to avoid code that looks like this:

Yes, we know it works, BUT...
C++ 11 To The Rescue
● Needed a better way to manage callbacks
○ With the variety of events, flexibility was key
○ Functio...
std::tr1::function
#include <functional>
#include <iostream>

struct Foo {
Foo(int num) : num_(num) {}
void print_add(int ...
std::tr1::bind
#include <random>
#include <iostream>
#include <functional>
struct Foo {
void print_sum(int n1, int n2)
{
s...
EventHandler Specialized Command
class EventHandler: public Command
{
public:
typedef std::tr1::function< void () > Fn;
Ev...
Inside the Subscribe Magic
// Inside the EventBindings Class
typedef std::tr1::function< Command* (Event&) > Binding;
temp...
The Github Project
● https://github.com/ralvies/EventDemo
○ Apache 2.0 Licensed
○ Looking for collaborators
○ Issues and p...
References and Attributions
●

http://en.cppreference.com/

●

http://www.optimization-world.com/
Upcoming SlideShare
Loading in …5
×

C++ Events

1,110 views
911 views

Published on

Denver C++ meetup presentation from Fall 2012. Covers a simple implementation of a C++ 11 approach to complex event handling.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,110
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C++ Events

  1. 1. C++ 11 Events Aaron Nielsen Rob Alvies @BloocoInc
  2. 2. Weeks and Microseconds (both?)
  3. 3. Complexity Management Navigation Targeting Guidance Host Interface Actuators Propulsion Fuse Health and Status Built-In Test Data Link
  4. 4. Pub Sub Mitigation Navigation Targeting Guidance Host Interface Actuators Event Manager Fuse Propulsion Health and Status Built-In Test Data Link
  5. 5. Goals ● Lightweight ● No Dependencies ● Fast Runtime Performance ● Leverage the Compiler ● Decoupled, Flexible Design ● Properly Abstracted Events ● Inversion of Control
  6. 6. EventDemo: General Applications ● When you need to extend an existing eventdriven system with C++ components (i.e. Node.js extensions) ● When you need to integrate existing C++ components in a decoupled manner ● When you need to demultiplex disparate events (message brokers, sockets, IPC, IO, etc) in a clean OO way ● Etc.
  7. 7. Entities ● Event Manager (reactor pattern) ● Event Listener ● Active Object (AO from ACE term) ● EventBasedAO ● Concrete Events ● Concrete Event Consumers
  8. 8. Entities Static UML EventMgr EventListener AO Simple subscribe/notify of concrete events EventBasedAO Everything concrete lives at this level ConsumerAO Adapters Concrete Events Etc
  9. 9. Coke Consumer AO Event Manager subscribe(coke) Create notify(coke) onEvent(coke) consume(coke) Consumer Adapter
  10. 10. Example (single threaded) void test1() { // Initialization EventMgr &events = EventMgr::instance(); ConsumerAO consumer; // Made to order Hamburger hamb; Coke coke; // Deliver the order events.notify( hamb ); events.notify( coke ); // Make another order hamb.makePlain(); coke.size( 12 ); // Deliver events.notify( hamb ); events.notify( coke ); // Eat up! consumer.start(); }
  11. 11. Event Notification void EventMgr::notify( unsigned int id, void *pEvent ) { // Create an event that wraps the opaque pointer Event e( id, pEvent ); for( EventListeners::iterator it = m_listeners.lower_bound( id ); it != m_listeners.upper_bound( id ); ++it ) { EventListener &l = *(it->second); // For the demo code, this calls an EventBasedAO object l.onEvent( e ); } } template< class T > void EventMgr::notify( T &event ) { // Call the TypeID utility function to get integer for type T notify( TypeID< T >::id(), &event ); }
  12. 12. Command Pattern class Command { public: Command(){} virtual ~Command(){} // Command interface virtual void execute() = 0; }; void EventBasedAO::run() { Command *pCmd = 0; while( m_queue.hasNext() ) { pCmd = m_queue.getNext(); pCmd->execute(); delete pCmd; pCmd = 0; } }
  13. 13. What about Event Subscription? Trying to avoid code that looks like this: Yes, we know it works, BUT...
  14. 14. C++ 11 To The Rescue ● Needed a better way to manage callbacks ○ With the variety of events, flexibility was key ○ Functions as first class objects? Not in C++ BUT we can still manage them better ○ C++ 11 adds useful functor tools ● Note: Code samples are using the std::tr1 namespace. Due to (current) lack of compiler support, we're using the Boost library (included in the github project)
  15. 15. std::tr1::function #include <functional> #include <iostream> struct Foo { Foo(int num) : num_(num) {} void print_add(int i) const { std::cout << num_+i << 'n'; } int num_; }; int main() { // store a call to a member function std::tr1::function<void(const Foo&, int)> f_add_display = &Foo::print_add; Foo foo(314159); f_add_display(foo, 1); }
  16. 16. std::tr1::bind #include <random> #include <iostream> #include <functional> struct Foo { void print_sum(int n1, int n2) { std::cout << n1+n2 << 'n'; } }; int main() { // bind to a member function Foo foo; auto f3 = std::tr1::bind(&Foo::print_sum, foo, 95, _1); f3(5); }
  17. 17. EventHandler Specialized Command class EventHandler: public Command { public: typedef std::tr1::function< void () > Fn; EventHandler( Fn &fn ); ~EventHandler(); // Command interface virtual void execute(); private: Fn m_fn; };
  18. 18. Inside the Subscribe Magic // Inside the EventBindings Class typedef std::tr1::function< Command* (Event&) > Binding; template< class T > Command* EventBasedAO::bind( std::tr1::function< void (T&) > const &callback, Event &e ) { T *pEvent = e.event< T >(); std::tr1::function< void () > fn = std::tr1::bind( callback, *pEvent ); // Create the new specialized event-handling command return new EventHandler( fn ); } template< class T > void EventBasedAO::subscribe( std::tr1::function< void (T&) > const &callback ) { unsigned int const id = TypeID< T >::id(); EventBindings::Binding b = std::tr1::bind( &bind< T >, callback, _1 ); m_bindings.registerBinding( id, b ); EventMgr::instance().subscribe< T >( *this ); }
  19. 19. The Github Project ● https://github.com/ralvies/EventDemo ○ Apache 2.0 Licensed ○ Looking for collaborators ○ Issues and pull requests welcome! ● Easiest: just search for @ralvies
  20. 20. References and Attributions ● http://en.cppreference.com/ ● http://www.optimization-world.com/

×