winss
wait_multiplexer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_WAIT_MULTIPLEXER_HPP_
18 #define LIB_WINSS_WAIT_MULTIPLEXER_HPP_
19 
20 #include <windows.h>
21 #include <chrono>
22 #include <vector>
23 #include <string>
24 #include <map>
25 #include <set>
26 #include <functional>
27 #include "handle_wrapper.hpp"
28 #include "event_wrapper.hpp"
29 
30 namespace winss {
32 
36 typedef std::function<void(WaitMultiplexer&)> Callback;
37 
41 typedef std::function<void(WaitMultiplexer&,
43 
52  std::string group;
54  std::chrono::system_clock::time_point timeout;
55  Callback callback;
64  bool operator<(const WaitTimeoutItem& rhs) const;
65 };
66 
71  private:
72  bool started = false;
73  bool stopping = false;
75  int return_code = 0;
77  std::vector<Callback> init_callbacks;
79  std::map<winss::HandleWrapper, TriggeredCallback> trigger_callbacks;
81  std::set<WaitTimeoutItem> timeout_callbacks;
83  std::vector<Callback> stop_callbacks;
84 
93  Callback GetTimeoutCallback();
94 
95  public:
98  WaitMultiplexer(const WaitMultiplexer&) = delete;
99  WaitMultiplexer(WaitMultiplexer&&) = delete;
106  virtual void AddInitCallback(Callback callback);
107 
114  virtual void AddTriggeredCallback(const winss::HandleWrapper& handle,
115  TriggeredCallback callback);
116 
125  virtual void AddTimeoutCallback(DWORD timeout,
126  Callback callback, std::string group = "");
127 
133  virtual void AddStopCallback(Callback callback);
134 
140  virtual bool RemoveTriggeredCallback(const winss::HandleWrapper& handle);
141 
147  virtual bool RemoveTimeoutCallback(std::string group);
148 
154  virtual DWORD GetTimeout() const;
155 
165  virtual int Start();
166 
173  virtual void Stop(int code);
174 
180  virtual bool IsStopping() const;
181 
187  virtual bool HasStarted() const;
188 
194  virtual int GetReturnCode() const;
195 
205  virtual void AddCloseEvent(const winss::EventWrapper& close_event,
206  DWORD return_code);
207 
209  WaitMultiplexer& operator=(const WaitMultiplexer&) = delete;
211  WaitMultiplexer& operator=(WaitMultiplexer&&) = delete;
212 };
213 } // namespace winss
214 
215 #endif // LIB_WINSS_WAIT_MULTIPLEXER_HPP_
A wrapper for a Windows HANDLE.
Definition: handle_wrapper.hpp:39
WaitMultiplexer()
The default constructor.
Definition: wait_multiplexer.hpp:97
Wraps a windows event.
Definition: event_wrapper.hpp:27
bool operator<(const WaitTimeoutItem &rhs) const
Used to order the timeout items such as next item is the one with the point in time closest to now...
Definition: wait_multiplexer.cpp:32
Definition: case_ignore.hpp:23
std::string group
Used to identify the group of items when removing them.
Definition: wait_multiplexer.hpp:52
std::chrono::system_clock::time_point timeout
The point in time the timeout will be in effect.
Definition: wait_multiplexer.hpp:54
std::function< void(WaitMultiplexer &)> Callback
A callback function for wait multiplexer events.
Definition: wait_multiplexer.hpp:31
Holds timeout information such that when a timeout occurs the multiplexer knows how to handle it...
Definition: wait_multiplexer.hpp:50
A HANDLE wait multiplexer.
Definition: wait_multiplexer.hpp:70
Callback callback
The call back for when the timeout occurs.
Definition: wait_multiplexer.hpp:55
std::function< void(WaitMultiplexer &, const winss::HandleWrapper &)> TriggeredCallback
A callback function specifically for triggered multiplexer events.
Definition: wait_multiplexer.hpp:42