]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeNode.h
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOTreeNode.h
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #ifndef __creaImageIOTreeNode_h_INCLUDED__
30 #define __creaImageIOTreeNode_h_INCLUDED__
31
32 #include <creaImageIOTreeLevelDescriptor.h>
33 #include <creaImageIOTreeComparators.h>
34 #include<boost/filesystem/operations.hpp>
35 #include <vector>
36 #include <map>
37
38
39 namespace creaImageIO
40 {
41
42   namespace tree
43   {
44           /**
45         * \ingroup Tree
46         */
47     //=====================================================================
48     /// Forward declaration of Tree
49     class Tree;
50     //=====================================================================  
51     
52     //=====================================================================
53     /// Abstract class to store user data on a Tree node
54     struct NodeData
55     { 
56       NodeData() {}
57       virtual ~NodeData() {}
58     };
59     //=====================================================================
60
61
62     //=====================================================================
63     /// Node of an attributed Tree structure
64     class Node
65     {
66     public:
67       typedef std::map<std::string,std::string> AttributeMapType;
68
69
70       /// Ctor with parent
71       Node(Node* parent);
72       /// Ctor with parent and attributes map 
73       Node(Node* parent, const AttributeMapType& );
74       /// Virtual destructor
75       virtual ~Node();
76
77       /// Initializes the attribute map i.e. creates the entries
78       void InitializeAttributeMap();
79  
80       /// Returns the level descriptor of the node
81       const LevelDescriptor& GetLevelDescriptor() const;
82
83
84       /// Returns the tree to which the node belongs
85       virtual Tree* GetTree() { return mParent->GetTree(); }
86       /// Returns the tree to which the node belongs
87       virtual const Tree* GetTree() const { return mParent->GetTree(); }
88       /// Returns the level of the node in the tree
89       virtual int GetLevel() const { return mParent->GetLevel()+1; }
90
91          
92       /// Returns the parent of the node
93       Node* GetParent() const { return mParent; }
94
95       /// Returns the number of children of the node.
96       /// Warning : if the children are not loaded then might return 0
97       ///           even if the node has children !
98       ///           see TreeHandler::GetNumberOfChildren 
99       unsigned int GetNumberOfChildren() const { return (int)mChildren.size(); }
100
101       /// Returns true iff the node's children are loaded
102       bool GetChildrenLoaded() const { return mChildrenLoaded; }
103
104       /// Sets the node's children 
105       void SetChildrenLoaded(bool l) { mChildrenLoaded = l; }
106
107       /// The type of children container
108       typedef std::vector<Node*> ChildrenListType;
109       /// Returns the list of children
110       ChildrenListType& GetChildrenList() { return mChildren; }
111       /// Returns the list of children (const)
112       const ChildrenListType& GetChildrenList() const { return mChildren; }
113
114       /// Remove the given children from the children list
115       int RemoveChildrenFromList(Node*);
116
117         
118           /// Get the Attributes Map
119       AttributeMapType& GetAttributeMap() { return mAttributeMap; }
120
121           /// Get the Attributes Map
122       const AttributeMapType& GetAttributeMap() const { return mAttributeMap; }
123
124           /// Get the Attribute for a specific key
125       const std::string& GetAttribute(const std::string& k) const;
126       
127           /// Get the Attribute for a specific key without OS dependance (not implemented)
128           // TODO : backslash OS uniformity
129           const std::string& GetCleanAttribute(const std::string& k) const;
130
131           /// Set an Attribute for a specific key
132       void SetAttribute(const std::string& k, const std::string& v);
133
134           /// Set an Attribute for a specific key(unsafe mode)
135       void UnsafeSetAttribute(const std::string& k, const std::string& v)
136       { mAttributeMap[k] = v; }
137     
138           /// Get Descriptor for an Attribute
139       const AttributeDescriptor& GetAttributeDescriptor(const std::string& k)const;
140       
141       /// Returns true if the KEY attributes of the node match those of the map provided
142       bool Matches( const AttributeMapType& ) const;
143
144       /// Returns the node data casted into the type T
145       template<class T> T GetData() const 
146       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
147
148       /// Sets the node data. Deletes existing data if any.
149           void SetData(boost::shared_ptr<NodeData> d) {mData.reset(); mData = d; }//{ if (mData) delete mData; mData = d; }
150
151       /// Sorts the children of the node 
152       void SortChildren(const LexicographicalComparator&);
153
154           /// Print the node
155       virtual void Print() const;
156
157           /// Get the Label of the node
158       std::string GetLabel() const;
159  
160
161     private:
162       /// The parent of the node
163       Node* mParent;
164       /// The list of children
165       ChildrenListType mChildren;
166       /// The map of attributes
167       AttributeMapType mAttributeMap;
168       /// User data
169       boost::shared_ptr<NodeData> mData;
170       /// Are the children loaded ?
171       bool mChildrenLoaded;
172       /// The number of children
173       // int mNumberOfChildren;
174
175     }; // class Node
176     //=====================================================================
177
178   } // namespace tree
179
180
181 } // namespace creaImageIO
182
183
184
185 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__