]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeNode.h
2905c1a8a0e2784d201da383fe54c2697919afd7
[creaImageIO.git] / src / 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          
64       /// Returns the parent of the node
65       Node* GetParent() const { return mParent; }
66
67       /// Returns the number of children of the node.
68       /// Warning : if the children are not loaded then might return 0
69       ///           even if the node has children !
70       ///           see TreeHandler::GetNumberOfChildren 
71       unsigned int GetNumberOfChildren() const { return mChildren.size(); }
72
73       /// Returns true iff the node's children are loaded
74       bool GetChildrenLoaded() const { return mChildrenLoaded; }
75
76       /// Sets the node's children 
77       void SetChildrenLoaded(bool l) { mChildrenLoaded = l; }
78
79       /// The type of children container
80       typedef std::vector<Node*> ChildrenListType;
81       /// Returns the list of children
82       ChildrenListType& GetChildrenList() { return mChildren; }
83       /// Returns the list of children (const)
84       const ChildrenListType& GetChildrenList() const { return mChildren; }
85
86       /// Remove the given children from the children list
87       int RemoveChildrenFromList(Node*);
88
89         
90           /// Get the Attributes Map
91       AttributeMapType& GetAttributeMap() { return mAttributeMap; }
92
93           /// Get the Attributes Map
94       const AttributeMapType& GetAttributeMap() const { return mAttributeMap; }
95
96           /// Get the Attribute for a specific key
97       const std::string& GetAttribute(const std::string& k) const;
98       
99           /// Get the Attribute for a specific key without OS dependance (not implemented)
100           // TODO : backslash OS uniformity
101           const std::string& GetCleanAttribute(const std::string& k) const;
102
103           /// Set an Attribute for a specific key
104       void SetAttribute(const std::string& k, const std::string& v);
105
106           /// Set an Attribute for a specific key(unsafe mode)
107       void UnsafeSetAttribute(const std::string& k, const std::string& v)
108       { mAttributeMap[k] = v; }
109     
110           /// Get Descriptor for an Attribute
111       const AttributeDescriptor& GetAttributeDescriptor(const std::string& k)const;
112       
113       /// Returns true if the KEY attributes of the node match those of the map provided
114       bool Matches( const AttributeMapType& ) const;
115
116       /// Returns the node data casted into the type T
117       template<class T> T GetData() const 
118       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
119
120       /// Sets the node data. Deletes existing data if any.
121           void SetData(boost::shared_ptr<NodeData> d) {mData.reset(); mData = d; }//{ if (mData) delete mData; mData = d; }
122
123       /// Sorts the children of the node 
124       void SortChildren(const LexicographicalComparator&);
125
126           /// Print the node
127       virtual void Print() const;
128
129           /// Get the Label of the node
130       std::string GetLabel() const;
131  
132
133     private:
134       /// The parent of the node
135       Node* mParent;
136       /// The list of children
137       ChildrenListType mChildren;
138       /// The map of attributes
139       AttributeMapType mAttributeMap;
140       /// User data
141       boost::shared_ptr<NodeData> mData;
142       /// Are the children loaded ?
143       bool mChildrenLoaded;
144       /// The number of children
145       // int mNumberOfChildren;
146
147     }; // class Node
148     //=====================================================================
149
150   } // namespace tree
151
152
153 } // namespace creaImageIO
154
155
156
157 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__