2009-12-19 07:26:01 +00:00
|
|
|
#ifndef MANGLE_STREAM_INPUT_H
|
|
|
|
#define MANGLE_STREAM_INPUT_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2010-06-03 18:13:27 +00:00
|
|
|
#include "../tools/shared_ptr.hpp"
|
2010-01-01 10:31:09 +00:00
|
|
|
#include <assert.h>
|
2009-12-19 07:26:01 +00:00
|
|
|
|
|
|
|
namespace Mangle {
|
|
|
|
namespace Stream {
|
|
|
|
|
|
|
|
/// An abstract interface for a stream data.
|
2009-12-28 13:09:17 +00:00
|
|
|
class Stream
|
2009-12-19 07:26:01 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Feature options. These should be set in the constructor.
|
|
|
|
|
|
|
|
/// If true, seek() works
|
|
|
|
bool isSeekable;
|
|
|
|
|
|
|
|
/// If true, tell() works
|
|
|
|
bool hasPosition;
|
|
|
|
|
|
|
|
/// If true, size() works
|
|
|
|
bool hasSize;
|
|
|
|
|
2010-08-04 10:20:46 +00:00
|
|
|
/// If true, write() works. Writing through pointer operations is
|
2010-09-07 09:08:09 +00:00
|
|
|
/// not (yet) supported.
|
2010-08-04 10:20:46 +00:00
|
|
|
bool isWritable;
|
|
|
|
|
2010-09-07 09:08:09 +00:00
|
|
|
/// If true, read() and eof() works.
|
|
|
|
bool isReadable;
|
|
|
|
|
2010-01-01 10:31:09 +00:00
|
|
|
/// If true, the getPtr() functions work
|
|
|
|
bool hasPtr;
|
|
|
|
|
2010-09-07 09:08:09 +00:00
|
|
|
/// Initialize all bools to false by default, except isReadable.
|
2010-01-01 10:31:09 +00:00
|
|
|
Stream() :
|
|
|
|
isSeekable(false), hasPosition(false), hasSize(false),
|
2010-09-07 09:08:09 +00:00
|
|
|
isWritable(false), isReadable(true), hasPtr(false) {}
|
2010-01-01 10:31:09 +00:00
|
|
|
|
2009-12-19 07:26:01 +00:00
|
|
|
/// Virtual destructor
|
2009-12-28 13:09:17 +00:00
|
|
|
virtual ~Stream() {}
|
2009-12-19 07:26:01 +00:00
|
|
|
|
|
|
|
/** Read a given number of bytes from the stream. Returns the actual
|
|
|
|
number read. If the return value is less than count, then the
|
2010-09-07 09:08:09 +00:00
|
|
|
stream is empty or an error occured. Only required for readable
|
|
|
|
streams.
|
2009-12-19 07:26:01 +00:00
|
|
|
*/
|
2010-09-07 09:08:09 +00:00
|
|
|
virtual size_t read(void* buf, size_t count) { assert(0); return 0; }
|
2009-12-19 07:26:01 +00:00
|
|
|
|
2010-08-04 10:20:46 +00:00
|
|
|
/** Write a given number of bytes from the stream. Semantics is
|
2010-09-07 09:08:09 +00:00
|
|
|
similar to read(). Only valid if isWritable is true.
|
|
|
|
|
|
|
|
The returned value is the number of bytes written. However in
|
|
|
|
most cases, unlike for read(), a write-count less than requested
|
|
|
|
usually indicates an error. The implementation should throw such
|
|
|
|
errors as exceptions rather than expect the caller to handle
|
|
|
|
them.
|
2010-08-04 10:20:46 +00:00
|
|
|
|
|
|
|
Since most implementations do NOT support writing we default to
|
|
|
|
an assert(0) here.
|
|
|
|
*/
|
|
|
|
virtual size_t write(const void *buf, size_t count) { assert(0); return 0; }
|
|
|
|
|
2010-08-04 10:47:48 +00:00
|
|
|
/// Flush an output stream. Does nothing for non-writing streams.
|
|
|
|
virtual void flush() {}
|
|
|
|
|
2009-12-19 07:26:01 +00:00
|
|
|
/// Seek to an absolute position in this stream. Not all streams are
|
|
|
|
/// seekable.
|
2010-09-07 09:08:09 +00:00
|
|
|
virtual void seek(size_t pos) { assert(0); }
|
2009-12-19 07:26:01 +00:00
|
|
|
|
|
|
|
/// Get the current position in the stream. Non-seekable streams are
|
|
|
|
/// not required to keep track of this.
|
2010-09-07 09:08:09 +00:00
|
|
|
virtual size_t tell() const { assert(0); return 0; }
|
2009-12-19 07:26:01 +00:00
|
|
|
|
2010-09-07 09:08:09 +00:00
|
|
|
/// Return the total size of the stream. For streams hasSize is
|
|
|
|
/// false, size() should fail in some way, since it is an error to
|
|
|
|
/// call it in those cases.
|
|
|
|
virtual size_t size() const { assert(0); return 0; }
|
2009-12-19 07:26:01 +00:00
|
|
|
|
2010-09-07 09:08:09 +00:00
|
|
|
/// Returns true if the stream is empty. Required for readable
|
|
|
|
/// streams.
|
|
|
|
virtual bool eof() const { assert(0); return 0; }
|
2010-01-01 10:31:09 +00:00
|
|
|
|
|
|
|
/// Return a pointer to the entire stream. This function (and the
|
|
|
|
/// other getPtr() variants below) should only be implemented for
|
|
|
|
/// memory-based streams where using them would be an optimization.
|
2010-03-04 10:12:22 +00:00
|
|
|
virtual const void *getPtr() { assert(0); return NULL; }
|
2010-01-01 10:31:09 +00:00
|
|
|
|
|
|
|
/// Get a pointer to a memory region of 'size' bytes starting from
|
|
|
|
/// position 'pos'
|
2010-03-04 10:12:22 +00:00
|
|
|
virtual const void *getPtr(size_t pos, size_t size) { assert(0); return NULL; }
|
2010-01-04 10:55:38 +00:00
|
|
|
|
|
|
|
/// Get a pointer to a memory region of 'size' bytes from the
|
|
|
|
/// current position. Unlike the two other getPtr variants, this
|
|
|
|
/// will advance the position past the returned area.
|
2010-03-04 10:12:22 +00:00
|
|
|
virtual const void *getPtr(size_t size) { assert(0); return NULL; }
|
2009-12-19 07:26:01 +00:00
|
|
|
};
|
|
|
|
|
2009-12-31 14:37:01 +00:00
|
|
|
typedef boost::shared_ptr<Stream> StreamPtr;
|
|
|
|
|
2009-12-19 07:26:01 +00:00
|
|
|
}} // namespaces
|
|
|
|
#endif
|