Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / kits / tracker / AttributeStream.h
blob5c1bc8bffb5f5f49179d7a4cc070e96c4c31e4ef
1 /*
2 Open Tracker License
4 Terms and Conditions
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.
32 All rights reserved.
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
48 // are also available
49 #ifndef _ATTRIBUTE_STREAM_H
50 #define _ATTRIBUTE_STREAM_H
53 #include <ObjectList.h>
54 #include <Node.h>
55 #include <Rect.h>
56 #include <String.h>
57 #include <TypeConstants.h>
59 #include <fs_attr.h>
62 namespace BPrivate {
64 struct AttributeTemplate {
65 // used for read-only attribute source
66 const char* fAttributeName;
67 uint32 fAttributeType;
68 off_t fSize;
69 const char* fBits;
73 class AttributeInfo {
74 // utility class for internal attribute description
75 public:
76 AttributeInfo();
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;
85 uint32 Type() const;
86 off_t Size() const;
88 private:
89 BString fName;
90 attr_info fInfo;
94 class AttributeStreamNode {
95 public:
96 AttributeStreamNode();
97 virtual ~AttributeStreamNode();
99 AttributeStreamNode &operator<<(AttributeStreamNode &source);
100 // workhorse call
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
106 // streaming
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
123 // work calls
124 virtual bool Drive();
125 // node at the head of the stream makes the entire stream
126 // feed it
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
136 // attribute data
138 virtual bool CanFeed() const { return false; }
139 // return true if can work as a source for the entire stream
141 private:
142 bool Start();
143 // utility call, used to start up the stream by finding the ultimate
144 // target of the stream and calling Drive on it
146 void Detach();
148 protected:
149 AttributeStreamNode* fReadFrom;
150 AttributeStreamNode* fWriteTo;
154 class AttributeStreamFileNode : public AttributeStreamNode {
155 // handles reading and writing attributes to and from the
156 // stream
157 public:
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);
169 void SetTo(BNode*);
171 BNode* Node() { return fNode; }
173 protected:
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;
183 private:
184 AttributeInfo fCurrentAttr;
185 BNode* fNode;
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
194 public:
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);
204 protected:
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;
212 class AttrNode {
213 public:
214 AttrNode(const char* name, uint32 type, off_t size, char* data)
216 fAttr(name, type, size),
217 fData(data)
221 ~AttrNode()
223 delete[] fData;
226 AttributeInfo fAttr;
227 char* fData;
230 // utility calls
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;
235 private:
236 BObjectList<AttrNode> fAttributes;
237 int32 fCurrentIndex;
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
246 public:
247 AttributeStreamTemplateNode(const AttributeTemplate*, int32 count);
249 virtual off_t Contains(const char* name, uint32 type);
251 protected:
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;
260 private:
261 AttributeInfo fCurrentAttr;
262 const AttributeTemplate* fAttributes;
263 int32 fCurrentIndex;
264 int32 fCount;
266 typedef AttributeStreamNode _inherited;
270 class AttributeStreamFilterNode : public AttributeStreamNode {
271 // filter node may not pass thru specified attributes
272 public:
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);
281 protected:
282 virtual bool Reject(const char* name, uint32 type, off_t size);
283 // override to implement filtering
284 virtual const AttributeInfo* Next();
286 private:
287 typedef AttributeStreamNode _inherited;
291 class NamesToAcceptAttrFilter : public AttributeStreamFilterNode {
292 // filter node that only passes thru attributes that match
293 // a list of names
294 public:
295 NamesToAcceptAttrFilter(const char**);
297 protected:
298 virtual bool Reject(const char* name, uint32 type, off_t size);
300 private:
301 const char** fNameList;
305 class SelectiveAttributeTransformer : public AttributeStreamNode {
306 // node applies a transformation on specified attributes
307 public:
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();
317 protected:
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,
326 char* data);
327 // transforms in place
328 virtual const AttributeInfo* Next();
329 virtual const char* Get();
331 private:
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 {
345 public:
346 AttributeStreamConstValue(const char* name, uint32 attributeType,
347 Type value);
349 protected:
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;
358 private:
359 AttributeInfo fAttr;
360 Type fValue;
361 bool fRewound;
363 typedef AttributeStreamNode _inherited;
367 template<class Type>
368 AttributeStreamConstValue<Type>::AttributeStreamConstValue(const char* name,
369 uint32 attributeType, Type value)
371 fAttr(name, attributeType, sizeof(Type)),
372 fValue(value),
373 fRewound(true)
378 template<class Type>
379 const AttributeInfo*
380 AttributeStreamConstValue<Type>::Next()
382 if (!fRewound)
383 return NULL;
385 fRewound = false;
386 return &fAttr;
390 template<class Type>
391 const char*
392 AttributeStreamConstValue<Type>::Get()
394 return (const char*)&fValue;
398 template<class Type>
399 bool
400 AttributeStreamConstValue<Type>::Fill(char* buffer) const
402 memcpy(buffer, &fValue, sizeof(Type));
403 return true;
407 template<class Type>
408 int32
409 AttributeStreamConstValue<Type>::Find(const char* name, uint32 type) const
411 if (strcmp(fAttr.Name(), name) == 0 && type == fAttr.Type())
412 return 0;
414 return -1;
418 class AttributeStreamBoolValue : public AttributeStreamConstValue<bool> {
419 public:
420 AttributeStreamBoolValue(const char* name, bool value)
422 AttributeStreamConstValue<bool>(name, B_BOOL_TYPE, value)
428 class AttributeStreamInt32Value : public AttributeStreamConstValue<int32> {
429 public:
430 AttributeStreamInt32Value(const char* name, int32 value)
432 AttributeStreamConstValue<int32>(name, B_INT32_TYPE, value)
438 class AttributeStreamInt64Value : public AttributeStreamConstValue<int64> {
439 public:
440 AttributeStreamInt64Value(const char* name, int64 value)
442 AttributeStreamConstValue<int64>(name, B_INT64_TYPE, value)
448 class AttributeStreamRectValue : public AttributeStreamConstValue<BRect> {
449 public:
450 AttributeStreamRectValue(const char* name, BRect value)
452 AttributeStreamConstValue<BRect>(name, B_RECT_TYPE, value)
458 class AttributeStreamFloatValue : public AttributeStreamConstValue<float> {
459 public:
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