Update: Translations from eints
[openttd-github.git] / src / saveload / saveload_filter.h
blob13702f34f526d4e97518184440b3d36fe53fe8f8
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file saveload_filter.h Declaration of filters used for saving and loading savegames. */
10 #ifndef SAVELOAD_FILTER_H
11 #define SAVELOAD_FILTER_H
13 /** Interface for filtering a savegame till it is loaded. */
14 struct LoadFilter {
15 /** Chained to the (savegame) filters. */
16 std::shared_ptr<LoadFilter> chain;
18 /**
19 * Initialise this filter.
20 * @param chain The next filter in this chain.
22 LoadFilter(std::shared_ptr<LoadFilter> chain) : chain(chain)
26 /** Make sure the writers are properly closed. */
27 virtual ~LoadFilter()
31 /**
32 * Read a given number of bytes from the savegame.
33 * @param buf The bytes to read.
34 * @param len The number of bytes to read.
35 * @return The number of actually read bytes.
37 virtual size_t Read(uint8_t *buf, size_t len) = 0;
39 /**
40 * Reset this filter to read from the beginning of the file.
42 virtual void Reset()
44 this->chain->Reset();
48 /**
49 * Instantiator for a load filter.
50 * @param chain The next filter in this chain.
51 * @tparam T The type of load filter to create.
53 template <typename T> std::shared_ptr<LoadFilter> CreateLoadFilter(std::shared_ptr<LoadFilter> chain)
55 return std::make_shared<T>(chain);
58 /** Interface for filtering a savegame till it is written. */
59 struct SaveFilter {
60 /** Chained to the (savegame) filters. */
61 std::shared_ptr<SaveFilter> chain;
63 /**
64 * Initialise this filter.
65 * @param chain The next filter in this chain.
67 SaveFilter(std::shared_ptr<SaveFilter> chain) : chain(chain)
71 /** Make sure the writers are properly closed. */
72 virtual ~SaveFilter()
76 /**
77 * Write a given number of bytes into the savegame.
78 * @param buf The bytes to write.
79 * @param len The number of bytes to write.
81 virtual void Write(uint8_t *buf, size_t len) = 0;
83 /**
84 * Prepare everything to finish writing the savegame.
86 virtual void Finish()
88 if (this->chain != nullptr) this->chain->Finish();
92 /**
93 * Instantiator for a save filter.
94 * @param chain The next filter in this chain.
95 * @param compression_level The requested level of compression.
96 * @tparam T The type of save filter to create.
98 template <typename T> std::shared_ptr<SaveFilter> CreateSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level)
100 return std::make_shared<T>(chain, compression_level);
103 #endif /* SAVELOAD_FILTER_H */