[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_WRITINGBUFFER_H
|
---|
| 30 | #define HU_VARADIISTVAN_SCPL_IO_POSIX_WRITINGBUFFER_H
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include "StreamBuffer.h"
|
---|
| 34 |
|
---|
| 35 | //------------------------------------------------------------------------------
|
---|
| 36 |
|
---|
| 37 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 38 |
|
---|
| 39 | //------------------------------------------------------------------------------
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * A buffer used for writing.
|
---|
| 43 | *
|
---|
| 44 | * The data to write should be stored in the buffer. Then write()
|
---|
| 45 | * should be called. If it returns true, it indicates that all data
|
---|
| 46 | * has been written. In that case the buffer is automatically emptied.
|
---|
| 47 | */
|
---|
| 48 | class WritingBuffer : public StreamBuffer
|
---|
| 49 | {
|
---|
| 50 | private:
|
---|
| 51 | /**
|
---|
| 52 | * The offset of the next byte to write.
|
---|
| 53 | */
|
---|
| 54 | size_t offset;
|
---|
| 55 |
|
---|
| 56 | protected:
|
---|
| 57 | /**
|
---|
| 58 | * Construct the buffer.
|
---|
| 59 | */
|
---|
| 60 | WritingBuffer(size_t capacity, BufferedStream* stream);
|
---|
| 61 |
|
---|
| 62 | public:
|
---|
| 63 | /**
|
---|
| 64 | * Reset the buffer. It resets the offset too.
|
---|
| 65 | */
|
---|
| 66 | void reset();
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Write the contents of the buffer using the associated buffered
|
---|
| 70 | * stream.
|
---|
| 71 | *
|
---|
| 72 | * If the buffer is empty, the function returns indicating
|
---|
| 73 | * success.
|
---|
| 74 | *
|
---|
| 75 | * If the complete remaining contents could be written, it also indicates
|
---|
| 76 | * success, and the buffer is reset, ready to be used again.
|
---|
| 77 | *
|
---|
| 78 | * If not everything can be written and the write operation
|
---|
| 79 | * indicates that it would block, this function returns failure
|
---|
| 80 | * after the stream has been set up to handle the corresponding
|
---|
| 81 | * poll events.
|
---|
| 82 | *
|
---|
| 83 | * Otherwise, if the writing operation fails for some other
|
---|
| 84 | * reason, the buffer's error code is set to the error's code and
|
---|
| 85 | * false is returned.
|
---|
| 86 | */
|
---|
| 87 | bool write();
|
---|
| 88 |
|
---|
| 89 | friend class BufferedStream;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | //------------------------------------------------------------------------------
|
---|
| 93 | // Inline definitions
|
---|
| 94 | //------------------------------------------------------------------------------
|
---|
| 95 |
|
---|
| 96 | inline WritingBuffer::WritingBuffer(size_t capacity, BufferedStream* stream):
|
---|
| 97 | StreamBuffer(capacity, stream),
|
---|
| 98 | offset(0)
|
---|
| 99 | {
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | //------------------------------------------------------------------------------
|
---|
| 103 |
|
---|
| 104 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 105 |
|
---|
| 106 | //------------------------------------------------------------------------------
|
---|
| 107 | #endif // HU_VARADIISTVAN_SCPL_IO_POSIX_WRITINGBUFFER_H
|
---|
| 108 |
|
---|
| 109 | // Local Variables:
|
---|
| 110 | // mode: C++
|
---|
| 111 | // c-basic-offset: 4
|
---|
| 112 | // indent-tabs-mode: nil
|
---|
| 113 | // End:
|
---|