dynamic typedef

S

Simon Moser

Hi,

I got a somewhat stupid question: Is it possible to do a dynamic
typedef and then use it afterwards ?

void createTypedef(std::string name){
typedef name;
}

The idea is to generate function pointers on the fly, i.e. during
runtime, so that I can call functions in plugins that I'm not aware of
at compile time, but they get delivered in a XML file accompagnying
the dll/so ?!
Any ideas are warmly appreciated!
Cheers,
Simon
 
J

John Tsiombikas (Nuclear / the Lab)

Hi,

I got a somewhat stupid question: Is it possible to do a dynamic
typedef and then use it afterwards ?

void createTypedef(std::string name){
typedef name;
}
no


The idea is to generate function pointers on the fly, i.e. during
runtime, so that I can call functions in plugins that I'm not aware of
at compile time, but they get delivered in a XML file accompagnying
the dll/so ?!
Any ideas are warmly appreciated!

Your operating system provides calls that give you pointers to the
functions in shared libraries by taking a string with its name, you
should check the documentation of your system for them (hint:
dlopen()/dlsym() on unix)
 
S

Simon Moser

Your operating system provides calls that give you pointers to the
functions in shared libraries by taking a string with its name, you
should check the documentation of your system for them (hint:
dlopen()/dlsym() on unix)

Ok, I know that I can get the entryPoint in my lib by using
dlopen/dlsym or LoadLibrary/getProcAdress (on Windows).
Problem is: After I got the entryPoint, I also need to call this
function (and probably pass parameters). So how could I do this if I
only know the function name and the input + output parameters at
runtime ?
 
S

Simon Moser

Ok, I know that I can get the entryPoint in my lib by using
dlopen/dlsym or LoadLibrary/getProcAdress (on Windows).
Problem is: After I got the entryPoint, I also need to call this
function (and probably pass parameters). So how could I do this if I
only know the function name and the input + output parameters at
runtime ?

That means that I need to construct the signature of the function to
be called on-the-fly, So my idea is to construct a string that has the
signature, and then convert it to a function pointer that I can call
(maybe using something like dynamic_cast??) But I can't seem to figure
out how. Thanks in advance for any help
 
J

John Tsiombikas (Nuclear / the Lab)

Ok, I know that I can get the entryPoint in my lib by using
dlopen/dlsym or LoadLibrary/getProcAdress (on Windows).
Problem is: After I got the entryPoint, I also need to call this
function (and probably pass parameters). So how could I do this if I
only know the function name and the input + output parameters at
runtime ?

That means that I need to construct the signature of the function to
be called on-the-fly, So my idea is to construct a string that has the
signature, and then convert it to a function pointer that I can call
(maybe using something like dynamic_cast??) But I can't seem to figure
out how. Thanks in advance for any help

Well if I understand what you're saying correctly, you can create a
symbol table at runtime that associates function signatures
(as strings), with function pointers. So for example you could have a
function

void call_func(const char *func_name);

that would search the table, find the apropriate function pointer and
call the function through that.
 
S

Simon Moser

Well if I understand what you're saying correctly, you can create a
symbol table at runtime that associates function signatures
(as strings), with function pointers. So for example you could have a
function

void call_func(const char *func_name);

that would search the table, find the apropriate function pointer and
call the function through that.

Well, the thing is I do not have a function pointer at compile time. I
need to generate this function pointer at runtime. I agree that in
case I would have a functionPointer at runtime, I could do what you
say. Let me give you an example.

My "User" species a plugin the following way (in his XML - see very
first note)

<plugin name=myPlugin/>
<function>
<name>myFunc</name>
<returnType>void</returnType>
<signature> () </signature>
</function>

so what I know at runtime is: I have a Library (so/dll) that is called
myPlugin, and there is a function in this plugin that looks like this:
void myFunc()

So, at runtime, here's what I do (giving win-specific code as an
example - you could replace LoadLibraray by dlopen and getProcAddress
by dlsym on UNIX systems ):

HINSTANCE libInstance = LoadLibrary("myPlugin");
if (HINSTANCE){

typedef void (*MYPROC)(); //[1]
MYPROC ProcAddr;

ProcAddr = (MYPROC) getProcAdress (libInstance, "myFunc");
if (NULL != ProcAddr){

(ProcAddr)();
}
}

This code is working! But the thing is: I made the typedef in [1] -
the functionPointer declaration - for this signature at compile time -
and this is *not* what I want. I want to do this at runtime -
constructing it from whatever I get in the XML description of the DLL.
I can't see how to do this besides creating a huge table containing
all possilble signatures that a user might ever come up with - and
than picking the correct sfunctionPointer for this signature out of
this table :)))
Since this isn't really feasible, maybe someone comes up with a better
idea how to create that functionPointer at runtime ?
 
M

Malcolm

Simon Moser said:
Since this isn't really feasible, maybe someone comes up with a
better idea how to create that functionPointer at runtime ?
Types are compile time constructs in C. One thing that it is impossible to
do in C is

/*
sig - %d = integer, %c = char, etc
*/
void callanything(const char *sig, .../*args */)
{
/* call a function with the arguments supplied */
}
 
S

Simon Moser

Not in C.

Well, although it would become off-topic in this NG - C++ would also
do it if it solves the Problem ;-) For a more detailled decription see
the other thread - also using 'speaking' examples (see below), so
don't take them too literally, please.
Is std::string a C type? I don't think so.

Me neither, but I gave this a a "speaking" example. Replace it by
char* if you like.
 
S

Simon Moser

Types are compile time constructs in C. One thing that it is impossible to
do in C is

/*
sig - %d = integer, %c = char, etc
*/
void callanything(const char *sig, .../*args */)
{
/* call a function with the arguments supplied */
}

That's what I feared, but maybe there's another way to construct a
functionPointer out of a string besides type. I'd like to talk about a
function that is

constructFunctionPointerWithGivenSignature(char* input)

Does something like this exist? I can't find anything!
 
J

Jeremy Yallop

Simon said:
Well, although it would become off-topic in this NG - C++ would also
do it if it solves the Problem ;-)

The signature of any function called in a C or C++ program, including
argument types, numbers of arguments and return types must be known at
compile-time. This is (roughly) what is meant by "static typing": the
type of every variable (and hence expression, and hence function-call)
is computable at compile-time. If you're not constrained to using C
or C++, note that this restriction is not true in a number of other
languages.

Jeremy.
 
J

Jeremy Yallop

Jeremy said:
The signature of any function called in a C or C++ program, including
argument types, numbers of arguments and return types must be known at
compile-time.

(With the exception of the "this" parameter in virtual function calls
in C++, where the most-derived type of the referenced type is not
known, but this won't help you here. Apologies for the
off-topicness).

Jeremy.
 
S

Severian

That's what I feared, but maybe there's another way to construct a
functionPointer out of a string besides type. I'd like to talk about a
function that is

constructFunctionPointerWithGivenSignature(char* input)

Does something like this exist? I can't find anything!

My suggestion: Call the plug-in with a fixed parameter list, perhaps a
pointer to a "context" structure, whose members the plug-in can access
at will.

For a good guide on plug-in design, look at the Photoshop SDK.
 
M

Malcolm

Simon Moser said:
constructFunctionPointerWithGivenSignature(char* input)

Does something like this exist? I can't find anything!
To do this in assembly is not trivial, but isn't impossible either. Most
compilers have a calling convention which is documented, and , if you think
about it, a compiler has to do at compile time exactly what you wnat to do a
runtime when a function is called.
 
R

Rob Thorpe

Simon Moser said:
Hi,

I got a somewhat stupid question: Is it possible to do a dynamic
typedef and then use it afterwards ?

void createTypedef(std::string name){
typedef name;
}

The idea is to generate function pointers on the fly, i.e. during
runtime, so that I can call functions in plugins that I'm not aware of
at compile time, but they get delivered in a XML file accompagnying
the dll/so ?!
Any ideas are warmly appreciated!
Cheers,
Simon

In addition to what others have said:

A simple solution would be to make every function present in a plugin
library of the form:

ltype foo(ltype *o)

ltype is then some type of list, like a lisp list. Make each element
of the list holds information about it's type, so you can pass
anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top