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