ScummVM API documentation
conf_serializer.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SHARED_CONF_CONF_SERIALIZER_H
23 #define SHARED_CONF_CONF_SERIALIZER_H
24 
25 #include "common/config-manager.h"
26 
27 namespace Ultima {
28 namespace Shared {
29 
34 private:
35  bool _isSaving;
36 public:
40  ConfSerializer(bool saving) : _isSaving(saving) {}
41 
46  if (_isSaving)
47  ConfMan.flushToDisk();
48  }
49 
53  bool isSaving() const {
54  return _isSaving;
55  }
56 
60  bool isLoading() const {
61  return !_isSaving;
62  }
63 
67  void syncAsString(const Common::String &key, Common::String &value,
68  const char *defaultValue = nullptr) {
69  if (_isSaving)
70  ConfMan.set(key, value);
71  else
72  value = ConfMan.hasKey(key) ? ConfMan.get(key) : Common::String(defaultValue);
73  }
74 
78  void syncAsBool(const Common::String &key, bool &value,
79  bool defaultValue = false) {
80  if (_isSaving)
81  ConfMan.setBool(key, value);
82  else
83  value = ConfMan.hasKey(key) ? ConfMan.getBool(key) : defaultValue;
84  }
85 
89  template<typename T>
90  void syncAsInt(const Common::String &key, T &value, T defaultValue = 0) {
91  if (_isSaving)
92  ConfMan.setInt(key, value);
93  else
94  value = ConfMan.hasKey(key) ? ConfMan.getInt(key) : defaultValue;
95  }
96 };
97 
98 } // End of namespace Shared
99 } // End of namespace Ultima
100 
101 #endif
Definition: conf_serializer.h:33
Definition: str.h:59
ConfSerializer(bool saving)
Definition: conf_serializer.h:40
void syncAsBool(const Common::String &key, bool &value, bool defaultValue=false)
Definition: conf_serializer.h:78
~ConfSerializer()
Definition: conf_serializer.h:45
void syncAsString(const Common::String &key, Common::String &value, const char *defaultValue=nullptr)
Definition: conf_serializer.h:67
Definition: detection.h:27
bool isLoading() const
Definition: conf_serializer.h:60
bool isSaving() const
Definition: conf_serializer.h:53
void syncAsInt(const Common::String &key, T &value, T defaultValue=0)
Definition: conf_serializer.h:90