GObject
Encyclopedia
The GLib
Object System, or GObject, is a free
software library providing a portable object system and transparent cross-language interoperability. GObject is designed for use both directly in C
programs to provide object-oriented C-based APIs and through binding
s to other languages to provide transparent cross-language interoperability.
and libc, GObject is a cornerstone of GNOME
and is used throughout GTK+
, Pango
, Accessibility Toolkit
, and most higher-level GNOME
libraries like GStreamer
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
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
-specific parts of the
applications.
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
puts GObject in its
called GType. The GType system holds a runtime description of all objects allowing glue code
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
s, strings
, and variously sized integer
s and floating point numbers.
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
— i.e., class hierarchies not based on the
As of GLib 2.9.2,
the non-classed built-in fundamental types are
The classed built-in fundamental types are
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.
roughly into four categories:
Enumerated types and “flags” types : In general, every enumerated type and every integer-based bitfield type (i.e., every
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
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 (
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,
-style interfaces, although these are still relatively rarely used, likely because they have only been around since GLib 2.4 (which was released on March 16, 2004). The GNU Image Manipulation Program uses GObject interfaces.
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.
The class structure : The class structure corresponds to the vtable 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.
Since a C structure cannot have access modifiers like “public”, “protected”, or “private”, a common technique is to include a pointer to the private data — conventionally called
The main drawback of the GObject framework is its verbosity. Large amounts of boilerplate code, such as manual definitions of type casting macros and obscure type registration incantations, are necessary to create a new class. The GObject Builder, or GOB2, is a tool that attempts to remedy this problem by offering a template syntax reminiscent of Java. Code written using GOB2 is pre-processed into vanilla C code prior to compilation. Another compiler-to-C for the GObject type system is Vala
, which uses a C#-style syntax.
projects, such as the GNOME
desktop, the GTK+
toolkit and the GIMP
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++
, Java
, Ruby, Python
, Common Lisp
, and .NET
/Mono
. As a result, it is usually relatively painless to create language binding
s for well-written libraries that use the GObject framework.
Writing GObject code in C in the first place, however, is relatively painful. The learning curve
is quite steep, and programmers with experience in high-level object-oriented languages are likely to find it very tedious to work with GObject in C. For example, creating a non-trivial subclass (even just a subclass of
Although they are not really first-class object
s (there are no actual metatypes in GType), metaobject
s like classes and interfaces are created by GObject applications at runtime, and provide good support for introspection. The introspective
capabilities are used by language bindings and user interface design applications like Glade
to allow doing things like loading a shared library 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.
and Objective-C
. (Though C++, in particular, also has many other features apart from its object system.) An easily observed difference between C++ and GObject is that GObject (like Java) does not support multiple inheritance
.
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. Hence, “C with GObject”, considered as a language separate from plain C, is a strict superset of plain C — 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
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
, but in object code
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 signals (called events
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 components much more encapsulated
and reusable than the ones using plain C++ or Java. Note however that signals are available on almost all platforms, although sometimes an extra library is needed, such as Boost.Signals2 for C++.
GLib
GLib is a cross-platform software utility library that began as part of the GTK+ project. However, before releasing version 2 of GTK+, the project's developers decided to separate non-GUI-specific code from the GTK+ platform, thus creating GLib as a separate product...
Object System, or GObject, is a free
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
software library providing a portable object system and transparent cross-language interoperability. GObject is designed for use both directly in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
programs to provide object-oriented C-based APIs and through binding
Language binding
In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...
s to other languages to provide transparent cross-language interoperability.
History
Depending only on GLibGLib
GLib is a cross-platform software utility library that began as part of the GTK+ project. However, before releasing version 2 of GTK+, the project's developers decided to separate non-GUI-specific code from the GTK+ platform, thus creating GLib as a separate product...
and libc, GObject is a cornerstone of GNOME
GNOME
GNOME is a desktop environment and graphical user interface that runs on top of a computer operating system. It is composed entirely of free and open source software...
and is used throughout GTK+
GTK+
GTK+ is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the X Window System, along with Qt.The name GTK+ originates from GTK;...
, Pango
Pango
Pango is an LGPL licensed open source computing library used by software developers for laying out and rendering text in high quality, emphasising support for multilingual text...
, Accessibility Toolkit
Accessibility Toolkit
In computing, Accessibility Toolkit refers in particular to the GNOME ATK.The GNOME ATK, a developer toolkit, allows programmers to use common GNOME accessibility features in their applications. This includes such features as high-contrast visual themes for the visually impaired and keyboard...
, and most higher-level GNOME
GNOME
GNOME is a desktop environment and graphical user interface that runs on top of a computer operating system. It is composed entirely of free and open source software...
libraries like GStreamer
GStreamer
GStreamer is a pipeline-based multimedia framework written in the C programming language with the type system based on GObject.GStreamer allows a programmer to create a variety of media-handling components, including simple audio playback, audio and video playback, recording, streaming and editing...
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
Gui
Gui or guee is a generic term to refer to grilled dishes in Korean cuisine. These most commonly have meat or fish as their primary ingredient, but may in some cases also comprise grilled vegetables or other vegetarian ingredients. The term derives from the verb, "gupda" in Korean, which literally...
-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 and serverServer (computing)
In the context of client-server architecture, a server is a computer program running to serve the requests of other programs, the "clients". Thus, the "server" performs some computational task on behalf of "clients"...
applications.
Relation to GLib
Though GObject has its own separate set of documentation and is usually compiled into its own shared library file, the source code for GObject resides in the GLibGLib
GLib is a cross-platform software utility library that began as part of the GTK+ project. However, before releasing version 2 of GTK+, the project's developers decided to separate non-GUI-specific code from the GTK+ platform, thus creating GLib as a separate product...
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
Debian is a computer operating system composed of software packages released as free and open source software primarily under the GNU General Public License along with other free software licenses. Debian GNU/Linux, which includes the GNU OS tools and Linux kernel, is a popular and influential...
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 systemType system
A type system associates a type with each computed value. By examining the flow of these values, a type system attempts to ensure or prove that no type errors can occur...
called GType. The GType system holds a runtime description of all objects allowing glue code
Glue code
In programming, glue code is code that does not contribute any functionality towards meeting the program's requirements, but instead serves solely to "glue together" different parts of code that would not otherwise be compatible...
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
Opaque pointer
In computer programming, an opaque pointer is a special case of an opaque data type, a datatype that is declared to be a pointer to a record or data structure of some unspecified type....
s, strings
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....
, and variously sized integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...
s and floating point numbers.
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
Reference counting
In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource...
, 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 classes 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 her own fundamental types, the possibility does exist and has been exploited to create custom class hierarchiesClass hierarchy
As in taxonomy, the classifications of species, a class hierarchy in computer science is a classification of object types, denoting objects as the instantiations of classes inter-relating the various classes by relationships such as "inherits", "extends", "is an abstraction of", "an interface...
— 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
, andG_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 fieldBit fieldA bit field is a common idiom used in computer programming to compactly store multiple logical values as a short series of bits where each of the single bits can be addressed separately. A bit field is most commonly used to represent integral types of known, fixed bit-width. A well-known usage of...
s (G_TYPE_ENUM
andG_TYPE_FLAGS
); - types for single- and double-precision IEEE floats, corresponding to C’s
float
anddouble
(G_TYPE_FLOAT
andG_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, analoguous to the base class type but representing the root of the standard interface inheritance tree (
G_TYPE_INTERFACE
) - a type for boxed 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 metadataMetadataThe term metadata is an ambiguous term which is used for two fundamentally different concepts . Although the expression "data about data" is often used, it does not apply to both in the same way. Structural metadata, the design and specification of data structures, cannot be about data, because at...
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 fallroughly 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
http://developer.gnome.org/doc/API/2.0/gobject/glib-mkenums.html 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 struct color { int r, g, b; }
. To avoid having to subclass GObject
, we can create a boxed type 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
Overkill
Overkill is the use of excessive force or action that goes further than is necessary to achieve its goal.-Nuclear weapons:Overkill is especially used to refer to a destructive nuclear capacity exceeding the amount needed to destroy an enemy...
. 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 JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
-style interfaces, although these are still relatively rarely used, likely because they have only been around since GLib 2.4 (which was released on March 16, 2004). The GNU Image Manipulation Program uses GObject interfaces.
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
Callback (computer science)
In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine defined in a higher-level layer....
. 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 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.
Since a C structure cannot have access modifiers like “public”, “protected”, or “private”, a common technique 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.The main drawback of the GObject framework is its verbosity. Large amounts of boilerplate code, such as manual definitions of type casting macros and obscure type registration incantations, are necessary to create a new class. The GObject Builder, or GOB2, is a tool that attempts to remedy this problem by offering a template syntax reminiscent of Java. Code written using GOB2 is pre-processed into vanilla C code prior to compilation. Another compiler-to-C for the GObject type system is Vala
Vala (programming language)
Vala is a programming language created with the goal of bringing modern language features to C, with no added runtime needs and with little overhead, by targeting the GObject object system. It is being developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#...
, which uses a C#-style syntax.
Usage
The combination of C and GObject is used in many successful free softwareFree software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
projects, such as the GNOME
GNOME
GNOME is a desktop environment and graphical user interface that runs on top of a computer operating system. It is composed entirely of free and open source software...
desktop, the GTK+
GTK+
GTK+ is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the X Window System, along with Qt.The name GTK+ originates from GTK;...
toolkit and the GIMP
GIMP
GIMP is a free software raster graphics editor. It is primarily employed as an image retouching and editing tool and is freely available in versions tailored for most popular operating systems including Microsoft Windows, Apple Mac OS X, and Linux.In addition to detailed image retouching and...
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++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, Ruby, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
, and .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
/Mono
Mono (software)
Mono, pronounced , is a free and open source project led by Xamarin to create an Ecma standard compliant .NET-compatible set of tools including, among others, a C# compiler and a Common Language Runtime....
. As a result, it is usually relatively painless to create language binding
Language binding
In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...
s for well-written libraries that use the GObject framework.
Writing GObject code in C in the first place, however, is relatively painful. The learning curve
Learning curve
A learning curve is a graphical representation of the changing rate of learning for a given activity or tool. Typically, the increase in retention of information is sharpest after the initial attempts, and then gradually evens out, meaning that less and less new information is retained after each...
is quite steep, and programmers with experience in high-level object-oriented languages are likely to find it very tedious to work with GObject in C. For example, creating a non-trivial subclass (even just a subclass of
GObject
) can require writing and/or copying hundreds of lines of code.Although they are not really first-class object
First-class object
In programming language design, a first-class citizen , in the context of a particular programming language, is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable...
s (there are no actual metatypes in GType), metaobject
Metaobject
In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. The object that the metaobject is about is called the base object...
s like classes and interfaces are created by GObject applications at runtime, and provide good support for introspection. The introspective
capabilities are used by language bindings and user interface design applications like Glade
Glade Interface Designer
Glade Interface Designer is a graphical user interface builder for GTK+, with additional components for GNOME. In its third version, Glade is programming language–independent, and does not produce code for events, but rather an XML file that is then used with an appropriate binding .Glade is...
to allow doing things like loading a shared library that provides a GObject class - usually some kind of widget
Web widget
In computing a web widget is a software widget for the web. It's a small application that can be installed and executed within a web page by an end user. They are derived from the idea of code reuse. Other terms used to describe web widgets include: portlet, gadget, badge, module, webjit, capsule,...
, 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 complete object system for C, it can be seen as an alternative to C-derived languages such as C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
and Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...
. (Though C++, in particular, also has many other features apart from its object system.) An easily observed difference between C++ and GObject is that GObject (like Java) does not support multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....
.
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. Hence, “C with GObject”, considered as a language separate from plain C, is a strict superset of plain C — unlike C++.
On platforms where there is no standard ABI
Application binary interface
In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...
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
Name mangling
In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....
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
Method overloading
Function overloading or method overloading is a feature found in various programming languages such as Ada, C#, VB.NET, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the...
multiple times, or identically named functions may appear in different namespaces
Namespace (computer science)
A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols . An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces...
, but in object code
Object code
Object code, or sometimes object module, is what a computer compiler produces. In a general sense object code is a sequence of statements in a computer language, usually a machine code language....
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 signals (called events
Event-driven programming
In computer programming, event-driven programming or event-based programming is a programming paradigm in which the flow of the program is determined by events—i.e., sensor outputs or user actions or messages from other programs or threads.Event-driven programming can also be defined as an...
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 components much more encapsulated
Information hiding
In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed...
and reusable than the ones using plain C++ or Java. Note however that signals are available on almost all platforms, although sometimes an extra library is needed, such as Boost.Signals2 for C++.
See also
- ValaVala (programming language)Vala is a programming language created with the goal of bringing modern language features to C, with no added runtime needs and with little overhead, by targeting the GObject object system. It is being developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#...
- GenieGenie (programming language)Genie is a modern, general-purpose high-level programming language in active development since 2008. It was designed as an alternative, simpler and cleaner dialect for the Vala compiler, while preserving the same functionality of the Vala language. Genie uses the same compiler and libraries as...