]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSData.h
b306a247ab8ddac770b0160e530b7504dc69ca8a
[apple/xnu.git] / libkern / libkern / c++ / OSData.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* IOData.h created by rsulack on Wed 17-Sep-1997 */
26 /* IOData.h converted to C++ by gvdl on Fri 1998-10-30 */
27
28 #ifndef _OS_OSDATA_H
29 #define _OS_OSDATA_H
30
31 #include <libkern/c++/OSObject.h>
32
33 class OSString;
34
35 /*!
36 @class OSData
37 @abstract A container class to manage an array of bytes.
38 */
39 class OSData : public OSObject
40 {
41 OSDeclareDefaultStructors(OSData)
42
43 protected:
44 void *data;
45 unsigned int length;
46 unsigned int capacity;
47 unsigned int capacityIncrement;
48
49 struct ExpansionData { };
50
51 /*! @var reserved
52 Reserved for future use. (Internal use only) */
53 ExpansionData *reserved;
54
55 public:
56 /*!
57 @function withCapacity
58 @abstract A static constructor function to create and initialize an empty instance of OSData with a given capacity.
59 @param inCapacity The initial capacity of the OSData object in bytes.
60 @result Returns an instance of OSData or 0 if a failure occurs.
61 */
62 static OSData *withCapacity(unsigned int inCapacity);
63 /*!
64 @function withBytes
65 @abstract A static constructor function to create and initialize an instance of OSData and copies in the provided data.
66 @param bytes A buffer of data.
67 @param inLength The size of the given buffer.
68 @result Returns an instance of OSData or 0 if a failure occurs.
69 */
70 static OSData *withBytes(const void *bytes, unsigned int inLength);
71 /*!
72 @function withBytesNoCopy
73 @abstract A static constructor function to create and initialize an instance of OSData which references a buffer of data.
74 @param bytes A reference to a block of data.
75 @param inLength The size of the data block.
76 @result Returns an instance of OSData or 0 if a failure occurs.
77 */
78 static OSData *withBytesNoCopy(void *bytes, unsigned int inLength);
79 /*!
80 @function withData
81 @abstract A static constructor function to create and initialize an instance of OSData with the data provided.
82 @param inData An OSData object which provides the initial data.
83 @result Returns an instance of OSData or 0 if a failure occurs.
84 */
85 static OSData *withData(const OSData *inData);
86 /*!
87 @function withData
88 @abstract A static constructor function to create and initialize an instance of OSData with a specific range of the data provided.
89 @param inData An OSData object which provides the initial data.
90 @param start The starting index at which the data will be copied.
91 @param inLength The number of bytes to be copied starting at index 'start'.
92 @result Returns an instance of OSData or 0 if a failure occurs.
93 */
94 static OSData *withData(const OSData *inData,
95 unsigned int start, unsigned int inLength);
96
97 /*!
98 @function initWithBytes
99 @abstract A member function to initialize an instance of OSData with the provided data.
100 @param bytes A pointer to a block of data to be copied.
101 @param inLength The length of the block of data.
102 @result Returns true if initialization was successful, false otherwise.
103 */
104 virtual bool initWithCapacity(unsigned int inCapacity);
105 /*!
106 @function initWithBytes
107 @abstract A member function to initialize an instance of OSData which references a block of data.
108 @param bytes A reference to a block of data
109 @param inLength The length of the block of data.
110 @result Returns true if initialization was successful, false otherwise.
111 */
112 virtual bool initWithBytes(const void *bytes, unsigned int inLength);
113 /*!
114 @function initWithBytes
115 @abstract A member function to initialize an instance of OSData which references a block of data.
116 @param bytes A reference to a block of data
117 @param inLength The length of the block of data.
118 @result Returns true if initialization was successful, false otherwise.
119 */
120 virtual bool initWithBytesNoCopy(void *bytes, unsigned int inLength);
121 /*!
122 @function initWithData
123 @abstract A member function to initialize an instance of OSData with the data provided.
124 @param inData An OSData object which provides the data to be copied.
125 @result Returns true if initialization was successful, false otherwise.
126 */
127 virtual bool initWithData(const OSData *inData);
128 /*!
129 @function initWithData
130 @abstract A member function to initialize an instance of OSData with a specific range of the data provided
131 @param inData An OSData object.
132 @param start The starting range of data to be copied.
133 @param inLength The length in bytes of the data to be copied.
134 @result Returns true if initialization was successful, false otherwise.
135 */
136 virtual bool initWithData(const OSData *inData,
137 unsigned int start, unsigned int inLength);
138 /*!
139 @function free
140 @abstract A member function which releases all resources created or used by the OSData object.
141 @discussion Do not call this function directly, use release() instead.
142 */
143 virtual void free();
144
145 /*!
146 @function getLength
147 @abstract A member function which returns the length of the internal data buffer.
148 @result Returns an integer value for the length of data in the object's internal data buffer.
149 */
150 virtual unsigned int getLength() const;
151 /*!
152 @function getCapacity
153 @abstract A member function which returns the capacity of the internal data buffer.
154 @result Returns an integer value for the size of the object's internal data buffer.
155 */
156 virtual unsigned int getCapacity() const;
157 /*!
158 @function getCapacityIncrement
159 @abstract A member function which returns the size by which the data buffer will grow.
160 @result Returns the size by which the data buffer will grow.
161 */
162 virtual unsigned int getCapacityIncrement() const;
163 /*!
164 @function setCapacityIncrement
165 @abstract A member function which sets the growth size of the data buffer.
166 @result Returns the new growth size.
167 */
168 virtual unsigned int setCapacityIncrement(unsigned increment);
169 /*!
170 @function ensureCapacity
171 @abstract A member function which will expand the size of the collection to a given storage capacity.
172 @param newCapacity The new capacity for the data buffer.
173 @result Returns the new capacity of the data buffer or the previous capacity upon error.
174 */
175 virtual unsigned int ensureCapacity(unsigned int newCapacity);
176 /*!
177 @function appendBytes
178 @abstract A member function which appends a buffer of data onto the end of the object's internal data buffer.
179 @param bytes A pointer to the block of data.
180 @param inLength The length of the data block.
181 @result Returns true if the object was able to append the new data, false otherwise.
182 */
183 virtual bool appendBytes(const void *bytes, unsigned int inLength);
184 /*!
185 @function appendBytes
186 @abstract A member function which appends the data contained in an OSData object to the receiver.
187 @param other An OSData object.
188 @result Returns true if appending the new data was successful, false otherwise.
189 */
190 virtual bool appendBytes(const OSData *other);
191
192 /*!
193 @function getBytesNoCopy
194 @abstract A member function to return a pointer to the OSData object's internal data buffer.
195 @result Returns a reference to the OSData object's internal data buffer.
196 */
197 virtual const void *getBytesNoCopy() const;
198 /*!
199 @function getBytesNoCopy
200 @abstract Returns a reference into the OSData object's internal data buffer at particular offset and with a particular length.
201 @param start The offset from the base of the internal data buffer.
202 @param inLength The length of window.
203 @result Returns a pointer at a particular offset into the data buffer, or 0 if the starting offset or length are not valid.
204 */
205 virtual const void *getBytesNoCopy(unsigned int start,
206 unsigned int inLength) const;
207
208 /*!
209 @function isEqualTo
210 @abstract A member function to test the equality of two OSData objects.
211 @param aData The OSData object to be compared to the receiver.
212 @result Returns true if the two objects are equivalent, false otherwise.
213 */
214 virtual bool isEqualTo(const OSData *aData) const;
215 /*!
216 @function isEqualTo
217 @abstract A member function to test the equality of an arbitrary block of data with the OSData object's internal data buffer.
218 @param someData A pointer to a block of data.
219 @param inLength The length of the block of data.
220 @result Returns true if the two blocks of data are equivalent, false otherwise.
221 */
222 virtual bool isEqualTo(const void *someData, unsigned int inLength) const;
223 /*!
224 @function isEqualTo
225 @abstract A member function to test the equality between an OSData object and an arbitrary OSObject derived object.
226 @param obj An OSObject derived object.
227 @result Returns true if the two objects are equivalent.
228 */
229 virtual bool isEqualTo(const OSMetaClassBase *obj) const;
230 /*!
231 @function isEqualTo
232 @abstract A member function to test the equality between an OSData object and an OSString object.
233 @param obj An OSString object
234 @result Returns true if the two objects are equivalent.
235 */
236 virtual bool isEqualTo(const OSString *obj) const;
237 /*!
238 @function serialize
239 @abstract A member function which archives the receiver.
240 @param s The OSSerialize object.
241 @result Returns true if serialization was successful, false if not.
242 */
243 virtual bool serialize(OSSerialize *s) const;
244
245 /*!
246 @function appendByte
247 @abstract A member function which appends a buffer of constant data onto the end of the object's internal data buffer.
248 @param byte A byte value to replicate as the added data.
249 @param inCount The length of the data to add.
250 @result Returns true if the object was able to append the new data, false otherwise.
251 */
252 virtual bool appendByte(unsigned char byte, unsigned int inCount);
253
254
255 OSMetaClassDeclareReservedUnused(OSData, 0);
256 OSMetaClassDeclareReservedUnused(OSData, 1);
257 OSMetaClassDeclareReservedUnused(OSData, 2);
258 OSMetaClassDeclareReservedUnused(OSData, 3);
259 OSMetaClassDeclareReservedUnused(OSData, 4);
260 OSMetaClassDeclareReservedUnused(OSData, 5);
261 OSMetaClassDeclareReservedUnused(OSData, 6);
262 OSMetaClassDeclareReservedUnused(OSData, 7);
263 };
264
265 #endif /* !_OS_OSDATA_H */