]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeNode.h
Correction sur le remove et sur la sélection du répertoire à scanner.
[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       int 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)const;
98       //      { return GetTypeDescription().GetFieldDescription(k); }
99
100
101       /// Returns true iff the KEY attributes of the node match those of the map provided
102       bool Matches( const AttributeMapType& ) const;
103
104       /// Returns the node data casted into the type T
105       template<class T> T GetData() const 
106       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
107
108       /// Sets the node data. Deletes existing data if any.
109       void SetData(NodeData* d) { if (mData) delete mData; mData = d; }
110
111       /// Sorts the children of the node 
112       void SortChildren(const LexicographicalComparator&);
113
114       virtual void Print() const;
115       std::string GetLabel() const;
116       /*
117         int ImageGetRows() const;
118         int ImageGetColumns() const;
119         int ImageGetFrames() const;
120         const std::string& ImageGetFullFileName() const { return UnsafeGetAttribute("FullFileName"); }
121       */
122
123     private:
124       /// The parent of the node
125       Node* mParent;
126       /// The list of children
127       ChildrenListType mChildren;
128       /// The map of attributes
129       AttributeMapType mAttributeMap;
130       /// User data
131       NodeData* mData;
132       /// Are the children loaded ?
133       bool mChildrenLoaded;
134       /// The number of children
135       // int mNumberOfChildren;
136
137     }; // class Node
138     //=====================================================================
139
140   } // namespace tree
141
142
143 } // namespace creaImageIO
144
145
146
147 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__