GObject Introspection
   HOME

TheInfoList



OR:

The GLib Object System, or GObject, is a free
software library In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and sub ...
providing a portable
object system Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
and transparent cross-language interoperability. GObject is designed for use both directly in C programs to provide object-oriented C-based APIs and through bindings to other languages to provide transparent cross-language interoperability, e.g.
PyGObject PyGTK is a set of Python wrappers for the GTK graphical user interface library. PyGTK is free software and licensed under the LGPL. It is analogous to PyQt/ PySide and wxPython, the Python wrappers for Qt and wxWidgets, respectively. Its ori ...
.


GObject Introspection


GObject introspection
(abbreviated GIR) is a middleware layer between C libraries (using GObject) and language bindings, cf. List of language bindings for GTK.


History

Depending only on
GLib GLib is a bundle of three (formerly five) low-level system libraries written in C and developed mainly by GNOME. GLib's code was separated from GTK, so it can be used by software other than GNOME and has been developed in parallel ever s ...
and
libc The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it was ...
, GObject is a cornerstone of
GNOME A gnome is a mythological creature and diminutive spirit in Renaissance magic and alchemy, first introduced by Paracelsus in the 16th century and later adopted by more recent authors including those of modern fantasy literature. Its characte ...
and is used throughout
GTK GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and propriet ...
, Pango, ATK, and most higher-level
GNOME A gnome is a mythological creature and diminutive spirit in Renaissance magic and alchemy, first introduced by Paracelsus in the 16th century and later adopted by more recent authors including those of modern fantasy literature. Its characte ...
libraries like
GStreamer GStreamer is a pipeline-based multimedia framework that links together a wide variety of media processing systems to complete complex workflows. For instance, GStreamer can be used to build a system that reads files in one format, processes the ...
and applications. Prior to GTK+ 2.0, code similar to GObject was part of the GTK codebase. (The name “GObject” was not yet in use — the common baseclass was called GtkObject.) At the release of GTK+ 2.0, the object system was extracted into a separate library due to its general utility. In the process, most non-
GUI The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
-specific parts of the GtkObject class were moved up into GObject, the new common baseclass. Having existed as a separate library since March 11, 2002 (the release date of GTK+ 2.0), the GObject library is now used by many non-GUI programs such as
command-line A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
and
server Server may refer to: Computing *Server (computing), a computer program or a device that provides functionality for other programs or devices, called clients Role * Waiting staff, those who work at a restaurant or a bar attending customers and su ...
applications.


Relation to GLib

Though GObject has its own separate set of documentation and is usually compiled into its own
shared library In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subro ...
file, the source code for GObject resides in the
GLib GLib is a bundle of three (formerly five) low-level system libraries written in C and developed mainly by GNOME. GLib's code was separated from GTK, so it can be used by software other than GNOME and has been developed in parallel ever s ...
source tree and is distributed along with GLib. For this reason, GObject uses the GLib version numbers and is typically packaged together with GLib (for example,
Debian Debian (), also known as Debian GNU/Linux, is a Linux distribution composed of free and open-source software, developed by the community-supported Debian Project, which was established by Ian Murdock on August 16, 1993. The first version of De ...
puts GObject in its libglib2.0 package family).


The type system

At the most basic level of the GObject framework lies a generic and dynamic
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
called GType. The GType system holds a runtime description of all objects allowing
glue code Adhesive, also known as glue, cement, mucilage, or paste, is any non-metallic substance applied to one or both surfaces of two separate items that binds them together and resists their separation. The use of adhesives offers certain advant ...
to facilitate multiple language bindings. The type system can handle any singly inherited class structure, in addition to ''non-classed'' types such as
opaque pointer In computer programming, an opaque pointer is a special case of an opaque data type, a data type declared to be a pointer to a record or data structure of some unspecified type. Opaque pointers are present in several programming languages includi ...
s, strings, and variously sized
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
s and
floating point number In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be r ...
s. The type system knows how to copy, assign, and destroy values belonging to any of the registered types. This is trivial for types like integers, but many complex objects are reference-counted, while some are complex but not reference-counted. When the type system “copies” a reference-counted object, it will typically just increase its reference count, whereas when copying a complex, non-reference-counted object (such as a string), it will typically create an actual copy by allocating memory. This basic functionality is used for implementing GValue, a type of generic container that can hold values of any type known by the type system. Such containers are particularly useful when interacting with dynamically typed language environments in which all native values reside in such type-tagged containers.


Fundamental types

Types that do not have any associated
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differently ...
es are called ''non-classed''. These types, together with all types that correspond to some form of root class, are known as ''fundamental types'': the types from which all other types are derived. These make up a relatively closed set, but although the average user is not expected to create their own fundamental types, the possibility does exist and has been exploited to create custom class hierarchies — i.e., class hierarchies not based on the GObject class. As of GLib 2.9.2, the ''non-classed'' built-in fundamental types are: * an empty type, corresponding to C's void (G_TYPE_NONE); * types corresponding to C's signed and unsigned char, int, long, and 64-bit integers (G_TYPE_CHAR, G_TYPE_UCHAR, G_TYPE_INT, G_TYPE_UINT, G_TYPE_LONG, G_TYPE_ULONG, G_TYPE_INT64, and G_TYPE_UINT64); * a boolean type (G_TYPE_BOOLEAN); * an enumeration type and a “flags” type, both corresponding to C's enum type, but differing in that the latter is only used for
bit field A bit field is a data structure that consists of one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected. A bit field is most commonly used to ...
s (G_TYPE_ENUM and G_TYPE_FLAGS); * types for single- and double-precision IEEE floats, corresponding to C's float and double (G_TYPE_FLOAT and G_TYPE_DOUBLE); * a string type, corresponding to C's char * (G_TYPE_STRING); * an opaque pointer type, corresponding to C's void * (G_TYPE_POINTER). The ''classed'' built-in fundamental types are: * a base class type for instances of GObject, the root of the standard class inheritance tree (G_TYPE_OBJECT) * a base interface type, analogous to the base class type but representing the root of the standard ''interface'' inheritance tree (G_TYPE_INTERFACE) * a type for
boxed Boxed may refer to: * Boxed.com, a wholesale on-line shopping site. * Boxed (Eurythmics), ''Boxed'' (Eurythmics), an eight album box set * Boxed (Mike Oldfield album), ''Boxed'' (Mike Oldfield album) * Boxed warning, a warning that appears on Unite ...
structures, which are used to wrap simple value objects or foreign objects in reference-counted “boxes” (G_TYPE_BOXED) * a type for “parameter specification objects,” which are used in GObject to describe
metadata Metadata is "data that provides information about other data", but not the content of the data, such as the text of a message or the image itself. There are many distinct types of metadata, including: * Descriptive metadata – the descriptive ...
for object properties (G_TYPE_PARAM). Types that can be instantiated automatically by the type system are called ''instantiable''. An important characteristic of these types is that the first bytes of any instance always contain a pointer to the ''class structure'' (a form of virtual table) associated to the type of the instance. For this reason, any instantiable type must be classed. Contrapositively, any non-classed type (such as ''integer'' or ''string'') must be non-instantiable. On the other hand, most classed types are instantiable, but some, such as interface types, are not.


Derived types

The types that are derived from the built-in GObject fundamental types fall roughly into four categories: ; Enumerated types and “flags” types : In general, every enumerated type and every integer-based bitfield type (i.e., every enum type) that one wishes to use in some way that is related to the object system — for example, as the type of an object property — should be registered with the type system. Typically, the initialization code that takes care of registering these types is generated by an automated tool called glib-mkenums and stored in a separate file. ; Boxed types : Some data structures that are too simple to be made full-fledged class types (with all the overhead incurred) may still need to be registered with the type system. For example, we might have a class to which we want to add a background-color property, whose values should be instances of a structure that looks like . To avoid having to subclass GObject, we can create a
boxed type In computer science, boxing (a.k.a. wrapping) is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrap ...
to represent this structure, and provide functions for copying and freeing. GObject ships with a handful of boxed types wrapping simple GLib data types. Another use for boxed types is as a way to wrap foreign objects in a tagged container that the type system can identify and will know how to copy and free. ; Opaque pointer types : Sometimes, for objects that need to be neither copied or reference-counted nor freed, even a boxed type would be overkill. While such objects can be used in GObject by simply treating them as opaque pointers (G_TYPE_POINTER), it is often a good idea to create a derived pointer type, documenting the fact that the pointers should reference a particular kind of object, even though nothing else is said about it. ; Class and interface types : Most types in a GObject application will be classes — in the normal object-oriented sense of the word — derived directly or indirectly from the root class, GObject. There are also interfaces, which, unlike classic
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
-style
interfaces Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
, can contain implemented methods. GObject interfaces can thus be described as
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depen ...
s.


Messaging system

The GObject messaging system consists of two complementary parts: ''closures'' and ''signals''. ; Closures : A GObject closure is a generalized version of a callback. Support exists for closures written in C and C++, as well as arbitrary languages (when bindings are provided). This allows code written in (for example) Python and Java to be invoked via a GObject closure. ; Signals : Signals are the primary mechanism by which closures are invoked. Objects register signal listeners with the type system, specifying a mapping between a given signal and a given closure. Upon emission of a registered signal, that signal's closure is invoked. In GTK, all native GUI events (such as mouse motion and keyboard actions) can generate GObject signals for listeners to potentially act upon.


Class implementation

Each GObject class is implemented by at least two structures: the ''class structure'' and the ''instance structure''. ; The class structure : The class structure corresponds to the
vtable In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding). Wh ...
of a C++ class. It must begin with the class structure of the superclass. Following that, it will hold a set of function pointers — one for each virtual method of the class. Class-specific variables can be used to emulate class members. ; The instance structure : The instance structure, which will exist in one copy per object instance, must begin with the instance structure of the superclass (this ensures that all instances begin with a pointer to the class structure, since all fundamental instantiable types share this property). After the data belonging to the superclass, the structure can hold any instance-specific variables, corresponding to C++ member variables. Defining a class in the GObject framework is complex, requiring large amounts of boilerplate code, such as manual definitions of type casting macros and obscure type registration incantations. Also, since a C structure cannot have access modifiers like “public”, “protected”, or “private”, workarounds must be used to provide encapsulation. One approach is to include a pointer to the private data — conventionally called _priv — in the instance structure. The ''private structure'' can be declared in the public header file, but defined only in the implementation file, with the effect that the private data is opaque to users, but transparent to the implementor. If the private structure is registered with GType, it will be automatically allocated by the object system. Indeed, it is not even necessary to include the _priv pointer, if one is willing to use the incantation G_TYPE_INSTANCE_GET_PRIVATE every time the private data is needed. To address some of these complexities, several higher-level languages exist that source-to-source compiles to GObject in C. The Vala programming language uses a C#-style syntax and is pre-processed into
vanilla Vanilla is a spice derived from orchids of the genus '' Vanilla'', primarily obtained from pods of the Mexican species, flat-leaved vanilla ('' V. planifolia''). Pollination is required to make the plants produce the fruit from whic ...
C code. The GObject Builder, o
GOB2
offers a template syntax reminiscent of
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
.


Usage

The combination of C and GObject is used in many successful
free software Free software or libre software is computer software distributed under terms that allow users to run the software for any purpose as well as to study, change, and distribute it and any adapted versions. Free software is a matter of liberty, n ...
projects, such as the
GNOME A gnome is a mythological creature and diminutive spirit in Renaissance magic and alchemy, first introduced by Paracelsus in the 16th century and later adopted by more recent authors including those of modern fantasy literature. Its characte ...
desktop, the
GTK GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and propriet ...
toolkit and the
GIMP GIMP ( ; GNU Image Manipulation Program) is a free and open-source raster graphics editor used for image manipulation (retouching) and image editing, free-form drawing, transcoding between different image file formats, and more specialized ...
image manipulation program. Though many GObject applications are written entirely in C, the GObject system maps well into the native object systems of many other languages, like
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, Python,
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
, and .NET/
Mono Mono may refer to: Common meanings * Infectious mononucleosis, "the kissing disease" * Monaural, monophonic sound reproduction, often shortened to mono * Mono-, a numerical prefix representing anything single Music Performers * Mono (Japanes ...
. As a result, it is usually relatively painless to create
language binding In programming and software design, binding is an application programming interface (API) that provides glue code specifically made to allow a programming language to use a foreign library or operating system service (one that is not native to th ...
s for well-written libraries that use the GObject framework. Writing GObject code in C in the first place, however, is relatively verbose. The library takes a good deal of time to learn, and programmers with experience in
high-level High-level and low-level, as technical terms, are used to classify, describe and point to specific goals of a systematic operation; and are applied in a wide range of contexts, such as, for instance, in domains as widely varied as computer scien ...
object-oriented languages often find it somewhat tedious to work with GObject in C. For example, creating a subclass (even just a subclass of GObject) can require writing and/or copying large amounts of
boilerplate code In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boile ...
. However, using
Vala Vala or VALA may refer to: Religion and mythology * Vala (Vedic), a demon or a stone cavern in the Hindu scriptures * Völva, also spelled Vala, a priestess in Norse mythology and Norse paganism Fiction * Vala (Middle-earth), an angelic being in ...
, a language that is designed primarily to work with GObject and which converts to C, is likely to make working with GObject or writing GObject based libraries nicer. Although they are not really
first-class object In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include ...
s (there are no actual metatypes in GType), metaobjects like classes and interfaces are created by GObject applications at runtime, and provide good support for
introspection Introspection is the examination of one's own conscious thoughts and feelings. In psychology, the process of introspection relies on the observation of one's mental state, while in a spiritual context it may refer to the examination of one's sou ...
. The introspective capabilities are used by language bindings and user interface design applications like Glade to allow doing things like loading a
shared library In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subro ...
that provides a GObject class—usually some kind of widget, in the case of Glade—and then obtain a list of all properties of the class, complete with type information and documentation strings.


Comparisons to other object systems

Since GObject provides a mostly complete object system for C, it can be seen as an alternative to C-derived languages such as
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
and
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXT ...
. (Though both also offer many other features beyond just their respective object systems.) An easily observed difference between C++ and GObject is that GObject (like Java) does not support
multiple inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
. GObject's use of
GLib GLib is a bundle of three (formerly five) low-level system libraries written in C and developed mainly by GNOME. GLib's code was separated from GTK, so it can be used by software other than GNOME and has been developed in parallel ever s ...
's g_malloc() memory allocation function will cause the program to exit unconditionally upon memory exhaustion, unlike the C library's
malloc C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely , , , and . The C++ programming language includes t ...
(), C++'s
new New is an adjective referring to something recently made, discovered, or created. New or NEW may refer to: Music * New, singer of K-pop group The Boyz Albums and EPs * ''New'' (album), by Paul McCartney, 2013 * ''New'' (EP), by Regurgitator, ...
, and other common memory allocators which allow a program to cope with or even fully recover from out-of-memory situations without simply crashing. This tends to work against including GObject in software where resilience in the face of limited memory is important, or where very many or very large objects are commonly handled. The g_try_new() can be used when a memory allocation is more likely to fail (for a large object for example), but this cannot grant that the allocation will not fail elsewhere in the code. Another important difference is that while C++ and Objective-C are separate languages, GObject is strictly a library and as such does not introduce any new syntax or compiler intelligence. For example, when writing GObject-based C code, it is frequently necessary to perform explicit
upcasting In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point valu ...
. Hence, “C with GObject”, considered as a language separate from plain C, is a strict superset of plain C — like Objective C, but unlike C++. On platforms where there is no standard ABI that works across all C++ compilers (which is not usually the case, since either the Itanium ABI or the Microsoft ABI are usually followed), a library compiled with one C++ compiler is not always able to call a library compiled with a different one. If such compatibility is required, the C++ methods must be exported as plain C functions, partly defeating the purpose of the C++ object system. The problem occurs in part because different C++ compilers use different kinds of
name mangling In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages. It provides a way of e ...
to ensure the uniqueness of all exported symbols. (This is necessary because, for example, two different classes may have identically named member functions, one function name may be overloaded multiple times, or identically named functions may appear in different
namespaces In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
, but in
object code In computing, object code or object module is the product of a compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ...
these overlaps are not allowed.) In contrast, since C does not support any form of overloading or namespacing, authors of C libraries will typically use explicit prefixes to ensure the global uniqueness of their exported names. Hence, despite being object-oriented, a GObject-based library written in C will always use the same external symbol names regardless of which compiler is used. Perhaps the most profound difference is GObject's emphasis on
signal In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The '' IEEE Transactions on Signal Processing' ...
s (called
event Event may refer to: Gatherings of people * Ceremony, an event of ritual significance, performed on a special occasion * Convention (meeting), a gathering of individuals engaged in some common interest * Event management, the organization of ev ...
s in other languages). This emphasis derives from the fact that GObject was specifically designed to meet the needs of a GUI toolkit. Whilst there are signal libraries for most object-oriented languages out there, in the case of GObject it is built into the object system. Because of this, a typical GObject application will tend to use signals to a much larger extent than a non-GObject application would, making GObject
component Circuit Component may refer to: •Are devices that perform functions when they are connected in a circuit.   In engineering, science, and technology Generic systems * System components, an entity with discrete structure, such as an assem ...
s much more encapsulated and reusable than the ones using plain C++ or Java. If using glibmm/
gtkmm gtkmm (formerly known as gtk-- or gtk minus minus) is the official C++ interface for the popular GUI library GTK. gtkmm is free software distributed under the GNU Lesser General Public License (LGPL). gtkmm allows the creation of user interface ...
, the official C++ wrappers to Glib/GTK respectively, the sibling project
libsigc++ libsigc++ is a C++ library for typesafe callbacks. libsigc++ implements a callback system for use in abstract interfaces and general programming. libsigc++ is one of the earliest implementations of the signals and slots concept implemented us ...
allows easy use of underlying GObject signals using standard C++. Of course, other implementations of signals are available on almost all platforms, although sometimes an extra library is needed, such as Boost.Signals2 for C++.


See also

*
Vala Vala or VALA may refer to: Religion and mythology * Vala (Vedic), a demon or a stone cavern in the Hindu scriptures * Völva, also spelled Vala, a priestess in Norse mythology and Norse paganism Fiction * Vala (Middle-earth), an angelic being in ...
— a GObject-based programming language with C#-like syntax. Source to source compiler to C. *
Genie Jinn ( ar, , ') – also romanized as djinn or anglicized as genies (with the broader meaning of spirit or demon, depending on sources) – are invisible creatures in early pre-Islamic Arabian religious systems and later in Islamic myt ...
— alternative syntax parser for the Python-style Vala compiler


References


External links


The GObject Reference Manual (and tutorial)

GObject Tutorial Aug 2004



Vala Homepage
{{DEFAULTSORT:Gobject C (programming language) libraries Free computer libraries Free software programmed in C Freedesktop.org GNOME libraries GTK