OpenTTD Source 20250312-master-gcdcc6b491d
alloc_type.hpp
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 ALLOC_TYPE_HPP
11#define ALLOC_TYPE_HPP
12
21template <typename T>
23private:
24 std::vector<T> buffer;
25
26public:
34 T *Allocate(size_t count)
35 {
36 if (this->buffer.size() < count) this->buffer.resize(count);
37 return this->buffer.data();
38 }
39
47 T *ZeroAllocate(size_t count)
48 {
49 this->buffer.clear();
50 this->buffer.resize(count, {});
51 return this->buffer.data();
52 }
53
58 inline const T *GetBuffer() const
59 {
60 return this->buffer.data();
61 }
62};
63
64#endif /* ALLOC_TYPE_HPP */
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
T * ZeroAllocate(size_t count)
Get buffer of at least count times T of default initialised elements.
const T * GetBuffer() const
Get the currently allocated buffer.
T * Allocate(size_t count)
Get buffer of at least count times T.