comp.lang.ada
 help / color / mirror / Atom feed
From: Andreas Almroth <andreas_no_spam@almroth.com>
Subject: Re: Interfacing C++ classes, but what about the destructor?
Date: Thu, 20 Oct 2005 01:40:14 +0200
Date: 2005-10-20T01:40:14+02:00	[thread overview]
Message-ID: <3ro3rbFkc5ntU1@individual.net> (raw)
In-Reply-To: <sa4ll0pjerr.fsf@snoopy.microcomaustralia.com.au>

Brian May wrote:
>>>>>>"Andreas" == Andreas Almroth <andreas_no_spam@almroth.com> writes:
> 
> 
>     Andreas> Hi all, I have been searching google, and read the Gnat
>     Andreas> RM&UG, and I have some working code interfacing C++
>     Andreas> classes. No problem really so far, although it feels as
>     Andreas> if I'm wading in unchartered waters... Documentation
>     Andreas> seems to be sparse, although the documentation that is
>     Andreas> there pretty much shows you how to do it...
> 
> I think you have already done way more then most of us - I seem to
> remember the recommended approach is usually to write a C layer
> between the Ada layer and C++ layer.
> 

Yes, this is my understanding as well, after reading some more on the
topic today. The recommended approach is to use C layer to wrap methods
working on C++ classes or class methods. Which is what I have done as
well. And it works quite well, although, I must admit, I was a bit
surprised that passing an Ada object (not access to) to these functions
worked "out-of-the-box" as I declare a Object *p in the C functions.
But hey, as long as it works... All this pass-by-reference, pass-by-copy
is just confusing me anyway... :)

>     Andreas> My question is though on the topic of C++ class
>     Andreas> destructors. When I search, I find references to
>     Andreas> CPP_Destructor pragma in some documentation, but when I
>     Andreas> compile the code, I get a warning stating that the pragma
>     Andreas> is unknown. I have tried GNAT 3.15p, gcc 3.4.2 and gcc
>     Andreas> 4.0.1, all the same.
> 
> I can't see any documentation on the CPP_Destructor call here (version
> 3.4 info pages).

Older documentation for GNAT 3.10 does mention the pragma. OK, I know
that version is _quite_ old...

> 
>     Andreas> As the pragma doesn't seem to exist, contrary to older
>     Andreas> documentation, I assume it it has been removed due to it
>     Andreas> doesn't work, or was deemed unneccesary.
> 
>     Andreas> Regardless of reasons, I 1) wonder the history behind
>     Andreas> this, 2) how to call the destructor of the C++ class from
>     Andreas> Ada side, or why I shouldn't.
> 
> Sorry, I suspect you may not get much help here :-(.
> 
> The example in my copy of the UG "2.11.3 A Simple Example" appears to
> use C bindings, not C++ bindings, even though the language is C++. A
> good example that uses C++ bindings would be appreciated...
> 

The simple example I have read is using a limited record to mimic a C++
class which has inherited from its parent. This approach is easy when
you don't have a huge class framework, or so I imagine.

I'm using CPP_Class pragma in order to retain the tagged<=>class
relationship. This way, to my understanding, I should be able to follow
the class tree in C++ with corresponding Ada tagged types.

> However, I would speculate that a virtual destructor (remember
> destructor don't need to be virtual, but it seems like a good idea) is
> treated like any other virtual function.
> 
> This might imply GNAT will not call the destructor automatically for
> you.

Quite right. I have worked on this some more, using a C layer wrapper
for the destructor, importing its mangled name, and calling it manually
in my Ada code. So, the neat feature with CPP_Constructor pragma will
have to be handled manually for the destructor. I will do some work on
this and calling the C++ destructor from my Finalize of my controlled
Ada type. I foresee a controlled type might interfere with the clean
mapping between my Ada type and the C++ class though.
Anyway,I hope this will work. Obviously not as neat as automating it
using a pragma, but will have the effect that I would like to achieve.

Thanks for your reply.

Regards,
Andreas



      reply	other threads:[~2005-10-19 23:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-19 12:34 Interfacing C++ classes, but what about the destructor? Andreas Almroth
2005-10-19 22:58 ` Brian May
2005-10-19 23:40   ` Andreas Almroth [this message]
replies disabled

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