|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" type="topic" id="introspection" xml:lang="es">
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<link type="guide" xref="index#specific-how-tos"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Philip Withnall</name>
|
|
Packit |
1470ea |
<email its:translate="no">philip.withnall@collabora.co.uk</email>
|
|
Packit |
1470ea |
<years>2015</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<include xmlns="http://www.w3.org/2001/XInclude" href="cc-by-sa-3-0.xml"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>GObject Introspection support in library code</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2016</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Javier Mazorra</mal:name>
|
|
Packit |
1470ea |
<mal:email>mazi.debian@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2016</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Introspección</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<synopsis>
|
|
Packit |
1470ea |
<title>Resumen</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<link href="https://wiki.gnome.org/Projects/GObjectIntrospection"> GObject
|
|
Packit |
1470ea |
introspection</link> (abbreviated ‘GIR’) is a system which extracts APIs
|
|
Packit |
1470ea |
from C code and produces binary type libraries which can be used by non-C
|
|
Packit |
1470ea |
language bindings, and other tools, to
|
|
Packit |
1470ea |
<link href="http://en.wikipedia.org/wiki/Type_introspection">introspect</link>
|
|
Packit |
1470ea |
or <link href="http://en.wikipedia.org/wiki/Language_binding">wrap</link>
|
|
Packit |
1470ea |
the original C libraries. It uses a system of annotations in documentation
|
|
Packit |
1470ea |
comments in the C code to expose extra information about the APIs which is
|
|
Packit |
1470ea |
not machine readable from the code itself.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
It should be enabled for all public APIs: so all libraries. It cannot be
|
|
Packit |
1470ea |
enabled for programs, since they expose no APIs. However, it is still
|
|
Packit |
1470ea |
recommended to <link xref="documentation#introspection-annotations">add
|
|
Packit |
1470ea |
introspection annotations to documentation comments</link> in program
|
|
Packit |
1470ea |
code, as they clarify the documentation.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>Active la introspección para todas las bibliotecas. (Consulte la <link xref="#using-introspection"/>) </item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Pay attention to warnings from <cmd>g-ir-scanner</cmd> and
|
|
Packit |
1470ea |
introspectable="0" attributes in GIR files.
|
|
Packit |
1470ea |
(<link xref="#using-introspection"/>)
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Add introspection annotations to all documentation comments.
|
|
Packit |
1470ea |
(<link xref="#using-introspection"/>)
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Design APIs to be introspectable from the start.
|
|
Packit |
1470ea |
(<link xref="#api-design"/>)
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</synopsis>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="using-introspection">
|
|
Packit |
1470ea |
<title>Usar la introspección</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
The first step for using introspection is to add it to the build system,
|
|
Packit |
1470ea |
following the instructions
|
|
Packit |
1470ea |
<link href="https://wiki.gnome.org/Projects/GObjectIntrospection/AutotoolsIntegration#Method_1_-_Recommended_-_most_portable">here</link>,
|
|
Packit |
1470ea |
following method 1. This should be done early in the life of a project, as
|
|
Packit |
1470ea |
introspectability affects <link xref="#api-design">API design</link>.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
This should result in a <file>.gir</file> and <file>.typelib</file> file
|
|
Packit |
1470ea |
being generated for the project. The <file>.gir</file> file is human
|
|
Packit |
1470ea |
readable, and can be inspected manually to see if the API has been
|
|
Packit |
1470ea |
introspected correctly (although the GIR compilation process will print
|
|
Packit |
1470ea |
error messages and warnings for any missing annotations or other
|
|
Packit |
1470ea |
problems). APIs with introspectable="0" will not be exposed
|
|
Packit |
1470ea |
to language bindings as they are missing annotations or are otherwise not
|
|
Packit |
1470ea |
representable in the GIR file.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
The next step is to
|
|
Packit |
1470ea |
<link xref="documentation#introspection-annotations">add annotations to
|
|
Packit |
1470ea |
the documentation comments for every piece of public API</link>. If a
|
|
Packit |
1470ea |
particular piece of API should not be exposed in the GIR file, use the
|
|
Packit |
1470ea |
(skip) annotation. Documentation on the available annotations
|
|
Packit |
1470ea |
is
|
|
Packit |
1470ea |
<link href="https://wiki.gnome.org/Projects/GObjectIntrospection/Annotations">here</link>.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
If annotating the code for a program, a good approach is to split the bulk
|
|
Packit |
1470ea |
of the code out into an internal, private convenience library. An internal
|
|
Packit |
1470ea |
API reference manual can be built from its documentation comments (see
|
|
Packit |
1470ea |
<link xref="documentation"/>). The library is then not installed, but is
|
|
Packit |
1470ea |
linked in to the program which is itself installed. This approach for
|
|
Packit |
1470ea |
generating internal API documentation is especially useful for large
|
|
Packit |
1470ea |
projects where the internal code may be large and hard to navigate.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Annotations do not have to be added exhaustively: GIR has a set of default
|
|
Packit |
1470ea |
annotations which it applies based on various conventions (see
|
|
Packit |
1470ea |
<link xref="#api-design"/>). For example, a const gchar*
|
|
Packit |
1470ea |
parameter does not need an explicit (transfer none)
|
|
Packit |
1470ea |
annotation, because the const modifier implies this already.
|
|
Packit |
1470ea |
Learning the defaults for annotations is a matter of practice.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="api-design">
|
|
Packit |
1470ea |
<title>Diseño de la API</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
In order to be introspectable without too many annotations, APIs must
|
|
Packit |
1470ea |
follow certain conventions, such as the
|
|
Packit |
1470ea |
<link href="https://developer.gnome.org/gobject/stable/gtype-conventions.html">standard
|
|
Packit |
1470ea |
GObject naming conventions</link>, and the
|
|
Packit |
1470ea |
<link href="https://wiki.gnome.org/Projects/GObjectIntrospection/WritingBindingableAPIs">conventions
|
|
Packit |
1470ea |
for bindable APIs</link>. This is necessary because of the flexibility of
|
|
Packit |
1470ea |
C: code can be written to behave in any way imaginable, but higher level
|
|
Packit |
1470ea |
languages don’t allow this kind of freedom. So in order for a C API to be
|
|
Packit |
1470ea |
representable in a higher level language, it has to conform to the
|
|
Packit |
1470ea |
behaviors supported by that language.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
For example, GIR expects that if a function can fail, it will have a
|
|
Packit |
1470ea |
GError** parameter, which will always be its final parameter.
|
|
Packit |
1470ea |
The GIR scanner detects this and automatically converts that parameter to
|
|
Packit |
1470ea |
an exception attribute on the method in the GIR file. It cannot do this if
|
|
Packit |
1470ea |
the GError* is returned directly, or is not the final
|
|
Packit |
1470ea |
function parameter, for example.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Therefore, APIs must be designed to be introspectable, and the GIR file
|
|
Packit |
1470ea |
should be checked as the APIs are being written. If the GIR doesn’t match
|
|
Packit |
1470ea |
what you expect for a new API, the API may need extra annotations, or even
|
|
Packit |
1470ea |
for its C declaration to be changed (as in the case of
|
|
Packit |
1470ea |
<link href="https://wiki.gnome.org/Projects/GObjectIntrospection/WritingBindingableAPIs#va_list">va_list </link>).
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<cmd>g-ir-scanner</cmd> emits warnings when it encounters code it does not
|
|
Packit |
1470ea |
understand. By passing <cmd>--warn-error</cmd> as well as
|
|
Packit |
1470ea |
<cmd>--warn-all</cmd> in INTROSPECTION_SCANNER_ARGS in
|
|
Packit |
1470ea |
<file>Makefile.am</file>, compilation will fail when unintrospectable APIs
|
|
Packit |
1470ea |
are encountered. This will ensure all new APIs are introspectable, and is
|
|
Packit |
1470ea |
highly recommended.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|