Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::PersistenceService Class Reference

Concrete implementation of the IPersistenceService interface. More...

#include <mitkPersistenceService.h>

Inheritance diagram for mitk::PersistenceService:
Collaboration diagram for mitk::PersistenceService:

Public Member Functions

 PersistenceService ()
 
 ~PersistenceService () override
 
std::string GetDefaultPersistenceFile () override
 Get the default file path used for persisting property lists. More...
 
mitk::PropertyList::Pointer GetPropertyList (std::string &id, bool *existed=nullptr) override
 Retrieve or create a PropertyList identified by the given id. More...
 
bool RemovePropertyList (std::string &id) override
 Remove the PropertyList identified by the given id. More...
 
std::string GetPersistenceNodePropertyName () override
 Get the name of the boolean property used to tag persistence DataNodes. More...
 
DataStorage::SetOfObjects::Pointer GetDataNodes (DataStorage *ds=nullptr) override
 Create DataNodes from all stored PropertyLists and optionally add them to a DataStorage. More...
 
bool Save (const std::string &fileName="", bool appendChanges=false) override
 Save all property lists to a file. More...
 
bool Load (const std::string &fileName="", bool enforeReload=true) override
 Load property lists from a file. More...
 
void SetAutoLoadAndSave (bool autoLoadAndSave) override
 Enable or disable automatic loading and saving of property lists. More...
 
bool GetAutoLoadAndSave () override
 Query whether automatic loading and saving is enabled. More...
 
void AddPropertyListReplacedObserver (PropertyListReplacedObserver *observer) override
 Register an observer to be notified when a PropertyList is replaced during Load(). More...
 
void RemovePropertyListReplacedObserver (PropertyListReplacedObserver *observer) override
 Unregister a previously added PropertyListReplacedObserver. More...
 
bool RestorePropertyListsFromPersistentDataNodes (const DataStorage *storage) override
 Restore property lists from persistence-tagged DataNodes in a DataStorage. More...
 
void Clear ()
 Clear all stored property lists and file modification time records. More...
 
void Unitialize ()
 Shut down the persistence service. More...
 
- Public Member Functions inherited from mitk::IPersistenceService
virtual ~IPersistenceService ()
 Destructor. More...
 

Static Public Member Functions

static std::string GetPersistencePropertyName ()
 Get the property name used to mark DataNodes as persistence nodes. More...
 
static std::string GetPersistencePropertyListName ()
 Get the property list name used for internal persistence service settings. More...
 
static void LoadModule ()
 Trigger loading of the persistence module. More...
 
static us::ModuleContext * GetModuleContext ()
 Get the micro services module context for this module. More...
 

Detailed Description

Concrete implementation of the IPersistenceService interface.

This service manages a collection of named PropertyList objects that can be persisted to and restored from files. It supports both XML-based storage (via PropertyListsXmlFileReaderAndWriter) and MITK scene file storage (via SceneIO). The service is registered as a micro service and can be retrieved via the module context.

On first use, the service lazily initializes itself by loading the default persistence file. If auto-load-and-save is enabled, property lists are automatically saved when Unitialize() is called (typically at application shutdown).

See also
IPersistenceService
IPersistable
PropertyListsXmlFileReaderAndWriter

Definition at line 40 of file mitkPersistenceService.h.

Constructor & Destructor Documentation

◆ PersistenceService()

mitk::PersistenceService::PersistenceService ( )

◆ ~PersistenceService()

mitk::PersistenceService::~PersistenceService ( )
override

Member Function Documentation

◆ AddPropertyListReplacedObserver()

void mitk::PersistenceService::AddPropertyListReplacedObserver ( PropertyListReplacedObserver *  observer)
overridevirtual

Register an observer to be notified when a PropertyList is replaced during Load().

Parameters
[in]observerPointer to the observer to add. Must not be nullptr.
See also
RemovePropertyListReplacedObserver()

Implements mitk::IPersistenceService.

◆ Clear()

void mitk::PersistenceService::Clear ( )

Clear all stored property lists and file modification time records.

◆ GetAutoLoadAndSave()

bool mitk::PersistenceService::GetAutoLoadAndSave ( )
overridevirtual

Query whether automatic loading and saving is enabled.

Returns
True if auto-load-and-save is enabled, false otherwise.

Implements mitk::IPersistenceService.

◆ GetDataNodes()

DataStorage::SetOfObjects::Pointer mitk::PersistenceService::GetDataNodes ( DataStorage *  ds = nullptr)
overridevirtual

Create DataNodes from all stored PropertyLists and optionally add them to a DataStorage.

Each PropertyList is cloned into a new DataNode. The DataNode's name is set to the PropertyList's id, and a boolean property (named per GetPersistencePropertyName()) is set to true.

Parameters
[in,out]dsOptional DataStorage to which created DataNodes are added. May be nullptr.
Returns
A set of newly created DataNodes containing the persisted property lists.

Implements mitk::IPersistenceService.

◆ GetDefaultPersistenceFile()

std::string mitk::PersistenceService::GetDefaultPersistenceFile ( )
overridevirtual

Get the default file path used for persisting property lists.

Returns "PersistentData.xml" located in the module's persistent data directory, or just "PersistentData.xml" in the working directory if no data directory is available.

Returns
The absolute or relative path to the default persistence file.

Implements mitk::IPersistenceService.

◆ GetModuleContext()

static us::ModuleContext* mitk::PersistenceService::GetModuleContext ( )
static

Get the micro services module context for this module.

Returns
Pointer to the us::ModuleContext.

◆ GetPersistenceNodePropertyName()

std::string mitk::PersistenceService::GetPersistenceNodePropertyName ( )
overridevirtual

Get the name of the boolean property used to tag persistence DataNodes.

Returns
The persistence property name string.
See also
GetPersistencePropertyName()

Implements mitk::IPersistenceService.

◆ GetPersistencePropertyListName()

static std::string mitk::PersistenceService::GetPersistencePropertyListName ( )
static

Get the property list name used for internal persistence service settings.

Returns
The string "PersistenceService".

◆ GetPersistencePropertyName()

static std::string mitk::PersistenceService::GetPersistencePropertyName ( )
static

Get the property name used to mark DataNodes as persistence nodes.

Returns
The string "PersistenceNode".

◆ GetPropertyList()

mitk::PropertyList::Pointer mitk::PersistenceService::GetPropertyList ( std::string &  id,
bool *  existed = nullptr 
)
overridevirtual

Retrieve or create a PropertyList identified by the given id.

If the id is empty, a new UUID is generated and assigned to the id parameter. If a PropertyList with the given id already exists, it is returned; otherwise a new empty PropertyList is created and stored.

Parameters
[in,out]idThe identifier string. If empty, a UUID will be generated and written back.
[out]existedOptional output flag; set to true if the PropertyList already existed, false if newly created.
Returns
A valid PropertyList associated with the given id.

Implements mitk::IPersistenceService.

◆ Load()

bool mitk::PersistenceService::Load ( const std::string &  fileName = "",
bool  enforeReload = true 
)
overridevirtual

Load property lists from a file.

If the file has an ".xml" extension, property lists are read as XML via PropertyListsXmlFileReaderAndWriter. Otherwise, the MITK SceneIO format is used. If enforceReload is false, the file is only reloaded when its modification time has changed since the last load.

Parameters
[in]fileNamePath to the input file. If empty, the default persistence file is used.
[in]enforeReloadIf true, always reload; if false, skip reloading unchanged files.
Returns
True on success, false if an error occurred (e.g., file not found or parse error).
Note
Existing PropertyLists with matching ids will be overwritten.
See also
AddPropertyListReplacedObserver()

Implements mitk::IPersistenceService.

◆ LoadModule()

static void mitk::PersistenceService::LoadModule ( )
static

Trigger loading of the persistence module.

This is a no-op beyond logging; it exists so that the module is loaded by the micro services framework on demand.

◆ RemovePropertyList()

bool mitk::PersistenceService::RemovePropertyList ( std::string &  id)
overridevirtual

Remove the PropertyList identified by the given id.

Parameters
[in]idThe identifier of the PropertyList to remove.
Returns
True if a PropertyList with the given id existed and was removed, false otherwise.

Implements mitk::IPersistenceService.

◆ RemovePropertyListReplacedObserver()

void mitk::PersistenceService::RemovePropertyListReplacedObserver ( PropertyListReplacedObserver *  observer)
overridevirtual

Unregister a previously added PropertyListReplacedObserver.

Parameters
[in]observerPointer to the observer to remove.
See also
AddPropertyListReplacedObserver()

Implements mitk::IPersistenceService.

◆ RestorePropertyListsFromPersistentDataNodes()

bool mitk::PersistenceService::RestorePropertyListsFromPersistentDataNodes ( const DataStorage *  storage)
overridevirtual

Restore property lists from persistence-tagged DataNodes in a DataStorage.

Scans all DataNodes in the given storage for ones marked with the persistence property. For each such node, its properties are cloned into the corresponding PropertyList managed by this service. Registered PropertyListReplacedObservers are notified before and after replacement.

Parameters
[in]storageThe DataStorage to scan for persistence nodes. Must not be nullptr.
Returns
True if at least one persistence node was found and restored, false otherwise.

Implements mitk::IPersistenceService.

◆ Save()

bool mitk::PersistenceService::Save ( const std::string &  fileName = "",
bool  appendChanges = false 
)
overridevirtual

Save all property lists to a file.

If the file has an ".xml" extension, property lists are written as XML via PropertyListsXmlFileReaderAndWriter. Otherwise, the MITK SceneIO format is used. Directories are created as needed.

Parameters
[in]fileNamePath to the output file. If empty, the default persistence file is used.
[in]appendChangesIf true, existing data in the file is loaded first and merged before saving.
Returns
True on success, false if an error occurred (e.g., cannot write to file).

Implements mitk::IPersistenceService.

◆ SetAutoLoadAndSave()

void mitk::PersistenceService::SetAutoLoadAndSave ( bool  autoLoadAndSave)
overridevirtual

Enable or disable automatic loading and saving of property lists.

When enabled, property lists are automatically loaded at initialization and saved at shutdown (via Unitialize()).

Parameters
[in]autoLoadAndSaveTrue to enable auto-load-and-save, false to disable.

Implements mitk::IPersistenceService.

◆ Unitialize()

void mitk::PersistenceService::Unitialize ( )

Shut down the persistence service.

If auto-load-and-save is enabled, all property lists are saved to the default file before shutdown.


The documentation for this class was generated from the following file: