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

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

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