1//----------------------------------------------------------------------
2//  This software is part of the OpenBeOS distribution and is covered
3//  by the OpenBeOS license.
4//
5//  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6//----------------------------------------------------------------------
7
8/*! \file SimulatedStream.h
9*/
10
11#ifndef _SIMULATED_STREAM_H
12#define _SIMULATED_STREAM_H
13
14#include "DataStream.h"
15
16/*! \brief Abstract DataStream wrapper around another DataStream and a sequence of
17	extents in said stream that allows for easy write access to said sequence
18	of extents as though they were a continuous chunk of data.
19
20	NOTE: The SimulatedStream object never modifies the data stream position
21	of the underlying data stream; all read/write/zero calls use the underlying
22	stream's ReadAt()/WriteAt()/ZeroAt() functions.
23*/
24class SimulatedStream : public DataStream {
25public:
26	SimulatedStream(DataStream &stream);
27	virtual status_t InitCheck() const;
28
29	virtual ssize_t Read(void *buffer, size_t size);
30	virtual	ssize_t ReadAt(off_t pos, void *buffer, size_t size);
31
32	virtual ssize_t Write(const void *buffer, size_t size);
33	virtual ssize_t WriteAt(off_t pos, const void *buffer, size_t size);
34
35	virtual ssize_t Write(BDataIO &data, size_t size);
36	virtual ssize_t	WriteAt(off_t pos, BDataIO &data, size_t size);
37
38	virtual ssize_t Zero(size_t size);
39	virtual ssize_t	ZeroAt(off_t pos, size_t size);
40
41	virtual off_t Seek(off_t pos, uint32 seek_mode);
42	virtual off_t Position() const { return fPosition; }
43
44	virtual status_t SetSize(off_t size) { return B_ERROR; }
45
46protected:
47	struct data_extent {
48		data_extent(off_t offset = 0, size_t size = 0)
49			: offset(offset)
50			, size(size)
51		{
52		}
53
54		off_t offset;
55		size_t size;
56	};
57
58	/*! \brief Should be implemented to return (via the output parameter
59		\a extent) the largest extent in the underlying data stream corresponding
60		to the extent in the simulated data stream starting at byte position
61		\a pos of byte length \a size.
62
63		NOTE: If the position is at or beyond the end of the simulated stream, the
64		function should return B_OK, and the value of extent.size should be 0.
65	*/
66	virtual status_t _GetExtent(off_t pos, size_t size, data_extent &extent) = 0;
67
68	/*! \brief Should be implemented to return the current size of the stream.
69	*/
70	virtual off_t _Size() = 0;
71
72private:
73	off_t fPosition;
74	DataStream &fStream;
75};
76
77#endif	// _SIMULATED_STREAM_H
78