]> 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         * \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       void RemoveChildrenFromList(Node*);
85
86
87       AttributeMapType& GetAttributeMap() { return mAttributeMap; }
88       const AttributeMapType& GetAttributeMap() const { return mAttributeMap; }
89       const std::string& GetAttribute(const std::string& k) const;
90       const std::string& GetCleanAttribute(const std::string& k) const;
91       const std::string& UnsafeGetAttribute(const std::string& k) const
92       { return mAttributeMap.find(k)->second; }
93       void SetAttribute(const std::string& k, const std::string& v);
94       void UnsafeSetAttribute(const std::string& k, const std::string& v)
95       { mAttributeMap[k] = v; }
96     
97       const AttributeDescriptor& GetAttributeDescriptor(const std::string& k)
98         const;
99       //      { return GetTypeDescription().GetFieldDescription(k); }
100
101
102       /// Returns true iff the KEY attributes of the node match those of the map provided
103       bool Matches( const AttributeMapType& ) const;
104
105       /// Returns the node data casted into the type T
106       template<class T> T GetData() const 
107       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
108
109       /// Sets the node data. Deletes existing data if any.
110       void SetData(NodeData* d) { if (mData) delete mData; mData = d; }
111
112       /// Sorts the children of the node 
113       void SortChildren(const LexicographicalComparator&);
114
115
116       virtual void Print() const;
117       std::string GetLabel() const;
118       /*
119         int ImageGetRows() const;
120         int ImageGetColumns() const;
121         int ImageGetFrames() const;
122         const std::string& ImageGetFullFileName() const { return UnsafeGetAttribute("FullFileName"); }
123       */
124
125     private:
126       /// The parent of the node
127       Node* mParent;
128       /// The list of children
129       ChildrenListType mChildren;
130       /// The map of attributes
131       AttributeMapType mAttributeMap;
132       /// User data
133       NodeData* mData;
134       /// Are the children loaded ?
135       bool mChildrenLoaded;
136       /// The number of children
137       // int mNumberOfChildren;
138
139     }; // class Node
140     //=====================================================================
141
142   } // namespace tree
143
144
145 } // namespace creaImageIO
146
147
148
149 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__