Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Casting function pointers to and from void*

Reply
Thread Tools

Casting function pointers to and from void*

 
 
Peter Olcott
Guest
Posts: n/a
 
      01-05-2007
I want to make a generic interface between a scripting language and native code,
the native code and the interpreter will both be written in C++. The interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.

Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was thinking about casting them to void* for this
purpose.

The interpreted function will be provided with the generic function pointer, and
the function prototype as text. The interpreter will prepare the stack itself
based on this function prototype. What I want to end up with is a simple
interface for users of the interpreter that is identical to the interface for
the native code users.

In other words if the function prototype is:
int FunctionName(int N);
I want uses of my interpreter to be able to specify:
int Result = FunctionName(56);
when they are calling the native code functions.

What kind of C++ syntax will be needed for this?


 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      01-05-2007

Peter Olcott wrote:
> I want to make a generic interface between a scripting language and native code,
> the native code and the interpreter will both be written in C++. The interpreter
> will probably be implemented as a subset of C/C++, thus will have the same
> syntax as C/C++.
>
> Somehow the interpreted code must be able to store generic function pointers
> because there is no way for the interpreter to know every possible function
> signature in advance. I was thinking about casting them to void* for this
> purpose.
>
> The interpreted function will be provided with the generic function pointer, and
> the function prototype as text. The interpreter will prepare the stack itself
> based on this function prototype. What I want to end up with is a simple
> interface for users of the interpreter that is identical to the interface for
> the native code users.
>
> In other words if the function prototype is:
> int FunctionName(int N);
> I want uses of my interpreter to be able to specify:
> int Result = FunctionName(56);
> when they are calling the native code functions.
>
> What kind of C++ syntax will be needed for this?


I don't understand your problem as stated but maybe boost::function
will aid you.

 
Reply With Quote
 
 
 
 
Philipp Reh
Guest
Posts: n/a
 
      01-05-2007
On Fri, 05 Jan 2007 10:21:46 -0600, Peter Olcott wrote:

> I want to make a generic interface between a scripting language and native code,
> the native code and the interpreter will both be written in C++. The interpreter
> will probably be implemented as a subset of C/C++, thus will have the same
> syntax as C/C++.
>
> Somehow the interpreted code must be able to store generic function pointers
> because there is no way for the interpreter to know every possible function
> signature in advance. I was thinking about casting them to void* for this
> purpose.
>
> The interpreted function will be provided with the generic function pointer, and
> the function prototype as text. The interpreter will prepare the stack itself
> based on this function prototype. What I want to end up with is a simple
> interface for users of the interpreter that is identical to the interface for
> the native code users.
>
> In other words if the function prototype is:
> int FunctionName(int N);
> I want uses of my interpreter to be able to specify:
> int Result = FunctionName(56);
> when they are calling the native code functions.
>
> What kind of C++ syntax will be needed for this?


You can't cast a pointer to a function to a pointer to an object.
reinterpret_cast can convert between two pointers to different functions
only.
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-05-2007

"Philipp Reh" <(E-Mail Removed)> wrote in message
news:459e9093$(E-Mail Removed)-siegen.de...
> On Fri, 05 Jan 2007 10:21:46 -0600, Peter Olcott wrote:
>
>> I want to make a generic interface between a scripting language and native
>> code,
>> the native code and the interpreter will both be written in C++. The
>> interpreter
>> will probably be implemented as a subset of C/C++, thus will have the same
>> syntax as C/C++.
>>
>> Somehow the interpreted code must be able to store generic function pointers
>> because there is no way for the interpreter to know every possible function
>> signature in advance. I was thinking about casting them to void* for this
>> purpose.
>>
>> The interpreted function will be provided with the generic function pointer,
>> and
>> the function prototype as text. The interpreter will prepare the stack itself
>> based on this function prototype. What I want to end up with is a simple
>> interface for users of the interpreter that is identical to the interface for
>> the native code users.
>>
>> In other words if the function prototype is:
>> int FunctionName(int N);
>> I want uses of my interpreter to be able to specify:
>> int Result = FunctionName(56);
>> when they are calling the native code functions.
>>
>> What kind of C++ syntax will be needed for this?

>
> You can't cast a pointer to a function to a pointer to an object.
> reinterpret_cast can convert between two pointers to different functions
> only.


Is there any standard C++ way to accomplish what I need to accomplish?


 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-05-2007

"Philipp Reh" <(E-Mail Removed)> wrote in message
news:459e9093$(E-Mail Removed)-siegen.de...
> On Fri, 05 Jan 2007 10:21:46 -0600, Peter Olcott wrote:
>
>> I want to make a generic interface between a scripting language and native
>> code,
>> the native code and the interpreter will both be written in C++. The
>> interpreter
>> will probably be implemented as a subset of C/C++, thus will have the same
>> syntax as C/C++.
>>
>> Somehow the interpreted code must be able to store generic function pointers
>> because there is no way for the interpreter to know every possible function
>> signature in advance. I was thinking about casting them to void* for this
>> purpose.
>>
>> The interpreted function will be provided with the generic function pointer,
>> and
>> the function prototype as text. The interpreter will prepare the stack itself
>> based on this function prototype. What I want to end up with is a simple
>> interface for users of the interpreter that is identical to the interface for
>> the native code users.
>>
>> In other words if the function prototype is:
>> int FunctionName(int N);
>> I want uses of my interpreter to be able to specify:
>> int Result = FunctionName(56);
>> when they are calling the native code functions.
>>
>> What kind of C++ syntax will be needed for this?

>
> You can't cast a pointer to a function to a pointer to an object.
> reinterpret_cast can convert between two pointers to different functions
> only.


void Test() {
printf("Hello World!\n");
}

void (*pf)() = Test;

void* vp = (void*) pf;

This all compiles and executes correctly on my compiler.

// All I need now is something like:
goto *vp;
// sets the instruction pointer to the address contain in vp.

Is there anyway that anything like this can be done in standard C++ ???


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      01-11-2007
Peter Olcott <(E-Mail Removed)> wrote:
> void Test() {
> printf("Hello World!\n");
> }
>
> void (*pf)() = Test;
>
> void* vp = (void*) pf;
>
> This all compiles and executes correctly on my compiler.


Even though it *seems* to work, it is not correct.

http://www.parashift.com/c++-faq-lit....html#faq-33.8

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-11-2007

"Marcus Kwok" <(E-Mail Removed)> wrote in message
news:eo61fo$4k4$(E-Mail Removed)...
> Peter Olcott <(E-Mail Removed)> wrote:
>> void Test() {
>> printf("Hello World!\n");
>> }
>>
>> void (*pf)() = Test;
>>
>> void* vp = (void*) pf;
>>
>> This all compiles and executes correctly on my compiler.

>
> Even though it *seems* to work, it is not correct.


It indeed does work, I checked the generated assembly language. It only has to
work on a single platform/compiler. When getting the job done requires breaking
the rules, the rules must be broken.

>
> http://www.parashift.com/c++-faq-lit....html#faq-33.8
>
> --
> Marcus Kwok
> Replace 'invalid' with 'net' to reply



 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      01-11-2007
Peter Olcott wrote:

> When getting the job done requires breaking the rules, the rules must be
> broken.


Please don't blame the job when it's you who decide to break the rules.

--
Salu2
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      01-11-2007
Peter Olcott <(E-Mail Removed)> wrote:
>
> "Marcus Kwok" <(E-Mail Removed)> wrote in message
> news:eo61fo$4k4$(E-Mail Removed)...
>> Peter Olcott <(E-Mail Removed)> wrote:
>>> void Test() {
>>> printf("Hello World!\n");
>>> }
>>>
>>> void (*pf)() = Test;
>>>
>>> void* vp = (void*) pf;
>>>
>>> This all compiles and executes correctly on my compiler.

>>
>> Even though it *seems* to work, it is not correct.

>
> It indeed does work, I checked the generated assembly language. It only has to
> work on a single platform/compiler. When getting the job done requires breaking
> the rules, the rules must be broken.


Sure, but then it is no longer Standard C++. Sorry, I don't have an
answer to your real question. Maybe someone on a group for your
platform or compiler will have a suggestion that is known to work for
your situation.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-11-2007

"Julián Albo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Olcott wrote:
>
>> When getting the job done requires breaking the rules, the rules must be
>> broken.

>
> Please don't blame the job when it's you who decide to break the rules.
>
> --
> Salu2


I don't have to get my system working in theory, I have to get it working in
practice. If it does not work in theory yet does work in practice, then the
theory is discarded.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
More questions about casting function pointers .. Alfonso Morra C Programming 3 08-31-2005 09:49 AM
Baffled by this ... (casting of function pointers) Alfonso Morra C Programming 7 08-26-2005 02:35 PM
casting function pointers ? Alfonso Morra C Programming 4 07-30-2005 12:46 AM
Casting Function pointers : Can anyone correct this MS Knowledge base Example pls ? Dirk Vanhaute C++ 10 10-22-2004 08:25 AM



Advertisments