]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeNode.h
#3218 creaImageIO Feature New Normal - vtk8itk4wx3-mingw64
[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       /// Ctor with parent
70       Node(Node* parent);
71       /// Ctor with parent and attributes map 
72       Node(Node* parent, const AttributeMapType& );
73       /// Virtual destructor
74       virtual ~Node();
75
76       /// Initializes the attribute map i.e. creates the entries
77       void InitializeAttributeMap();
78  
79       /// Returns the level descriptor of the node
80       const LevelDescriptor& GetLevelDescriptor() const;
81
82
83       /// Returns the tree to which the node belongs
84       virtual Tree* GetTree() { return mParent->GetTree(); }
85       /// Returns the tree to which the node belongs
86       virtual const Tree* GetTree() const { return mParent->GetTree(); }
87       /// Returns the level of the node in the tree
88       virtual int GetLevel() const { return mParent->GetLevel()+1; }
89
90       /// Returns the parent of the node
91       Node* GetParent() const { return mParent; }
92
93       /// Returns the number of children of the node.
94       /// Warning : if the children are not loaded then might return 0
95       ///           even if the node has children !
96       ///           see TreeHandler::GetNumberOfChildren 
97       unsigned int GetNumberOfChildren() const { return (int)mChildren.size(); }
98
99       /// Returns true iff the node's children are loaded
100       bool GetChildrenLoaded() const { return mChildrenLoaded; }
101
102       /// Sets the node's children 
103       void SetChildrenLoaded(bool l) { mChildrenLoaded = l; }
104
105       /// The type of children container
106       typedef std::vector<Node*> ChildrenListType;
107       /// Returns the list of children
108       ChildrenListType& GetChildrenList() { return mChildren; }
109       /// Returns the list of children (const)
110       const ChildrenListType& GetChildrenList() const { return mChildren; }
111
112       /// Remove the given children from the children list
113       int RemoveChildrenFromList(Node*);
114         
115           /// Get the Attributes Map
116       AttributeMapType& GetAttributeMap() { return mAttributeMap; }
117
118           /// Get the Attributes Map
119       const AttributeMapType& GetAttributeMap() const { return mAttributeMap; }
120
121           /// Get the Attribute for a specific key
122       const std::string& GetAttribute(const std::string& k) const;
123       
124           /// Get the Attribute for a specific key without OS dependance (not implemented)
125           // TODO : backslash OS uniformity
126           const std::string& GetCleanAttribute(const std::string& k) const;
127
128           /// Set an Attribute for a specific key
129       void SetAttribute(const std::string& k, const std::string& v);
130
131           /// Set an Attribute for a specific key(unsafe mode)
132       void UnsafeSetAttribute(const std::string& k, const std::string& v)
133       { mAttributeMap[k] = v; }
134     
135           /// Get Descriptor for an Attribute
136       const AttributeDescriptor& GetAttributeDescriptor(const std::string& k)const;
137       
138       /// Returns true if the KEY attributes of the node match those of the map provided
139       bool Matches( const AttributeMapType& ) const;
140
141       /// Returns the node data casted into the type T
142       template<class T> T GetData() const 
143       { if (mData!=0) return dynamic_cast<T>(mData); return 0; }
144
145       /// Sets the node data. Deletes existing data if any.
146           void SetData(boost::shared_ptr<NodeData> d) {mData.reset(); mData = d; }//{ if (mData) delete mData; mData = d; }
147
148       /// Sorts the children of the node 
149       void SortChildren(const LexicographicalComparator&);
150
151           /// Print the node
152       virtual void Print() const;
153
154           /// Get the Label of the node
155       std::string GetLabel() const;
156  
157
158     private:
159       /// The parent of the node
160       Node* mParent;
161       /// The list of children
162       ChildrenListType mChildren;
163       /// The map of attributes
164       AttributeMapType mAttributeMap;
165       /// User data
166       boost::shared_ptr<NodeData> mData;
167       /// Are the children loaded ?
168       bool mChildrenLoaded;
169       /// The number of children
170       // int mNumberOfChildren;
171
172     }; // class Node
173     //=====================================================================
174
175   } // namespace tree
176
177
178 } // namespace creaImageIO
179
180
181
182 #endif // #ifndef __creaImageIOTreeNode_h_INCLUDED__