home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!nic!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.c++
- Subject: Re: Determining object type at runtime ?
- Message-ID: <1992Jul31.043437.21113@netlabs.com>
- Date: 31 Jul 92 04:34:37 GMT
- References: <Bs43r6.C24@ibg1.ibg.sub.org>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 67
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <Bs43r6.C24@ibg1.ibg.sub.org> ado@ibg1.ibg.sub.org (Christoph Adomeit) writes:
- : Hi C++ freaks,
- :
- : I need an idea about how to solve the following problem:
- : I embedded some C++ classes into an interpreter (perl).
- : Perl only understands C-calls, so I wrapped some functions
- : like
- :
- : extern "C"
- : {
- : void *new_myobject(constructor vars)
- : { return (void*)new myobject(constructor vars); }
- :
- : int myobject_print(void *myobj)
- : {
- : myobject *object=(myobject*)myobj;
- : return object->print;
- : }
- : }
- : ...
-
- You shouldn't need to go to all that trouble. If your init routine
- that's called from userinit() is called, say, init_foo(), then in your
- foo.cc (or foo.C) file, say
-
- extern "C" {
- int init_foo();
- }
-
- Everything else can be in native C++, since the init_foo() routine will
- resolve anything that would look strange to C.
-
- Of course, this may work better with my version of perl.h, which is
- already C++ clean... :-)
-
- : I can get a reference to myobject from perl by calling &new_myobject.
- : Afterwards I can work from perl with the class like this:
- : $object=&new_myobject(constructor vars);
- : $result=&myobject_print($object);
- :
- : everything works fine !. The only Problem is, if the ptr passed to
- : myobject print is not a pointer to a class "myobject". The program
- : will of course dump core. How I can make sure that myobject->print
- : is only called with a valid ptr to an object of type myobject ?
- :
- : One solution would be to implement a method "type" to all known objects
- : and compare object->type, but most of the time i call functions with
- : int's char's ... for which I can't implement such a method.
- :
- : Another idea would be to keep a list of valid pointers for each known
- : object and look up the adress. I think it's the only way.
- :
- : Any idea ?
-
- Since the pointer that Perl is passing around is presumably opaque, why
- not pass a type around with that, and check it when you go to glue your
- thread of control back into C++?
-
- I find this very interesting, because I've got C++ linked with Perl right
- now too. Also, if you have any bright ideas for how you'd like to see
- more object support in Perl 5, now's the time to speak up. (I certainly
- don't intend to turn Perl into C++, since C++ is already pretty good
- at that. But if I can do something to make the mapping a little less
- painful, I'm all ears.)
-
- Larry Wall
- lwall@netlabs.com
-