Codechange: allow mapping enums as parameter and return type from scripts
[openttd-github.git] / src / saveload / saveload_filter.h
blob47b6fb46fa38fade02b7cb1fb286f4637514c835
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() = default;
29 /**
30 * Read a given number of bytes from the savegame.
31 * @param buf The bytes to read.
32 * @param len The number of bytes to read.
33 * @return The number of actually read bytes.
35 virtual size_t Read(uint8_t *buf, size_t len) = 0;
37 /**
38 * Reset this filter to read from the beginning of the file.
40 virtual void Reset()
42 this->chain->Reset();
46 /**
47 * Instantiator for a load filter.
48 * @param chain The next filter in this chain.
49 * @tparam T The type of load filter to create.
51 template <typename T> std::shared_ptr<LoadFilter> CreateLoadFilter(std::shared_ptr<LoadFilter> chain)
53 return std::make_shared<T>(chain);
56 /** Interface for filtering a savegame till it is written. */
57 struct SaveFilter {
58 /** Chained to the (savegame) filters. */
59 std::shared_ptr<SaveFilter> chain;
61 /**
62 * Initialise this filter.
63 * @param chain The next filter in this chain.
65 SaveFilter(std::shared_ptr<SaveFilter> chain) : chain(chain)
69 /** Make sure the writers are properly closed. */
70 virtual ~SaveFilter() = default;
72 /**
73 * Write a given number of bytes into the savegame.
74 * @param buf The bytes to write.
75 * @param len The number of bytes to write.
77 virtual void Write(uint8_t *buf, size_t len) = 0;
79 /**
80 * Prepare everything to finish writing the savegame.
82 virtual void Finish()
84 if (this->chain != nullptr) this->chain->Finish();
88 /**
89 * Instantiator for a save filter.
90 * @param chain The next filter in this chain.
91 * @param compression_level The requested level of compression.
92 * @tparam T The type of save filter to create.
94 template <typename T> std::shared_ptr<SaveFilter> CreateSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level)
96 return std::make_shared<T>(chain, compression_level);
99 #endif /* SAVELOAD_FILTER_H */