4

I'm thinking about how to design my APIs, I plan to have a C++ application with a scripting layer in LUA .

For a couple of key points in my design I would like to give the ability to the user to create a function object that represents what he wants to do in LUA, than send this function object from LUA to C/C++ .

In pseudo code, in C++ I have a class T

class T {
...
int num1 = 0;
float num2 = 0.0f;
std::string str{"NONE"};
...
};

And I would like to manipulate an instance of T with a function object provided from LUA like so

void applyFunc(T t,F f){
f(t);
}

The problem is that I can't find nothing in LUA that creates a function object like a C++11 lambda or std::function or any other object that can be considered a function object.

Really my point is: how to define a C++ compatible function object in LUA ?

14
  • You may find the LuaBridge library helpful in this situation. Commented Feb 21, 2014 at 21:27
  • @TimCooper could you provide a working example for this ? Because I got the impression that any popular LUA<->C++ solution really works from C++ to LUA and not viceversa. Commented Feb 21, 2014 at 22:17
  • @user2485710: Getting C++ to call a Lua function is beyond trivial. The hard part is when you want that Lua code to be able to manipulate a C++ object, as in your example. That's the problem the libraries help with. Commented Feb 21, 2014 at 22:30
  • SWIG (www.swig.org) is a code generator that makes it really easy to wrap your C++ classes so they can be used from Lua. You then instantiate a Lua interpreter in your C++ app, execute code in the interpreter to load your Lua module (created via SWIG), and you can then run Lua scripts that use your C++ classes. I have a library lua-icxx which does the basics of this (work in progress so YMMV -- the part to integrate with SWIG is still only in SVN no release yet). Commented Feb 22, 2014 at 3:01
  • @Schollii I know SWIG and it's not what I want. stackoverflow.com/questions/21944455/… Commented Feb 22, 2014 at 10:47

1 Answer 1

1

The problem is that I can't find nothing in LUA that creates a function object like a C++11 lambda or std::function or any other object that can be considered a function object.

That's what the function keyword does. This is a lambda. It's easy enough to pass these to C++ and let the C++ code call them.

As for this:

void applyFunc(T t,F f){  
   f(t);  
}  

In principle it's easy: push the C++ object pointer onto the Lua stack as a userdata and call the Lua function. The problem is the Lua code can't do anything with a C++ pointer.

If you want the Lua code to be able to manipulate the object you pass it, you'll need to write manipulator methods in C++ and expose them to Lua. You usually do that by creating a metatable for the userdata.

There are libraries that this that automatically for C++. If you want to do it by hand (my preference), you should probably start here.

If you Google "C++ object Lua __index" that should net you numerous examples. I could code up an example later, but I'm at work at the moment.

Sign up to request clarification or add additional context in comments.

4 Comments

and how this fits in as an equivalent of a C++ function object and how to do what I asked for with this function keyword ? As far as I know that looks like more like a plain old C function.
@user2485710 Lua functions are simply one type of value. You can test a value to see if its type is "function". If so, you can call it with whatever parameters you want. Local and global variables, table members (values and keys), parameters and return values can all have functions as values. You want the user to create a function that takes one parameter t and allow them to invoke documented methods on it. Lua allows that, using "late binding", if you will. You just have to write the glue code on the C side of the userdata parameter t.
@TomBlodget the best I can do with that is using a C function or a function pointer, I need a function object, something like a C++11 lambda, please read the question. Your posts and comments are interesting but they are all OT.
Lua functions are lambdas, and they are closures, capable of carrying state. None of that is relevant to your question. You're not thinking through what f(t) actually means. Yes, you can pass t (a C++ object) to f (a Lua function), but you have to provide a way for f to manipulate t from Lua or it's completely worthless. It seems like your eyes glaze over when when you see what looks like binding a C++ object to Lua, but that's exactly what you need. Without it, you can't implement applyFunc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.