summaryrefslogtreecommitdiff
path: root/indra/llcorehttp/bufferarray.h
blob: b26ad1b2976059f11d58f7ec9d819af5d90c9290 (plain)
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
/**
 * @file bufferarray.h
 * @brief Public-facing declaration for the BufferArray scatter/gather class
 *
 * $LicenseInfo:firstyear=2012&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 *
 * 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;
 * version 2.1 of the License only.
 *
 * 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
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef	_LLCORE_BUFFER_ARRAY_H_
#define	_LLCORE_BUFFER_ARRAY_H_


#include <cstdlib>
#include <vector>

#include "_refcounted.h"


namespace LLCore
{


/// A very simple scatter/gather type map for bulk data.  The motivation
/// for this class is the writedata callback used by libcurl.  Response
/// bodies are delivered to the caller in a sequence of sequential write
/// operations and this class captures them without having to reallocate
/// and move data.
///
/// The interface looks a little like a unix file descriptor but only
/// just.  There is a notion of a current position, starting from 0,
/// which is used as the position in the data when performing read and
/// write operations.  The position also moves after various operations:
/// - seek(...)
/// - read(...)
/// - write(...)
/// - append(...)
/// - appendBufferAlloc(...)
/// The object also keeps a total length value which is updated after
/// write and append operations and beyond which the current position
/// cannot be set.
///
/// Threading:  not thread-safe
///
/// Allocation:  Refcounted, heap only.  Caller of the constructor
/// is given a single refcount.
///
class BufferArray : public LLCoreInt::RefCounted
{
public:
	BufferArray();
	virtual ~BufferArray();

private:
	BufferArray(const BufferArray &);			// Not defined
	void operator=(const BufferArray &);		// Not defined

public:
	/// Appends the indicated data to the BufferArray
	/// modifying current position and total size.  New
	/// position is one beyond the final byte of the buffer.
	///
	/// @return			Count of bytes copied to BufferArray
	size_t append(const char * src, size_t len);

	/// Similar to @see append(), this call guarantees a
	/// contiguous block of memory of requested size placed
	/// at the current end of the BufferArray.  On return,
	/// the data in the memory is considered valid whether
	/// the caller writes to it or not.
	///
	/// @return			Pointer to contiguous region at end
	///					of BufferArray of 'len' size.
	char * appendBufferAlloc(size_t len);

	/// Current count of bytes in BufferArray instance.
	size_t size() const
		{
			return mLen;
		}

	/// Set the current position for subsequent read and
	/// write operations.  'pos' values before the beginning
	/// or greater than the size of the buffer are coerced
	/// to a value within the buffer.
	///
	/// @return			Actual current position after seek.
	size_t seek(size_t pos);

	/// Copies data from the current position in the instance
	/// to the caller's buffer.  Will return a short count of
	/// bytes copied if the 'len' extends beyond the data.
	size_t read(char * dst, size_t len);

	/// Copies data from the caller's buffer to the instance
	/// at the current position.  May overwrite existing data,
	/// append data when current position is equal to the
	/// size of the instance or do a mix of both.
	size_t write(const char * src, size_t len);
	
protected:
	int findBlock(size_t pos, size_t * ret_offset);
	
protected:
	class Block;
	typedef std::vector<Block *> container_t;

	container_t			mBlocks;
	size_t				mPos;
	size_t				mLen;
};  // end class BufferArray


#if 0

// Conceptual for now.  Another possibility is going with
// something like Boost::asio's buffers interface.  They're
// trying to achieve the same thing above and below....

class BufferStream : public std::streambuf
{
public:
	BufferStream(BufferArray * buffer);
	virtual ~BufferStream();

private:
	BufferStream(const BufferStream &);			// Not defined
	void operator=(const BufferStream &);		// Not defined

public:
	// Types
	typedef std::streambuf::pos_type pos_type;
	typedef std::streambuf::off_type off_type;

	virtual int underflow();

	virtual int overflow(int c);

	virtual int sync();

	virtual pos_type seekoff(off_type off, std::ios::seekdir way, std::ios::openmode which);

protected:
	BufferArray *		mBufferArray;
};  // end class BufferStream

#endif // 0

}  // end namespace LLCore

#endif	// _LLCORE_BUFFER_ARRAY_H_