comp.lang.ada
 help / color / mirror / Atom feed
From: Damien Carbonne <damien.carbonne@free.fr>
Subject: Cairo Ada binding questions
Date: Sun, 24 Sep 2006 18:25:14 +0200
Date: 2006-09-24T18:25:15+02:00	[thread overview]
Message-ID: <4516b16a$0$31395$626a54ce@news.free.fr> (raw)

I have started to write an Ada binding for Cairo
(http://cairographics.org). Cairo is written in C and I know how to
write a C/Ada thin binding.

My trouble is with a thick Ada API. I will try to explain it with an
example.
Cairo has a concept of Surface class that can is specialized into PDF 
Surface, SVG surface, etc. In the C API, Surfaces (as all Cairo 
"classes") are always manipulated through pointers. As a Surface can be 
shared, the C API provides a way to increment / decrement the reference 
counter.
So, I see here two concepts : Surface and Surface Handle.

In Ada, I could have something like this :
    type Surface_Record (<>) is tagged limited private;
    type Surface_Ref is access all Surface_Record'Class;
The would be used that way:
    procedure Foo (Surface : in [out] Surface_Record'Class; ...);
And when necessary:
   procedure Bar (Surface : in Surface_Ref; ...);
or, probably better(?):
   procedure Bar (Surface : access Surface_Record['Class]; ...);

However, by doing that, I let the user manage himself reference
counting. If I want to provide a Handle for ref counting, I have several
solutions based on smart pointers:
1) provide a Handle for root class of each hierarchy.
2) provide a Handle for each class of the hierarchy.
This second solution seems quite heavy to write. One needs to maintain
two parallel hierarchies, which is not really satisfying.
In both cases, smart pointers usage may also be quite uneasy, as there
is no way to make an object look like an access in Ada. At least, this
was impossible in Ada 95 (cf. Smart Pointers article by Matthew Heaney
on AdaPower).
So we would have something like this:
    type Surface_Handle is private;
    function Ref (Handle : Surface_Handle) return Surface_Ref;
that could be used like this :
    Surface : Surface_Handle;
begin
    Foo (Ref (Surface).all, ...);
or with the renaming trick to avoid this wordy thing.

There is also another solution, that would consist in hiding the Surface
concept, providing only the Surface_Handle one.
In that case, API would be modified like this :
    procedure Foo (Surface : in Surface_Handle'Class; ...);
Direction would (almost ?) always be in.

 From user point of view, this is far easier. From engineering point of
view, I really wonder.

Searching a solution to this issue, I looked at Ada.Text_IO and saw that
File_Type is often passed as a in parameter when, IMO, it should not.
For example :
    procedure New_Line (File : File_Type; Spacing : Positive_Count := 1);
Adding a new line modifies a file. This implies that File_Type must be
implemented with an indirection. Unless there is something I have not
understood, I don't find this really satisfying.

I Think my problem is quite general and may also exist in pure Ada code.
IIRC, GtkAda adopted the very first solution: It's user responsibility
to handle memory correctly.

To summarize solutions :
1) manual memory management
   1.a) Class Hierarchy and Ref (Ada Access) concepts
   1.b) ???
2) automatic memory management
   2.a) Class Hierarchy and Handle (Root only)
   2.b) Class Hierarchy and Handle hierarchy
   2.c) Handle hierarchy
   2.d) ???

Any advice would be appreciated !

Thanks.

Damien

PS1: If a Cairo Ada binding already exists, let me know !
PS2: If this binding reaches a satisfying state and is of interest to 
anyone, I will publish it.



             reply	other threads:[~2006-09-24 16:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-24 16:25 Damien Carbonne [this message]
2006-09-24 19:50 ` Cairo Ada binding questions Dmitry A. Kazakov
2006-09-24 20:43 ` tmoran
2006-09-24 21:39 ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox