source: vscpl/src/hu/varadiistvan/scpl/io/_win32/Completer.h@ 28:9b3c2d3ea9f3

Last change on this file since 28:9b3c2d3ea9f3 was 9:84f0dcb053b4, checked in by István Váradi <ivaradi@…>, 12 years ago

Imported the Win32-specific I/O code

File size: 6.4 KB
Line 
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_COMPLETER_H
30#define HU_VARADIISTVAN_SCPL_IO_WIN32_COMPLETER_H
31//------------------------------------------------------------------------------
32
33#include "../Failable.h"
34
35#include "Overlapped.h"
36#include "Overlappable.h"
37
38//------------------------------------------------------------------------------
39
40namespace hu { namespace varadiistvan { namespace scpl { namespace io {
41
42//------------------------------------------------------------------------------
43
44/**
45 * Base class for objects that handle the completion of some
46 * operation. They are associated with an Overlappable, and if that
47 * overlappable is non-blocking (i.e. has a waiter), an Overlapped
48 * object is created and stored in this object.
49 */
50class Completer : public FailableReference<Completer>
51{
52protected:
53 /**
54 * The overlappable object this completer belongs to.
55 */
56 Overlappable& overlappable;
57
58 /**
59 * The overlapped object.
60 */
61 Overlapped* overlapped;
62
63protected:
64 /**
65 * Construct the completer for the given Overlappable.
66 */
67 Completer(Overlappable* overlappable);
68
69 /**
70 * Destroy the completer.
71 */
72 virtual ~Completer();
73
74 /**
75 * Add the overlapped object (if any) to the given waiter.
76 */
77 void addTo(Waiter* waiter);
78
79 /**
80 * Remove the overlapped object (if any) from the waiter it is
81 * associated with.
82 */
83 void removeFromWaiter();
84
85 /**
86 * Determine if there is an overlapped object and it is being
87 * waited for.
88 */
89 bool isWaited() const;
90
91 /**
92 * Get the result of the overlapped operation, if any.
93 *
94 * @see Overlapped::getResult
95 */
96 bool getResult(DWORD& size);
97
98 /**
99 * Check whether we are waiting for an overlapped result and if
100 * so, return that.
101 *
102 * @param result will contain the result to return if we are
103 * waiting for an overlapped result.
104 *
105 * @return whether we are waiting for an overlapped result
106 */
107 bool checkWaitedResult(bool& result);
108
109 /**
110 * Handle the given result coming from an overlapped object.
111 */
112 virtual void handleWaitedResult(DWORD size) = 0;
113
114private:
115 /**
116 * Get the failable object.
117 */
118 const Failable& getFailable() const;
119
120 /**
121 * Get the failable object.
122 */
123 Failable& getFailable();
124
125 friend class FailableReference<Completer>;
126};
127
128//------------------------------------------------------------------------------
129// Inline definitions
130//------------------------------------------------------------------------------
131
132inline Completer::Completer(Overlappable* overlappable) :
133 overlappable(*overlappable),
134 overlapped((overlappable->waiter==0) ? 0 : new Overlapped(*overlappable))
135{
136}
137
138//------------------------------------------------------------------------------
139
140inline Completer::~Completer()
141{
142 delete overlapped;
143}
144
145//------------------------------------------------------------------------------
146
147inline void Completer::addTo(Waiter* waiter)
148{
149 if (overlapped!=0 && waiter!=0) overlapped->addTo(*waiter);
150}
151
152//------------------------------------------------------------------------------
153
154inline void Completer::removeFromWaiter()
155{
156 if (overlapped!=0) overlapped->removeFromWaiter();
157}
158
159//------------------------------------------------------------------------------
160
161inline bool Completer::isWaited() const
162{
163 return overlapped!=0 && overlapped->isWaited();
164}
165
166//------------------------------------------------------------------------------
167
168inline bool Completer::getResult(DWORD& size)
169{
170 return overlapped!=0 && overlapped->getResult(size, overlappable.handle);
171}
172
173//------------------------------------------------------------------------------
174
175inline bool Completer::checkWaitedResult(bool& result)
176{
177 if (!isWaited()) {
178 return false;
179 }
180
181 DWORD size = 0;
182 result = getResult(size);
183
184 if (result) {
185 removeFromWaiter();
186 handleWaitedResult(size);
187 } else if (overlappable.failed()) {
188 removeFromWaiter();
189 }
190
191 return true;
192}
193
194//------------------------------------------------------------------------------
195
196inline const Failable& Completer::getFailable() const
197{
198 return overlappable;
199}
200
201//------------------------------------------------------------------------------
202
203inline Failable& Completer::getFailable()
204{
205 return overlappable;
206}
207
208//------------------------------------------------------------------------------
209
210} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
211
212//------------------------------------------------------------------------------
213#endif // HU_VARADIISTVAN_SCPL_IO_WIN32_COMPLETER_H
214
215// Local Variables:
216// mode: C++
217// c-basic-offset: 4
218// indent-tabs-mode: nil
219// End:
Note: See TracBrowser for help on using the repository browser.