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