VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gfutureevent.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gfutureevent.h
19 ///
20 
21 #ifndef G_FUTURE_EVENT__H
22 #define G_FUTURE_EVENT__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gexception.h"
27 #include "geventhandler.h"
28 
29 namespace GNet
30 {
31  class FutureEvent ;
32  class FutureEventHandler ;
33  class FutureEventImp ;
34 }
35 
36 /// \class GNet::FutureEvent
37 /// An object that hooks into the event loop and calls back to the client
38 /// code with a small unsigned integer payload. The trigger function send()
39 /// is typically called from a "future/promise" worker thread just before
40 /// it finishes.
41 ///
42 /// Eg:
43 /// \code
44 /// struct Foo : private FutureEventHandler
45 /// {
46 /// Foo() ;
47 /// void onFutureEvent( unsigned int result ) ;
48 /// void run( FutureEvent::handle_type ) ;
49 /// FutureEvent m_future_event ;
50 /// std::thread m_thread ;
51 /// }
52 /// Foo::Foo() : m_thread(&Foo::run,this,m_future_event.handle())
53 /// {
54 /// }
55 /// void Foo::run( FutureEvent::handle_type h )
56 /// {
57 /// ... // do blocking work
58 /// FutureEvent::send( h , 123 ) ;
59 /// }
60 /// \endcode
61 ///
63 {
64 public:
65  G_EXCEPTION( Error , "FutureEvent error" ) ;
66  typedef HWND handle_type ;
67 
68  explicit FutureEvent( FutureEventHandler & ) ;
69  ///< Constructor.
70 
71  ~FutureEvent() ;
72  ///< Destructor.
73 
74  handle_type handle() const ;
75  ///< Returns a handle that can be passed between threads
76  ///< and used in send().
77 
78  static bool send( handle_type handle , unsigned int payload ) g__noexcept ;
79  ///< Pokes the event payload into the main event loop so that
80  ///< the callback is called once the stack is unwound.
81  ///< Returns true on success.
82 
83 private:
84  FutureEvent( const FutureEvent & ) ;
85  void operator=( const FutureEvent & ) ;
86 
87 private:
88  friend class FutureEventImp ;
89  unique_ptr<FutureEventImp> m_imp ;
90 } ;
91 
92 /// \class GNet::FutureEventHandler
93 /// A callback interface for GNet::FutureEvent.
94 ///
96 {
97 public:
98  virtual void onFutureEvent( unsigned int ) = 0 ;
99  ///< Callback function that delivers the payload value
100  ///< from FutureEvent::send().
101 
102 protected:
103  virtual ~FutureEventHandler() ;
104  ///< Destructor.
105 
106 private:
107  void operator=( const FutureEventHandler & ) ;
108 } ;
109 
110 #endif
A callback interface for GNet::FutureEvent.
Definition: gfutureevent.h:95
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events and t...
Definition: geventhandler.h:44
An object that hooks into the event loop and calls back to the client code with a small unsigned inte...
Definition: gfutureevent.h:62
handle_type handle() const
Returns a handle that can be passed between threads and used in send().
~FutureEvent()
Destructor.
virtual void onFutureEvent(unsigned int)=0
Callback function that delivers the payload value from FutureEvent::send().
FutureEvent(FutureEventHandler &)
Constructor.
virtual ~FutureEventHandler()
Destructor.
A pimple-pattern implementation class used by GNet::FutureEvent.
static bool send(handle_type handle, unsigned int payload) g__noexcept
Pokes the event payload into the main event loop so that the callback is called once the stack is unw...