libcommonc++  0.7
Stream.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Stream_hxx
24 #define __ccxx_Stream_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/Buffer.h++>
30 
31 namespace ccxx {
32 
33 class AsyncIOTask; // fwd decl
34 
36 enum IOMode {
43 };
44 
46 enum SeekMode {
53 };
54 
61 {
62  friend class Process;
63 
64  public:
65 
70  static const uint_t MAX_IOBLOCK_COUNT;
71 
72  protected:
73 
75  Stream();
76 
85  Stream(FileHandle handle, bool seekable = true, bool readable = true,
86  bool writable = true);
87 
88  public:
89 
91  virtual ~Stream();
92 
101  virtual size_t read(ByteBuffer& buffer);
102 
111  virtual size_t read(CharBuffer& buffer);
112 
135  template<typename T> size_t read(Buffer<T>& buffer, size_t& partial)
136  {
137  size_t n = _readElem(reinterpret_cast<byte_t *>(buffer.getPointer()),
138  sizeof(T), buffer.getRemaining(), partial);
139 
140  if(n > 0)
141  buffer.bump(n);
142 
143  return(n);
144  }
145 
155  virtual size_t read(byte_t* buffer, size_t buflen);
156 
168  size_t read(byte_t* buffer, size_t buflen, int64_t offset,
169  AsyncIOTask& task);
170 
182  size_t read(ByteBuffer& buffer, int64_t offset, AsyncIOTask& task);
183 
195  size_t write(const byte_t* buffer, size_t buflen, int64_t offset,
196  AsyncIOTask& task);
197 
209  size_t write(ByteBuffer& buffer, int64_t offset, AsyncIOTask& task);
210 
223  virtual size_t readFully(ByteBuffer& buffer, size_t count = 0);
224 
237  virtual size_t readFully(CharBuffer& buffer, size_t count = 0);
238 
262  template<typename T> size_t readFully(Buffer<T>& buffer, size_t& partial)
263  {
264  size_t n = _readElemFully(reinterpret_cast<byte_t *>(buffer.getPointer()),
265  sizeof(T), buffer.getRemaining(), partial);
266 
267  if(n > 0)
268  buffer.bump(n);
269 
270  return(n);
271  }
272 
283  virtual size_t readFully(byte_t* buffer, size_t buflen);
284 
296  virtual size_t read(MemoryBlock* vec, uint_t count);
297 
305  virtual size_t write(ByteBuffer& buffer);
306 
314  virtual size_t write(CharBuffer& buffer);
315 
337  template<typename T> size_t write(Buffer<T>& buffer, size_t& partial)
338  {
339  size_t n = _writeElem(
340  reinterpret_cast<const byte_t *>(buffer.getPointer()),
341  sizeof(T), buffer.getRemaining(), partial);
342 
343  if(n > 0)
344  buffer.bump(n);
345 
346  return(n);
347  }
348 
358  virtual size_t write(const byte_t* buffer, size_t buflen);
359 
368  virtual size_t writeFully(ByteBuffer& buffer);
369 
378  virtual size_t writeFully(CharBuffer& buffer);
379 
402  template<typename T> size_t writeFully(Buffer<T>& buffer, size_t& partial)
403  {
404  size_t n = _writeElemFully(
405  reinterpret_cast<const byte_t *>(buffer.getPointer()),
406  sizeof(T), buffer.getRemaining(), partial);
407 
408  if(n > 0)
409  buffer.bump(n);
410 
411  return(n);
412  }
413 
422  virtual size_t writeFully(const byte_t* buffer, size_t buflen);
423 
434  virtual size_t write(const MemoryBlock* vec, uint_t count);
435 
445  virtual int64_t seek(int64_t offset, SeekMode mode = SeekAbsolute);
446 
454  virtual int64_t tell();
455 
457  inline bool isOpen() const
458  { return(_canRead || _canWrite); }
459 
461  inline bool isSeekable() const
462  { return(_seekable); }
463 
465  inline bool isReadable() const
466  { return(_canRead); }
467 
469  inline bool isWritable() const
470  { return(_canWrite); }
471 
476  inline bool isFullDuplex() const
477  { return(_canRead && _canWrite); }
478 
483  inline bool isHalfDuplex() const
484  { return(! _canRead || ! _canWrite); }
485 
491  virtual void close(IOMode mode = IOReadWrite);
492 
498  virtual void setTimeout(timespan_ms_t timeout);
499 
501  inline timespan_ms_t getTimeout() const
502  { return(_timeout); }
503 
504  protected:
505 
514  void _init(FileHandle handle, bool seekable, bool readable, bool writable);
515 
517  FileHandle _handle;
518  bool _seekable;
519  bool _canRead;
520  bool _canWrite;
521  timespan_ms_t _timeout;
524  private:
525 
526  size_t _readAsync(byte_t* buffer, size_t buflen, int64_t offset,
527  AsyncIOTask& task);
528  size_t _writeAsync(const byte_t* buffer, size_t buflen, int64_t offset,
529  AsyncIOTask& task);
530 
531  size_t _readElem(byte_t* buffer, size_t size, size_t nelem, size_t& partial);
532  size_t _readElemFully(byte_t* buffer, size_t size, size_t nelem,
533  size_t& partial);
534 
535  size_t _writeElem(const byte_t* buffer, size_t size, size_t nelem,
536  size_t& partial);
537  size_t _writeElemFully(const byte_t* buffer, size_t size, size_t nelem,
538  size_t& partial);
539 
541 };
542 
543 } // namespace ccxx
544 
545 #endif // __ccxx_Stream_hxx
Write-only mode.
Definition: Stream.h++:40
Seek from the current stream position.
Definition: Stream.h++:50
IOMode
Stream I/O modes.
Definition: Stream.h++:36
An encapsulation of a block of memory.
Definition: MemoryBlock.h++:37
T * getPointer()
Get a pointer to the element at the current position.
Definition: Buffer.h++:170
Seek from the beginning of the stream.
Definition: Stream.h++:48
An unbuffered I/O stream.
Definition: Stream.h++:60
bool isWritable() const
Test if the stream can be written to.
Definition: Stream.h++:469
static const uint_t MAX_IOBLOCK_COUNT
The maximum number of I/O buffers that can be passed to the vector I/O methods.
Definition: Stream.h++:70
bool isFullDuplex() const
Test if the stream is full-duplex (i.e., supports both reading and writing).
Definition: Stream.h++:476
size_t write(Buffer< T > &buffer, size_t &partial)
Write data to the stream from a Buffer of arbitrary type.
Definition: Stream.h++:337
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
int FileHandle
Definition: Common.h++:225
SeekMode
Stream seek modes.
Definition: Stream.h++:46
Read/write mode.
Definition: Stream.h++:42
Read-only mode.
Definition: Stream.h++:38
Seek backward from the end of the stream.
Definition: Stream.h++:52
uint_t getRemaining() const
Get the number of elements available to be read or written.
Definition: Buffer.h++:186
bool isReadable() const
Test if the stream can be read from.
Definition: Stream.h++:465
size_t writeFully(Buffer< T > &buffer, size_t &partial)
Write data to the stream from a Buffer of arbitrary type, until either the entire buffer is written o...
Definition: Stream.h++:402
bool isSeekable() const
Test if the stream supports seeking.
Definition: Stream.h++:461
uint_t bump(uint_t delta)
Bump (advance) the position by the given number of elements, or to the limit, whichever occurs first...
size_t readFully(Buffer< T > &buffer, size_t &partial)
Read data from the stream into a Buffer of arbitrary type, until either the buffer is full or an erro...
Definition: Stream.h++:262
#define COMMONCPP_API
Definition: Common.h++:126
A buffer for storing a contiguous sequence of elements.
Definition: Buffer.h++:44
A system process.
Definition: Process.h++:158
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
int timespan_ms_t
A timespan expressed in milliseconds.
Definition: Integers.h++:104
bool isHalfDuplex() const
Test if the stream is half-duplex (i.e., supports either reading or writing, but not both)...
Definition: Stream.h++:483
size_t read(Buffer< T > &buffer, size_t &partial)
Read data from the stream into a Buffer of arbitrary type.
Definition: Stream.h++:135
Definition: AllocationMap.c++:25
timespan_ms_t getTimeout() const
Get the stream I/O timeout, in milliseconds.
Definition: Stream.h++:501
bool isOpen() const
Test if the stream is open.
Definition: Stream.h++:457
An object representing an asynchronous I/O operation.
Definition: AsyncIOTask.h++:43
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68