39 #include "final/util/fdata.h" 40 #include "final/util/fstring.h" 56 using std::runtime_error::runtime_error;
75 using handler_t = std::function<void(Monitor*, short)>;
86 enum class State : uInt8
113 virtual auto getClassName()
const ->
FString;
114 auto getEvents()
const noexcept -> short;
115 auto getFileDescriptor()
const noexcept -> int;
116 template <
typename T>
117 auto getUserContext()
const -> clean_fdata_t<T>&;
120 auto isActive()
const noexcept -> bool;
121 auto isSuspended()
const noexcept -> bool;
122 auto hasValidFileDescriptor()
const noexcept -> bool;
123 auto hasHandler()
const noexcept -> bool;
126 virtual void resume() noexcept;
127 virtual void suspend() noexcept;
128 virtual void deactivate() noexcept;
132 static constexpr
int NO_FILE_DESCRIPTOR{-1};
135 void setFileDescriptor (
int) noexcept;
136 void setEvents (
short) noexcept;
137 void setHandler (handler_t&&) noexcept;
138 void setHandler (
const handler_t& handler);
139 template <
typename T>
140 void setUserContext (T&&) noexcept;
141 void clearUserContext() noexcept;
142 void setInitialized() noexcept;
145 auto isInitialized()
const -> bool;
148 virtual void trigger (
short);
152 using FDataAccessPtr = std::shared_ptr<FDataAccess>;
155 auto getState()
const noexcept -> State;
158 void validateEventLoop()
const;
159 void setState (State) noexcept;
163 std::atomic<State> state{State::Inactive};
164 std::atomic<int> fd{NO_FILE_DESCRIPTOR};
165 std::atomic<short> events{0};
166 std::atomic<bool> monitor_initialized{
false};
168 FDataAccessPtr user_context{
nullptr};
176 inline auto Monitor::getClassName()
const ->
FString 177 {
return "Monitor"; }
180 inline auto Monitor::getEvents()
const noexcept ->
short 181 {
return events.load(); }
184 inline auto Monitor::getFileDescriptor()
const noexcept ->
int 185 {
return fd.load(); }
188 template <
typename T>
189 auto Monitor::getUserContext()
const -> clean_fdata_t<T>&
191 static T empty_lvalue = T{};
198 inline auto Monitor::isActive()
const noexcept ->
bool 199 {
return getState() == State::Active; }
202 inline auto Monitor::isSuspended()
const noexcept ->
bool 203 {
return getState() == State::Suspended; }
206 inline auto Monitor::hasValidFileDescriptor()
const noexcept ->
bool 207 {
return fd.load() != NO_FILE_DESCRIPTOR; }
210 inline auto Monitor::hasHandler()
const noexcept ->
bool 211 {
return static_cast<bool>(handler); }
214 inline void Monitor::resume() noexcept
215 { setState(State::Active); }
218 inline void Monitor::suspend() noexcept
219 { setState(State::Suspended); }
222 inline void Monitor::deactivate() noexcept
223 { setState(State::Inactive); }
226 inline void Monitor::trigger (
short return_events)
228 if ( ! handler || ! isActive() )
233 handler (
this, return_events);
243 inline void Monitor::setFileDescriptor (
int file_descriptor) noexcept
244 { fd.store(file_descriptor); }
247 inline void Monitor::setEvents (
short ev) noexcept
248 { events.store(ev); }
251 inline void Monitor::setHandler (handler_t&& hdl) noexcept
252 { handler = std::move(hdl); }
255 inline void Monitor::setHandler(
const handler_t& hdl)
259 template <
typename T>
260 inline void Monitor::setUserContext (T&& uc) noexcept
261 { user_context = makeFData(std::forward<T>(uc)); }
264 inline void Monitor::clearUserContext() noexcept
265 { user_context.reset(); }
268 inline void Monitor::setInitialized() noexcept
269 { monitor_initialized.store(
true); }
272 inline auto Monitor::isInitialized()
const ->
bool 273 {
return monitor_initialized.load(); }
276 inline auto Monitor::getState()
const noexcept -> State
277 {
return state.load(); }
280 inline void Monitor::setState (State new_state) noexcept
281 { state.store(new_state); }
Definition: eventloop.h:49
Definition: class_template.cpp:25