* Interfacing C++ classes, but what about the destructor? @ 2005-10-19 12:34 Andreas Almroth 2005-10-19 22:58 ` Brian May 0 siblings, 1 reply; 3+ messages in thread From: Andreas Almroth @ 2005-10-19 12:34 UTC (permalink / raw) Hi all, I have been searching google, and read the Gnat RM&UG, and I have some working code interfacing C++ classes. No problem really so far, although it feels as if I'm wading in unchartered waters... Documentation seems to be sparse, although the documentation that is there pretty much shows you how to do it... My question is though on the topic of C++ class destructors. When I search, I find references to CPP_Destructor pragma in some documentation, but when I compile the code, I get a warning stating that the pragma is unknown. I have tried GNAT 3.15p, gcc 3.4.2 and gcc 4.0.1, all the same. As the pragma doesn't seem to exist, contrary to older documentation, I assume it it has been removed due to it doesn't work, or was deemed unneccesary. Regardless of reasons, I 1) wonder the history behind this, 2) how to call the destructor of the C++ class from Ada side, or why I shouldn't. Well, any pointers is appreciated... Regards, /Andreas ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Interfacing C++ classes, but what about the destructor? 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 0 siblings, 1 reply; 3+ messages in thread From: Brian May @ 2005-10-19 22:58 UTC (permalink / raw) >>>>> "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. 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). 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... 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. -- Brian May <bam@snoopy.apana.org.au> ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Interfacing C++ classes, but what about the destructor? 2005-10-19 22:58 ` Brian May @ 2005-10-19 23:40 ` Andreas Almroth 0 siblings, 0 replies; 3+ messages in thread From: Andreas Almroth @ 2005-10-19 23:40 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-10-19 23:40 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox