5

I've got an implemented function MyFunc(int, double, string) in my project. How can I call this function with necessary parameters having its string representation, for example

std::string str = "MyFunc(2, 3.12, \"Lemon Juice\")";

And what about the standard functions, for example, how can I call time() having std::string "time()"?


Ok, here's more detailed task. I've got a map <string, Data> Data is a class-wrapper with many childs. When I call Data.GetValue() method, it returns a std::string, depending of child class inner data. One of Data childs must return the string representation of int, another - the string representation of double, and the last one - the string representation of current date and time. Here's the problem - I don't know how to call the standard function ctime() for getting information from one of Data childs.

Sergey Shafiev
  • 4,205
  • 4
  • 26
  • 37
  • Not easily. Do you want to be able to call *any* function, or just a few specific ones? – BoBTFish Jun 18 '12 at 10:36
  • 1
    Thats the the main application for scripting language. Many scripting languages integrate quite fine with C++ aswell, take a look at LUA for example. – smerlin Jun 18 '12 at 10:38
  • 1
    what do you need this for? As others have remarked, this is very, very difficult. There are probably easier alternatives for what you need. – Konrad Rudolph Jun 18 '12 at 10:38
  • Steve, you are right, I've corrected the description. Of course, Data is not an abstract class. – Sergey Shafiev Jun 18 '12 at 11:28
  • This reminds me of a blog I wanted to read: http://runtimecompiledcplusplus.blogspot.fr/ – Nils Jun 18 '12 at 11:01
  • I have written **[a sample using Spirit to parse a simple 'script language'](http://stackoverflow.com/a/7897417/85371)** before. Perhaps it can give you a simple idea to start things. Otherwise, I can only recommend embedding Lua, Scheme, Python etc. to provide the scripting capability. – sehe Jun 18 '12 at 11:58

3 Answers3

3

You cannot in general execute code in C++ whose source is contained in a string. Probably the most compelling motivation for this is so that a C++ implementation is not required to have a C++ compiler on the machine that the code runs on, although there is more to it than just that.

You may be able to do it for specific cases. That could be:

Platform-specific, for example:

  • Wrap the source up in some boilerplate, then either call the compiler externally, link against it, or embed it in your program. This gives you a program or library.
  • Run that new program externally, or dynamically link against that library.

Input-specific, for example:

  • You could parse a function call to a particular known function, with literals as arguments, relatively easily without needing a complete C++ compiler. Get the argument values out into variables and call the function. But if you're going to do that, you could specify a more easily parsable format for the string, than one that looks like a C++ function call.

It sounds as though in your task you have a string that is one of:

  • a representation of an int
  • a representation of a double
  • a representation of a date and time.

You don't say what you want to do with this string, but whatever that is you probably need to (a) examine the string to find out which of the three it is, and then (b) do something appropriate to that format. Better, you could give the derived class the responsibility of returning the same representation no matter which of the three GetValue() returns. For example, if what you really want is seconds since 1970, add a GetSecondsSinceEpoc function, and implement it differently in each class.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
1

As mentioned by others, C++ in itself is not able to do that. However external frameworks can help you.

ROOT (used at CERN) provides reflection for C++ along with an interpreter. You will be able to execute/interpret a method call or a macro written in C++ from within your code.

Barth
  • 15,135
  • 20
  • 70
  • 105
  • The ROOT website is down due to security issues found by Computer Security Team, it should be back soon... – Barth Jun 18 '12 at 11:42
0

You can not do that using C++.

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • 2
    While more or less (there *are* C++ scripting libraries) accurate, this isn’t really helpful … – Konrad Rudolph Jun 18 '12 at 10:36
  • 4
    Of course he can do that, he just needs to write a parser that translates those strings into calls. We have a library at my company that does this for hundreds, maybe thousands of library functions. – PlasmaHH Jun 18 '12 at 10:39
  • Any open source ones I can use in my home? or local projects? – elcuco Jun 18 '12 at 10:51