OpenTTD Source: src/saveload/saveload_filter.h Source File

OpenTTD Source 15.0-beta1

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() = default;
28
35 virtual size_t Read(uint8_t *buf, size_t len) = 0;
36
40 virtual void Reset()
41 {
42 this->chain->Reset();
43 }
44};
45
51 template <typename T> std::shared_ptr<LoadFilter> CreateLoadFilter(std::shared_ptr<LoadFilter> chain)
52{
53 return std::make_shared<T>(chain);
54}
55
57 struct SaveFilter {
59 std::shared_ptr<SaveFilter> chain;
60
65 SaveFilter(std::shared_ptr<SaveFilter> chain) : chain(chain)
66 {
67 }
68
70 virtual ~SaveFilter() = default;
71
77 virtual void Write(uint8_t *buf, size_t len) = 0;
78
82 virtual void Finish()
83 {
84 if (this->chain != nullptr) this->chain->Finish();
85 }
86};
87
94 template <typename T> std::shared_ptr<SaveFilter> CreateSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level)
95{
96 return std::make_shared<T>(chain, compression_level);
97}
98
99#endif /* SAVELOAD_FILTER_H */
CreateSaveFilter
std::shared_ptr< SaveFilter > CreateSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Instantiator for a save filter.
Definition saveload_filter.h:94
CreateLoadFilter
std::shared_ptr< LoadFilter > CreateLoadFilter(std::shared_ptr< LoadFilter > chain)
Instantiator for a load filter.
Definition saveload_filter.h:51
LoadFilter
Interface for filtering a savegame till it is loaded.
Definition saveload_filter.h:14
LoadFilter::Read
virtual size_t Read(uint8_t *buf, size_t len)=0
Read a given number of bytes from the savegame.
LoadFilter::~LoadFilter
virtual ~LoadFilter()=default
Make sure the writers are properly closed.
LoadFilter::Reset
virtual void Reset()
Reset this filter to read from the beginning of the file.
Definition saveload_filter.h:40
LoadFilter::chain
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
Definition saveload_filter.h:16
LoadFilter::LoadFilter
LoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition saveload_filter.h:22
SaveFilter
Interface for filtering a savegame till it is written.
Definition saveload_filter.h:57
SaveFilter::Write
virtual void Write(uint8_t *buf, size_t len)=0
Write a given number of bytes into the savegame.
SaveFilter::Finish
virtual void Finish()
Prepare everything to finish writing the savegame.
Definition saveload_filter.h:82
SaveFilter::SaveFilter
SaveFilter(std::shared_ptr< SaveFilter > chain)
Initialise this filter.
Definition saveload_filter.h:65
SaveFilter::chain
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.
Definition saveload_filter.h:59
SaveFilter::~SaveFilter
virtual ~SaveFilter()=default
Make sure the writers are properly closed.