libevdevxx 0.5.0
A C++ wrapper for libevdev.
Loading...
Searching...
No Matches
basic_wrapper.hpp
Go to the documentation of this file.
1/*
2 * basic_wrapper - base class for C++ wrappers
3 * Copyright 2025 Daniel K. O. (dkosmari)
4 *
5 * This software can be distributed under the terms of any of the following licenses:
6 * SPDX-License-Identifier: Apache-2.0
7 * SPDX-License-Identifier: GPL-3.0-or-later
8 * SPDX-License-Identifier: LGPL-3.0-or-later
9 * SPDX-License-Identifier: MIT
10 * SPDX-License-Identifier: Zlib
11*/
12
13#ifndef LIBEVDEVXX_BASIC_WRAPPER_HPP
14#define LIBEVDEVXX_BASIC_WRAPPER_HPP
15
16
17namespace evdev::detail {
18
19 template<typename T,
20 T InvalidValue = T{}>
22
23 protected:
24
25 T raw{InvalidValue};
26
27
28 // Note: we can't call destroy() from the derived class from here.
30 noexcept = default;
31
32
33 public:
34
35 using raw_type = T;
37
38 static constexpr raw_type invalid_value = InvalidValue;
39
40
41 constexpr
43 noexcept = default;
44
45
46 constexpr
47 explicit
49 noexcept :
50 raw{src}
51 {}
52
53
55 constexpr
57 noexcept
58 {
59 acquire(other.release());
60 }
61
62
66 noexcept
67 {
68 if (this != &other) {
69 destroy();
70 acquire(other.release());
71 }
72 return *this;
73 }
74
75
76 // Remember to always call destroy() from the derived class' destructor.
77 virtual
78 void
80 noexcept = 0;
81
82
83 [[nodiscard]]
84 bool
86 const noexcept
87 {
88 return raw != invalid_value;
89 }
90
91
92 [[nodiscard]]
93 explicit
94 operator bool()
95 const noexcept
96 {
97 return is_valid();
98 }
99
100
103 noexcept
104 {
105 return raw;
106 }
107
108
109 const raw_type
111 const noexcept
112 {
113 return raw;
114 }
115
116
117 void
119 noexcept
120 {
121 raw = new_state;
122 }
123
124
127 noexcept
128 {
129 auto old_raw = raw;
130 raw = {};
131 return old_raw;
132 }
133
134 }; // class basic_wrapper
135
136} // namespace evdev::detail
137
138#endif
Definition basic_wrapper.hpp:21
virtual void destroy() noexcept=0
raw_type state_type
Definition basic_wrapper.hpp:36
static constexpr raw_type invalid_value
Definition basic_wrapper.hpp:38
constexpr basic_wrapper() noexcept=default
~basic_wrapper() noexcept=default
state_type release() noexcept
Definition basic_wrapper.hpp:126
basic_wrapper & operator=(basic_wrapper &&other) noexcept
Move assignment.
Definition basic_wrapper.hpp:65
libevdev * raw
Definition basic_wrapper.hpp:25
bool is_valid() const noexcept
Definition basic_wrapper.hpp:85
void acquire(state_type new_state) noexcept
Definition basic_wrapper.hpp:118
const raw_type data() const noexcept
Definition basic_wrapper.hpp:110
T raw_type
Definition basic_wrapper.hpp:35
constexpr basic_wrapper(basic_wrapper &&other) noexcept
Move constructor.
Definition basic_wrapper.hpp:56
raw_type data() noexcept
Definition basic_wrapper.hpp:102
Definition basic_wrapper.hpp:17