OpenTTD Source: src/core/alloc_type.hpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef ALLOC_TYPE_HPP
11#define ALLOC_TYPE_HPP
12
21template <typename T>
22 class ReusableBuffer {
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 */
ReusableBuffer
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition alloc_type.hpp:22
ReusableBuffer::ZeroAllocate
T * ZeroAllocate(size_t count)
Get buffer of at least count times T of default initialised elements.
Definition alloc_type.hpp:47
ReusableBuffer::GetBuffer
const T * GetBuffer() const
Get the currently allocated buffer.
Definition alloc_type.hpp:58
ReusableBuffer::Allocate
T * Allocate(size_t count)
Get buffer of at least count times T.
Definition alloc_type.hpp:34