mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-06-18 19:19:29 +01:00
Codechange: Add AutoRelease helper to use function as unique_ptr deleter. (#14353)
This allows passing the function as a template parameter instead of requiring a custom deleter functor.
This commit is contained in:
parent
f8776b0a6f
commit
9c16603da6
@ -11,6 +11,7 @@
|
||||
#define FILEIO_TYPE_H
|
||||
|
||||
#include "core/enum_type.hpp"
|
||||
#include "misc/autorelease.hpp"
|
||||
|
||||
/** The different abstract types of files that the system knows about. */
|
||||
enum AbstractFileType : uint8_t {
|
||||
@ -142,15 +143,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
/** Helper to close a FILE * with a \c std::unique_ptr. */
|
||||
struct FileDeleter {
|
||||
void operator ()(FILE *f)
|
||||
{
|
||||
if (f != nullptr) fclose(f);
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<FILE, FileDeleter> f;
|
||||
AutoRelease<FILE, fclose> f;
|
||||
|
||||
FileHandle(FILE *f) : f(f) { assert(this->f != nullptr); }
|
||||
};
|
||||
|
@ -1,5 +1,6 @@
|
||||
add_files(
|
||||
alternating_iterator.hpp
|
||||
autorelease.hpp
|
||||
binaryheap.hpp
|
||||
dbg_helpers.cpp
|
||||
dbg_helpers.h
|
||||
|
27
src/misc/autorelease.hpp
Normal file
27
src/misc/autorelease.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file autorelease.hpp Helper for std::unique_ptr to use an arbitrary function as the deleter. */
|
||||
|
||||
#ifndef AUTORELEASE_HPP
|
||||
#define AUTORELEASE_HPP
|
||||
|
||||
/** Deleter that calls a function rather than deleting the pointer. */
|
||||
template <auto Tfunc>
|
||||
struct DeleterFromFunc {
|
||||
template <typename T>
|
||||
constexpr void operator()(T *arg) const
|
||||
{
|
||||
if (arg != nullptr) Tfunc(arg);
|
||||
}
|
||||
};
|
||||
|
||||
/** Specialisation of std::unique_ptr for objects which must be deleted by calling a function. */
|
||||
template <typename T, auto Tfunc>
|
||||
using AutoRelease = std::unique_ptr<T, DeleterFromFunc<Tfunc>>;
|
||||
|
||||
#endif /* AUTORELEASE_HPP */
|
@ -8,6 +8,8 @@
|
||||
/** @file sound_opus.cpp Loading of opus sounds. */
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "misc/autorelease.hpp"
|
||||
#include "random_access_file_type.h"
|
||||
#include "sound_type.h"
|
||||
#include "soundloader_type.h"
|
||||
@ -51,7 +53,7 @@ public:
|
||||
sound.file->ReadBlock(tmp.data(), tmp.size());
|
||||
|
||||
int error = 0;
|
||||
auto of = std::unique_ptr<OggOpusFile, OggOpusFileDeleter>(op_open_memory(tmp.data(), tmp.size(), &error));
|
||||
auto of = AutoRelease<OggOpusFile, op_free>(op_open_memory(tmp.data(), tmp.size(), &error));
|
||||
if (error != 0) return false;
|
||||
|
||||
size_t datapos = 0;
|
||||
@ -81,15 +83,6 @@ public:
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
/** Helper class to RAII release an OggOpusFile. */
|
||||
struct OggOpusFileDeleter {
|
||||
void operator()(OggOpusFile *of)
|
||||
{
|
||||
if (of != nullptr) op_free(of);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
static SoundLoader_Opus s_sound_loader_opus;
|
||||
|
Loading…
Reference in New Issue
Block a user