* binding
@ 2003-07-07 16:30 Cephus�
2003-07-07 17:04 ` binding Ching Bon Lam
2003-07-07 17:40 ` binding Ludovic Brenta
0 siblings, 2 replies; 3+ messages in thread
From: Cephus� @ 2003-07-07 16:30 UTC (permalink / raw)
Hello all, I am new to Ada and I guess really programming alltogether. I
read a lot here about bindings and I was wondering if someone could give me
a simple definition of the term? thanks for your time...
beau
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: binding
2003-07-07 16:30 binding Cephus�
@ 2003-07-07 17:04 ` Ching Bon Lam
2003-07-07 17:40 ` binding Ludovic Brenta
1 sibling, 0 replies; 3+ messages in thread
From: Ching Bon Lam @ 2003-07-07 17:04 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 427 bytes --]
I'm not the best explainer.. so:
http://www.oopweb.com/Ada/Documents/Lovelace/Volume/s16s3.htm
greetings,
cbl
"Cephus�" <beau@hiwaay.net> wrote in
news:vgj7veqj2uib1e@corp.supernews.com:
> Hello all, I am new to Ada and I guess really programming alltogether.
> I read a lot here about bindings and I was wondering if someone could
> give me a simple definition of the term? thanks for your time...
>
> beau
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: binding
2003-07-07 16:30 binding Cephus�
2003-07-07 17:04 ` binding Ching Bon Lam
@ 2003-07-07 17:40 ` Ludovic Brenta
1 sibling, 0 replies; 3+ messages in thread
From: Ludovic Brenta @ 2003-07-07 17:40 UTC (permalink / raw)
"Cephus�" <beau@hiwaay.net> writes:
> Hello all, I am new to Ada and I guess really programming alltogether. I
> read a lot here about bindings and I was wondering if someone could give me
> a simple definition of the term? thanks for your time...
Before defining a binding, let me first define a library; if you
already know what this is you can skip ahead. A library is a
collection of subprograms that are packaged together so that people
can reuse them in many different programs. For example, the GIMP
ToolKit (http://www.gtk.org) is a library (actually a set of several
libraries) that makes it easy to write programs with a GUI.
You can write a library in Ada if you want (in fact there are quite a
few libraries written in Ada), but there are other languages as well.
In particular, many libraries are written in C, because this language
is very simple and is a least common denominator amongst languages.
That is the reason why GTK is written in C.
Ada has special built-in support for "interfacing with foreign
languages". This means that, from an Ada program, you can call
subprograms that are written in other languages, in particular C. A
"binding", therefore, is essentially a set of subprogram
specifications that make library subprograms visible to you, the Ada
programmer. Many bindings exist for many different libraries; for one
example, see http://libre.act-europe.fr/GtkAda for an Ada binding to
GTK.
There are two kinds of bindings: "thin" and "thick". A "thin" binding
provides no abstraction over the library; it just exposes the
subprograms from the library. Usually, these subprograms are
associated with data types; it is possible to define Ada data types
that are equivalent to the C data types.
By contrast, a "thick" binding presents a cleaner and more Ada-ish
interface which you can use to call library subprograms. For example,
a "thick" binding would use Ada exceptions, rather than error codes,
to report problems; it would also utilise Ada's overloading features
to make the binding simpler to use than the original library. C has
neither exceptions nor overloading, so libraries written in C tend to
be clumsier to use than Ada libraries or Ada thick bindings to them.
Here is an example of a binding that I posted not long ago; it binds
the function "popen" from the POSIX C library and makes it available
to Ada programmers. This function, in C, has the following prototype:
FILE * popen (const char *COMMAND, const char *MODE)
meaning that it takes two C-style "strings" and returns a pointer to a
FILE structure. If there is an error, popen returns a null pointer.
I've provided a thick binding to this function below; instead of
returning a special value (null), it raises an exception if something
goes wrong. Also, the second parameter (MODE) is modelled by an Ada
enumerated type because there is a finite set of acceptable values.
Inside the Ada procedure Popen, there is a nested function called
Popen_Native. This is a "thin" binding : it matches the C function
exactly. So, as is usually the case, the "thick" binding is built on
top of a "thin" binding; here, I hide the "thin" binding from the Ada
programmer by nesting it into the "thick" binding.
HTH
with Interfaces.C; use Interfaces.C;
with Interfaces.C_Streams;
with System;
with Ada.Text_IO;
procedure Spawn_And_Read is
type Popen_Mode is (Read, Write, Read_Write);
Popen_Error : exception;
procedure Popen (Command : in String;
Mode : in Popen_Mode;
Stream : out Interfaces.C_Streams.FILEs)
-- Executes COMMAND in a subshell, and attaches STREAM to the
-- subprocess' standard input and/or output. If MODE = Read,
-- you can read the process' output from STREAM. If MODE = Write,
-- you can write into the process' standard input with STREAM.
-- if MODE = Read_Write, both operations are allowed.
is
function Popen_Native (Command, Mode : Interfaces.C.char_array)
return Interfaces.C_Streams.FILEs;
pragma Import (C, Popen_Native, "popen");
use type Interfaces.C_Streams.FILEs;
begin
case Mode is
when Read =>
Stream := Popen_Native (To_C (Command), To_C ("r"));
when Write =>
Stream := Popen_Native (To_C (Command), To_C ("w"));
when Read_Write =>
Stream := Popen_Native (To_C (Command), To_C ("rw"));
end case;
if Stream = Interfaces.C_Streams.NULL_Stream then
raise Popen_Error;
end if;
end Popen;
procedure Pclose (Stream : Interfaces.C_Streams.FILEs);
pragma Import (C, Pclose, "pclose");
Stream : Interfaces.C_Streams.FILEs;
Buffer : String (1 .. 100);
Bytes_Read : Interfaces.C_Streams.size_t;
begin
Popen (Command => "ls -lF /", Mode => Read, Stream => Stream);
while Interfaces.C_Streams.Feof (Stream) = 0 loop
Bytes_Read := Interfaces.C_Streams.fread (Buffer => Buffer'Address,
Size => 1,
Count => Buffer'Length,
Stream => Stream);
Ada.Text_IO.Put (Buffer (1 .. Integer (Bytes_Read)));
end loop;
Pclose (Stream);
end Spawn_And_Read;
--
Ludovic Brenta.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-07-07 17:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-07 16:30 binding Cephus�
2003-07-07 17:04 ` binding Ching Bon Lam
2003-07-07 17:40 ` binding Ludovic Brenta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox