[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_POSIX_WAITABLE_H
|
---|
| 30 | #define HU_VARADIISTVAN_SCPL_IO_POSIX_WAITABLE_H
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include "../Failable.h"
|
---|
| 34 |
|
---|
| 35 | #include <cerrno>
|
---|
| 36 |
|
---|
| 37 | //------------------------------------------------------------------------------
|
---|
| 38 |
|
---|
| 39 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 40 |
|
---|
| 41 | //------------------------------------------------------------------------------
|
---|
| 42 |
|
---|
| 43 | class Waiter;
|
---|
| 44 |
|
---|
| 45 | //------------------------------------------------------------------------------
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Base class of objects for which one can wait in a Waiter.
|
---|
| 49 | *
|
---|
| 50 | * It is basically a file descriptor.
|
---|
| 51 | */
|
---|
| 52 | class Waitable : public Failable
|
---|
| 53 | {
|
---|
| 54 | public:
|
---|
| 55 | /**
|
---|
| 56 | * Set the given file descriptor to non-blocking.
|
---|
| 57 | */
|
---|
| 58 | static void setNonBlocking(int fd);
|
---|
| 59 |
|
---|
| 60 | protected:
|
---|
| 61 | /**
|
---|
| 62 | * The waiter this waitable belongs to, if any.
|
---|
| 63 | */
|
---|
| 64 | Waiter* waiter;
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * The file descriptor
|
---|
| 68 | */
|
---|
| 69 | int fd;
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * The events this waitable is interested in.
|
---|
| 73 | */
|
---|
| 74 | short events;
|
---|
| 75 |
|
---|
| 76 | protected:
|
---|
| 77 | /**
|
---|
| 78 | * Construct the waitable.
|
---|
| 79 | */
|
---|
| 80 | Waitable(Waiter* waiter, int fd = -1, short events = 0);
|
---|
| 81 |
|
---|
| 82 | public:
|
---|
| 83 | /**
|
---|
| 84 | * Destroy the waitable. It will be removed from the waiter.
|
---|
| 85 | */
|
---|
| 86 | virtual ~Waitable();
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Get the waiter.
|
---|
| 90 | */
|
---|
| 91 | Waiter* getWaiter() const;
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Determine if the waitable is ready or not, i.e. if there is no
|
---|
| 95 | * need to wait for it.
|
---|
| 96 | *
|
---|
| 97 | * It first checks the events. If they are 0, the waitable is
|
---|
| 98 | * ready. Then it calls isReady() and returns the result of that
|
---|
| 99 | * function.
|
---|
| 100 | */
|
---|
| 101 | bool ready();
|
---|
| 102 |
|
---|
| 103 | protected:
|
---|
| 104 | /**
|
---|
| 105 | * Indicate if the waitable is ready, i.e. there is no need to
|
---|
| 106 | * wait for it.
|
---|
| 107 | *
|
---|
| 108 | * Calling this function should not delete the ready
|
---|
| 109 | * indication. It should be deleted by actually processing the
|
---|
| 110 | * data.
|
---|
| 111 | */
|
---|
| 112 | virtual bool isReady() = 0;
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Handle the given events.
|
---|
| 116 | */
|
---|
| 117 | virtual void handleEvents(short events) = 0;
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Set the error code from errno.
|
---|
| 121 | */
|
---|
| 122 | void setErrorCodeFromErrno();
|
---|
| 123 |
|
---|
| 124 | friend class Waiter;
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | //------------------------------------------------------------------------------
|
---|
| 128 | // Inline defintions
|
---|
| 129 | //------------------------------------------------------------------------------
|
---|
| 130 |
|
---|
| 131 | inline Waiter* Waitable::getWaiter() const
|
---|
| 132 | {
|
---|
| 133 | return waiter;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //------------------------------------------------------------------------------
|
---|
| 137 |
|
---|
| 138 | inline bool Waitable::ready()
|
---|
| 139 | {
|
---|
| 140 | return (events==0) || isReady();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | //------------------------------------------------------------------------------
|
---|
| 144 |
|
---|
| 145 | inline void Waitable::setErrorCodeFromErrno()
|
---|
| 146 | {
|
---|
| 147 | setErrorCode(errno);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | //------------------------------------------------------------------------------
|
---|
| 151 |
|
---|
| 152 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 153 |
|
---|
| 154 | //------------------------------------------------------------------------------
|
---|
| 155 | #endif // HU_VARADIISTVAN_SCPL_IO_POSIX_WAITABLE_H
|
---|
| 156 |
|
---|
| 157 | // Local Variables:
|
---|
| 158 | // mode: C++
|
---|
| 159 | // c-basic-offset: 4
|
---|
| 160 | // indent-tabs-mode: nil
|
---|
| 161 | // End:
|
---|