MythTV  master
configuration.h
Go to the documentation of this file.
1 // Program Name: configuration.h
3 // Created : Feb. 12, 2007
4 //
5 // Purpose : XML Configuration file Class
6 //
7 // Copyright (c) 2007 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef CONFIGURATION_H
14 #define CONFIGURATION_H
15 
16 #include <QDomDocument>
17 #include <QStringList>
18 #include "mythbaseexp.h"
19 
21 {
22  public:
23 
24  virtual ~Configuration() = default;
25 
26  virtual bool Load ( void ) = 0;
27  virtual bool Save ( void ) = 0;
28 
29  virtual int GetValue( const QString &sSetting, int Default ) = 0;
30  virtual QString GetValue( const QString &sSetting, const QString &Default ) = 0;
31  virtual bool GetBoolValue( const QString &sSetting, bool Default ) = 0;
32 
33  virtual void SetValue( const QString &sSetting, int value ) = 0;
34  virtual void SetValue( const QString &sSetting, const QString &value ) = 0;
35  virtual void ClearValue( const QString &sSetting ) = 0;
36  virtual void SetBoolValue( const QString &sSetting, bool value ) = 0;
37 
38  template <typename T>
39  typename std::enable_if<std::chrono::__is_duration<T>::value, T>::type
40  GetDuration(const QString &sSetting, T defaultval = T::zero())
41  { return T(GetValue(sSetting, static_cast<int>(defaultval.count()))); }
42  template <typename T>
43  typename std::enable_if<std::chrono::__is_duration<T>::value, void>::type
44  SetDuration(const QString &sSetting, T value)
45  { SetValue(sSetting, static_cast<int>(value.count())); }
46 };
47 
48 
50 //
51 // **NOTE: Performance Issue ***
52 //
53 // This class loads an XML file into a QDomDocument. All requests for
54 // getting or setting values navigates the DOM each time. All settings should
55 // only be read/written once if possible.
56 //
58 
60 {
61  protected:
62 
63  QString m_sPath;
64  QString m_sFileName;
65 
66  QDomDocument m_config;
67  QDomNode m_rootNode;
68 
69  QDomNode FindNode( const QString &sName, bool bCreate = false );
70  QDomNode FindNode( QStringList &sParts, QDomNode &curNode, bool bCreate = false );
71 
72  public:
73 
74  explicit XmlConfiguration( const QString &sFileName );
75 
76  ~XmlConfiguration() override = default;
77 
78  bool Load ( void ) override; // Configuration
79  bool Save ( void ) override; // Configuration
80 
81  int GetValue( const QString &sSetting, int Default ) override; // Configuration
82  QString GetValue( const QString &sSetting, const QString &Default ) override; // Configuration
83  bool GetBoolValue( const QString &sSetting, bool Default ) override // Configuration
84  {return static_cast<bool>(GetValue(sSetting, static_cast<int>(Default))); }
85 
86  void SetValue( const QString &sSetting, int value ) override; // Configuration
87  void SetValue( const QString &sSetting, const QString &value ) override; // Configuration
88  void ClearValue( const QString &sSetting ) override; // Configuration
89  void SetBoolValue( const QString &sSetting, bool value ) override // Configuration
90  {SetValue(sSetting, static_cast<int>(value)); }
91 };
92 
94 //
96 
98 {
99  public:
100 
101  DBConfiguration() = default;
102 
103  ~DBConfiguration() override = default;
104 
105  bool Load ( void ) override; // Configuration
106  bool Save ( void ) override; // Configuration
107 
108  int GetValue( const QString &sSetting, int Default ) override; // Configuration
109  QString GetValue( const QString &sSetting, const QString &Default ) override; // Configuration
110  bool GetBoolValue( const QString &sSetting, bool Default ) override // Configuration
111  {return static_cast<bool>(GetValue(sSetting, static_cast<int>(Default))); }
112 
113  void SetValue( const QString &sSetting, int value ) override; // Configuration
114  void SetValue( const QString &sSetting, const QString &value ) override; // Configuration
115  void ClearValue( const QString &sSetting ) override; // Configuration
116  void SetBoolValue( const QString &sSetting, bool value ) override // Configuration
117  {SetValue(sSetting, static_cast<int>(value)); }
118 };
119 
120 #endif // CONFIGURATION_H
Configuration::GetDuration
std::enable_if< std::chrono::__is_duration< T >::value, T >::type GetDuration(const QString &sSetting, T defaultval=T::zero())
Definition: configuration.h:40
Configuration::SetDuration
std::enable_if< std::chrono::__is_duration< T >::value, void >::type SetDuration(const QString &sSetting, T value)
Definition: configuration.h:44
XmlConfiguration::m_sFileName
QString m_sFileName
Definition: configuration.h:64
mythbaseexp.h
Configuration::Load
virtual bool Load(void)=0
XmlConfiguration::m_rootNode
QDomNode m_rootNode
Definition: configuration.h:67
DBConfiguration::SetBoolValue
void SetBoolValue(const QString &sSetting, bool value) override
Definition: configuration.h:116
XmlConfiguration
Definition: configuration.h:59
Configuration::ClearValue
virtual void ClearValue(const QString &sSetting)=0
Configuration::SetValue
virtual void SetValue(const QString &sSetting, int value)=0
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
XmlConfiguration::m_config
QDomDocument m_config
Definition: configuration.h:66
XmlConfiguration::GetBoolValue
bool GetBoolValue(const QString &sSetting, bool Default) override
Definition: configuration.h:83
DBConfiguration::GetBoolValue
bool GetBoolValue(const QString &sSetting, bool Default) override
Definition: configuration.h:110
XmlConfiguration::SetBoolValue
void SetBoolValue(const QString &sSetting, bool value) override
Definition: configuration.h:89
Configuration
Definition: configuration.h:20
Configuration::Save
virtual bool Save(void)=0
Configuration::GetValue
virtual int GetValue(const QString &sSetting, int Default)=0
DBConfiguration
Definition: configuration.h:97
XmlConfiguration::m_sPath
QString m_sPath
Definition: configuration.h:63