From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a60bbbe59db2715 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Andreas Almroth Newsgroups: comp.lang.ada Subject: Re: Interfacing C++ classes, but what about the destructor? Date: Thu, 20 Oct 2005 01:40:14 +0200 Message-ID: <3ro3rbFkc5ntU1@individual.net> References: <3rmsq8Fk1nvkU1@individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net mteWhxUizlRRagQHFYJoUgsTBxoGo2FuNsxSUa31M0QO/Jp64E User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en In-Reply-To: Xref: g2news1.google.com comp.lang.ada:5811 Date: 2005-10-20T01:40:14+02:00 List-Id: Brian May wrote: >>>>>>"Andreas" == Andreas Almroth 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