ladybird/UI/Gtk/GLibPtr.h
Johan Dahlin e8be4a88d9 UI/Gtk: Add GLib event loop integration and application entry point
Add the core infrastructure for the GTK frontend:
- EventLoopImplementationGtk: GLib main loop integration with
  Core::EventLoop, timers, notifiers, and signal handling
- GObjectPtr RAII wrapper for GObject lifecycle management
- Application subclass with AdwApplication and D-Bus registration
- Minimal main.cpp entry point

The application starts and runs the event loop but does not yet
open any windows.
2026-04-17 11:17:56 -04:00

69 lines
1.2 KiB
C++

/*
* Copyright (c) 2026, Johan Dahlin <jdahlin@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <glib-object.h>
namespace Ladybird {
// RAII wrapper for GObject-derived pointers. Calls g_object_unref on destruction.
// Does not add a reference on construction — assumes ownership of a floating or
// newly-created reference.
template<typename T>
class GObjectPtr {
public:
GObjectPtr() = default;
explicit GObjectPtr(T* ptr)
: m_ptr(ptr)
{
}
~GObjectPtr()
{
clear();
}
GObjectPtr(GObjectPtr const&) = delete;
GObjectPtr& operator=(GObjectPtr const&) = delete;
GObjectPtr(GObjectPtr&& other)
: m_ptr(other.leak())
{
}
GObjectPtr& operator=(GObjectPtr&& other)
{
if (this != &other) {
clear();
m_ptr = other.leak();
}
return *this;
}
T* ptr() const { return m_ptr; }
operator T*() const { return m_ptr; }
T* leak()
{
auto* ptr = m_ptr;
m_ptr = nullptr;
return ptr;
}
void clear()
{
if (m_ptr) {
g_object_unref(m_ptr);
m_ptr = nullptr;
}
}
private:
T* m_ptr { nullptr };
};
}