12 #ifndef mitkIPersistenceService_h
13 #define mitkIPersistenceService_h
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;
119 #define PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION us::GetModuleContext()
121 #define PERSISTENCE_GET_SERVICE_MACRO \
122 mitk::IPersistenceService *persistenceService = 0; \
123 us::ModuleContext *context = PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION; \
126 us::ServiceReference<mitk::IPersistenceService> persistenceServiceRef = \
127 context->GetServiceReference<mitk::IPersistenceService>(); \
128 if (persistenceServiceRef) \
130 persistenceService = dynamic_cast<mitk::IPersistenceService *>( \
131 context->GetService<mitk::IPersistenceService>(persistenceServiceRef)); \
135 #define PERSISTENCE_GET_SERVICE_METHOD_MACRO \
136 mitk::IPersistenceService *GetPersistenceService() const \
138 PERSISTENCE_GET_SERVICE_MACRO \
139 return persistenceService; \
142 #define PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME) \
145 bool Save(const std::string &fileName = "", bool appendChanges = false) \
147 mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
148 bool noError = persistenceService != 0; \
150 this->ToPropertyList(); \
152 noError = persistenceService->Save(fileName, appendChanges); \
155 bool Load(const std::string &fileName = "", bool enforeReload = true) \
157 mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
158 bool noError = persistenceService != 0 && persistenceService->Load(fileName, enforeReload); \
161 this->FromPropertyList(); \
165 void ToPropertyList() \
167 mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
168 this->InitializePropertyListReplacedObserver(persistenceService); \
169 if (!persistenceService) \
171 mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME);
173 #define PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME) \
175 void FromPropertyList() \
177 mitk::IPersistenceService *persistenceService = this->GetPersistenceService(); \
178 this->InitializePropertyListReplacedObserver(persistenceService); \
179 if (!persistenceService) \
181 mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME);
183 #define PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) \
186 std::string GetId() const { return ID_MEMBER_NAME; } \
188 PERSISTENCE_GET_SERVICE_METHOD_MACRO \
189 struct MyPropertyListReplacedObserver : public mitk::PropertyListReplacedObserver \
191 MyPropertyListReplacedObserver() : m_##THE_CLASS_NAME(0), m_PersistenceService(0) {} \
192 ~MyPropertyListReplacedObserver() \
194 if (m_PersistenceService) \
195 m_PersistenceService->RemovePropertyListReplacedObserver(this); \
197 void AfterPropertyListReplaced(const std::string &id, mitk::PropertyList *) \
199 if (m_##THE_CLASS_NAME && m_##THE_CLASS_NAME->GetId() == id) \
200 m_##THE_CLASS_NAME->FromPropertyList(); \
202 void Initialize(THE_CLASS_NAME *_##THE_CLASS_NAME, mitk::IPersistenceService *persistenceService) \
204 m_##THE_CLASS_NAME = _##THE_CLASS_NAME; \
205 m_PersistenceService = persistenceService; \
206 if (m_PersistenceService) \
207 m_PersistenceService->AddPropertyListReplacedObserver(this); \
211 THE_CLASS_NAME *m_##THE_CLASS_NAME; \
212 mitk::IPersistenceService *m_PersistenceService; \
214 MyPropertyListReplacedObserver m_MyPropertyListReplacedObserver; \
215 void InitializePropertyListReplacedObserver(mitk::IPersistenceService *persistenceService) \
217 static bool observerInitialized = false; \
218 if (observerInitialized == false && persistenceService) \
220 m_MyPropertyListReplacedObserver.Initialize(this, persistenceService); \
221 observerInitialized = true; \
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)
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)
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)
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)
266 #define PERSISTENCE_CREATE5(THE_CLASS_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)
287 #define PERSISTENCE_CREATE6(THE_CLASS_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)
311 #define PERSISTENCE_CREATE7(THE_CLASS_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)
338 #define PERSISTENCE_CREATE8(THE_CLASS_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)