Added getPtr() family to Stream

actorid
Nicolay Korslund 15 years ago
parent c45170f420
commit 38501777b0

@ -22,6 +22,11 @@ class SliceStream : public Stream
// Make sure we can actually fit inside the source stream
assert(src->size() <= offset+length);
isSeekable = true;
hasPosition = true;
hasSize = true;
hasPtr = src->hasPtr;
}
size_t read(void *buf, size_t count)
@ -48,6 +53,21 @@ class SliceStream : public Stream
bool eof() { return pos == length; }
size_t tell() { return pos; }
size_t size() { return length; }
void *getPtr() { return getPtr(0, length); }
void *getPtr(size_t size) { return getPtr(pos, size); }
void *getPtr(size_t pos, size_t size)
{
// Boundry checks on pos and size. Bounding the size is
// important even when getting pointers, as the source stream
// may use the size parameter for something (such as memory
// mapping or buffering.)
if(pos > length) pos = length;
if(pos+size > length) size = length-pos;
// Ask the source to kindly give us a pointer
return src->getPtr(offset+pos, size);
}
};
typedef boost::shared_ptr<SliceStream> SliceStreamPtr;

@ -37,6 +37,7 @@ class MemoryStream : public Stream
isSeekable = true;
hasPosition = true;
hasSize = true;
hasPtr = true;
}
size_t read(void *buf, size_t count)
@ -69,6 +70,15 @@ class MemoryStream : public Stream
size_t size() const { return length; }
bool eof() const { return pos == length; }
/// Get the base pointer to the entire buffer
const void *getPtr() const { return data; }
const void *getPtr(size_t size) const { return ((char*)data)+pos; }
const void *getPtr(size_t pos, size_t size)
{
if(pos > length) pos = length;
return ((char*)data)+pos;
}
// New members in MemoryStream:
/// Set a new buffer and length. This will rewind the position to zero.
@ -79,9 +89,6 @@ class MemoryStream : public Stream
pos = 0;
}
/// Get the base pointer to the entire buffer
const void *getPtr() const { return data; }
/// Clone this memory stream
/** Make a new stream of the same buffer. If setPos is true, we also
set the clone's position to be the same as ours.

@ -3,6 +3,7 @@
#include <stdlib.h>
#include "../tools/shared_ptr.h"
#include <assert.h>
namespace Mangle {
namespace Stream {
@ -22,6 +23,14 @@ class Stream
/// If true, size() works
bool hasSize;
/// If true, the getPtr() functions work
bool hasPtr;
/// Initialize all bools to false by default
Stream() :
isSeekable(false), hasPosition(false), hasSize(false),
hasPtr(false) {}
/// Virtual destructor
virtual ~Stream() {}
@ -45,6 +54,19 @@ class Stream
/// Returns true if the stream is empty
virtual bool eof() const = 0;
/// 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.
virtual void *getPtr() const { assert(0); }
/// Get a pointer to a memory region of 'size' bytes from the
/// current position.
virtual void *getPtr(size_t size) const { assert(0); }
/// Get a pointer to a memory region of 'size' bytes starting from
/// position 'pos'
virtual void *getPtr(size_t pos, size_t size) const { assert(0); }
};
typedef boost::shared_ptr<Stream> StreamPtr;

Loading…
Cancel
Save