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