 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jarda Gresula Guest
|
Posted: Sun Dec 11, 2005 12:56 pm Post subject: Decomposing a C++ class to an equivalent set of C functions |
|
|
Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
<example_of_expected_functionality>
//----- myclass.hpp - input
class MyClass
{
public:
virtual int Fun( int i ) = 0;
// ..
};
//----- myclass_c.h - generated
DEFINE_HANDLE(HMyClass);
int MyClass_Fun( HMyClass* handle, int i );
//----- myclass_c.cxx - generated
int MyClass_Fun( HMyClass* handle, int i )
{
MyClass* obj = POINTER_FROM_HANDLE(handle);
return obj->Fun(i);
}
</example>
Thanks
--
Jarda
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co Guest
|
Posted: Mon Dec 12, 2005 12:09 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
Jarda Gresula wrote:
| Quote: | Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
|
Yes, a compiler. Nothing short will work. Comeau offers such a product,
which is based on an EDG frontend. I'm not aware of any other
companies.
(The old cfront had the same capability)
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
benben Guest
|
Posted: Mon Dec 12, 2005 12:13 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
Jarda Gresula wrote:
| Quote: | Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
|
It really is just a pointer into an array of pointers to function.
Ben
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Dec 12, 2005 7:31 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
[email]Michiel.Salters (AT) tomtom (DOT) com[/email] wrote:
| Quote: | Jarda Gresula wrote:
Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
Yes, a compiler. Nothing short will work. Comeau offers such a product,
which is based on an EDG frontend. I'm not aware of any other
companies.
(The old cfront had the same capability)
|
A compiler would not be of much help here. As the example demonstrates,
generating the C declarations from the C++ class interface is purely a
textual transformation - and an extremely portable one at that. With no
dependencies on either the particulars of name mangling or vtable
layout, it should work anywhere.
More formally, the transformation could be done with a "reverse" IDL
compiler. Software components usually define their interface in an
interface description language (IDL), which in turn is compiled to
generate header files specific to a language. In this case the poster
essentially wants to go backwards and obtain the IDL from a C++
interface declaration. Once the IDL had been obtained, it could then be
used to generate a C interface.
I'm not sure whether such a compiler exists. But given how regular the
transformation is, I would recommend a quick and simple Perl (or
similar) script to process the file. Below is some code in Perl to
demonstrate how this might be done. (Note that the program is not
complete, in particular it does not handle any function pointer
parameters correctly). But it will transform this line:
virtual int Fun2( int a, long b ) = 0;
into
int MyClass_Fun2(HMyClass *handle, int a, long b );
{
MyClass* obj = POINTER_FROM_HANDLE(handle);
obj->Fun2( a, b );
}
which is basically the desired result:
my $line;
while( $line = <STDIN>)
{
chomp($line);
if ($line =~ /virtuals+(w+s)+(w+)[(]([^)]*)[)]/)
{
my $retType = $1;
my $fName = $2;
my $args = $3;
my $argNames = $args . ",";
$argNames =~ s/.*?(w+s*,)/ $1/g;
$argNames =~ s/,[^,]*$//;
print $retType . " MyClass_" . $fName .
"(HMyClass *handle," . $args . ");n";
print "{n";
print "tMyClass* obj = POINTER_FROM_HANDLE(handle);n";
print "tobj->" . $fName . "(" . $argNames . ");n" ;
print "}n";
}
}
Of course a C++ version would be more apropos. And had I a lot of free
time, I could have written one .
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jarda Gresula Guest
|
Posted: Tue Dec 13, 2005 5:02 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
Greg Herlihy wrote:
| Quote: | [...]
generating the C declarations from the C++ class interface is purely a
textual transformation - and an extremely portable one at that. With no
dependencies on either the particulars of name mangling or vtable
layout, it should work anywhere.
|
Right, the portability is my motivation here. Generally, the textual
transformation is not as straightfoward as it could seem at the first
glance. You have to cope with references, default parameters, bool
type, etc.
There are code generators like SWIG which produce bindings from various
scripting languages to C++. So I just wondered that some could generate
a 'C binding'.
| Quote: | [...]
I'm not sure whether such a compiler exists. But given how regular the
transformation is, I would recommend a quick and simple Perl (or
similar) script to process the file. Below is some code in Perl to
[...]
Of course a C++ version would be more apropos. And had I a lot of free
time, I could have written one .
|
Yes, if I had more time I would have written one in Python based on the
gccxml output :)
Regards
--
Jarda
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co Guest
|
Posted: Tue Dec 13, 2005 6:34 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
Greg Herlihy wrote:
| Quote: | Michiel.Salters (AT) tomtom (DOT) com wrote:
Jarda Gresula wrote:
Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
Yes, a compiler. Nothing short will work. Comeau offers such a product,
which is based on an EDG frontend. I'm not aware of any other
companies.
(The old cfront had the same capability)
A compiler would not be of much help here. As the example demonstrates,
generating the C declarations from the C++ class interface is purely a
textual transformation - and an extremely portable one at that. With no
dependencies on either the particulars of name mangling or vtable
layout, it should work anywhere.
|
Sure. Now add a few more lines of C++
namespace interfaces {
class B {
virtual std::string foo();
};
class D : public interfaces::B {
virtual void bar();
};
}
Good luck with your textual transformation. I don't even know what the
equivalent of D::foo should be in C.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co Guest
|
Posted: Tue Dec 13, 2005 6:35 pm Post subject: Re: Decomposing a C++ class to an equivalent set of C functi |
|
|
benben wrote:
| Quote: | Jarda Gresula wrote:
Does not anyone know some tool that is able to automatically decompose
a C++ abstract class to an equivalent set of C functions?
It really is just a pointer into an array of pointers to function.
|
Sometimes. Multiple inheritance can break that assumption as well.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|