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