ivi-main-loop
ivi-main-loop-common.h
1  /* Copyright (C) 2015 Jacques Guillou */
2  /* Copyright (C) 2015 Pelagicore AB <info@pelagicore.com> */
3 
4  /* This library is free software; you can redistribute it and/or */
5  /* modify it under the terms of the GNU Library General Public */
6  /* License as published by the Free Software Foundation; either */
7  /* version 2 of the License, or (at your option) any later version. */
8 
9  /* This library 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 GNU */
12  /* Library General Public License for more details. */
13 
14  /* A copy of the GNU Library General Public License is included in the */
15  /* "LICENSE" file. */
16 
17 
18 #pragma once
19 
20 #include <functional>
21 
22 namespace ivi {
23 
24 typedef int FileDescriptor;
25 
26 /**
27  * Any event source inherits from this class
28  */
30 {
31 public:
32  enum class ReportStatus
33  {
34  /// Returning that value from a source's callback function causes the source to remain enabled
36 
37  /// Returning that value from a source's callback function causes the source to be disabled
38  DISABLE
39  };
40 
41  /**
42  * Enables the source.
43  */
44  virtual void enable() = 0;
45 
46  /**
47  * Disables the source
48  */
49  virtual void disable() = 0;
50 
51  /**
52  * Returns true if the source is currently enabled, false otherwise
53  */
54  virtual bool isEnabled() const = 0;
55 
56 };
57 
58 /**
59  * An idle event source can be used to be notified whenever a dispatcher has no non-idle to trigger
60  */
62  public EventSource
63 {
64 public:
65  typedef std::function<ReportStatus()> CallBackFunction;
66 
67  IdleEventSource(const CallBackFunction &function) :
68  m_func(function)
69  {
70  }
71 
72  virtual ~IdleEventSource()
73  {
74  }
75 
76 protected:
77  CallBackFunction m_func;
78 
79 };
80 
81 typedef int DurationInMilliseconds;
82 
83 /**
84  * A timeout source is triggered after a certain amount of time.
85  */
87  public EventSource
88 {
89 public:
90  typedef std::function<ReportStatus()> CallBackFunction;
91 
92  TimeOutEventSource(DurationInMilliseconds duration, const CallBackFunction &callBackFunction) :
93  m_duration(duration), m_func(callBackFunction)
94  {
95  }
96 
97  virtual ~TimeOutEventSource()
98  {
99  }
100 
101  DurationInMilliseconds getDuration() const
102  {
103  return m_duration;
104  }
105 
106  virtual void setDuration(DurationInMilliseconds duration) = 0;
107 
108 protected:
109  DurationInMilliseconds m_duration;
110  CallBackFunction m_func;
111 
112 };
113 
114 /**
115  * This kind of source can be used to be notified whenever an event has occurred concerning a channel.
116  * The channel is typically identified by a file descriptor.
117  */
119  public EventSource
120 {
121 public:
122  enum class Event
123  {
124  /// No event occurred
125  NONE,
126 
127  /// Some data is available from the channel
128  READ_AVAILABLE = 1,
129 
130  /// Some data can be written to the channel without blocking
131  WRITE_AVAILABLE = 2,
132 
133  /// The channel has been close, which means no data can be read from the corresponding file descriptor
134  HANG_UP = 4
135  };
136 
137  typedef std::function<ReportStatus(Event)> CallBackFunction;
138 
139  ChannelWatchEventSource(FileDescriptor fileDescriptor, const CallBackFunction &callBackFunction) :
140  m_callBack(callBackFunction), m_fileDescriptor(fileDescriptor)
141  {
142  }
143 
144  virtual ~ChannelWatchEventSource()
145  {
146  }
147 
148  virtual FileDescriptor getFileDescriptor() const {
149  return m_fileDescriptor;
150  }
151 
152 protected:
153  CallBackFunction m_callBack;
154  FileDescriptor m_fileDescriptor;
155 };
156 
157 /**
158  * That interface is used to create new sources attached to a dispatcher
159  */
161 {
162 public:
163  virtual ~EventSourceFactory()
164  {
165  }
166 
167  /**
168  * Create a new idle event source.
169  */
170  virtual IdleEventSource *newIdleEventSource(const IdleEventSource::CallBackFunction &callBackFunction) = 0;
171 
172  /**
173  * Create a new timeout event source. The initial duration can be provided here, but it can also be altered using the returned object
174  */
175  virtual TimeOutEventSource *newTimeOutEventSource(const TimeOutEventSource::CallBackFunction &callBackFunction,
176  DurationInMilliseconds duration) = 0;
177 
178  /**
179  * Create a new channel watch event source.
180  */
181  virtual ChannelWatchEventSource *newChannelWatchEventSource(const ChannelWatchEventSource::CallBackFunction &callBackFunction,
182  FileDescriptor fileDescriptor,
183  ChannelWatchEventSource::Event events) = 0;
184 
185 };
186 
187 /**
188  * Event dispatcher interface. An event dispatcher implements a main loop is able to handle various event sources.
189  */
191  public EventSourceFactory
192 {
193 
194  /**
195  * Runs the main loop. That method does not return until the quit() method has been called.
196  */
197  virtual void run() = 0;
198 
199  /**
200  * Stops the main loop. Calling that method causes the main loop to stop handling any event and ensures that the run() method exits as soon as possible.
201  */
202  virtual void quit() = 0;
203 
204 };
205 
206 }
virtual void disable()=0
Disables the source.
Returning that value from a source's callback function causes the source to be disabled.
That interface is used to create new sources attached to a dispatcher.
A timeout source is triggered after a certain amount of time.
This kind of source can be used to be notified whenever an event has occurred concerning a channel...
virtual bool isEnabled() const =0
Returns true if the source is currently enabled, false otherwise.
Any event source inherits from this class.
Returning that value from a source's callback function causes the source to remain enabled...
virtual void enable()=0
Enables the source.
An idle event source can be used to be notified whenever a dispatcher has no non-idle to trigger...
Event dispatcher interface.
Some data can be written to the channel without blocking.
Some data is available from the channel.
virtual ChannelWatchEventSource * newChannelWatchEventSource(const ChannelWatchEventSource::CallBackFunction &callBackFunction, FileDescriptor fileDescriptor, ChannelWatchEventSource::Event events)=0
Create a new channel watch event source.
The channel has been close, which means no data can be read from the corresponding file descriptor...
virtual TimeOutEventSource * newTimeOutEventSource(const TimeOutEventSource::CallBackFunction &callBackFunction, DurationInMilliseconds duration)=0
Create a new timeout event source.
virtual IdleEventSource * newIdleEventSource(const IdleEventSource::CallBackFunction &callBackFunction)=0
Create a new idle event source.