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