1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H
|
---|
30 | #define HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "EventFailable.h"
|
---|
34 |
|
---|
35 | #include <windows.h>
|
---|
36 |
|
---|
37 | //------------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
40 |
|
---|
41 | //------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | class Waiter;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Wrapper for an event.
|
---|
47 | */
|
---|
48 | class Event
|
---|
49 | {
|
---|
50 | private:
|
---|
51 | /**
|
---|
52 | * The object receiving the failure codes.
|
---|
53 | */
|
---|
54 | EventFailable& eventFailable;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * The handle of the event.
|
---|
58 | */
|
---|
59 | HANDLE handle;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * The object waiting for the event.
|
---|
63 | */
|
---|
64 | Waiter* waiter;
|
---|
65 |
|
---|
66 | public:
|
---|
67 | /**
|
---|
68 | * Construct the event.
|
---|
69 | */
|
---|
70 | Event(EventFailable& eventFailable);
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Destroy the event. It will be removed from the waiter if it is
|
---|
74 | * in one.
|
---|
75 | */
|
---|
76 | ~Event();
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Get the handle of the event.
|
---|
80 | */
|
---|
81 | HANDLE getHandle() const;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get the waiter.
|
---|
85 | */
|
---|
86 | Waiter* getWaiter() const;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Register the event with the given waiter. If it is already
|
---|
90 | * registered with a waiter, it will be removed from that.
|
---|
91 | */
|
---|
92 | void addTo(Waiter& w);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Remove the object from the waiter it is registered with.
|
---|
96 | */
|
---|
97 | void removeFromWaiter();
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Determine if the event is being waited for, i.e. it is
|
---|
101 | * associated with a waiter.
|
---|
102 | */
|
---|
103 | bool isWaited() const;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Set the event.
|
---|
107 | */
|
---|
108 | void fire();
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Determine if the event is fired or not.
|
---|
112 | */
|
---|
113 | bool isFired() const;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Clear the event.
|
---|
117 | *
|
---|
118 | * @return if clearing succeeded.
|
---|
119 | */
|
---|
120 | bool clear();
|
---|
121 |
|
---|
122 | protected:
|
---|
123 | /**
|
---|
124 | * Set the error code on the failable object.
|
---|
125 | */
|
---|
126 | void setErrorCode(errorCode_t errorCode);
|
---|
127 | };
|
---|
128 |
|
---|
129 | //------------------------------------------------------------------------------
|
---|
130 | // Inline definitions
|
---|
131 | //------------------------------------------------------------------------------
|
---|
132 |
|
---|
133 | inline Event::Event(EventFailable& eventFailable) :
|
---|
134 | eventFailable(eventFailable),
|
---|
135 | handle(CreateEvent(0, true, false, 0)),
|
---|
136 | waiter(0)
|
---|
137 | {
|
---|
138 | if (handle==0) eventFailable.setErrorCode(GetLastError());
|
---|
139 | }
|
---|
140 |
|
---|
141 | //------------------------------------------------------------------------------
|
---|
142 |
|
---|
143 | inline Event::~Event()
|
---|
144 | {
|
---|
145 | removeFromWaiter();
|
---|
146 | CloseHandle(handle);
|
---|
147 | }
|
---|
148 |
|
---|
149 | //------------------------------------------------------------------------------
|
---|
150 |
|
---|
151 | inline HANDLE Event::getHandle() const
|
---|
152 | {
|
---|
153 | return handle;
|
---|
154 | }
|
---|
155 |
|
---|
156 | //------------------------------------------------------------------------------
|
---|
157 |
|
---|
158 | inline Waiter* Event::getWaiter() const
|
---|
159 | {
|
---|
160 | return waiter;
|
---|
161 | }
|
---|
162 |
|
---|
163 | //------------------------------------------------------------------------------
|
---|
164 |
|
---|
165 | inline bool Event::isWaited() const
|
---|
166 | {
|
---|
167 | return waiter!=0;
|
---|
168 | }
|
---|
169 |
|
---|
170 | //------------------------------------------------------------------------------
|
---|
171 |
|
---|
172 | inline void Event::fire()
|
---|
173 | {
|
---|
174 | if (!SetEvent(handle)) {
|
---|
175 | eventFailable.setErrorCode(GetLastError());
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | //------------------------------------------------------------------------------
|
---|
180 |
|
---|
181 | inline bool Event::clear()
|
---|
182 | {
|
---|
183 | if (!ResetEvent(handle)) {
|
---|
184 | eventFailable.setErrorCode(GetLastError());
|
---|
185 | return false;
|
---|
186 | } else {
|
---|
187 | return true;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | //------------------------------------------------------------------------------
|
---|
192 |
|
---|
193 | inline void Event::setErrorCode(errorCode_t errorCode)
|
---|
194 | {
|
---|
195 | eventFailable.setErrorCode(errorCode);
|
---|
196 | }
|
---|
197 |
|
---|
198 | //------------------------------------------------------------------------------
|
---|
199 |
|
---|
200 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
201 |
|
---|
202 | //------------------------------------------------------------------------------
|
---|
203 | #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H
|
---|
204 |
|
---|
205 | // Local Variables:
|
---|
206 | // mode: C++
|
---|
207 | // c-basic-offset: 4
|
---|
208 | // indent-tabs-mode: nil
|
---|
209 | // End:
|
---|