6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
35 // attribute streams allow copying/filtering/transformation of attributes
36 // between file and/or memory nodes
38 // for example one can use constructs of nodes like:
40 // destinationNode << transformer << buffer << filter << sourceNode
42 // transformer may for instance perform endian-swapping or offsetting of
43 // a B_RECT attribute filter may withold certain attributes buffer is a
44 // memory allocated snapshot of attributes, may be repeatedly streamed into
45 // other files, buffers
47 // In addition to the whacky (but usefull) << syntax, calls like Read, Write
49 #ifndef _ATTRIBUTE_STREAM_H
50 #define _ATTRIBUTE_STREAM_H
53 #include <ObjectList.h>
57 #include <TypeConstants.h>
64 struct AttributeTemplate
{
65 // used for read-only attribute source
66 const char* fAttributeName
;
67 uint32 fAttributeType
;
74 // utility class for internal attribute description
77 AttributeInfo(const AttributeInfo
& other
);
78 AttributeInfo(const char* name
, attr_info info
);
79 AttributeInfo(const char* name
, uint32 type
, off_t size
);
81 void SetTo(const AttributeInfo
& other
);
82 void SetTo(const char* name
, attr_info info
);
83 void SetTo(const char* name
, uint32 type
, off_t size
);
84 const char* Name() const;
94 class AttributeStreamNode
{
96 AttributeStreamNode();
97 virtual ~AttributeStreamNode();
99 AttributeStreamNode
&operator<<(AttributeStreamNode
&source
);
101 // to the outside makes this node a part of the stream, passing on
102 // any data it has, gets, transforms, doesn't filter out
104 // under the hood sets up streaming into the next node; hooking
105 // up source and destination, forces the stream head to start
108 virtual void Rewind();
109 // get ready to start all over again
110 virtual void MakeEmpty() {}
111 // remove any attributes the node may have
113 virtual off_t
Contains(const char*, uint32
);
114 // returns size of attribute if found
116 virtual off_t
Read(const char* name
, const char* foreignName
,
117 uint32 type
, off_t size
, void* buffer
, void (*swapFunc
)(void*) = 0);
118 // read from this node
119 virtual off_t
Write(const char* name
, const char* foreignName
,
120 uint32 type
, off_t size
, const void* buffer
);
121 // write to this node
124 virtual bool Drive();
125 // node at the head of the stream makes the entire stream
127 virtual const AttributeInfo
* Next();
128 // give me the next attribute in the stream
129 virtual const char* Get();
130 // give me the data of the attribute in the stream that was just
131 // returned by Next assumes there is a buffering node somewhere on the
132 // way to the source, from which the resulting buffer is borrowed
133 virtual bool Fill(char* buffer
) const;
134 // fill the buffer with data of the attribute in the stream that was
135 // just returned by next <buffer> is big enough to hold the entire
138 virtual bool CanFeed() const { return false; }
139 // return true if can work as a source for the entire stream
143 // utility call, used to start up the stream by finding the ultimate
144 // target of the stream and calling Drive on it
149 AttributeStreamNode
* fReadFrom
;
150 AttributeStreamNode
* fWriteTo
;
154 class AttributeStreamFileNode
: public AttributeStreamNode
{
155 // handles reading and writing attributes to and from the
158 AttributeStreamFileNode();
159 AttributeStreamFileNode(BNode
*);
161 virtual void MakeEmpty();
162 virtual void Rewind();
163 virtual off_t
Contains(const char* name
, uint32 type
);
164 virtual off_t
Read(const char* name
, const char* foreignName
, uint32 type
,
165 off_t size
, void* buffer
, void (*swapFunc
)(void*) = 0);
166 virtual off_t
Write(const char* name
, const char* foreignName
, uint32 type
,
167 off_t size
, const void* buffer
);
171 BNode
* Node() { return fNode
; }
174 virtual bool CanFeed() const { return true; }
176 virtual bool Drive();
177 // give me all the attributes, I'll write them into myself
178 virtual const AttributeInfo
* Next();
179 // return the info for the next attribute I can read for you
180 virtual const char* Get();
181 virtual bool Fill(char* buffer
) const;
184 AttributeInfo fCurrentAttr
;
187 typedef AttributeStreamNode _inherited
;
191 class AttributeStreamMemoryNode
: public AttributeStreamNode
{
192 // in memory attribute buffer; can be both target of writing and source
193 // of reading at the same time
195 AttributeStreamMemoryNode();
197 virtual void MakeEmpty();
198 virtual off_t
Contains(const char* name
, uint32 type
);
199 virtual off_t
Read(const char* name
, const char* foreignName
,
200 uint32 type
, off_t size
, void* buffer
, void (*swapFunc
)(void*) = 0);
201 virtual off_t
Write(const char* name
, const char* foreignName
,
202 uint32 type
, off_t size
, const void* buffer
);
205 virtual bool CanFeed() const { return true; }
206 virtual void Rewind();
207 virtual bool Drive();
208 virtual const AttributeInfo
* Next();
209 virtual const char* Get();
210 virtual bool Fill(char* buffer
) const;
214 AttrNode(const char* name
, uint32 type
, off_t size
, char* data
)
216 fAttr(name
, type
, size
),
231 virtual AttrNode
* BufferingGet();
232 virtual AttrNode
* BufferingGet(const char* name
, uint32 type
, off_t size
);
233 int32
Find(const char* name
, uint32 type
) const;
236 BObjectList
<AttrNode
> fAttributes
;
239 typedef AttributeStreamNode _inherited
;
243 class AttributeStreamTemplateNode
: public AttributeStreamNode
{
244 // in read-only memory attribute source
245 // can only be used as a source for Next and Get
247 AttributeStreamTemplateNode(const AttributeTemplate
*, int32 count
);
249 virtual off_t
Contains(const char* name
, uint32 type
);
252 virtual bool CanFeed() const { return true; }
253 virtual void Rewind();
254 virtual const AttributeInfo
* Next();
255 virtual const char* Get();
256 virtual bool Fill(char* buffer
) const;
258 int32
Find(const char* name
, uint32 type
) const;
261 AttributeInfo fCurrentAttr
;
262 const AttributeTemplate
* fAttributes
;
266 typedef AttributeStreamNode _inherited
;
270 class AttributeStreamFilterNode
: public AttributeStreamNode
{
271 // filter node may not pass thru specified attributes
273 AttributeStreamFilterNode()
275 virtual off_t
Contains(const char* name
, uint32 type
);
276 virtual off_t
Read(const char* name
, const char* foreignName
,
277 uint32 type
, off_t size
, void* buffer
, void (*swapFunc
)(void*) = 0);
278 virtual off_t
Write(const char* name
, const char* foreignName
,
279 uint32 type
, off_t size
, const void* buffer
);
282 virtual bool Reject(const char* name
, uint32 type
, off_t size
);
283 // override to implement filtering
284 virtual const AttributeInfo
* Next();
287 typedef AttributeStreamNode _inherited
;
291 class NamesToAcceptAttrFilter
: public AttributeStreamFilterNode
{
292 // filter node that only passes thru attributes that match
295 NamesToAcceptAttrFilter(const char**);
298 virtual bool Reject(const char* name
, uint32 type
, off_t size
);
301 const char** fNameList
;
305 class SelectiveAttributeTransformer
: public AttributeStreamNode
{
306 // node applies a transformation on specified attributes
308 SelectiveAttributeTransformer(const char* attributeName
,
309 bool (*)(const char*, uint32
, off_t
, void*, void*), void* params
);
310 virtual ~SelectiveAttributeTransformer();
312 virtual off_t
Read(const char* name
, const char* foreignName
, uint32 type
,
313 off_t size
, void* buffer
, void (*swapFunc
)(void*) = 0);
315 virtual void Rewind();
318 virtual bool WillTransform(const char* name
, uint32 type
, off_t size
,
319 const char* data
) const;
320 // override to implement filtering, should only return true if
321 // transformation will occur
322 virtual char* CopyAndApplyTransformer(const char* name
, uint32 type
,
323 off_t size
, const char* data
);
324 // makes a copy of data
325 virtual bool ApplyTransformer(const char* name
, uint32 type
, off_t size
,
327 // transforms in place
328 virtual const AttributeInfo
* Next();
329 virtual const char* Get();
332 AttributeInfo fCurrentAttr
;
333 const char* fAttributeNameToTransform
;
334 bool (*fTransformFunc
)(const char*, uint32
, off_t
, void*, void*);
335 void* fTransformParams
;
337 BObjectList
<char> fTransformedBuffers
;
339 typedef AttributeStreamNode _inherited
;
343 template <class Type
>
344 class AttributeStreamConstValue
: public AttributeStreamNode
{
346 AttributeStreamConstValue(const char* name
, uint32 attributeType
,
350 virtual bool CanFeed() const { return true; }
351 virtual void Rewind() { fRewound
= true; }
352 virtual const AttributeInfo
* Next();
353 virtual const char* Get();
354 virtual bool Fill(char* buffer
) const;
356 int32
Find(const char* name
, uint32 type
) const;
363 typedef AttributeStreamNode _inherited
;
368 AttributeStreamConstValue
<Type
>::AttributeStreamConstValue(const char* name
,
369 uint32 attributeType
, Type value
)
371 fAttr(name
, attributeType
, sizeof(Type
)),
380 AttributeStreamConstValue
<Type
>::Next()
392 AttributeStreamConstValue
<Type
>::Get()
394 return (const char*)&fValue
;
400 AttributeStreamConstValue
<Type
>::Fill(char* buffer
) const
402 memcpy(buffer
, &fValue
, sizeof(Type
));
409 AttributeStreamConstValue
<Type
>::Find(const char* name
, uint32 type
) const
411 if (strcmp(fAttr
.Name(), name
) == 0 && type
== fAttr
.Type())
418 class AttributeStreamBoolValue
: public AttributeStreamConstValue
<bool> {
420 AttributeStreamBoolValue(const char* name
, bool value
)
422 AttributeStreamConstValue
<bool>(name
, B_BOOL_TYPE
, value
)
428 class AttributeStreamInt32Value
: public AttributeStreamConstValue
<int32
> {
430 AttributeStreamInt32Value(const char* name
, int32 value
)
432 AttributeStreamConstValue
<int32
>(name
, B_INT32_TYPE
, value
)
438 class AttributeStreamInt64Value
: public AttributeStreamConstValue
<int64
> {
440 AttributeStreamInt64Value(const char* name
, int64 value
)
442 AttributeStreamConstValue
<int64
>(name
, B_INT64_TYPE
, value
)
448 class AttributeStreamRectValue
: public AttributeStreamConstValue
<BRect
> {
450 AttributeStreamRectValue(const char* name
, BRect value
)
452 AttributeStreamConstValue
<BRect
>(name
, B_RECT_TYPE
, value
)
458 class AttributeStreamFloatValue
: public AttributeStreamConstValue
<float> {
460 AttributeStreamFloatValue(const char* name
, float value
)
462 AttributeStreamConstValue
<float>(name
, B_FLOAT_TYPE
, value
)
467 } // namespace BPrivate
469 using namespace BPrivate
;
472 #endif // _ATTRIBUTE_STREAM_H