[6] | 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_WAITER_H
|
---|
| 30 | #define HU_VARADIISTVAN_SCPL_IO_WIN32_WAITER_H
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include "../Failable.h"
|
---|
| 34 |
|
---|
| 35 | #include <set>
|
---|
| 36 |
|
---|
| 37 | #include <cassert>
|
---|
| 38 |
|
---|
| 39 | //------------------------------------------------------------------------------
|
---|
| 40 |
|
---|
| 41 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 42 |
|
---|
| 43 | //------------------------------------------------------------------------------
|
---|
| 44 |
|
---|
| 45 | class Event;
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Class that can be used to register OVERLAPPED structures and then
|
---|
| 49 | * wait for their events.
|
---|
| 50 | */
|
---|
| 51 | class Waiter : public Failable
|
---|
| 52 | {
|
---|
| 53 | private:
|
---|
| 54 | /**
|
---|
| 55 | * Type for the mapping of events to overlapped objects.
|
---|
| 56 | */
|
---|
| 57 | typedef std::set<Event*> events_t;
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * The events to be handled.
|
---|
| 61 | */
|
---|
| 62 | events_t events;
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * The number of events.
|
---|
| 66 | */
|
---|
| 67 | size_t numEvents;
|
---|
| 68 |
|
---|
| 69 | public:
|
---|
| 70 | /**
|
---|
| 71 | * Construct the waiter.
|
---|
| 72 | */
|
---|
| 73 | Waiter();
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Destroy the waiter. It will be removed from any Event
|
---|
| 77 | * objects it may be related to.
|
---|
| 78 | */
|
---|
| 79 | ~Waiter();
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Wait for an event to occur with the given timeout.
|
---|
| 83 | *
|
---|
| 84 | * @return true if any of the events belonging to the Overlapped
|
---|
| 85 | * objects become ready, false if the timeout occurs.
|
---|
| 86 | */
|
---|
| 87 | bool wait(int timeout = -1);
|
---|
| 88 |
|
---|
| 89 | protected:
|
---|
| 90 | /**
|
---|
| 91 | * Add the given event to the set of those to be waited for.
|
---|
| 92 | */
|
---|
| 93 | void add(Event* event);
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Remove the given event from the set of those to be waited for.
|
---|
| 97 | */
|
---|
| 98 | void remove(Event* event);
|
---|
| 99 |
|
---|
| 100 | friend class Event;
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | //------------------------------------------------------------------------------
|
---|
| 104 | // Inline definitions
|
---|
| 105 | //------------------------------------------------------------------------------
|
---|
| 106 |
|
---|
| 107 | inline Waiter::Waiter() :
|
---|
| 108 | numEvents(0)
|
---|
| 109 | {
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | //------------------------------------------------------------------------------
|
---|
| 113 |
|
---|
| 114 | inline void Waiter::add(Event* event)
|
---|
| 115 | {
|
---|
| 116 | events.insert(event);
|
---|
| 117 | ++numEvents;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | //------------------------------------------------------------------------------
|
---|
| 121 |
|
---|
| 122 | inline void Waiter::remove(Event* event)
|
---|
| 123 | {
|
---|
| 124 | assert(numEvents>0);
|
---|
| 125 | --numEvents;
|
---|
| 126 | events.erase(event);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | //------------------------------------------------------------------------------
|
---|
| 130 |
|
---|
| 131 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 132 |
|
---|
| 133 | //------------------------------------------------------------------------------
|
---|
| 134 | #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_WAITER_H
|
---|
| 135 |
|
---|
| 136 | // Local Variables:
|
---|
| 137 | // mode: C++
|
---|
| 138 | // c-basic-offset: 4
|
---|
| 139 | // indent-tabs-mode: nil
|
---|
| 140 | // End:
|
---|