OpenTTD Source  20241108-master-g80f628063a
saveload_filter.h
Go to the documentation of this file.
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  */
7 
10 #ifndef SAVELOAD_FILTER_H
11 #define SAVELOAD_FILTER_H
12 
14 struct LoadFilter {
16  std::shared_ptr<LoadFilter> chain;
17 
22  LoadFilter(std::shared_ptr<LoadFilter> chain) : chain(chain)
23  {
24  }
25 
27  virtual ~LoadFilter()
28  {
29  }
30 
37  virtual size_t Read(uint8_t *buf, size_t len) = 0;
38 
42  virtual void Reset()
43  {
44  this->chain->Reset();
45  }
46 };
47 
53 template <typename T> std::shared_ptr<LoadFilter> CreateLoadFilter(std::shared_ptr<LoadFilter> chain)
54 {
55  return std::make_shared<T>(chain);
56 }
57 
59 struct SaveFilter {
61  std::shared_ptr<SaveFilter> chain;
62 
67  SaveFilter(std::shared_ptr<SaveFilter> chain) : chain(chain)
68  {
69  }
70 
72  virtual ~SaveFilter()
73  {
74  }
75 
81  virtual void Write(uint8_t *buf, size_t len) = 0;
82 
86  virtual void Finish()
87  {
88  if (this->chain != nullptr) this->chain->Finish();
89  }
90 };
91 
98 template <typename T> std::shared_ptr<SaveFilter> CreateSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level)
99 {
100  return std::make_shared<T>(chain, compression_level);
101 }
102 
103 #endif /* SAVELOAD_FILTER_H */
std::shared_ptr< SaveFilter > CreateSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Instantiator for a save filter.
std::shared_ptr< LoadFilter > CreateLoadFilter(std::shared_ptr< LoadFilter > chain)
Instantiator for a load filter.
Interface for filtering a savegame till it is loaded.
virtual size_t Read(uint8_t *buf, size_t len)=0
Read a given number of bytes from the savegame.
virtual ~LoadFilter()
Make sure the writers are properly closed.
virtual void Reset()
Reset this filter to read from the beginning of the file.
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
LoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Interface for filtering a savegame till it is written.
virtual void Write(uint8_t *buf, size_t len)=0
Write a given number of bytes into the savegame.
virtual ~SaveFilter()
Make sure the writers are properly closed.
virtual void Finish()
Prepare everything to finish writing the savegame.
SaveFilter(std::shared_ptr< SaveFilter > chain)
Initialise this filter.
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.