TrinityCore
Memory.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_MEMORY_H
19#define TRINITY_MEMORY_H
20
21#include "CompilerDefs.h"
22#include <concepts>
23#include <memory>
24
25#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-Wignored-attributes"
28#endif
29
30namespace Trinity
31{
32namespace Impl
33{
34template<typename T, typename Del>
36{
37 using pointer = T;
38 explicit(false) stateful_unique_ptr_deleter(Del deleter) : _deleter(std::move(deleter)) { }
39 void operator()(pointer ptr) const { (void)_deleter(ptr); }
40
41private:
43};
44
45template<typename T, auto Del>
47{
48 using pointer = T;
49 void operator()(pointer ptr) const
50 {
51 if constexpr (std::is_member_function_pointer_v<decltype(Del)>)
52 (void)(ptr->*Del)();
53 else
54 (void)Del(ptr);
55 }
56};
57}
58
78template <typename Ptr, typename Del> requires std::invocable<Del, Ptr> && std::is_pointer_v<Ptr>
80{
81 return Impl::stateful_unique_ptr_deleter<Ptr, Del>(std::move(deleter));
82}
83
101template <typename Ptr, auto Del> requires std::invocable<decltype(Del), Ptr> && std::is_pointer_v<Ptr>
103{
105}
106
132template<typename Ptr, typename T = std::remove_pointer_t<Ptr>, typename Del> requires std::invocable<Del, Ptr> && std::is_pointer_v<Ptr>
133std::unique_ptr<T, Impl::stateful_unique_ptr_deleter<Ptr, Del>> make_unique_ptr_with_deleter(Ptr ptr, Del deleter)
134{
135 return std::unique_ptr<T, Impl::stateful_unique_ptr_deleter<Ptr, Del>>(ptr, std::move(deleter));
136}
137
163template<auto Del, typename Ptr, typename T = std::remove_pointer_t<Ptr>> requires std::invocable<decltype(Del), Ptr> && std::is_pointer_v<Ptr>
165{
166 return std::unique_ptr<T, Impl::stateless_unique_ptr_deleter<Ptr, Del>>(ptr);
167}
168}
169
170#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
171#pragma GCC diagnostic pop
172#endif
173
174#endif // TRINITY_MEMORY_H
constexpr uint32 Ptr
std::unique_ptr< T, Impl::stateful_unique_ptr_deleter< Ptr, Del > > make_unique_ptr_with_deleter(Ptr ptr, Del deleter)
Definition: Memory.h:133
Impl::stateful_unique_ptr_deleter< Ptr, Del > unique_ptr_deleter(Del deleter)
Definition: Memory.h:79
void operator()(pointer ptr) const
Definition: Memory.h:39
void operator()(pointer ptr) const
Definition: Memory.h:49