ScummVM API documentation
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 AGS_PLUGINS_SERIALIZER_H
23 #define AGS_PLUGINS_SERIALIZER_H
24 
25 #include "ags/shared/util/iags_stream.h"
26 #include "ags/plugins/ags_plugin.h"
27 #include "common/serializer.h"
28 
29 namespace AGS3 {
30 namespace Plugins {
31 
32 class Serializer {
33 private:
34  IAGSEngine *_engine;
35  long _file;
36  bool _isLoading;
37  public:
38  Serializer(IAGSEngine *engine, long file, bool isLoading) :
39  _engine(engine), _file(file), _isLoading(isLoading) {}
40 
41  bool isLoading() const {
42  return _isLoading;
43  }
44  bool isSaving() const {
45  return !_isLoading;
46  }
47 
48  template<typename T>
49  void syncAsInt(T &value) {
50  byte buf[4];
51  if (_isLoading) {
52  _engine->FRead(buf, 4, _file);
53  value = READ_LE_INT32(buf);
54  } else {
55  WRITE_LE_UINT32(buf, value);
56  _engine->FWrite(buf, 4, _file);
57  }
58  }
59 
60  void syncAsBool(bool &value) {
61  if (_isLoading)
62  _engine->FRead(&value, 1, _file);
63  else
64  _engine->FWrite(&value, 1, _file);
65  }
66 
67  void syncAsInt8(int8 &value) {
68  if (_isLoading)
69  _engine->FRead(&value, 1, _file);
70  else
71  _engine->FWrite(&value, 1, _file);
72  }
73 
74  void syncAsByte(byte &value) {
75  if (_isLoading)
76  _engine->FRead(&value, 1, _file);
77  else
78  _engine->FWrite(&value, 1, _file);
79  }
80 
81  void syncAsFloat(float &value) {
82  if (_isLoading)
83  _engine->FRead(&value, sizeof(float), _file);
84  else
85  _engine->FWrite(&value, sizeof(float), _file);
86  }
87 
88  void syncAsDouble(double &value) {
89  if (_isLoading)
90  _engine->FRead(&value, sizeof(double), _file);
91  else
92  _engine->FWrite(&value, sizeof(double), _file);
93  }
94 
95  void unreadInt() {
96  _engine->FSeek(-4, AGS::Shared::kSeekCurrent, _file);
97  }
98 };
99 
100 } // namespace Plugins
101 } // namespace AGS3
102 
103 #endif
Definition: serializer.h:32
Definition: ags_plugin.h:296
Definition: ags.h:40