Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIPersistenceService.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 #ifndef mitkIPersistenceService_h
17 #define mitkIPersistenceService_h
18 
19 // mitk
21 #include "usGetModuleContext.h"
22 #include "usModuleContext.h"
23 #include "usServiceReference.h"
24 // for microservices
25 #include "mitkDataStorage.h"
26 #include <mitkServiceInterface.h>
27 
28 namespace mitk
29 {
45  {
46  public:
53  virtual mitk::PropertyList::Pointer GetPropertyList(std::string &id, bool *existed = 0) = 0;
58  virtual bool RemovePropertyList(std::string &id) = 0;
62  virtual std::string GetDefaultPersistenceFile() = 0;
67  virtual std::string GetPersistenceNodePropertyName() = 0;
74  virtual DataStorage::SetOfObjects::Pointer GetDataNodes(DataStorage *ds = 0) = 0;
79  virtual bool RestorePropertyListsFromPersistentDataNodes(const DataStorage *storage) = 0;
87  virtual bool Save(const std::string &fileName = "", bool appendChanges = false) = 0;
97  virtual bool Load(const std::string &fileName = "", bool enforeReload = true) = 0;
102  virtual void SetAutoLoadAndSave(bool autoLoadAndSave) = 0;
106  virtual bool GetAutoLoadAndSave() = 0;
110  virtual void AddPropertyListReplacedObserver(PropertyListReplacedObserver *observer) = 0;
114  virtual void RemovePropertyListReplacedObserver(PropertyListReplacedObserver *observer) = 0;
118  virtual ~IPersistenceService();
119  };
120 }
121 
122 // MACROS FOR AUTOMATIC SAVE FUNCTION
123 #define PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION us::GetModuleContext()
124 
125 #define PERSISTENCE_GET_SERVICE_MACRO \
126  mitk::IPersistenceService *persistenceService = 0; \
127  us::ModuleContext *context = PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION; \
128  if (context) \
129  { \
130  us::ServiceReference<mitk::IPersistenceService> persistenceServiceRef = \
131  context->GetServiceReference<mitk::IPersistenceService>(); \
132  if (persistenceServiceRef) \
133  { \
134  persistenceService = dynamic_cast<mitk::IPersistenceService *>( \
135  context->GetService<mitk::IPersistenceService>(persistenceServiceRef)); \
136  } \
137  }
138 
139 #define PERSISTENCE_GET_SERVICE_METHOD_MACRO \
140  mitk::IPersistenceService *GetPersistenceService() const \
141  { \
142  PERSISTENCE_GET_SERVICE_MACRO \
143  return persistenceService; \
144  }
145 
146 #define PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
147  \
148 public: \
149  bool Save(const std::string &fileName = "", bool appendChanges = false) \
150  { \
151  mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
152  bool noError = persistenceService != 0; \
153  if (noError) \
154  this->ToPropertyList(); \
155  if (noError) \
156  noError = persistenceService->Save(fileName, appendChanges); \
157  return noError; \
158  } \
159  bool Load(const std::string &fileName = "", bool enforeReload = true) \
160  { \
161  mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
162  bool noError = persistenceService != 0 && persistenceService->Load(fileName, enforeReload); \
163  if (noError) \
164  { \
165  this->FromPropertyList(); \
166  } \
167  return noError; \
168  } \
169  void ToPropertyList() \
170  { \
171  mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
172  this->InitializePropertyListReplacedObserver(persistenceService); \
173  if (!persistenceService) \
174  return; \
175  mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME);
176 
177 #define PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
178  } \
179  void FromPropertyList() \
180  { \
181  mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
182  this->InitializePropertyListReplacedObserver(persistenceService); \
183  if (!persistenceService) \
184  return; \
185  mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME);
186 
187 #define PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) \
188  } \
189  \
190  std::string GetId() const { return ID_MEMBER_NAME; } \
191 private: \
192  PERSISTENCE_GET_SERVICE_METHOD_MACRO \
193  struct MyPropertyListReplacedObserver : public mitk::PropertyListReplacedObserver \
194  { \
195  MyPropertyListReplacedObserver() : m_##THE_CLASS_NAME(0), m_PersistenceService(0) {} \
196  ~MyPropertyListReplacedObserver() \
197  { \
198  if (m_PersistenceService) \
199  m_PersistenceService->RemovePropertyListReplacedObserver(this); \
200  } \
201  void AfterPropertyListReplaced(const std::string &id, mitk::PropertyList *propertyList) \
202  { \
203  if (m_##THE_CLASS_NAME && m_##THE_CLASS_NAME->GetId() == id) \
204  m_##THE_CLASS_NAME->FromPropertyList(); \
205  } \
206  void Initialize(THE_CLASS_NAME *_##THE_CLASS_NAME, mitk::IPersistenceService *persistenceService) \
207  { \
208  m_##THE_CLASS_NAME = _##THE_CLASS_NAME; \
209  m_PersistenceService = persistenceService; \
210  if (m_PersistenceService) \
211  m_PersistenceService->AddPropertyListReplacedObserver(this); \
212  } \
213  \
214  private: \
215  THE_CLASS_NAME *m_##THE_CLASS_NAME; \
216  mitk::IPersistenceService *m_PersistenceService; \
217  }; \
218  MyPropertyListReplacedObserver m_MyPropertyListReplacedObserver; \
219  void InitializePropertyListReplacedObserver(mitk::IPersistenceService *persistenceService) \
220  { \
221  static bool observerInitialized = false; \
222  if (observerInitialized == false && persistenceService) \
223  { \
224  m_MyPropertyListReplacedObserver.Initialize(this, persistenceService); \
225  observerInitialized = true; \
226  } \
227  }
228 
229 #define PERSISTENCE_CREATE(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME) \
230  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
231  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
232  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
233  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
234  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
235 
236 #define PERSISTENCE_CREATE2(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME) \
237  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
238  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
239  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
240  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
241  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
242  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
243  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
244 
245 #define PERSISTENCE_CREATE3(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME) \
246  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
247  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
248  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
249  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
250  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
251  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
252  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
253  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
254  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
255 
256 #define PERSISTENCE_CREATE4( \
257  THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME) \
258  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
259  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
260  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
261  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
262  propList->Set(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
263  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
264  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
265  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
266  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
267  propList->Get(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
268  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
269 
270 #define PERSISTENCE_CREATE5(THE_CLASS_NAME, \
271  ID_MEMBER_NAME, \
272  PARAM_MEMBER_NAME, \
273  PARAM2_MEMBER_NAME, \
274  PARAM3_MEMBER_NAME, \
275  PARAM4_MEMBER_NAME, \
276  PARAM5_MEMBER_NAME) \
277  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
278  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
279  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
280  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
281  propList->Set(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
282  propList->Set(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
283  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
284  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
285  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
286  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
287  propList->Get(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
288  propList->Get(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
289  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
290 
291 #define PERSISTENCE_CREATE6(THE_CLASS_NAME, \
292  ID_MEMBER_NAME, \
293  PARAM_MEMBER_NAME, \
294  PARAM2_MEMBER_NAME, \
295  PARAM3_MEMBER_NAME, \
296  PARAM4_MEMBER_NAME, \
297  PARAM5_MEMBER_NAME, \
298  PARAM6_MEMBER_NAME) \
299  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
300  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
301  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
302  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
303  propList->Set(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
304  propList->Set(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
305  propList->Set(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
306  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
307  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
308  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
309  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
310  propList->Get(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
311  propList->Get(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
312  propList->Get(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
313  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
314 
315 #define PERSISTENCE_CREATE7(THE_CLASS_NAME, \
316  ID_MEMBER_NAME, \
317  PARAM_MEMBER_NAME, \
318  PARAM2_MEMBER_NAME, \
319  PARAM3_MEMBER_NAME, \
320  PARAM4_MEMBER_NAME, \
321  PARAM5_MEMBER_NAME, \
322  PARAM6_MEMBER_NAME, \
323  PARAM7_MEMBER_NAME) \
324  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
325  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
326  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
327  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
328  propList->Set(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
329  propList->Set(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
330  propList->Set(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
331  propList->Set(#PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME); \
332  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
333  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
334  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
335  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
336  propList->Get(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
337  propList->Get(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
338  propList->Get(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
339  propList->Get(#PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME); \
340  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
341 
342 #define PERSISTENCE_CREATE8(THE_CLASS_NAME, \
343  ID_MEMBER_NAME, \
344  PARAM_MEMBER_NAME, \
345  PARAM2_MEMBER_NAME, \
346  PARAM3_MEMBER_NAME, \
347  PARAM4_MEMBER_NAME, \
348  PARAM5_MEMBER_NAME, \
349  PARAM6_MEMBER_NAME, \
350  PARAM7_MEMBER_NAME, \
351  PARAM8_MEMBER_NAME) \
352  PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
353  propList->Set(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
354  propList->Set(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
355  propList->Set(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
356  propList->Set(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
357  propList->Set(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
358  propList->Set(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
359  propList->Set(#PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME); \
360  propList->Set(#PARAM8_MEMBER_NAME, PARAM8_MEMBER_NAME); \
361  PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
362  propList->Get(#PARAM_MEMBER_NAME, PARAM_MEMBER_NAME); \
363  propList->Get(#PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME); \
364  propList->Get(#PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME); \
365  propList->Get(#PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME); \
366  propList->Get(#PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME); \
367  propList->Get(#PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME); \
368  propList->Get(#PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME); \
369  propList->Get(#PARAM8_MEMBER_NAME, PARAM8_MEMBER_NAME); \
370  PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)
371 
372 MITK_DECLARE_SERVICE_INTERFACE(mitk::IPersistenceService, "org.mitk.services.IPersistenceService")
373 
374 #endif
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
#define MITKCORE_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
MITK_DECLARE_SERVICE_INTERFACE(mitk::ISimulationService,"org.mitk.ISimulationService")