Medical Imaging Interaction Toolkit  2023.04.00
Medical Imaging Interaction Toolkit
mitkPreferences.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 
13 #ifndef mitkPreferences_h
14 #define mitkPreferences_h
15 
16 #include <mitkIPreferences.h>
17 
18 #include <memory>
19 #include <unordered_map>
20 
21 namespace mitk
22 {
23  class IPreferencesStorage;
24 
32  class Preferences : public IPreferences
33  {
34  public:
35  using Properties = std::unordered_map<std::string, std::string>;
36 
37  Preferences(const Properties& properties, const std::string& name, Preferences* parent, IPreferencesStorage* storage);
38  ~Preferences() override;
39 
40  Preferences(const Preferences&) = delete;
41  Preferences& operator=(const Preferences&) = delete;
42 
43  Preferences(Preferences&&) = default;
44  Preferences& operator=(Preferences&&) = default;
45 
46  std::string Get(const std::string& key, const std::string& def) const override;
47  void Put(const std::string& key, const std::string& value) override;
48 
49  int GetInt(const std::string& key, int def) const override;
50  void PutInt(const std::string& key, int value) override;
51 
52  bool GetBool(const std::string& key, bool def) const override;
53  void PutBool(const std::string& key, bool value) override;
54 
55  float GetFloat(const std::string& key, float def) const override;
56  void PutFloat(const std::string& key, float value) override;
57 
58  double GetDouble(const std::string& key, double def) const override;
59  void PutDouble(const std::string& key, double value) override;
60 
61  std::vector<std::byte> GetByteArray(const std::string& key, const std::byte* def, size_t size) const override;
62  void PutByteArray(const std::string& key, const std::byte* array, size_t size) override;
63 
64  void Remove(const std::string& key) override;
65  void Clear() override;
66  std::vector<std::string> Keys() const override;
67  std::vector<std::string> ChildrenNames() const override;
68  IPreferences* Parent() override;
69  const IPreferences* Parent() const override;
70  IPreferences* Root() override;
71  const IPreferences* Root() const override;
72  IPreferences* Node(const std::string& path) override;
73  void RemoveNode() override;
74  std::string Name() const override;
75  std::string AbsolutePath() const override;
76  void Flush() override;
77 
78  const Properties& GetProperties() const;
79  const std::vector<std::unique_ptr<Preferences>>& GetChildren() const;
80 
81  private:
82  template<typename T>
83  void Put(const std::string& key, const T& value, const std::function<std::string(const T&)>& toString)
84  {
85  const auto oldValue = m_Properties[key];
86  const auto newValue = toString(value);
87  m_Properties[key] = newValue;
88 
89  if (oldValue != newValue)
90  {
91  this->OnChanged(this);
92  this->OnPropertyChanged(ChangeEvent(this, key, oldValue, newValue));
93  }
94  }
95 
96  Properties m_Properties;
97  std::vector<std::unique_ptr<Preferences>> m_Children;
98  std::string m_Path;
99  std::string m_Name;
100  Preferences* m_Parent;
101  Preferences* m_Root;
102  IPreferencesStorage* m_Storage;
103  };
104 }
105 
106 #endif
mitk::Preferences::PutByteArray
void PutByteArray(const std::string &key, const std::byte *array, size_t size) override
Put binary data into a property value.
mitk::Preferences::GetByteArray
std::vector< std::byte > GetByteArray(const std::string &key, const std::byte *def, size_t size) const override
Get a property value as typeless binary representation.
mitk::Preferences::PutDouble
void PutDouble(const std::string &key, double value) override
Set a double property value.
mitk::Preferences::Properties
std::unordered_map< std::string, std::string > Properties
Definition: mitkPreferences.h:35
mitk::Preferences::Name
std::string Name() const override
Get the name of this preferences node.
mitk::IPreferences::ChangeEvent
Event object sent on IPreferences::OnPropertyChanged events.
Definition: mitkIPreferences.h:56
mitk::Preferences::GetProperties
const Properties & GetProperties() const
mitk::Preferences::Node
IPreferences * Node(const std::string &path) override
Get (and possibly create) the preferences node specified by the given path.
mitk::Preferences::Preferences
Preferences(const Properties &properties, const std::string &name, Preferences *parent, IPreferencesStorage *storage)
mitk::IPreferences::OnPropertyChanged
Message1< const ChangeEvent & > OnPropertyChanged
Notify on property changes.
Definition: mitkIPreferences.h:293
mitk::Preferences::GetDouble
double GetDouble(const std::string &key, double def) const override
Get a property value as double.
mitk::Preferences::Clear
void Clear() override
Remove all properties from this preferences node.
mitk::Preferences::ChildrenNames
std::vector< std::string > ChildrenNames() const override
Get the names of all direct child preference nodes.
mitk::Preferences::PutBool
void PutBool(const std::string &key, bool value) override
Set a bool property value.
mitk::Preferences::Keys
std::vector< std::string > Keys() const override
Get the names of all properties of this preferences node.
mitk::IPreferencesStorage
The backend for persistent preferences.
Definition: mitkIPreferencesStorage.h:31
mitk::Preferences::GetInt
int GetInt(const std::string &key, int def) const override
Get a property value as int.
mitk::Preferences::GetFloat
float GetFloat(const std::string &key, float def) const override
Get a property value as float.
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::Preferences::RemoveNode
void RemoveNode() override
Remove this preferences node from its parent node.
mitk::IPreferences::OnChanged
Message1< const IPreferences * > OnChanged
Notify on node changes.
Definition: mitkIPreferences.h:292
mitk::Preferences::PutInt
void PutInt(const std::string &key, int value) override
Set an int property value.
mitk::Preferences::Root
IPreferences * Root() override
Get the root preferences node.
mitk::Preferences
Implementation of the IPreferences interface.
Definition: mitkPreferences.h:32
mitk::Preferences::GetBool
bool GetBool(const std::string &key, bool def) const override
Get a property value as bool.
mitk::Preferences::PutFloat
void PutFloat(const std::string &key, float value) override
Set a float property value.
mitk::Preferences::Put
void Put(const std::string &key, const std::string &value) override
Set a property value.
mitk::Preferences::Remove
void Remove(const std::string &key) override
Remove a property from this preferences node.
mitk::Preferences::operator=
Preferences & operator=(const Preferences &)=delete
mitkIPreferences.h
mitk::IPreferences
Interface to application preferences.
Definition: mitkIPreferences.h:50
mitk::Preferences::GetChildren
const std::vector< std::unique_ptr< Preferences > > & GetChildren() const
mitk::Preferences::~Preferences
~Preferences() override
mitk::Preferences::Get
std::string Get(const std::string &key, const std::string &def) const override
Get a property value as string.
mitk::Preferences::Flush
void Flush() override
Write all (!) preferences to disk.
mitk::Preferences::AbsolutePath
std::string AbsolutePath() const override
Get the absolute path (relative to the root node) of this preferences node.
mitk::Preferences::Parent
IPreferences * Parent() override
Get the parent preferences node or nullptr in case of the root node.