OpenTTD Source: src/misc/autorelease.hpp Source File

OpenTTD Source 15.3

autorelease.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 AUTORELEASE_HPP
11#define AUTORELEASE_HPP
12
14template <auto Tfunc>
15 struct DeleterFromFunc {
16 template <typename T>
17 constexpr void operator()(T *arg) const
18 {
19 if (arg != nullptr) Tfunc(arg);
20 }
21};
22
24template <typename T, auto Tfunc>
25 using AutoRelease = std::unique_ptr<T, DeleterFromFunc<Tfunc>>;
26
27#endif /* AUTORELEASE_HPP */
AutoRelease
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
Definition autorelease.hpp:25
DeleterFromFunc
Deleter that calls a function rather than deleting the pointer.
Definition autorelease.hpp:15