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