/usr/include/cxxtools/iodevice.h is in libcxxtools-dev 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | /*
* Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
* Copyright (C) 2006-2007 Marc Boris Duerner
* Copyright (C) 2006-2007 PTV AG
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* As a special exception, you may use this file as part of a free
* software library without restriction. Specifically, if other files
* instantiate templates or use macros or inline functions from this
* file, or you compile this file and link it with other files to
* produce an executable, this file does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. This exception does not however invalidate any other
* reasons why the executable file might be covered by the GNU Library
* General Public License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CXXTOOLS_IODevice_h
#define CXXTOOLS_IODevice_h
#include <cxxtools/signal.h>
#include <cxxtools/api.h>
#include <cxxtools/ioerror.h>
#include <cxxtools/selectable.h>
#include <limits>
#include <ios>
namespace cxxtools {
enum IOS_OpenMode
{
IOS_Sync = 0,
IOS_Async = 1L << 0,
IOS_Read = 1L << 1,
IOS_Write = 1L << 2,
IOS_AtEnd = 1L << 3,
IOS_Append = 1L << 4,
IOS_Trunc = 1L << 5,
IOS_OpenModeEnd = 1L << 16
};
inline IOS_OpenMode operator&(IOS_OpenMode a, IOS_OpenMode b)
{ return IOS_OpenMode(static_cast<int>(a) & static_cast<int>(b)); }
inline IOS_OpenMode operator|(IOS_OpenMode a, IOS_OpenMode b)
{ return IOS_OpenMode(static_cast<int>(a) | static_cast<int>(b)); }
inline IOS_OpenMode operator^(IOS_OpenMode a, IOS_OpenMode b)
{ return IOS_OpenMode(static_cast<int>(a) ^ static_cast<int>(b)); }
inline IOS_OpenMode& operator|=(IOS_OpenMode& a, IOS_OpenMode b)
{ return a = a | b; }
inline IOS_OpenMode& operator&=(IOS_OpenMode& a, IOS_OpenMode b)
{ return a = a & b; }
inline IOS_OpenMode& operator^=(IOS_OpenMode& a, IOS_OpenMode b)
{ return a = a ^ b; }
inline IOS_OpenMode operator~(IOS_OpenMode a)
{ return IOS_OpenMode(~static_cast<int>(a)); }
class IODeviceImpl;
/** @brief Endpoint for I/O operations
This class serves as the base class for all kinds of I/O devices. The
interface supports synchronous and asynchronous I/O operations, peeking
and seeking. I/O buffers and I/O streams within the cxxtools framework use
IODevices as endpoints and therefore fully feaured standard C++ compliant
IOStreams can be constructed at runtime.
Examples of %IODevices are the SerialDevice, the endpoints of a Pipe
or the FileDevice. A Selector can be used to wait on activity on an
%IODevice, which will send the %Signal inputReady or outputReady of the
%IODevice that is ready to perform I/O.
*/
class CXXTOOLS_API IODevice : public Selectable
{
public:
typedef std::char_traits<char>::pos_type pos_type;
typedef std::char_traits<char>::off_type off_type;
typedef std::ios_base::seekdir SeekDir;
typedef IOS_OpenMode OpenMode;
static const OpenMode Sync = IOS_Sync;
static const OpenMode Async = IOS_Async;
static const OpenMode Read = IOS_Read;
static const OpenMode Write = IOS_Write;
static const OpenMode AtEnd = IOS_AtEnd;
static const OpenMode Append = IOS_Append;
static const OpenMode Trunc = IOS_Trunc;
public:
//! @brief Destructor
virtual ~IODevice();
void beginRead(char* buffer, size_t n);
size_t endRead();
//! @brief Read data from I/O device
/*!
Reads up to n bytes and stores them in buffer. Returns the number
of bytes read, which may be less than requested and even 0 if the
device operates in asynchronous (non-blocking) mode. In case of
EOF the IODevice is set to eof.
\param buffer buffer where to place the data to be read.
\param n number of bytes to read
\return number of bytes read, which may be less than requested.
\throw IOError
*/
size_t read(char* buffer, size_t n);
size_t beginWrite(const char* buffer, size_t n);
size_t endWrite();
//! @brief Write data to I/O device
/**
Writes n bytes from buffer to this I/O device. Returns the number
of bytes written, which may be less than requested and even 0 if the
device operates in asynchronous (non-blocking) mode. In case of
EOF the IODevice is set to eof.
\param buffer buffer containing the data to be written.
\param n number of bytes that should be written.
\return number of bytes written, which may be less than requested.
\throw IOError
*/
size_t write(const char* buffer, size_t n);
/** @brief Cancels asynchronous reading and writing
*/
void cancel();
//! @brief Returns true if device is seekable
/**
Tests if the device is seekable.
\return true if the device is seekable, false otherwise.
*/
bool seekable() const;
//! @brief Move the next read position to the given offset
/**
Tries to move the current read position to the given offset.
SeekMode determines the relative starting point of offset.
\param offset the offset the pointer should be moved by.
\param mode determines the relative starting offset.
\return the new abosulte read positing.
\throw IOError
*/
pos_type seek(off_type offset, std::ios::seekdir sd);
//! @brief Read data from I/O device without consuming them
/**
Tries to extract up to n bytes from this object
without consuming them. The bytes are stored in
buffer, and the number of bytes peeked is returned.
\param buffer buffer where to place the data to be read.
\param n number of bytes to peek
\return number of bytes peek.
\throw IOError
*/
size_t peek(char* buffer, size_t n);
//! @brief Synchronize device
/**
Commits written data to physical device.
\throw IOError
*/
void sync();
//! @brief Returns the current I/O position
/**
The current I/O position is returned or an IOError
is thrown if the device is not seekable. Seekability
can be tested with BasicIODevice::seekable().
\throw IOError
*/
pos_type position();
//! @brief Returns if the device has reached EOF
/*!
Test if the I/O device has reached eof.
\return true if the I/O device has reached eof, false otherwise.
*/
bool eof() const;
/** @brief Returns true if the device operates in asynchronous mode
*/
bool async() const;
/** @brief Notifies about available data
This signal is send when the IODevice is monitored
in a Selector or EventLoop and data becomes available.
*/
Signal<IODevice&> inputReady;
/** @brief Notifies when data can be written
This signal is send when the IODevice is monitored
in a Selector or EventLoop and the device is ready
to write data.
*/
Signal<IODevice&> outputReady;
virtual IODeviceImpl& ioimpl() = 0;
bool reading() const
{ return _rbuf != 0; }
bool writing() const
{ return _wbuf != 0; }
char* rbuf() const
{ return _rbuf; }
size_t rbuflen() const
{ return _rbuflen; }
size_t ravail() const
{ return _ravail; }
const char* wbuf() const
{ return _wbuf; }
size_t wbuflen() const
{ return _wbuflen; }
size_t wavail() const
{ return _wavail; }
protected:
//! @brief Default Constructor
IODevice();
virtual size_t onBeginRead(char* buffer, size_t n, bool& eof) = 0;
virtual size_t onEndRead(bool& eof) = 0;
//! @brief Read bytes from device
virtual size_t onRead(char* buffer, size_t count, bool& eof) = 0;
virtual size_t onBeginWrite(const char* buffer, size_t n) = 0;
virtual size_t onEndWrite() = 0;
//! @brief Write bytes to device
virtual size_t onWrite(const char* buffer, size_t count) = 0;
virtual void onCancel() = 0;
//! @brief Read data from I/O device without consuming them
virtual size_t onPeek(char*, size_t)
{ return 0; }
//! @brief Returns true if device is seekable
virtual bool onSeekable() const
{ return false; }
//! @brief Move the next read position to the given offset
virtual pos_type onSeek(off_type, std::ios::seekdir)
{ throw IOError("Could not seek on device."); }
//! @brief Synchronize device
virtual void onSync() const
{ }
//! @brief Returns the size of the device
virtual size_t onSize() const
{ return 0; }
//! @brief Sets or unsets the device to eof
void setEof(bool eof);
//! @brief Sets or unsets the device to async
void setAsync(bool async);
private:
bool _eof;
bool _async;
protected:
char* _rbuf;
size_t _rbuflen;
size_t _ravail;
const char* _wbuf;
size_t _wbuflen;
size_t _wavail;
void* _reserved;
};
} // namespace cxxtools
#endif
|