1 | // Copyright (c) 2012 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of libxplcommon, a common utility library for
|
---|
4 | // development related to X-Plane
|
---|
5 |
|
---|
6 | // Redistribution and use in source and binary forms, with or without
|
---|
7 | // modification, are permitted provided that the following conditions are met:
|
---|
8 |
|
---|
9 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
10 | // list of conditions and the following disclaimer.
|
---|
11 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
12 | // this list of conditions and the following disclaimer in the documentation
|
---|
13 | // and/or other materials provided with the distribution.
|
---|
14 |
|
---|
15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
16 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
18 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
19 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
20 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
21 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
22 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
24 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 |
|
---|
26 | // The views and conclusions contained in the software and documentation are those
|
---|
27 | // of the authors and should not be interpreted as representing official policies,
|
---|
28 | // either expressed or implied, of the FreeBSD Project.
|
---|
29 |
|
---|
30 | #ifndef XPLCOMMON_WIN32_COMPLETER_H
|
---|
31 | #define XPLCOMMON_WIN32_COMPLETER_H
|
---|
32 | //------------------------------------------------------------------------------
|
---|
33 |
|
---|
34 | #include "../Failable.h"
|
---|
35 |
|
---|
36 | #include "Overlapped.h"
|
---|
37 | #include "Overlappable.h"
|
---|
38 |
|
---|
39 | //------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | namespace xplcommon { namespace win32 {
|
---|
42 |
|
---|
43 | //------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Base class for objects that handle the completion of some
|
---|
47 | * operation. They are associated with an Overlappable, and if that
|
---|
48 | * overlappable is non-blocking (i.e. has a waiter), an Overlapped
|
---|
49 | * object is created and stored in this object.
|
---|
50 | */
|
---|
51 | class Completer : public ::xplcommon::FailableReference<Completer>
|
---|
52 | {
|
---|
53 | protected:
|
---|
54 | /**
|
---|
55 | * The overlappable object this completer belongs to.
|
---|
56 | */
|
---|
57 | Overlappable& overlappable;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The overlapped object.
|
---|
61 | */
|
---|
62 | Overlapped* overlapped;
|
---|
63 |
|
---|
64 | protected:
|
---|
65 | /**
|
---|
66 | * Construct the completer for the given Overlappable.
|
---|
67 | */
|
---|
68 | Completer(Overlappable* overlappable);
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Destroy the completer.
|
---|
72 | */
|
---|
73 | virtual ~Completer();
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Add the overlapped object (if any) to the given waiter.
|
---|
77 | */
|
---|
78 | void addTo(Waiter* waiter);
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Remove the overlapped object (if any) from the waiter it is
|
---|
82 | * associated with.
|
---|
83 | */
|
---|
84 | void removeFromWaiter();
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Determine if there is an overlapped object and it is being
|
---|
88 | * waited for.
|
---|
89 | */
|
---|
90 | bool isWaited() const;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Get the result of the overlapped operation, if any.
|
---|
94 | *
|
---|
95 | * @see Overlapped::getResult
|
---|
96 | */
|
---|
97 | bool getResult(DWORD& size);
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Check whether we are waiting for an overlapped result and if
|
---|
101 | * so, return that.
|
---|
102 | *
|
---|
103 | * @param result will contain the result to return if we are
|
---|
104 | * waiting for an overlapped result.
|
---|
105 | *
|
---|
106 | * @return whether we are waiting for an overlapped result
|
---|
107 | */
|
---|
108 | bool checkWaitedResult(bool& result);
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Handle the given result coming from an overlapped object.
|
---|
112 | */
|
---|
113 | virtual void handleWaitedResult(DWORD size) = 0;
|
---|
114 |
|
---|
115 | private:
|
---|
116 | /**
|
---|
117 | * Get the failable object.
|
---|
118 | */
|
---|
119 | const ::xplcommon::Failable& getFailable() const;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Get the failable object.
|
---|
123 | */
|
---|
124 | ::xplcommon::Failable& getFailable();
|
---|
125 |
|
---|
126 | friend class FailableReference<Completer>;
|
---|
127 | };
|
---|
128 |
|
---|
129 | //------------------------------------------------------------------------------
|
---|
130 | // Inline definitions
|
---|
131 | //------------------------------------------------------------------------------
|
---|
132 |
|
---|
133 | inline Completer::Completer(Overlappable* overlappable) :
|
---|
134 | overlappable(*overlappable),
|
---|
135 | overlapped((overlappable->waiter==0) ? 0 : new Overlapped(*overlappable))
|
---|
136 | {
|
---|
137 | }
|
---|
138 |
|
---|
139 | //------------------------------------------------------------------------------
|
---|
140 |
|
---|
141 | inline Completer::~Completer()
|
---|
142 | {
|
---|
143 | delete overlapped;
|
---|
144 | }
|
---|
145 |
|
---|
146 | //------------------------------------------------------------------------------
|
---|
147 |
|
---|
148 | inline void Completer::addTo(Waiter* waiter)
|
---|
149 | {
|
---|
150 | if (overlapped!=0 && waiter!=0) overlapped->addTo(*waiter);
|
---|
151 | }
|
---|
152 |
|
---|
153 | //------------------------------------------------------------------------------
|
---|
154 |
|
---|
155 | inline void Completer::removeFromWaiter()
|
---|
156 | {
|
---|
157 | if (overlapped!=0) overlapped->removeFromWaiter();
|
---|
158 | }
|
---|
159 |
|
---|
160 | //------------------------------------------------------------------------------
|
---|
161 |
|
---|
162 | inline bool Completer::isWaited() const
|
---|
163 | {
|
---|
164 | return overlapped!=0 && overlapped->isWaited();
|
---|
165 | }
|
---|
166 |
|
---|
167 | //------------------------------------------------------------------------------
|
---|
168 |
|
---|
169 | inline bool Completer::getResult(DWORD& size)
|
---|
170 | {
|
---|
171 | return overlapped!=0 && overlapped->getResult(size, overlappable.handle);
|
---|
172 | }
|
---|
173 |
|
---|
174 | //------------------------------------------------------------------------------
|
---|
175 |
|
---|
176 | inline bool Completer::checkWaitedResult(bool& result)
|
---|
177 | {
|
---|
178 | if (!isWaited()) {
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | DWORD size = 0;
|
---|
183 | result = getResult(size);
|
---|
184 |
|
---|
185 | if (result) {
|
---|
186 | removeFromWaiter();
|
---|
187 | handleWaitedResult(size);
|
---|
188 | } else if (overlappable.failed()) {
|
---|
189 | removeFromWaiter();
|
---|
190 | }
|
---|
191 |
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | //------------------------------------------------------------------------------
|
---|
196 |
|
---|
197 | inline const ::xplcommon::Failable& Completer::getFailable() const
|
---|
198 | {
|
---|
199 | return overlappable;
|
---|
200 | }
|
---|
201 |
|
---|
202 | //------------------------------------------------------------------------------
|
---|
203 |
|
---|
204 | inline ::xplcommon::Failable& Completer::getFailable()
|
---|
205 | {
|
---|
206 | return overlappable;
|
---|
207 | }
|
---|
208 |
|
---|
209 | //------------------------------------------------------------------------------
|
---|
210 |
|
---|
211 | } /* namespace xplcommon::win32 */ } /* namespace xplcommon */
|
---|
212 |
|
---|
213 | //------------------------------------------------------------------------------
|
---|
214 | #endif // XPLCOMMON_WIN32_COMPLETER_H
|
---|
215 |
|
---|
216 | // Local Variables:
|
---|
217 | // mode: C++
|
---|
218 | // c-basic-offset: 4
|
---|
219 | // indent-tabs-mode: nil
|
---|
220 | // End:
|
---|