Dynamic source code in C ++

How to handle dynamic source code in C ++? Is it possible to use something like eval ("foo")?

I have some functions that need to be called depending on the user's choice:

void function1 (); void function2 (); ... void functionN (); int main (int argv, char * argv []) { char * myString = new char [100]; ... myString = "1" //user input cout << eval("function"+myString)(); } 

How is this usually done?

UPD Based on the answers of slacy and clinisbut, I think I need to create a function registry. I suppose this should be done as an array of function pointers. Here's the question, how to declare an array of function pointers?

+4
source share
14 answers

C ++ is a compiled language, and thus there is no equivalent to "eval ()".

In the specific example you mentioned, you can create a function registry that maps inputs (strings) to outputs (function pointers) and then calls the resulting function.

There are several C ++ interpreter libraries, although the performance will be very poor, they can accomplish what you want. Google search for "C ++ interpreter". I saw results for " Ch ", " CINT " and "clipp"

+16
source

The real answer to your question is this:

 extern "C" { void function1 (); void function2 (); void function3 (); } int main (int argv, char * argv []) { char * myString = new char [100]; ... myString = "function1"; //user input void (*f)() = dlsym(RTLD_NEXT, myString); f(); } 

You can get the function defined in your binary (by its name if it was declared using extern "C") and call it.

In windows, it gets uglier but still possible - read on GetProcAddress

+17
source

You can embed the C / C ++ interpreter in your program if you really need it. However, you can also implement a more script-like language .

+7
source

To answer your question about how this is usually done, the answer is: it is not.

The need to use something like eval() usually a design issue. If you really need it, insert a scripting language such as Lua or Python.

+4
source

If you put function1 .. functionN in a DLL, you can just pull them out and call them by name using dlopen / dlsym, which will probably give you 90% of how you want to go.

+4
source

What about function pointers?

+4
source

C ++ language itself cannot do this directly. But you can use special functions to call any function in a given DLL, according to user input (or any other). For example, on Windows, find GetProcAddress () and LoadLibrary ()

+2
source

This is a good example for implementing an interpreted language such as LUA. If you create LUA bindings to your C ++ API, you can dynamically execute your code using LUA code from your application.

+2
source

There are two ways to do this:

  • Dynamic loading

    • Windows: GetModuleFileName() with hModule = NULL this gives the path to the executable. Use LoadLibrary(Executable_Name) to load itself into memory.
    • Linux: use dloopen() with NULL and it loads into memory.

    On Windows and Linux, you can define a void* (function*)() pointer. This always works with all signatures without parameters. This does not work on class methods.

  • Using the namespace system

    This namespace would be easy to use with the Inspect and InvokeMember to dynamically call class methods and access a variable inside the class.

+2
source

You cannot do this in C ++. This is a compiled language.

A lot of the information needed to implement eval () will still be lost at runtime.

+1
source

I would build an array or list of STL pointers to functions and strings that you find to invoke them. If you really need to call an arbitrary function, you should investigate RTTI http://en.wikipedia.org/wiki/Run-time_type_information

+1
source

how to declare an array of function pointers?

C programmer's answer to this question:

use cdecl.

cdecl> declares a functional as a pointer to function (int, pointer to int) return double

double (* functable) (int, int *)

cdecl> declares a functional as an array of 17 pointers to a function (int) that returns double

double (* functable [17]) (int)

Note that functions are automatically converted to function pointers, in the same sense that arrays decay into pointers. You do not need a and take the address of the function to put it in the pointer.

If you're in C ++, this may help use std :: map <YourFunctionPointerType> instead of an array, since you still want to display strings ...

OO approach:

Use polymorphism instead. It does not immediately solve your problems, but there is a good chance that if you do this, it makes sense to have a class for each function.

Note that vtable is essentially a table of function pointers.

+1
source

You want reflection . It is not supported by default in C ++, but many people have written complex reflection systems for all kinds of purposes. The one that comes to mind from my head associates C ++ functions with a custom scripting language.

Others mentioned the use of a switch statement (or more precisely, an if tree). It is about as simple as a reflection system is likely to come ... and it is probably the best solution for your project. Reflection in C ++ is a fairly advanced method and often requires a lot of additional code ... sometimes even custom preprocessors.

Also note that for security purposes, eval () will almost never be used for use in any language.

0
source

The best way to embed and eval code in C ++ is for an application to probably use a scripting language like python. Use Boost.Python , "C ++ - a library that provides seamless interoperability between the C ++ and Python programming languages."

0
source

All Articles