SlideShare a Scribd company logo
1 of 20
Download to read offline
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-In
Test

Data Link
Pub Sub Mitigation
Navigation

Targeting

Guidance
Host Interface

Actuators

Event Manager

Fuse

Propulsion

Health and
Status
Built-In
Test

Data Link
Goals
● Lightweight
● No Dependencies
● Fast Runtime Performance
● Leverage the Compiler
● Decoupled, Flexible Design
● Properly Abstracted Events
● Inversion of Control
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.
Entities
● Event Manager (reactor pattern)
● Event Listener
● Active Object (AO from ACE term)
● EventBasedAO
● Concrete Events
● Concrete Event Consumers
Entities Static UML
EventMgr

EventListener

AO

Simple subscribe/notify of concrete events
EventBasedAO

Everything concrete lives at this level

ConsumerAO

Adapters
Concrete Events
Etc
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;
// 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();
}
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 );
}
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;
}
}
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
○ 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)
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);
}
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);
}
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;
};
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 );
}
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
References and Attributions
●

http://en.cppreference.com/

●

http://www.optimization-world.com/

More Related Content

What's hot

jimmy hacking (at) Microsoft
jimmy hacking (at) Microsoftjimmy hacking (at) Microsoft
jimmy hacking (at) MicrosoftJimmy Schementi
 
parallel programming in the PVM- task creation-Advanced system architecture
parallel programming in the PVM- task creation-Advanced system architectureparallel programming in the PVM- task creation-Advanced system architecture
parallel programming in the PVM- task creation-Advanced system architectureRoslinJoseph
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemShareek Ahamed
 
Vtk Image procesing
Vtk Image procesingVtk Image procesing
Vtk Image procesingSonu Mangal
 
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...Flink Forward
 
Do while loop
Do while loopDo while loop
Do while loopBU
 
Let’s Build a Python Profiler in 25 LOC
Let’s Build a Python Profiler in 25 LOCLet’s Build a Python Profiler in 25 LOC
Let’s Build a Python Profiler in 25 LOCNoam Elfanbaum
 
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...Francesco Versaci - Flink in genomics - efficient and scalable processing of ...
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...Flink Forward
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextEdward Willink
 
Two C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp InsightsTwo C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp InsightsAlison Chaiken
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RKevin Smith
 
TMPA-2017: A Survey of High-Performance Computing for Software Verification
TMPA-2017: A Survey of High-Performance Computing for Software VerificationTMPA-2017: A Survey of High-Performance Computing for Software Verification
TMPA-2017: A Survey of High-Performance Computing for Software VerificationIosif Itkin
 
Stream Loops on Flink - Reinventing the wheel for the streaming era
Stream Loops on Flink - Reinventing the wheel for the streaming eraStream Loops on Flink - Reinventing the wheel for the streaming era
Stream Loops on Flink - Reinventing the wheel for the streaming eraParis Carbone
 

What's hot (19)

Jit compilation
Jit compilationJit compilation
Jit compilation
 
What's New in C# 6
What's New in C# 6What's New in C# 6
What's New in C# 6
 
jimmy hacking (at) Microsoft
jimmy hacking (at) Microsoftjimmy hacking (at) Microsoft
jimmy hacking (at) Microsoft
 
parallel programming in the PVM- task creation-Advanced system architecture
parallel programming in the PVM- task creation-Advanced system architectureparallel programming in the PVM- task creation-Advanced system architecture
parallel programming in the PVM- task creation-Advanced system architecture
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime System
 
FRP
FRPFRP
FRP
 
Vtk Image procesing
Vtk Image procesingVtk Image procesing
Vtk Image procesing
 
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
 
Do while loop
Do while loopDo while loop
Do while loop
 
Let’s Build a Python Profiler in 25 LOC
Let’s Build a Python Profiler in 25 LOCLet’s Build a Python Profiler in 25 LOC
Let’s Build a Python Profiler in 25 LOC
 
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...Francesco Versaci - Flink in genomics - efficient and scalable processing of ...
Francesco Versaci - Flink in genomics - efficient and scalable processing of ...
 
Os2
Os2Os2
Os2
 
Procedure
ProcedureProcedure
Procedure
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for Xtext
 
Two C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp InsightsTwo C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp Insights
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
 
TMPA-2017: A Survey of High-Performance Computing for Software Verification
TMPA-2017: A Survey of High-Performance Computing for Software VerificationTMPA-2017: A Survey of High-Performance Computing for Software Verification
TMPA-2017: A Survey of High-Performance Computing for Software Verification
 
Vlsi lab2
Vlsi lab2Vlsi lab2
Vlsi lab2
 
Stream Loops on Flink - Reinventing the wheel for the streaming era
Stream Loops on Flink - Reinventing the wheel for the streaming eraStream Loops on Flink - Reinventing the wheel for the streaming era
Stream Loops on Flink - Reinventing the wheel for the streaming era
 

Viewers also liked

Vision Cast (Short)
Vision Cast (Short)Vision Cast (Short)
Vision Cast (Short)guest90848c3
 
Ucapan Aluan Pengerusi Jks Part One
Ucapan Aluan Pengerusi Jks Part OneUcapan Aluan Pengerusi Jks Part One
Ucapan Aluan Pengerusi Jks Part Onezafeen zafeen
 
Edisi15sep Nasional
Edisi15sep NasionalEdisi15sep Nasional
Edisi15sep Nasionalepaper
 
La Hora
La HoraLa Hora
La Horapotasz
 
Tim Presentation Goldman Sachs Conference Set09 Eng
Tim Presentation Goldman Sachs Conference Set09 EngTim Presentation Goldman Sachs Conference Set09 Eng
Tim Presentation Goldman Sachs Conference Set09 EngTIM RI
 
August 10th, 2009 Mike Cruezer Twitter
August 10th, 2009 Mike Cruezer TwitterAugust 10th, 2009 Mike Cruezer Twitter
August 10th, 2009 Mike Cruezer TwitterStraight North
 
Worksheet on dw report 1 m 1sukan run
Worksheet on dw report 1 m 1sukan runWorksheet on dw report 1 m 1sukan run
Worksheet on dw report 1 m 1sukan runMokhzani Fadir
 
Press Release 3 Q99 Tele Celular Sul En
Press Release 3 Q99   Tele Celular Sul EnPress Release 3 Q99   Tele Celular Sul En
Press Release 3 Q99 Tele Celular Sul EnTIM RI
 
Percubaan Upsr Matematik 2008
Percubaan Upsr Matematik 2008Percubaan Upsr Matematik 2008
Percubaan Upsr Matematik 2008cikgusabriconvent
 
Edisi 18 Medan
Edisi 18 MedanEdisi 18 Medan
Edisi 18 Medanepaper
 
Press Release 1 Q03 Tele Nordeste Celular En
Press Release 1 Q03   Tele Nordeste Celular EnPress Release 1 Q03   Tele Nordeste Celular En
Press Release 1 Q03 Tele Nordeste Celular EnTIM RI
 
Press Release 2 T09 En
Press Release 2 T09 EnPress Release 2 T09 En
Press Release 2 T09 EnTIM RI
 
Apresentação Institucional 2T16
Apresentação Institucional 2T16Apresentação Institucional 2T16
Apresentação Institucional 2T16TIM RI
 
Press Release 4 Q06
Press Release 4 Q06Press Release 4 Q06
Press Release 4 Q06TIM RI
 
Resultados Do Ano 2002
Resultados Do Ano 2002Resultados Do Ano 2002
Resultados Do Ano 2002TIM RI
 
2 Des Aceh
2 Des Aceh2 Des Aceh
2 Des Acehepaper
 

Viewers also liked (20)

Vision Cast (Short)
Vision Cast (Short)Vision Cast (Short)
Vision Cast (Short)
 
Ucapan Aluan Pengerusi Jks Part One
Ucapan Aluan Pengerusi Jks Part OneUcapan Aluan Pengerusi Jks Part One
Ucapan Aluan Pengerusi Jks Part One
 
Edisi15sep Nasional
Edisi15sep NasionalEdisi15sep Nasional
Edisi15sep Nasional
 
La Hora
La HoraLa Hora
La Hora
 
Tim Presentation Goldman Sachs Conference Set09 Eng
Tim Presentation Goldman Sachs Conference Set09 EngTim Presentation Goldman Sachs Conference Set09 Eng
Tim Presentation Goldman Sachs Conference Set09 Eng
 
August 10th, 2009 Mike Cruezer Twitter
August 10th, 2009 Mike Cruezer TwitterAugust 10th, 2009 Mike Cruezer Twitter
August 10th, 2009 Mike Cruezer Twitter
 
Worksheet on dw report 1 m 1sukan run
Worksheet on dw report 1 m 1sukan runWorksheet on dw report 1 m 1sukan run
Worksheet on dw report 1 m 1sukan run
 
Press Release 3 Q99 Tele Celular Sul En
Press Release 3 Q99   Tele Celular Sul EnPress Release 3 Q99   Tele Celular Sul En
Press Release 3 Q99 Tele Celular Sul En
 
Percubaan Upsr Matematik 2008
Percubaan Upsr Matematik 2008Percubaan Upsr Matematik 2008
Percubaan Upsr Matematik 2008
 
Edisi 18 Medan
Edisi 18 MedanEdisi 18 Medan
Edisi 18 Medan
 
Press Release 1 Q03 Tele Nordeste Celular En
Press Release 1 Q03   Tele Nordeste Celular EnPress Release 1 Q03   Tele Nordeste Celular En
Press Release 1 Q03 Tele Nordeste Celular En
 
Fun with Ruby and Cocoa
Fun with Ruby and CocoaFun with Ruby and Cocoa
Fun with Ruby and Cocoa
 
Press Release 2 T09 En
Press Release 2 T09 EnPress Release 2 T09 En
Press Release 2 T09 En
 
Apresentação Institucional 2T16
Apresentação Institucional 2T16Apresentação Institucional 2T16
Apresentação Institucional 2T16
 
Press Release 4 Q06
Press Release 4 Q06Press Release 4 Q06
Press Release 4 Q06
 
Resultados Do Ano 2002
Resultados Do Ano 2002Resultados Do Ano 2002
Resultados Do Ano 2002
 
Consumers Sw Blue
Consumers Sw BlueConsumers Sw Blue
Consumers Sw Blue
 
10-010 Waste management
10-010 Waste management10-010 Waste management
10-010 Waste management
 
Tmpg Final Dbwmp Nov 30
Tmpg Final Dbwmp Nov 30Tmpg Final Dbwmp Nov 30
Tmpg Final Dbwmp Nov 30
 
2 Des Aceh
2 Des Aceh2 Des Aceh
2 Des Aceh
 

Similar to C++ Events

Democratizing Serverless
Democratizing ServerlessDemocratizing Serverless
Democratizing ServerlessShaun Smith
 
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...Intel® Software
 
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...Kaxil Naik
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
Evolution of Real-time User Engagement Event Consumption at Pinterest
Evolution of Real-time User Engagement Event Consumption at PinterestEvolution of Real-time User Engagement Event Consumption at Pinterest
Evolution of Real-time User Engagement Event Consumption at PinterestHostedbyConfluent
 
Flyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoFlyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoKetanUmare
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React NativeEric Deng
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurThakurkirtika
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started CppLong Cao
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++Joan Puig Sanz
 
The fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose ReactThe fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose ReactOliver N
 
Running Flink in Production: The good, The bad and The in Between - Lakshmi ...
Running Flink in Production:  The good, The bad and The in Between - Lakshmi ...Running Flink in Production:  The good, The bad and The in Between - Lakshmi ...
Running Flink in Production: The good, The bad and The in Between - Lakshmi ...Flink Forward
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxsangeetaSS
 
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...Maarten Balliauw
 
The Fn Project: A Quick Introduction (December 2017)
The Fn Project: A Quick Introduction (December 2017)The Fn Project: A Quick Introduction (December 2017)
The Fn Project: A Quick Introduction (December 2017)Oracle Developers
 

Similar to C++ Events (20)

Democratizing Serverless
Democratizing ServerlessDemocratizing Serverless
Democratizing Serverless
 
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
 
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
 
Evolution of Real-time User Engagement Event Consumption at Pinterest
Evolution of Real-time User Engagement Event Consumption at PinterestEvolution of Real-time User Engagement Event Consumption at Pinterest
Evolution of Real-time User Engagement Event Consumption at Pinterest
 
Flyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiegoFlyte kubecon 2019 SanDiego
Flyte kubecon 2019 SanDiego
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++
 
The fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose ReactThe fundamental problems of GUI applications and why people choose React
The fundamental problems of GUI applications and why people choose React
 
Running Flink in Production: The good, The bad and The in Between - Lakshmi ...
Running Flink in Production:  The good, The bad and The in Between - Lakshmi ...Running Flink in Production:  The good, The bad and The in Between - Lakshmi ...
Running Flink in Production: The good, The bad and The in Between - Lakshmi ...
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
 
Flink internals web
Flink internals web Flink internals web
Flink internals web
 
Activity 5
Activity 5Activity 5
Activity 5
 
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
 
The Fn Project: A Quick Introduction (December 2017)
The Fn Project: A Quick Introduction (December 2017)The Fn Project: A Quick Introduction (December 2017)
The Fn Project: A Quick Introduction (December 2017)
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

C++ Events

  • 1. C++ 11 Events Aaron Nielsen Rob Alvies @BloocoInc
  • 4. Pub Sub Mitigation Navigation Targeting Guidance Host Interface Actuators Event Manager Fuse Propulsion Health and Status Built-In Test Data Link
  • 5. Goals ● Lightweight ● No Dependencies ● Fast Runtime Performance ● Leverage the Compiler ● Decoupled, Flexible Design ● Properly Abstracted Events ● Inversion of Control
  • 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. Entities ● Event Manager (reactor pattern) ● Event Listener ● Active Object (AO from ACE term) ● EventBasedAO ● Concrete Events ● Concrete Event Consumers
  • 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
  • 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. 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. 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. What about Event Subscription? Trying to avoid code that looks like this: Yes, we know it works, BUT...
  • 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. 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. 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. 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. 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. 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