Update readme.md
[openttd-joker.git] / src / saveload / saveload_filter.h
blobf8db35ae0076f65cd53c525ab02db4d738e6c1d0
1 /* $Id: saveload_filter.h 21395 2010-12-05 14:41:34Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file saveload_filter.h Declaration of filters used for saving and loading savegames. */
12 #ifndef SAVELOAD_FILTER_H
13 #define SAVELOAD_FILTER_H
15 /** Interface for filtering a savegame till it is loaded. */
16 struct LoadFilter {
17 /** Chained to the (savegame) filters. */
18 LoadFilter *chain;
20 /**
21 * Initialise this filter.
22 * @param chain The next filter in this chain.
24 LoadFilter(LoadFilter *chain) : chain(chain)
28 /** Make sure the writers are properly closed. */
29 virtual ~LoadFilter()
31 delete this->chain;
34 /**
35 * Read a given number of bytes from the savegame.
36 * @param buf The bytes to read.
37 * @param len The number of bytes to read.
38 * @return The number of actually read bytes.
40 virtual size_t Read(byte *buf, size_t len) = 0;
42 /**
43 * Reset this filter to read from the beginning of the file.
45 virtual void Reset()
47 this->chain->Reset();
51 /**
52 * Instantiator for a load filter.
53 * @param chain The next filter in this chain.
54 * @tparam T The type of load filter to create.
56 template <typename T> LoadFilter *CreateLoadFilter(LoadFilter *chain)
58 return new T(chain);
61 /** Interface for filtering a savegame till it is written. */
62 struct SaveFilter {
63 /** Chained to the (savegame) filters. */
64 SaveFilter *chain;
66 /**
67 * Initialise this filter.
68 * @param chain The next filter in this chain.
70 SaveFilter(SaveFilter *chain) : chain(chain)
74 /** Make sure the writers are properly closed. */
75 virtual ~SaveFilter()
77 delete this->chain;
80 /**
81 * Write a given number of bytes into the savegame.
82 * @param buf The bytes to write.
83 * @param len The number of bytes to write.
85 virtual void Write(byte *buf, size_t len) = 0;
87 /**
88 * Prepare everything to finish writing the savegame.
90 virtual void Finish()
92 if (this->chain != nullptr) this->chain->Finish();
96 /**
97 * Instantiator for a save filter.
98 * @param chain The next filter in this chain.
99 * @param compression_level The requested level of compression.
100 * @tparam T The type of save filter to create.
102 template <typename T> SaveFilter *CreateSaveFilter(SaveFilter *chain, byte compression_level)
104 return new T(chain, compression_level);
107 #endif /* SAVELOAD_FILTER_H */