winss
handle_wrapper.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_HANDLE_WRAPPER_HPP_
18 #define LIB_WINSS_HANDLE_WRAPPER_HPP_
19 
20 #include <windows.h>
21 #include <vector>
22 #include "easylogging/easylogging++.hpp"
23 
24 namespace winss {
32 };
33 
34 struct WaitResult;
35 
40  protected:
41  bool owned;
42  HANDLE handle;
43  DWORD dup_rights;
52  static WaitResult Wait(DWORD timeout, const std::vector<HANDLE>& handles);
53 
57  void CloseHandle();
58 
59  public:
63  HandleWrapper();
64 
72  explicit HandleWrapper(HANDLE handle, bool owned = true,
73  DWORD dup_rights = 0);
74 
82  HandleWrapper(const HandleWrapper& h);
83 
92 
98  bool HasHandle() const;
99 
105  bool IsOwner() const;
106 
112  DWORD GetDuplicateRights() const;
113 
120  WaitResult Wait(DWORD timeout) const;
121 
132  template<typename Iterator>
133  static WaitResult Wait(DWORD timeout, Iterator begin, Iterator end) {
134  std::vector<HANDLE> handles;
135 
136  for (auto it = begin; it != end; ++it) {
137  handles.push_back(it->handle);
138  }
139 
140  WaitResult result = Wait(timeout, handles);
141  VLOG(7) << "Wait status: " << result.state;
142  return result;
143  }
144 
151  HANDLE Duplicate(bool inherit) const;
152 
159  bool operator==(const HandleWrapper& rhs) const;
160 
167  bool operator!=(const HandleWrapper& rhs) const;
168 
175  bool operator<(const HandleWrapper& rhs) const;
176 
183  bool operator<=(const HandleWrapper& rhs) const;
184 
191  bool operator>(const HandleWrapper& rhs) const;
192 
199  bool operator>=(const HandleWrapper& rhs) const;
200 
208  friend bool operator==(const HandleWrapper &lhs, const HANDLE &rhs);
209 
217  friend bool operator==(const HANDLE &lhs, const HandleWrapper &rhs);
218 
226  friend bool operator!=(const HandleWrapper &lhs, const HANDLE &rhs);
227 
235  friend bool operator!=(const HANDLE &lhs, const HandleWrapper &rhs);
236 
244  friend bool operator<(const HandleWrapper &lhs, const HANDLE &rhs);
245 
253  friend bool operator<(const HANDLE &lhs, const HandleWrapper &rhs);
254 
262  friend bool operator<=(const HandleWrapper &lhs, const HANDLE &rhs);
263 
271  friend bool operator<=(const HANDLE &lhs, const HandleWrapper &rhs);
272 
280  friend bool operator>(const HandleWrapper &lhs, const HANDLE &rhs);
281 
289  friend bool operator>(const HANDLE &lhs, const HandleWrapper &rhs);
290 
298  friend bool operator>=(const HandleWrapper &lhs, const HANDLE &rhs);
299  friend bool operator>=(const HANDLE &lhs, const HandleWrapper &rhs);
300 
310 
320 
324  virtual ~HandleWrapper();
325 };
326 
328  public:
332  TrustedHandleWrapper() = default;
333 
340  explicit TrustedHandleWrapper(HANDLE handle, DWORD dup_rights = 0);
341 
349  TrustedHandleWrapper(const TrustedHandleWrapper& h) = default;
350 
359 
365  HANDLE GetHandle() const;
366 
372  HandleWrapper GetHandleWrapper() const;
373 
382 
392 };
393 
397 struct WaitResult {
400 };
401 } // namespace winss
402 
403 #endif // LIB_WINSS_HANDLE_WRAPPER_HPP_
The wait result for when waiting on handles.
Definition: handle_wrapper.hpp:397
A wrapper for a Windows HANDLE.
Definition: handle_wrapper.hpp:39
bool operator<(const HandleWrapper &rhs) const
Checks this handle is less than the rhs.
Definition: handle_wrapper.cpp:103
HANDLE handle
The wrapped handle.
Definition: handle_wrapper.hpp:42
HandleWrapper & operator=(const HandleWrapper &h)
Copies the handle wrapper.
Definition: handle_wrapper.cpp:125
DWORD dup_rights
The duplicate rights.
Definition: handle_wrapper.hpp:43
bool IsOwner() const
Gets if this instance owns the handle.
Definition: handle_wrapper.cpp:41
bool owned
If this instance owns the handle.
Definition: handle_wrapper.hpp:41
bool operator!=(const HandleWrapper &rhs) const
Checks the handles are not equal.
Definition: handle_wrapper.cpp:99
virtual ~HandleWrapper()
Cleans up the handle.
Definition: handle_wrapper.cpp:148
bool operator>=(const HandleWrapper &rhs) const
Checks this handle is greater than or equal to the rhs.
Definition: handle_wrapper.cpp:115
Definition: case_ignore.hpp:23
HANDLE Duplicate(bool inherit) const
Duplicates the handle.
Definition: handle_wrapper.cpp:79
bool operator<=(const HandleWrapper &rhs) const
Checks this handle is less than or equal to the rhs.
Definition: handle_wrapper.cpp:107
bool HasHandle() const
Gets if the wrapper contains a handle.
Definition: handle_wrapper.cpp:37
bool operator==(const HandleWrapper &rhs) const
Checks the handles are equal.
Definition: handle_wrapper.cpp:95
static WaitResult Wait(DWORD timeout, Iterator begin, Iterator end)
Waits for an event on a sequence of handles.
Definition: handle_wrapper.hpp:133
static WaitResult Wait(DWORD timeout, const std::vector< HANDLE > &handles)
Waits for an event to happen on the given list of handles.
Definition: handle_wrapper.cpp:49
The wait timed out.
Definition: handle_wrapper.hpp:30
bool operator>(const HandleWrapper &rhs) const
Checks this handle is greater than the rhs.
Definition: handle_wrapper.cpp:111
DWORD GetDuplicateRights() const
Gets the duplicate rights of the handle.
Definition: handle_wrapper.cpp:45
WaitResultState state
Definition: handle_wrapper.hpp:398
HandleWrapper()
Create an empty handle wrapper.
Definition: handle_wrapper.cpp:23
The wait was a success.
Definition: handle_wrapper.hpp:29
Definition: handle_wrapper.hpp:327
HandleWrapper handle
Definition: handle_wrapper.hpp:399
void CloseHandle()
Close the handle if the instance owns it.
Definition: handle_wrapper.cpp:119
WaitResultState
The wait result state for when waiting on handles.
Definition: handle_wrapper.hpp:28
The wait failed.
Definition: handle_wrapper.hpp:31