]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeNode.h
55d6ab570b36a46c7adad26c00419fb0e5ccf284
[creaImageIO.git] / src2 / creaImageIOTreeNode.h
1 #ifndef __creaImageIOTreeNode_h_INCLUDED__
2 #define __creaImageIOTreeNode_h_INCLUDED__
3
4 #include <creaImageIOTreeDescriptor.h>
5 #include <creaImageIOTreeComparators.h>
6 #include <vector>
7 #include <map>
8
9 namespace creaImageIO
10 {
11
12   namespace tree
13   {
14           /**
15         * \ingroup Tree
16         */
17     //=====================================================================
18     /// Forward declaration of Tree
19     class Tree;
20     //=====================================================================  
21     
22     //=====================================================================
23     /// Abstract class to store user data on a Tree node
24     struct NodeData
25     { 
26       NodeData() {}
27       virtual ~NodeData() {}
28     };
29     //=====================================================================
30
31
32     //=====================================================================
33     /// Node of an attributed Tree structure
34     class Node
35     {
36     public:
37       typedef std::map<std::string,std::string> AttributeMapType;
38
39
40       /// Ctor with parent
41       Node(Node* parent);
42       /// Ctor with parent and attributes map 
43       Node(Node* parent, const AttributeMapType& );
44       /// Virtual destructor
45       virtual ~Node();
46
47       /// Initializes the attribute map i.e. creates the entries
48       void InitializeAttributeMap();
49  
50       /// Returns the level descriptor of the node
51       const LevelDescriptor& GetLevelDescriptor() const;
52
53
54       /// Returns the tree to which the node belongs
55       virtual Tree* GetTree() { return mParent->GetTree(); }
56       /// Returns the tree to which the node belongs
57       virtual const Tree* GetTree() const { return mParent->GetTree(); }
58       /// Returns the level of the node in the tree
59       virtual int GetLevel() const { return mParent->GetLevel()+1; }
60
61       /// Returns the parent of the node
62       Node* GetParent() const { return mParent; }
63
64       /// Returns the number of children of the node.
65       /// Warning : if the children are not loaded then might return 0
66       ///           even if the node has children !
67       ///           see TreeHandler::GetNumberOfChildren 
68       unsigned int GetNumberOfChildren() const { return mChildren.size(); }
69
70       /// Returns true iff the node's children are loaded
71       bool GetChildrenLoaded() const { return mChildrenLoaded; }
72
73       /// Sets the node's children 
74       void SetChildrenLoaded(bool l) { mChildrenLoaded = l; }
75
76       /// The type of children container
77       typedef std::vector<Node*> ChildrenListType;
78       /// Returns the list of children
79       ChildrenListType& GetChildrenList() { return mChildren; }
80       /// Returns the list of children (const)
81       const ChildrenListType& GetChildrenList() const { return mChildren; }
82
83       /// Remove the given children from the children list
84       int RemoveChildrenFromList(Node*);
85
86         
87           /// Get the Attributes Map
88       AttributeMapType& GetAttributeMap() { return mAttributeMap; }
89
90           /// Get the Attributes Map
91       const AttributeMapType& GetAttributeMap() const { return mAttributeMap; }
92
93           /// Get the Attribute for a specific key
94       const std::string& GetAttribute(const std::string& k) const;
95       
96           /// Get the Attribute for a specific key without OS dependance (not implemented)
97           // TODO : backslash OS uniformity
98           const std::string& GetCleanAttribute(const std::string& k) const;
99
100           /// Set an Attribute for a specific key
101       void SetAttribute(const std::string& k, const std::string& v);
102
103           /// Set an Attribute for a specific key(unsafe mode)
104       void UnsafeSetAttribute(const std::string& k, const std::string& v)
105       { mAttributeMap[k] = v; }
106     
107           /// Get Descriptor for an Attribute
108       const AttributeDescriptor& GetAttributeDescriptor(const std::string& k)const;
109       
110       /// Returns true if the KEY attributes of the node match those of the map provided
111       bool Matches( const AttributeMapType& ) const;
112
113       /// Returns the node data casted into the type T
114       template<class T> T GetData() const 
115       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
116
117       /// Sets the node data. Deletes existing data if any.
118       void SetData(NodeData* d) { if (mData) delete mData; mData = d; }
119
120       /// Sorts the children of the node 
121       void SortChildren(const LexicographicalComparator&);
122
123           /// Print the node
124       virtual void Print() const;
125
126           /// Get the Label of the node
127       std::string GetLabel() const;
128  
129
130     private:
131       /// The parent of the node
132       Node* mParent;
133       /// The list of children
134       ChildrenListType mChildren;
135       /// The map of attributes
136       AttributeMapType mAttributeMap;
137       /// User data
138       NodeData* mData;
139       /// Are the children loaded ?
140       bool mChildrenLoaded;
141       /// The number of children
142       // int mNumberOfChildren;
143
144     }; // class Node
145     //=====================================================================
146
147   } // namespace tree
148
149
150 } // namespace creaImageIO
151
152
153
154 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__