2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* OSSerialize.h created by rsulack on Wen 25-Nov-1998 */
30 #ifndef _OS_OSSERIALIZE_H
31 #define _OS_OSSERIALIZE_H
33 #include <libkern/c++/OSObject.h>
43 * This header declares the OSSerialize class.
51 * OSSerialize coordinates serialization of Libkern C++ objects
55 * This class is for the most part internal to the OSContainer classes,
56 * used for transferring property tables between the kernel and user space.
57 * It should not be used directly.
58 * Classes that participate in serialization
61 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
62 * OSObject::serialize@/link</code> .
65 * <b>Use Restrictions</b>
67 * With very few exceptions in the I/O Kit, all Libkern-based C++
68 * classes, functions, and macros are <b>unsafe</b>
69 * to use in a primary interrupt context.
70 * Consult the I/O Kit documentation related to primary interrupts
71 * for more information.
73 * OSSerialize provides no concurrency protection;
74 * it's up to the usage context to provide any protection necessary.
75 * Some portions of the I/O Kit, such as
76 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
77 * handle synchronization via defined member functions
78 * for serializing properties.
82 OSUnserializeBinary(const void *buffer
, size_t bufferSize
);
84 class OSSerialize
: public OSObject
86 OSDeclareDefaultStructors(OSSerialize
)
87 friend class OSBoolean
;
90 char * data
; // container for serialized data
91 unsigned int length
; // of serialized data (counting NULL)
92 unsigned int capacity
; // of container
93 unsigned int capacityIncrement
; // of container
96 OSDictionary
* tags
; // tags for all objects seen
98 struct ExpansionData
{ };
100 /* Reserved for future use. (Internal use only) */
101 ExpansionData
*reserved
;
103 #ifdef XNU_KERNEL_PRIVATE
105 typedef const OSMetaClassBase
* (*Editor
)(void * reference
,
107 OSCollection
* container
,
108 const OSSymbol
* name
,
109 const OSMetaClassBase
* value
);
111 typedef void * Editor
;
120 bool binarySerialize(const OSMetaClassBase
*o
);
121 bool addBinary(const void * data
, size_t size
);
122 bool addBinaryObject(const OSMetaClassBase
* o
, uint32_t key
, const void * _bits
, size_t size
);
127 * @function withCapacity
130 * Creates and initializes an empty OSSerialize object.
132 * @param capacity The initial size of the XML buffer.
135 * A new instance of OSSerialize
136 * with a retain count of 1;
137 * <code>NULL</code> on failure.
140 * The serializer will grow as needed to accommodate more data.
142 static OSSerialize
* withCapacity(unsigned int capacity
);
144 static OSSerialize
* binaryWithCapacity(unsigned int inCapacity
, Editor editor
= 0, void * reference
= 0);
150 * Returns the XML text serialized so far.
153 * The nul-terminated XML data serialized so far.
155 virtual char * text() const;
159 * @function clearText
162 * Resets the OSSerialize object.
165 * This function is a useful optimization if you are serializing
166 * the same object repeatedly.
168 virtual void clearText();
170 // stuff to serialize your object
173 * @function previouslySerialized
176 * Checks whether the object has already been serialized
177 * into the XML stream, emitting a reference if it has.
179 * @param object The object to check.
182 * <code>true</code> if <code>object</code> has already been serialized
183 * by this OSSerialize object and a reference
184 * to it is successfully added to the XML stream,
185 * <code>false</code> otherwise.
189 * This function both reduces the size of generated XML
190 * by emitting shorter references to existing objects with the same
191 * value (particularly for OSString, OSSymbol, and OSData),
192 * and also preserves instance references
193 * so that the user-space I/O Kit library can reconstruct
194 * an identical graph of object relationships.
196 * All classes that override
198 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
199 * OSObject::serialize@/link</code>.
200 * should call this function before doing any actual serialization;
201 * if it returns <code>true</code>, the <code>serialize</code> implementation
202 * can immediately return <code>true</code>.
204 virtual bool previouslySerialized(const OSMetaClassBase
* object
);
208 * @function addXMLStartTag
211 * Appends an XML start tag to the XML stream.
213 * @param object The object being serialized.
214 * @param tagString The name of the XML tag to emit; for example, "string".
217 * <code>true</code> if an XML start tag for <code>tagString</code>
218 * is successfully added to the XML stream, <code>false</code> otherwise.
221 * This function emits the named tag,
222 * enclosed within a pair of angle brackets.
224 * A class that implements serialization should call this function
225 * with the name of the XML tag that best represents the serialized
226 * contents of the object.
227 * A limited number of tags are supported by the user-space
238 * A call to this function must be balanced with one to
239 * <code>@link addXMLEndTag addXMLEndTag@/link</code>
240 * using the same <code>tagString</code>.
242 virtual bool addXMLStartTag(
243 const OSMetaClassBase
* object
,
244 const char * tagString
);
248 * @function addXMLEndTag
251 * Appends an XML end tag to the XML stream.
253 * @param tagString The name of the XML tag to emit; for example, "string".
256 * <code>true</code> if an XML end tag for <code>tagString</code>
257 * is successfully added to the XML stream, <code>false</code> otherwise.
260 * This function emits the named tag,
261 * preceded by a slash character to indicate the closing of an entity,
262 * all enclosed within a pair of angle brackets.
264 * A call to this function must balance an earlier call to
265 * <code>@link addXMLStartTag addXMLStartTag@/link</code>
266 * using the same <code>tagString</code>.
268 virtual bool addXMLEndTag(const char * tagString
);
275 * Appends a single character to the XML stream.
277 * @param aChar The character to append to the XML stream.
280 * <code>true</code> if <code>char</code>
281 * is successfully added to the XML stream, <code>false</code> otherwise.
283 virtual bool addChar(const char aChar
);
287 * @function addString
290 * Appends a C string to the XML stream.
292 * @param cString The C string to append to the XML stream.
295 * <code>true</code> if <code>cString</code>
296 * is successfully added to the XML stream, <code>false</code> otherwise.
298 virtual bool addString(const char * cString
);
300 // stuff you should never have to use (in theory)
302 virtual bool initWithCapacity(unsigned int inCapacity
);
303 virtual unsigned int getLength() const;
304 virtual unsigned int getCapacity() const;
305 virtual unsigned int getCapacityIncrement() const;
306 virtual unsigned int setCapacityIncrement(unsigned increment
);
307 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
308 virtual void free() APPLE_KEXT_OVERRIDE
;
310 OSMetaClassDeclareReservedUnused(OSSerialize
, 0);
311 OSMetaClassDeclareReservedUnused(OSSerialize
, 1);
312 OSMetaClassDeclareReservedUnused(OSSerialize
, 2);
313 OSMetaClassDeclareReservedUnused(OSSerialize
, 3);
314 OSMetaClassDeclareReservedUnused(OSSerialize
, 4);
315 OSMetaClassDeclareReservedUnused(OSSerialize
, 5);
316 OSMetaClassDeclareReservedUnused(OSSerialize
, 6);
317 OSMetaClassDeclareReservedUnused(OSSerialize
, 7);
320 // xx-review: this whole class seems to be unused!
322 typedef bool (*OSSerializerCallback
)(void * target
, void * ref
,
323 OSSerialize
* serializer
);
325 class OSSerializer
: public OSObject
327 OSDeclareDefaultStructors(OSSerializer
)
331 OSSerializerCallback callback
;
335 static OSSerializer
* forTarget(
337 OSSerializerCallback callback
,
340 virtual bool serialize(OSSerialize
* serializer
) const APPLE_KEXT_OVERRIDE
;
343 #endif /* _OS_OSSERIALIZE_H */