LMMS
Loading...
Searching...
No Matches
RaiiHelpers.h
Go to the documentation of this file.
1/*
2 * RaiiHelpers.h
3 *
4 * Copyright (c) 2022 Dominic Clark <mrdomclark/at/gmail.com>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef LMMS_RAII_HELPERS_H
26#define LMMS_RAII_HELPERS_H
27
28#include <cstddef>
29#include <memory>
30
31
32namespace lmms
33{
34
35template<typename T, T Null>
37{
38public:
39 NullableResource() = default;
40 NullableResource(std::nullptr_t) noexcept { }
42 operator T() const noexcept { return m_value; }
43 explicit operator bool() const noexcept { return m_value != Null; }
44 friend bool operator==(NullableResource a, NullableResource b) noexcept { return a.m_value == b.m_value; }
45 friend bool operator==(NullableResource a, T b) noexcept { return a.m_value == b; }
46 friend bool operator==(T a, NullableResource b) noexcept { return a == b.m_value; }
47 friend bool operator!=(NullableResource a, NullableResource b) noexcept { return a.m_value != b.m_value; }
48 friend bool operator!=(NullableResource a, T b) noexcept { return a.m_value != b; }
49 friend bool operator!=(T a, NullableResource b) noexcept { return a != b.m_value; }
50
51private:
52 T m_value = Null;
53};
54
55template<typename T, T Null, auto Deleter>
57{
59 void operator()(T value) const noexcept { Deleter(value); }
60};
61
62template<typename T, T Null, auto Deleter>
63using UniqueNullableResource = std::unique_ptr<T, NullableResourceDeleter<T, Null, Deleter>>;
64
65} // namespace lmms
66
67#endif // LMMS_RAII_HELPERS_H
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
Definition RaiiHelpers.h:37
friend bool operator!=(NullableResource a, T b) noexcept
Definition RaiiHelpers.h:48
NullableResource(T value) noexcept
Definition RaiiHelpers.h:41
friend bool operator==(T a, NullableResource b) noexcept
Definition RaiiHelpers.h:46
T m_value
Definition RaiiHelpers.h:52
friend bool operator==(NullableResource a, T b) noexcept
Definition RaiiHelpers.h:45
friend bool operator==(NullableResource a, NullableResource b) noexcept
Definition RaiiHelpers.h:44
friend bool operator!=(NullableResource a, NullableResource b) noexcept
Definition RaiiHelpers.h:47
NullableResource(std::nullptr_t) noexcept
Definition RaiiHelpers.h:40
friend bool operator!=(T a, NullableResource b) noexcept
Definition RaiiHelpers.h:49
static PuglViewHint int value
Definition pugl.h:1708
Definition AudioAlsa.cpp:35
std::unique_ptr< T, NullableResourceDeleter< T, Null, Deleter > > UniqueNullableResource
Definition RaiiHelpers.h:63
Definition RaiiHelpers.h:57
void operator()(T value) const noexcept
Definition RaiiHelpers.h:59
NullableResource< T, Null > pointer
Definition RaiiHelpers.h:58
b
Definition crypt.c:628
#define const
Definition zconf.h:137