Search code examples
gtk

Where is the implementation of GTK_IS_ROOT in gtk4?


Where is the implementation of that function?

is a macro function

I searched the source code but couldn't find it


Solution

  • GTK_IS_ROOT() is defined by the G_DECLARE_INTERFACE (GtkRoot, gtk_root, GTK, ROOT, GtkWidget) at https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gtk/gtkroot.h#L35 macro. This is a GObject macro and GTK uses GObject extensively.

    Basically: when writing GObject classes or interfaces, you need to write quite a bit of boilerplace to implement inheritance right, ideally in a way that helps with ABI stability. GObject provides macros that make it easier for you: G_DECLARE_FINAL_TYPE(), G_DECLARE_DERIVABLE_TYPE() and G_DECLARE_INTERFACE() are macros that respectively do the declaration for a final class (which can't be subclassed), a derivable class, and an interface.

    One of the things that you often want to do with runtime inheritance, is to perform certain checks related to the type of a certain parameter or variable. The aforementioned macros help out with that by generating some extra macros to implement such functionality.

    Given a namespace Xxx (for example Gtk) and a type name Yyy (for example Root) it will provide an XXX_IS_YYY(var) macro to check if the given variable var is truly an instance of the class (i.e. it implements the instanceof() operator you might know of other languages)