1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // This contains the definitions of CDataDelete, CDataNoDelete, CIntAttribute, CNode, and CTypedNode.
15 // CDataNoDelete is a base class to CDataDelete and are essentially a wrapper around an HBufC16.
16 // The node owns the data that is added to it and therefore is responsible for deleting all its data,
17 // hence theses two classes allow a user to have non deletable data. Internally to the node it is also
18 // sometimes necessary to change the data into either deletable or non-deletable data api's are provided
20 // CIntAttribute is wrapper around a TInt, this is provided as the nodes attribute value is a CBase* however
21 // it might be desirable to store integer's in here. Attribute value is owned by the node therefore the node is
22 // responsible for deleting it.
23 // CNode is the basis for constructing a tree. It consists of an array of child nodes, node type, a parent node,
24 // an array of attributes, and a data member. Internally the data member is defined as CDataNoDelete however
25 // to the user all the exported api's take an HBufC16*. Data is owned by the node and is destroyed by the node,
26 // However in certain circumstances this is not desirable hence the api's to make the data non-deletable. The
27 // node type is defined as a TAny* however normal usage would use the templated node - CTypedNode. The node type
28 // is used to identify groups of nodes. The attribute array is fairy simple and consists of AttributeType and
29 // AttributeValue. AttributeType can be defined by using the templated class and should be a 32bit value. AttributeValue
30 // can be any object derived from CBase and the node takes ownership therefore the node delete's it.
31 // Basic usage should be to use the templated class in order to make use of the templated types (TNodeType,TAttributeType).
32 // Create a node for example:
33 // CTypedNode<CNode*, const TDesC*> *tree = CTypedNode<CNode*, const TDesC*>::NewL(0,0);
35 // CNODE *TestChildNode1 = tree->AppendNodeL(aTempNodeForNodeType);
37 // TestChildNode1->SetDataL(aHBufC16);
39 // TestChildNode1->->AddAttributeL(aTAttributeType,aCBasePointerAttributeValue);
40 // Explanation of individual api's is documented below.
58 //Granularity of arrays
59 const TInt KGranularity = 5;
61 //enum of panic reasons
71 inline void Panic(TNodePanic aPanic);
73 //Wrapper around an HBufC16 doesn't delete data
74 //##ModelId=3B666BCC0303
75 class CDataNoDelete : public CBase
76 /** Provides a wrapper around an HBufC16: the buffer is not deleted when the object is deleted.
80 //##ModelId=3B666BCC032D
81 CDataNoDelete(HBufC16* aData);
82 //##ModelId=3B666BCC032C
83 virtual ~CDataNoDelete();
84 //##ModelId=3B666BCC0324
85 HBufC16* SetData(HBufC16* aData);
86 //##ModelId=3B666BCC0322
87 virtual void ResetDataPointer(HBufC16* aData);
88 //##ModelId=3B666BCC0321
92 /** The wrapped buffer. */
93 //##ModelId=3B666BCC0319
97 //Wrapper around an HBufC16 does delete data
98 //##ModelId=3B666BCC0399
99 class CDataDelete : public CDataNoDelete
100 /** Provides a wrapper around an HBufC16: the buffer is deleted when the
105 //##ModelId=3B666BCC03B7
106 CDataDelete(HBufC16* aData);
107 //##ModelId=3B666BCC03B0
108 virtual ~CDataDelete();
109 //##ModelId=3B666BCC03AE
110 virtual void ResetDataPointer(HBufC16* aData);
113 //Wrapper around an HBufC16 does delete data (FileName)
114 // After Removing referenced File
115 //##ModelId=3B666BC7026F
116 class CFileDataDelete : public CDataNoDelete
117 /** Provides a wrapper around a filename: the referenced file is deleted when the object is deleted.
121 //##ModelId=3B666BC7028F
122 CFileDataDelete(HBufC16* aData);
123 //##ModelId=3B666BC7028E
124 virtual ~CFileDataDelete();
125 //##ModelId=3B666BC70285
126 virtual void ResetDataPointer(HBufC16* aData);
128 //##ModelId=3B666BC70284
132 //Wrapper around a TInt used for attribute values in order to allow the node to call a dtor
133 //##ModelId=3B666BC6023E
134 class CIntAttribute : public CBase
135 /** Provides an object wrapper around a TInt value. */
138 //##ModelId=3B666BC6025B
139 inline CIntAttribute(TInt aInteger);
140 //##ModelId=3B666BC6025A
141 inline TInt Int() const;
143 //##ModelId=3B666BC60264
145 //##ModelId=3B666BC60253
151 // Normal usage would be to use CTypedNode in order to use specific templated types
154 //##ModelId=3B666BCD02D2
155 class CNode : public CBase
158 //##ModelId=3B666BCE0139
161 //NewL parameters aType to indentify the type of node, CNode* to set the parent of this node
162 //##ModelId=3B666BCE0125
163 IMPORT_C static CNode* NewL(TAny* aType,CNode* aParent);
165 //Deletes a child node which is passed in as a parameter
166 //##ModelId=3B666BCE011C
167 IMPORT_C void DeleteChildNode(CNode* aNode);
169 //Deletes all the child nodes haging of this node
170 //##ModelId=3B666BCE011B
171 IMPORT_C void DeleteAllChildNodes();
173 //Creates a new node and adds it to the childlist, reference to new node is returned. Can leave if it can't allocate memory for a new node
174 //Type of node is the parameter
175 //##ModelId=3B666BCE0108
176 IMPORT_C CNode& AppendNodeL(TAny* aType = 0);
178 //Appends a node which is passed in as a parameter to this node, so its added to the childlist
179 //##ModelId=3B666BCE00FD
180 IMPORT_C void AppendNodeToThisNodeL(CNode* aNode);
182 //Sets the data in the node to the parameter that is passed in. It's deletable and the node will delete it in dtor
183 //Push aDataNowNodeOwns onto the CleanupStack before calling then pop off on return
184 //##ModelId=3B666BCE00F3
185 IMPORT_C void SetDataL(HBufC16* aDataNowNodeOwns);
187 //returns the data stored in the node, which is returned as an HBufC16*
188 //##ModelId=3B666BCE00EB
189 IMPORT_C HBufC16* Data() const;
191 //WARNING this function can leave as it deletes the wrapper object and then creates
192 //a new non deletable wrapper object and sets the data pointer inside the new object
193 //IF THIS LEAVES YOU WILL LOSE YOUR DATA
194 //##ModelId=3B666BCE00EA
195 IMPORT_C void SetDataNoDeleteL();
197 //WARNING this function can leave as it deletes the wrapper object and then creates
198 //a new deletable wrapper object then sets the pointer in the new object
199 //##ModelId=3B666BCE00E9
200 IMPORT_C void ClearSetDataNoDeleteL();
202 // Sets the data in the node to the FileName parameter that is passed in.
203 // It's deletable and the node will delete it, and the file it refers to in dtor
204 // Push aDataLocationNowNodeOwns onto the CleanupStack before calling then pop off on return
205 //##ModelId=3B666BCE00DF
206 IMPORT_C void SetFileDataL(HBufC16* aFileDataLocationNowNodeOwns);
208 //Resets the data pointer to point to aData parameter will delete the data that is owned by the node
209 //##ModelId=3B666BCE00D5
210 IMPORT_C void ResetDataPointer(HBufC16* aData);
212 //returns a reference to the absolute root of the tree
213 //##ModelId=3B666BCE00CB
214 IMPORT_C const CNode& Root() const;
216 //returns a child node which is stored in the array of children. The child node is accessed by the index that is the parameter.
217 //##ModelId=3B666BCE00C1
218 IMPORT_C CNode* Child(TInt aByIndex) const;
220 //Returns either the first child of this node if the parameter is NULL. Or it returns the next chld in the array after
221 //the child passed in as a parameter
222 //##ModelId=3B666BCE00AE
223 IMPORT_C CNode* NextChild(const CNode* aNode = NULL) const;
225 //returns the previous child to the child passed in as a parameter. The node parameter is a reference because its the child previous
226 //to the node passed in which obviously must exist
227 //##ModelId=3B666BCE00A4
228 IMPORT_C CNode* PrevChild(const CNode& aNode) const;
230 //Returns the parent of this node
231 //##ModelId=3B666BCE00A3
232 IMPORT_C CNode* Parent() const;
234 //WARNING this function can leave as it calls AppendNodeToThisNode. The aParent parameter is the node to which you would like to make
235 //the parent of 'this' node.
236 //It removes itself from the childlist of it's current parent
237 //##ModelId=3B666BCE0099
238 IMPORT_C void ReparentL(CNode* aParent);
240 //Returns the next sibling which means in effect that it asks for the next child of its parent.
241 //Sibling means brother or sister.
242 //##ModelId=3B666BCE0090
243 IMPORT_C CNode* NextSibling() const;
245 //Returns the previous sibling which means in effect that it asks for the previous child of its parent.
246 //Sibling means brother or sister.
247 //##ModelId=3B666BCE008F
248 IMPORT_C CNode* PrevSibling() const;
250 //returns the number of children that this node has
251 //##ModelId=3B666BCE0085
252 IMPORT_C TInt NumberImmediateChildren() const;
254 //Deletes the attribute of which is of aAttributeType (the parameter)
255 //WARNING Attribute values of nodes will be deleted as the node owns them if you don't want it deleted then use a wrapper
256 //##ModelId=3B666BCE007C
257 IMPORT_C void DeleteAttribute(TAny* aAttributeType);
259 //Deletes all the attributes of this node
260 //WARNING Attribute values of nodes will be deleted as the node owns them if you don't want it deleted then use a wrapper
261 //##ModelId=3B666BCE007B
262 IMPORT_C void DeleteAllAttributes();
264 //remove an attribute without deleting it, this is done by simply setting the attributeValue pointer to NULL
265 //WARNING you are now responsiblefor the destruction of this attribute value
266 //##ModelId=3B666BCE0071
267 IMPORT_C void RemoveAttributeNoDelete(TAny* aAttributeType);
269 // Returns the number of attributes that this node has
270 //##ModelId=3B666BCE0067
271 IMPORT_C TInt AttributeCount() const;
273 //Returns the type of attribute (AttributeType) at a given index...NOT the attributeValue
274 //##ModelId=3B666BCE005D
275 IMPORT_C TAny* AttributeTypeByIndex(TInt aIndex) const;
277 //Returns the value of an attribute (AttributeValue) at a given index...NOT the attributeType
278 //##ModelId=3B666BCE003F
279 IMPORT_C CBase* AttributeByIndex(TInt aIndex) const;
280 //##ModelId=3B666BCE0049
281 IMPORT_C CBase* AttributeByIndex(TInt aIndex,TAny*& aType) const;
283 //Adds an attribute, parameters are the type of attribute and its value
284 //WARNING node takes ownership of aAttributeValue
285 //Push aAttributeValue onto the CleanupStack before calling then pop off on return
286 //##ModelId=3B666BCE002B
287 IMPORT_C void AddAttributeL(TAny* AttributeType, CBase* aAttributeValue);
289 //Adds data to the node and also adds an attribute, parameters are the Data to be added, the type of attribute and its value
290 //WARNING node takes ownership of aData and aAttributeValue
291 //Push aAttributeValue and aData onto the CleanupStack before calling then pop off on return
292 //##ModelId=3B666BCE000D
293 IMPORT_C void AddDataAndAttributeL(HBufC16 *aData, TAny* AttributeType, CBase* aAttributeValue);
295 //Returns an attribute value for the given AttributeType(the parameter)
296 //##ModelId=3B666BCE0003
297 IMPORT_C CBase* Attribute(TAny* AttributeType) const;
299 //Returns TRUE if the attribute of the given type exists
300 //##ModelId=3B666BCD03E1
301 IMPORT_C TBool AttributeExists(TAny* aAttributeType) const;
303 //Returns the node type
304 //##ModelId=3B666BCD03D7
305 IMPORT_C TAny* Type() const;
307 //Sets the node type to be aType (the parameter)
308 //##ModelId=3B666BCD03CD
309 IMPORT_C void SetType(TAny* aType);
314 //##ModelId=3B666BCD03B9
315 CNode(TAny* aType, CNode* aParent);
317 //internal finds a child which is passed in as a parameter
318 //##ModelId=3B666BCD03AF
319 TInt FindChild(const CNode* aNode) const;
321 //##ModelId=3B666BCD03A7
322 HBufC16* SetupDeletableOrNonDeleteableDataLC();
323 //##ModelId=3B666BCD03A6
324 void AdjustBasePointers();
326 // Reserved for future expansion
327 //##ModelId=3B666BCD039B
328 virtual void Reserved1();
329 //##ModelId=3B666BCD03A5
330 virtual void Reserved1() const;
334 //##ModelId=3B666BCD0392
338 //##ModelId=3B666BCD037E
341 // stores attribute type and value. iTypes must be Flat array
342 //##ModelId=3B666BCD0372
343 CArrayPtrFlat<CBase> iValues;
344 //##ModelId=3B666BCD034C
345 CArrayPtrFlat<TAny> iTypes;
347 //##ModelId=3B666BCD0324
348 TInt32* iTypesBasePtr;
349 //##ModelId=3B666BCD0310
350 CDataNoDelete* iDataValue;
352 //An array of child nodes
353 //##ModelId=3B666BCD02FC
354 CArrayPtr<CNode> *iChildList;
356 //##ModelId=3B666BCD02DF
357 TAny* iReserved; // Reserved for future expansion
361 //CTypedNode is derived from CNode and is a thin template. TNodeType you should define as a 32 bit value
362 //TAttributeType should be defined as a 32 bit value
363 //FOR EXPLANATION OF API'S SEE ABOVE
364 //##ModelId=3B666BCC01A4
365 template <class TNodeType, class TAttributeType>
366 class CTypedNode : public CNode
367 /** Template class for a node in a node tree.
369 The node type is set to the template parameter TNodeType and the attribute type to
370 TAttributeType. These parameters should be pointers to the type required to store
371 the type value: e.g. for a string, a const TDesC*.
373 The class is thin template over CNode. */
376 //##ModelId=3B666BCC024A
377 inline static CTypedNode* NewL(TNodeType aType,CNode* aParent);
378 //##ModelId=3B666BCC0248
379 inline void DeleteChildNode(CNode* aNode);
380 //##ModelId=3B666BCC0247
381 inline void DeleteAllChildNodes();
382 //##ModelId=3B666BCC0245
383 inline CTypedNode<TNodeType,TAttributeType>& AppendNodeL(TNodeType aType);
384 //##ModelId=3B666BCC023B
385 inline void AppendNodeToThisNodeL(CNode* aNode);
386 //##ModelId=3B666BCC0228
387 inline void SetDataL(HBufC16* aDataNowNodeOwns);
388 //##ModelId=3B666BCC0227
389 inline void SetDataNoDeleteL();
390 //##ModelId=3B666BCC0221
391 inline void ClearSetDataNoDeleteL();
392 //##ModelId=3B666BCC021F
393 inline void SetFileDataL(HBufC16* aFileDataLocationNowNodeOwns);
394 //##ModelId=3B666BCC021D
395 inline void ResetDataPointer(HBufC16* aData);
396 //##ModelId=3B666BCC0215
397 inline HBufC16* Data() const;
398 //##ModelId=3B666BCC0214
399 inline const CTypedNode& Root() const;
400 //##ModelId=3B666BCC020B
401 inline CTypedNode* Child(TInt aByIndex) const;
402 //##ModelId=3B666BCC0209
403 inline CTypedNode* NextChild(const CNode* aNode = NULL) const;
404 //##ModelId=3B666BCC0201
405 inline CTypedNode* PrevChild( const CNode& aNode) const;
406 //##ModelId=3B666BCC0200
407 inline CTypedNode* Parent() const;
408 //##ModelId=3B666BCC01FE
409 inline void ReparentL(CNode* aParent);
410 //##ModelId=3B666BCC01F5
411 inline CTypedNode* NextSibling() const;
412 //##ModelId=3B666BCC01F4
413 inline CTypedNode* PrevSibling() const;
414 //##ModelId=3B666BCC01EE
415 inline TInt NumberImmediateChildren() const;
416 //##ModelId=3B666BCC01EC
417 inline void DeleteAttribute(TAttributeType aAttributeType);
418 //##ModelId=3B666BCC01EB
419 inline void DeleteAllAttributes();
420 //##ModelId=3B666BCC01E5
421 inline void RemoveAttributeNoDelete(TAttributeType aAttributeType);
422 //##ModelId=3B666BCC01E4
423 inline TInt AttributeCount() const; // Returns the number of attributes
424 //##ModelId=3B666BCC01E2
425 inline TAttributeType AttributeTypeByIndex(TInt aIndex) const;
426 //##ModelId=3B666BCC01DC
427 inline CBase* AttributeByIndex(TInt aIndex) const;
428 //##ModelId=3B666BCC01DE
429 inline CBase* AttributeByIndex(TInt aIndex,TAttributeType& aType) const;
430 //##ModelId=3B666BCC01D9
431 inline void AddAttributeL(TAttributeType aAttributeType, CBase* aAttributeValue);
432 //##ModelId=3B666BCC01D0
433 inline void AddDataAndAttributeL(HBufC16 *aData, TAttributeType aAttributeType, CBase* aAttributeValue);
434 //##ModelId=3B666BCC01CE
435 inline CBase* Attribute(TAttributeType aAttributeType) const;
436 //##ModelId=3B666BCC01CC
437 inline TBool AttributeExists(TAttributeType aAttributeType) const;
438 //##ModelId=3B666BCC01C9
439 inline TNodeType Type() const;
440 //##ModelId=3B666BCC01C7
441 inline void SetType(TNodeType aType);
443 //##ModelId=3B666BCC01C4
444 CTypedNode(TNodeType aType, CNode* aParent);