Debian Codegraph @

README.md

raw symbols SCIP
1 # cppgir
2
3 `cppgir` is a [GObject-Introspection](https://wiki.gnome.org/Projects/GObjectIntrospection)
4 C++ binding wrapper generator. That is, it processes `.gir` files derived
5 from GObject-Introspection annotations into a set of C++ files defining
6 suitable namespaces, classes and other types that together from a C++ binding.
7 In this way, the plain C libraries and objects become available as native
8 objects along with (RAII) managed resource handling. The generated code
9 only requires a C++14 compiler and library (as well as obviously the underlying
10 C headers and libraries that are being wrapped).
11
12
13 ## Installation
14
15 The code generator can be built using [CMake](https://cmake.org) or
16 [meson](https://mesonbuild.com/) and requires
17 Boost and either [fmtlib](http://fmtlib.net/) or a C++20 std library with format
18 support (e.g. gcc 13+). While it obviously depends on distribution,
19 these may be typically be obtained by installing following packages;
20
21 libfmt-dev libboost-dev
22
23 If it is required to compile the manual page ronn is required to be installed
24
25 The following step will provide an additional dependency as a submodule;
26
27 git submodule update --init
28
29 With all that in place, the usual steps apply, so e.g.
30
31 mkdir build
32 cd build
33 cmake ..
34 cmake --build .
35 cmake --install .
36
37 or alternatively
38
39 mkdir build
40 cd build
41 meson setup . ..
42 meson compile
43 meson install
44
45 The installed components consist of:
46
47 * the code generator executable
48 * (if not embedded by build option) default ignore files
49 * common headers required by the generated code and default overrides
50
51 The latter is needed when using the generated code, the former only
52 at generation time.
53
54 In case of CMake build, it is also possible to build some examples whose
55 sources are also installed along with some documentation.
56 As such, the meson system only builds and install the (most) relevant parts,
57 as may be useful when used as a subproject.
58
59 For either build system, an [example](examples/external) shows how `cppgir`
60 can be used in a project.
61
62 ## Release numbering and API stability
63
64 The generated code for an even-numbered major version should be (compile) API
65 stable. If the major version turns odd, then a cycle/series of incompatible
66 changes may occur, which after actual practice and time can then become
67 the next even stable version.
68
69
70 ## Running
71
72 All details can be found in the [manpage](docs/cppgir.md), but a simple
73 example wrapping [GStreamer](https://gstreamer.freedesktop.org/) libraries is
74 as follows:
75
76 generate --output /tmp/gi GStreamer-1.0
77
78 The above (obviously) assumes that suitable `.gir` files are present in
79 the usual location, as typically installed by a `-dev` package. It is
80 also assumed the generator has been installed (so "running installed").
81 Running the generator uninstalled is also possible, but then (if not embedded) the
82 default ignore files in `data` directory have to explicitly specified as well
83 (using either option or environment variable as specified in manual).
84
85 That's it, all wrapping code is now available in `/tmp/gi` (in a number of
86 subdirectories). It only requires a C++14 compiler and library and can be
87 included and compiled into the target library or executable. The latter then
88 has no additional dependencies other than the plain C libraries (gstreamer and
89 lower level ones in this case).
90
91
92 ## Compilation
93
94 The wrapping binding code presents an API along the lines of any other binding,
95 which also means it maps pretty straight onto the original library API.
96 So all API is simply found where expected. To use it fully inline,
97 the following include snippet suffices:
98
99 #define GI_INLINE 1
100 #include <gst/gst.hpp>
101
102 Of course, the include path must have been setup properly to contain the output
103 root of the generated code (i.e. `/tmp/gi` in previous example). It must also
104 contain the proper path for header code supplied by this repo (using e.g. a
105 pkgconfig fragment if it has been installed, or an IMPORT target or by other
106 means).
107
108 See the [manpage](docs/cppgir.md) for additional essential remarks on the API
109 of the generated code or typical non-inline use of the code.
110
111
112 ## Examples
113
114 The examples (in the so-named directory) illustrate and cover various practical
115 aspects and use of the generated API. While they are all simple and trivial,
116 they do illustrate use in various domains such as `Gio`, `Gst` and `Gtk`.
117
118 For example, when it comes to traditional C `libgio`, there is usually a choice
119 between a synchronous (blocking) call and a corresponding asynchronous
120 (non-blocking) call. The latter is then typically used to establish a chain of
121 completion handlers. In other settings and languages, it has become customary
122 to employ async/await operations/keywords. That way, the flow of code remains
123 linear as in the blocking case. However, some form of callback chain is
124 typically present behind the (implementation) scenes and a more practical
125 consequence is that async/await keywords need to be sprinkled in quite some
126 places (down to call stack depth). That, in turn, is then intrusive/disruptive
127 in its own way.
128
129 Alternatively, one can use so-called stackful co-routines or fibers to maintain
130 stack context when execution has to be suspended (e.g. awaiting I/O, or some
131 condition). An example implementation is provided by [Boost
132 Fiber](https://www.boost.org/doc/libs/latest/libs/fiber/doc/html/index.html)
133 which caters for (cooperative) switching between multiple fibers on a single
134 thread. This is viable in C++ since such code should be exception-safe anyway
135 and as such should not be taken by surprise upon code execution not making it
136 all the way through a code sequence. After all, that might happen at any time
137 upon exception, as in the particular case of fiber context destruction in stead
138 of regular fiber termination. This approach also allows using legacy code in
139 multiple fibers on a single thread, since no additional synchronization is
140 required, and no special support (or keyword sprinkling) is needed along the
141 callstack (down to point of suspension). To come down to it, the async Gio
142 example provides a `GMainContext` based `Fiber` scheduler to integrate and
143 support using Fiber in a standard GLib mainloop setup. In particular, that
144 allows turning `Gio`'s async calls into (apparent) blocking calls when run on a
145 fiber, which then leads to a concise sequential code flow.
146
147
148 ## Features
149
150 A few simple but nice to have features of the generated code are:
151
152 * well structured and easily understood
153 * it can be used either fully inline or have implementation code compiled
154 into one or more object files for subsequent linking
155 * it allows for extensions/overrides (similar to e.g. PyGObject)
156 supplied by separate files (so not in the generated ones),
157 either default ones supplied along with this repo or added custom by
158 the project using the generated code
159
160 Another feauture might be handy even if one is not interested in generated
161 C++ code. As the `.gir` files as processed, a number of consistency checks
162 are performed and warned about. In that way, the generator also acts somewhat
163 as annotation validator, and can typically spot some missing `(out)` or
164 `(array)` (and related) annotations.
165
166 Some other features have been implicitly mentioned above, but are perhaps best
167 high-lighted by addressing how it differs from [gktmm](https://www.gtkmm.org)
168 (and the related question; why another C++ interface). To this end, first
169 note or recall that gtkmm consists of many repositories (glibmm, gtkmm,
170 gstreamermm, ...), all of which typically also yield a distribution
171 package for library code and headers. So, when using e.g. gstreamermm, several
172 such packages are required, either for their headers at compile time or for the
173 libraries at runtime. In contrast, only 1 repo is needed here, and using the
174 generated code then incurs no additional (runtime) dependencies other than the
175 (unavoidable) C libraries.
176
177 The code in gtkmm's (and friends') libraries can be considered as hand-crafted.
178 Well, it is produced based on templates, but those are manually maintained. It
179 does not use or consider the GObject-Introspection annotations in any way (not
180 in the least because it predates that system). So, as a C library and API
181 evolves, the corresponding gtkmm has to be manually synchronized. That is, if
182 there is at least a corresponding gtkmm one. For a not-so-well-known-or-popular
183 one there may not be, and definitely not for a custom developed one. On the
184 other hand, whenever (minimal) annotations are available, you are good to go
185 with any binding, whether PyGObject or the one provided here.
186
187 Since the gtkmm code is hand-crafted API from the ground up, that allows for
188 some nifty things (such as the signal approach mentioned further below). On the
189 other hand, sometimes it might be too nifty in that the C++ API does not quite
190 track or match the original one. Instead, it is then more of an alternative or
191 parallel one. For example, in GStreamer, there is (only) one (mini-object)
192 C-type of `GstEvent` (with a type field indicating the precise type of event).
193 So it is handled this way throughout the API and in bindings (e.g. PyGObject).
194 However, in gstreamermm it has been chosen to have many types (i.e. subclasses)
195 of `Gst::Event`. This may well be a natural C++ way (or a Python one if done
196 with Python classes), but all together it presents an API where things are not
197 quite where expected, and it is in that regard not a straight binding. In
198 contrast, the generated code here is as straight as can be, and functions to
199 call are right where the fingers expect them to be, whether in PyGObject or
200 here.
201
202 So, in the concept of "C++ binding API", gtkmm has emphasis on C++
203 up to the point of almost a parallel or independent API with quite some
204 trimming. The latter is illustrated by the separate
205 [libsigcplusplus](https://github.com/GNOME/libsigcplusplus) (with similar
206 notions in e.g. [boost signals2](https://github.com/boostorg/signals2)). Here,
207 however, the emphasis is on binding.
208
209 Of course, other than this straight binding, one is still free to use and bring
210 in any other lib, e.g. one of the aforementioned signal helper libs. However,
211 in most cases the "straight bindings" will suffice. Moreover, a few small
212 additional RAII helpers are provided that may be useful. See the examples for
213 an illustration on how they can be used.
214
215 ## Limitations and Remarks
216
217 A compile-time binding is somewhat different than a typical more runtime script
218 language binding (e.g. Python). While most of the annotated API is usually
219 well enough handled and covered for practical use, there are some limitations
220 and issues to consider. Again, more details and further remarks are provided
221 in the [manpage](docs/cppgir.md)