mirror of
https://gitlab.uni-freiburg.de/opensourcevdi/spice
synced 2026-01-27 03:18:59 +00:00
utils: Add red::unique_ptr
red::unique_link will be used to manage "priv" fields. Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
parent
61affed2a2
commit
b86f6e9a53
@ -34,4 +34,62 @@ inline T* add_ref(T* p)
|
||||
}
|
||||
|
||||
|
||||
/* Smart pointer allocated once
|
||||
*
|
||||
* It just keep the pointer passed to constructor and delete
|
||||
* the object in the destructor. No copy or move allowed.
|
||||
* Very easy but make sure we don't change it and that's
|
||||
* initialized.
|
||||
*/
|
||||
template <typename T>
|
||||
class unique_link
|
||||
{
|
||||
public:
|
||||
unique_link(): p(new T())
|
||||
{
|
||||
}
|
||||
unique_link(T* p): p(p)
|
||||
{
|
||||
}
|
||||
~unique_link()
|
||||
{
|
||||
delete p;
|
||||
}
|
||||
T* operator->() noexcept
|
||||
{
|
||||
return p;
|
||||
}
|
||||
const T* operator->() const noexcept
|
||||
{
|
||||
return p;
|
||||
}
|
||||
private:
|
||||
T *const p;
|
||||
unique_link(const unique_link&)=delete;
|
||||
void operator=(const unique_link&)=delete;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct GLibDeleter {
|
||||
void operator()(T* p)
|
||||
{
|
||||
g_free(p);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using glib_unique_ptr = std::unique_ptr<T, GLibDeleter<T>>;
|
||||
|
||||
|
||||
/* Returns the size of an array.
|
||||
* Introduced in C++17 but lacking in C++11
|
||||
*/
|
||||
template <class T, size_t N>
|
||||
constexpr size_t size(const T (&array)[N]) noexcept
|
||||
{
|
||||
return N;
|
||||
}
|
||||
|
||||
|
||||
} // namespace red
|
||||
|
||||
Loading…
Reference in New Issue
Block a user