]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeHandler.h
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOTreeHandler.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 #ifndef __creaImageIOTreeHandler_h_INCLUDED__
29 #define __creaImageIOTreeHandler_h_INCLUDED__
30
31 #include <creaImageIOTree.h>
32
33 namespace creaImageIO
34 {
35
36
37   //=======================================================================
38   //class TreeHandlerStatistics;
39   //=======================================================================
40   /**
41    * \ingroup Model
42    */
43   //=======================================================================
44
45   /// Abstract class which 'handles' a Tree structure 
46   class TreeHandler
47   {
48   public:
49
50     //====================================================================
51     //  typedef TreeHandlerStatistics Statistics;
52     //====================================================================
53
54     //====================================================================
55     /// Ctor
56     TreeHandler() {}
57     /// Virtual dtor
58     virtual ~TreeHandler() {}
59     //====================================================================
60
61     //====================================================================
62     /// Returns the Tree handled 
63     tree::Tree& GetTree() { return mTree; }
64     /// Returns the Tree handled (const)
65     const tree::Tree& GetTree() const { return mTree; }
66     //====================================================================
67
68     //====================================================================
69     // QUERY METHODS
70     /// Is the 'source' readable ?
71     virtual bool IsReadable() { return false; }
72     /// Is the 'source' writable ?
73     virtual bool IsWritable() { return false; }
74     //====================================================================
75
76
77     //====================================================================
78     // INITIALIZATION / FINALIZATION
79     //====================================================================
80
81     //====================================================================
82     /// Opens an existing 'source' 
83     // Default mode is read only 
84     // If IsWritable and writable==true then opens in read/write mode
85     virtual bool Open(bool writable = false) { return false; }
86     /// Closes the 'source'
87     virtual bool Close() { return false; }
88     /// Creates a new 'source' 
89     // Default mode is read only 
90     // If IsWritable and writable==true then opens in read/write mode
91     virtual bool Create(bool writable = false) { return false; }
92     /// Destroys the 'source'
93     virtual bool Destroy() { return false; }
94         /// Begins a transaction
95         virtual void BeginTransaction(){}
96         ///Commits results and ends transaction
97         virtual void EndTransaction(){}
98     //====================================================================
99
100
101     //====================================================================
102     // READ METHODS
103     //====================================================================
104
105
106     //====================================================================
107     /// Returns the number of children of the Node *WITHOUT LOADING THEM*
108     // REM : The Tree itself is a Node and asking for its number of 
109     //       children returns the number of children of level 1.
110     virtual unsigned int GetNumberOfChildren(tree::Node* n) { return 0; }
111     //====================================================================
112
113         //====================================================================
114     /// Returns the attribute requested. Useful for synchronization.
115         virtual void GetAttribute(std::string levelDescriptor,
116                                                                            std::string searchParam, 
117                                                                            std::string searchVal, 
118                                                                            std::string key, 
119                                                                            std::string& result){}
120     //====================================================================
121
122     //====================================================================
123     /// Recursively loads the children of node 'parent' until maxlevel 
124     // is reached.
125     // If maxlevel <= 0 then loads all the sub-tree rooted at parent 
126     // If parent == NULL or parent == tree then starts with the 'children' of 
127     // the tree itself.
128     // Returns the total number of children loaded.
129     virtual int LoadChildren(tree::Node* parent, int maxlevel) 
130     { return 0; }
131     //====================================================================
132
133     //====================================================================
134     /// Unloads the Node and its descendants
135     // WITHOUT altering the source, e.g. the database
136     virtual void UnLoad(tree::Node* n) { return; }
137     //====================================================================
138
139         //====================================================================
140     /// Returns the top level node id for the given search param and search value
141     virtual void GetTopLevelNodeId(const std::string& searchParam, 
142                                                                                         const std::string& searchValue, 
143                                                                                         std::string& parent_id){ return; }
144     ///====================================================================
145
146
147     //====================================================================
148     // WRITE METHODS : WORK ONLY IN WRITE MODE
149     //====================================================================
150     typedef tree::Node::AttributeMapType AttributeMapType;
151     /// Adds a branch in the tree with the attributes provided
152     // returns the Level in the tree where the branch was connected 
153     // (-1 for error, 0 for top level, etc. ) 
154     // Of course the branch is loaded on exit
155     virtual int AddBranch( const AttributeMapType& ) { return -1; }
156     /// Removes the node and its descendants 
157     virtual bool Remove(tree::Node*)  { return false; }
158     /// Sets an attribute of a Node
159     virtual bool SetAttribute(tree::Node*, 
160                               const std::string& key,
161                               const std::string& value) { return false; }
162         // Sets an attribute
163     virtual void SetAttribute(const std::string& levelDescriptor, 
164                               const std::string& key,
165                               const std::string& value,
166                                   const std::string& searchParam, 
167                                   const std::string& searchVal){}
168         //Deletes the tuple that matches the parameters given
169         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value){}
170         //Deletes the entries that match the parameters given
171         virtual void RemoveEntries(const std::string i_table, 
172                 const std::string i_attribute, 
173                 const std::string i_operand, 
174                 const std::string i_val){}
175  
176     //====================================================================
177         /// get all attributes for a file
178         virtual void getAllAttributes(std::string i_filename, std::map<std::string, std::string> &i_results) =0;
179
180   private:
181     /// The handled tree
182     tree::Tree mTree;
183
184   };
185   // EO class TreeHandler
186   //=======================================================================
187   /*
188   //=======================================================================
189   /// Memorizes statistics on operations done by a tree handler
190   // (nodes created, removed, ...)
191   class TreeHandlerStatistics
192   {
193   public:
194     //====================================================================
195     /// Ctor
196     TreeHandler(TreeHandler* tree) : mTreeHandler(tree) { Reset(); }
197     /// Dtor
198     ~TreeHandler() {}
199     /// Resets the stats
200     void Reset();
201     /// Prints the stats
202     void Print();
203
204     /// 
205     void CreateNode(int level) { mNumberCreatedNode[level]++; }
206     void DeleteNode(int level) { mNumberDeletedNode[level]++; }
207
208   protected:
209       TreeHandler* mTreeHandler;
210     std::vector<int> mNumberCreatedNode;
211     std::vector<int> mNumberDeletedNode;
212     
213     
214     ///====================================================================
215   };
216   // EO class TreeHandlerStatistics
217   //=======================================================================
218   */
219
220 } // EO namespace creaImageIO
221
222 // EOF
223 #endif  
224