C++ features by examples
Active_object Struct Reference

Active object More...

Inheritance diagram for Active_object:
Collaboration diagram for Active_object:

Public Types

typedef function< void()> Command
 

Public Member Functions

 Active_object (Interface &s)
 
 ~Active_object ()
 
int method () override
 
- Public Member Functions inherited from Interface
virtual int method ()=0
 
virtual ~Interface ()=default
 

Public Attributes

Interfacesubject
 

Protected Attributes

Synchronized_queue< Commandcmd_q
 
thread th
 

Detailed Description

Member Typedef Documentation

◆ Command

typedef function<void()> Active_object::Command

Definition at line 760 of file patterns.cpp.

Constructor & Destructor Documentation

◆ Active_object()

Active_object::Active_object ( Interface s)
inline

Definition at line 762 of file patterns.cpp.

763 : subject(s)
764 {
765 th = thread([this] {
766 while (true) {
767 auto cmd = cmd_q.pull();
768 if (!cmd.has_value())
769 break;
770 cmd.value()();
771 }
772 });
773 }
Interface & subject
Definition: patterns.cpp:761
Synchronized_queue< Command > cmd_q
Definition: patterns.cpp:788

◆ ~Active_object()

Active_object::~Active_object ( )
inline

Definition at line 774 of file patterns.cpp.

774 {
775 cmd_q.stop = true;
776 cmd_q.cv.notify_one();
777 th.join();
778 }

Member Function Documentation

◆ method()

int Active_object::method ( )
inlineoverridevirtual

Implements Interface.

Definition at line 780 of file patterns.cpp.

780 {
781 promise<int> p;
782 future f = p.get_future();
783 cmd_q.push([&p, this] { p.set_value(subject.method());});
784 return f.get();
785 }
virtual int method()=0
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cmd_q

Synchronized_queue<Command> Active_object::cmd_q
protected

Definition at line 788 of file patterns.cpp.

◆ subject

Interface& Active_object::subject

Definition at line 761 of file patterns.cpp.

◆ th

thread Active_object::th
protected

Definition at line 789 of file patterns.cpp.


The documentation for this struct was generated from the following file: