Register non-static C++ methods in Lua - c++

I'm trying to make a small C++/Lua system where I would create my objects and attach behaviors to them in Lua. Right now I'm using LuaWrapper (a small header with basic C++ to Lua stuff), my problem is that as far as I can see Lua only let me register static class methods (or non-static functions), a little research and I figured its because the Lua typedef expects a method with only one parameter lua_State* L and non-static methods have the implicit this.
I was hoping for a way to solve this without dependency on other libraries, all I need is non-static classes/properties in Lua, so I see no reason to use LuaBind+Boost or other heavy-dependant wrappers.

LuaWrapper isn't meant to hook up directly to non-static functions in a class. I suppose it could be with some special trickery, but this is how I designed it to be used:
static int Widget_AddChild(lua_State* L)
{
Widget* parent = luaW_check<Widget>(L, 1);
Widget* child = luaW_check<Widget>(L, 2);
if (parent && child)
{
lua_pushboolean(L, parent->AddChild(child));
return 1;
}
return 0;
}
// ...
static luaL_reg Widget_metatable[] =
{
{ "AddChild", Widget_Addchild },
// ...
{ NULL, NULL }
};
I usually keep the non-lua stuff in a separate file. In this case Widget.cpp/hpp. Then I have a LuaWidget file which just contains bindings like these which I write as needed. (I also have a number of snipmate snippets to make writing these functions quick and painless. If you're using vim I'd be happy to share them)

You can make a static function that will accept an instance of the class and an argument and call that function on the instance:
void func_static(MyClass* inst, T arg) {
inst->func(arg);
}
Then register a function to call that function as a metafunction so you can do in lua
blah:x(y)
which will call the function that will receive the userdata that blah contains, as well as the argument y, and call func_static with blah and y.

You may want to look into using toLua++ (http://www.codenix.com/~tolua/).
It can parse class definitions and output a c++ code file to make the non-static class members available in Lua.

You could also take a look at LuaCppWrapper. It's intended for simple bindings only. If you want a full fledged solution, maybe OOLua or Simple Lua Binder are what you need.

Related

Pointer to function from another pointer

I'm new to c++ and I'm trying to make a generic switch (i.e. the device, not the C++ statement) that could be used to blink lights, turn beeps on and off, etc, in my Arduino project.
I could create a switchable interface and implement that in the classes that I want to "switch". But since I'm doing it as study purposes and I saw the pointer-to-functions ability in C++ (that is new to me since I come from C# and Java), I tough it would be a good opportunity to give it a try...
The problem is that I can pass the function in my code only if it's a local function but it won't work if I try to pass a function from another object like a led for example.
Some code to illustrate the problem. This is the switch.cpp, it recieves the On and Off functions in it's constructor and it has a update method that is called inside the loop method in the Arduino ino main class:
auto_switch.cpp
using switch_function = void(*)();
auto_switch::auto_switch(const switch_function on_function, const switch_function off_function, const int max_speed_count)
{
//sets all variables...
}
void auto_switch::update(const unsigned long millis)
{
//turn switch on and off...
}
And this is my ino file
ino file
#include <Arduino.h>
#include "led.h"
#include "auto_switch.h"
led* main_led;
auto_switch* led_switch;
int slow_speed;
//ugly code
void turn_led_on()
{
main_led->turn_on();
}
//ugly code
void turn_led_off()
{
main_led->turn_off();
}
void setup() {
main_led = new led(2, 3, 4, true, color::white);
//ugly code
led_switch = new auto_switch(turn_led_on, turn_led_off, 3);
slow_speed = led_switch->add_speed(100, 100, 3, 1000);
led_switch->set_active_speed(slow_speed);
led_switch->turn_on();
}
void loop() {
led_switch->update(millis());
}
It works but I had to make a local function (turn_led_on and turn_led_off) to be able to assign the inner functions as a parameter to the auto_switch constructor, the parts that I've wrote //ugly code
I wanted to do something like this, without the glue code in between:
//doesn't work
led_switch = new auto_switch(main_led->turn_on, main_led->turn_off, 3);
Is it possible? I've read something about static pointer to function and some std functions that help with that, if I get it right the glue code is necessary in this case so that the compiler can know where the functions are coming from I guess (from which object), but since the functions I need to call cannot be static I've discarded this option, and the std functions I believe it can't be used with the Arduino or could but shouldn't for performance limitations...
Anyway, does it make sense, can it be done using pointer to functions or should I create a interface or something different?
Before deciding how to do it, the qquestion is what do you want to do and why. Because, maybe there are better alternatives using simple C++ idioms.
Option 1: specialization with polymorphism
Do you want to specialize some functions of your switch, so instead of calling the function of the auto_switch you'd call dome more specialized ones ?
In this case you wouldn't do:
//doesn't work
led_switch = new auto_switch(main_led->turn_on, main_led->turn_off, 3);
but instead you would rely on polymorphism with virtual functions in the base class:
class auto_switch {
...
virtual void turn_on();
virtual void turn_off();
...
};
and write a specialized class for the leds:
class led_witch : public auto_switch {
...
void turn_on() override;
void turn_off() override;
...
};
In fact, the compiler will generate some function pointers behind the scene, but you don't have to care:
auto_switch s1=new auto_switch(...);
auto_switch s2=new led_switch(...); // no problem !!
s1->turn_on(); // calls auto_switch::turn_on()
s2->turn_on(); // calls led_switch::turn_on() since the real type of s2 is led_switch
But event if each object's behavior is dynamic on the the base of the real class of the object, the objects of the same class share a behavior that was predefined at compile time. If this is not ok, go to the next option.
Option 2: the member function pointer
The functions of another objects can only be invoked with that object at hand. So having a function pointer to a led function is not sufficient: you also need to know on which led it shall be applied.
This is why member function pointers are different and somewhat constraint: you can only invoke functions of class of your member function pointer. If polymorphism is sufficient (i.e. if derived class has a different implementation of a function already foreseen in the base classe) then you are lucky. If you want to use a function that only exists in the derived class and not in the base class, it won't compile.
Here a simplified version of auto_swith: I provide a function, but allso a pointer to the object on which the function has to be invoked:
class auto_switch{
void (led::*action)();
led *ld;
public:
auto_switch(void(led::*a)(), led*l) : action(a), ld(l) {}
void go () { (ld->*action)(); }
};
// usage:
auto_switch s(&led::turn_off, &l1);
s.go();
Online demo
Option 3 : the functional way (may that's what you're looking for ?)
Another variant would be to use the standard functional library to bind a member function and the object on which it shall be executed (as well as any need parameters):
class auto_switch{
std::function<void()> action;
public:
auto_switch(function<void()>a) : action(a) {}
void go () { action(); }
};
Here you can bind anything: any function of any class:
auto_switch s(bind(&led::turn_off, l1));
s.go();
auto_switch s2(bind(&blinking_led::blink, l2));
s2.go();
Online demo
Option 4 : command pattern
Now if you want to perform something on an object when you turn on and off the switch, but you need total flexibility, you can just implement the command pattern : this lets you execute anything on any object. And you don't even need a function pointer.

C++ using callbacks of an arbitrary class with arbitrary parameters

I want to implement a class with a function able to callback to methods from the object that called that function, without information about it.
Imagine we want to callback to some methods of the class Game from Library:
void Game::display(string a) {
cout << a << endl;
}
int Game::sum(int a, int b) {
return a + b;
}
void Game::start() {
lib = new Library();
lib->doSomething(/* somehow pass information about the functions*/);
}
And then:
void Library::doSomething(list_of_callbacks L /*or something like this*/) {
L[0]("hi"); //L[0] is Game::display
L[1](2,3); //L[1] is Game::sum
}
It needs to work not just with Game, but with any class. The methods we want to call back may need access to the object's attributes (so not necessarily static).
I'm kinda new to C++; I've been searching on the topic for hours now but in most cases the return type or the parameters of all the callback methods are the same. I've tried to get something working with templates and std::function / std::bind but unsuccessfully.
Thanks, and sorry if I didn't make myself clear enough, it's my first post around here.
EDIT:
The doSomething function is going to be generated (translated) by an external tool, which has information about the list of functions (like, the function in L[0] takes these parameters or these others, etc).
If what I'm asking can't be done in C++, isn't there any other way to achieve the same goal? Being able to get information from the caller?

Callback function pointers C++ with/without classes

I got stuck. I am trying to form a function that will eat classless function pointers and ones from objects. Here is my current code that hopefully explains more.
(It should run on a Arduino, so I cannot use big libraries.)
First off, I am using this library for the Arduino:
/* SimpleTimer - A timer library for Arduino.
* Author: mromani#ottotecnica.com
* Copyright (c) 2010 OTTOTECNICA Italy
*/
Which takes functions which it calls on a set timer interval of this type:
typedef void (*timer_callback)(void);
As far as my knowledge goes, it's a classles function, the webpage Pointers to member functions got me really far but, not far enough. Probably a terminology deficit on my side.
Now, I have made my own class which I would like in turn to use this SimpleTimer library. But if I feed the SimpleTimer my class functions, it does not like them (what I understand). But how would it be possible to make this happen without altering the SimpleTimer library.
So there is the class Robot, which has Robot::halt(). I want the robot to move forward for a set amount of time. Like so:
void Robot::forward(int speed, long time) {
reset();
timer.setTimer(time, c_func, 1);
analogWrite(l_a, speed);
analogWrite(r_a, speed);
isMoving(true);
}
void Robot::halt() {
__isMoving = false;
digitalWrite(r_a, LOW);
digitalWrite(r_b, LOW);
digitalWrite(l_b, LOW);
digitalWrite(l_a, LOW);
}
The c_func variable is a classless function at this point, but I would like to use the Robot::halt function. I have looked, read, learned but haven't succeeded yet. I just can't seem to wrap my head around this one because I am missing some angle.
I tried:
timer.setTimer(time, (this->*halt), 1);
timer.setTimer(time, Robot::*halt, 1);
timer.setTimer(time, &Robot::halt), 1);
But it would all amount to the same problem/ me just stabbing in the dark here...
EDIT
Earlier, I said not wanting to change the SimpleTimer library code. I want to comeback on this one, I guess altering it there would be the better option.
Thanks for all the current answers already, I was only allowed to flag one as a viable answer, actually everyhting I read here was extremely helpful.
To continue this, changing the SimpleTimer code. This class needs to have a reference to the object that holds my "halt" function, right? So, overloading the settimer function to something that takes my object and my function as two seperate pointers would work...? I think I am getting the hang of this but, I am not there yet with my head.
EDIT
I don't know who came with this one again but, anyone finding this thread. If found Member Function Pointers and the Fastest Possible C++ Delegates to give a very nice introduction in function pointers and member function pointers.
EDIT
Got it working, changed the SimpleTimer library to use this Delegate system:
http://www.codeproject.com/KB/cpp/FastDelegate.aspx
It integrated very nicely, and it could be nice to have a standard Delegate system like this in the Arduino library.
Code as in test (working)
typedef
typedef FastDelegate0<> FuncDelegate;
Code in robot class:
void Robot::test(){
FuncDelegate f_delegate;
f_delegate = MakeDelegate(this, &Robot::halt);
timer.setTimerDelg(1, f_delegate, 1);
}
void Robot::halt() {
Serial.println("TEST");
}
Code in SimpleTimer class:
int SimpleTimer::setTimerDelg(long d, FuncDelegate f, int n){
f();
}
Arduino prints TEST in the console.
Next step putting it in an array, don't see a lot of problems there. Thanks everyone, I can't believe the stuff I learned in two days.
What's that smell? Is that the smell of...? Success!
For the ones interested, the used Delegate system does not amount to memory capacity issues:
With FastDelegate
AVR Memory Usage
----------------
Device: atmega2560
Program: 17178 bytes (6.6% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
Without FastDelegate:
AVR Memory Usage
----------------
Device: atmega2560
Program: 17030 bytes (6.5% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
You can do this by making a functor object, that acts as a proxy between the timer code and your code.
class MyHaltStruct
{
public:
MyHaltStruct(Robot &robot)
: m_robot(robot)
{ }
void operator()()
{ robot.halt(); }
private:
Robot &m_robot;
}
// ...
timer.setTimer(time, MyHaltStruct(*this), 1);
Edit
If it can't be done via a functor object, you could global variables and functions instead, maybe in a namespace:
namespace my_robot_halter
{
Robot *robot = 0;
void halt()
{
if (robot)
robot->halt();
}
}
// ...
my_robot_halter::robot = this;
timer.setTimer(time, my_robot_halter::halt, 1);
This only works if you have one robot instance though.
Since the timer callback signature doesn't take any argument, you unfortunately need to use some global (or static) state:
Robot *global_robot_for_timer;
void robot_halt_callback()
{
global_robot_for_timer->halt();
}
you can at least wrap that lot into it's own file, but it isn't pretty. As Matthew Murdoch suggested, it might be better to edit the SimpleTimer itself. A more conventional interface would be:
typedef void (*timer_callback)(void *);
SimpleTimer::setTimer(long time, timer_callback f, void *data);
void robot_halt_callback(void *data)
{
Robot *r = (Robot *)data;
r->halt();
}
ie, when you call setTimer, you provide an argument which is passed back to the callback.
The smallest change to SimpleTimer would be something like:
SimpleTimer.h
typedef void (*timer_function)(void *);
struct timer_callback {
timer_function func;
void *arg;
};
// ... every method taking a callback should look like this:
int SimpleTimer::setTimeout(long, timer_function, void *);
SimpleTimer.cpp
// ... callbacks is now an array of structures
callbacks[i] = {0};
// ... findFirstFreeSlot
if (callbacks[i].func == 0) {
// ... SimpleTimer::setTimer can take the timer_callback structure, but
// that means it's callers have to construct it ...
int SimpleTimer::setTimeout(long d, timer_function func, void *arg) {
timer_callback cb = {func, arg};
return setTimer(d, cb, RUN_ONCE);
}
You can't pass a non-static member function there - only a static one. The signature should be like this:
static void halt()
{
//implementation
}
the reason is that each non-static member function has an implicit Robot* parameter known as this pointer which facilitates access to the current object. Since the callback signature doesn't have such Robot* parameter you can't possibly pass a member function of class Robot unless it is static.
So that in your implementation
void halt();
is in effect
static void halt( Robot* thisPointer );
and when you do
void Robot::halt() {
__isMoving = false;
}
you effectively have this:
void Robot::halt( Robot* thisPointer ) {
thisPointer->__isMoving = false;
}
and of course a halt( Robot*) function pointer can't be passed in place of void (*)(void) C callback function.
And yes, if you need access to non-static member variables of class Robot from inside the callback you'll have to somehow retrieve the pointer to class Robot instance elsewhere - for example, store it as a static member variable so that you don't rely on this pointer.
It's important to understand that function pointers and pointers to class members are different not for an arbitrary reason but the fact that instance methods have an implicit this argument (also, they have to work with inherited and virtual functions, which adds even more complexity; hence they can be 16 or more bytes in size). In other words, a function pointer to a class member is only meaningful together with an instance of the class.
As the currently-top answer says, your best bet is to go with functors. While the setTimer function might only accept function pointers, it is possible to write a template function to wrap the call and accept both. For even more fine-grained processing, you can write a template metaprogram (Boost.TypeTraits has is_pointer, is_function and even is_member_function_pointer) to handle the different cases.
How you make the functors is a different story. You can opt for writing them by hand (which means implementing a class with operator() for each one of them), but depending on your needs that might be tedious. A couple of options:
std::bind: you can use it to create a functor whose first parameter will be bound to the value you specify - in the case of member functions, it will be the instance.
Depending on your compiler, you might not have access to std::bind - in this case I suggest boost::bind. It is a header-only library and provides the same functionality.
You can use another delegate implementation. I don't have experience with this one, but claims to be faster than other implementations (including std::function).
The mentioned libraries are header-only, so they probably don't count as "big libraries".

How do you clone() in linux inside a class and namespace?

I'm taking an intro to operating systems course and we're to use the clone() call in linux to create threads and then do some stuff with them. I seem to be having trouble just using clone() at all.
I've structured my code into a single class (called Homework) which is in the namespace for the class (Course). This may be the problem as this is the first time I've really used the namespace keyword. I'm trying to use the things I rarely do to become more experienced with it so if I have a dumb mistake, so be it.
I found some articles on the web but they didn't help much. I've read the man page but I guess I'm not experienced enough to understand what the problem is. One day! Thanks for any assistance :)
I want to have the method to catch the clones inside the class:
// -- Header -- //
namespace _Course_ {
class _Homework_ {
...
int threadCatch(void *);
...
};
}
// -- Source -- //
namespace _Course_ {
void _Homework_::threadTest(void) {
...
// From web article
void **childStack;
childStack = ( void **) malloc(KILOBYTE);
clone(threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);
...
}
int _Homework_::threadCatch(void * ){
cout << getpid() << " cloned." << endl;
exit(0);
}
}
Is what I currently have. I've tried different ways (taking the catcher out of the class, then namespace). It's compiled twice but when I try to recompiled after a make clean it tells me the function (threadCreate) is declared in multiple locations. Because of these weird errors I'm sure I'm doing something wrong and instead of hack at it I'll take some opinions. What should I do, or what should I read next? Thanks!
Define your catch function as a static class function.
static int threadCatch(void *);
Also (and you probably don't need this, but just in case, I'll say it here) you might also need to use the scope resolution operators to send it to clone(). I don't think so, since you're using it inside of the Homework class already. but I say it just in case, it might help you.
clone(Homework::threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);
The clone(2) system call expects a pointer to a function with C linkage. Since you're using C++ I'd recommend moving your threadCatch() function into the global namespace and declare it as an extern "C" function. You could also declare the method in your class as static but I feel that making it a free function with C linkage more closely matches how the function is to be passed as a parameter.
If you need to make calls to C++ objects inside your threadCatch() function that exist outside of it's scope you can pass pointers to those objects as the arg parameter to the clone() call. Your threadCatch() function would then cast the arg to the appropriate type so that you can access your C++ object(s) accordingly.

calling methods in cpp like #selector(someMethod:) in Objective-C

In Objective-C you can pass a method A as a parameter of other method B.
and call method A from inside method B
very easily like this:
-(void) setTarget:(id)object action:(SEL)selectorA
{
if[object respondsToSelector:selectorA]{
[object performSelector:selectorA withObject:nil afterDelay:0.0];
}
}
Is there any functionally like this in C++ ?
C++ and Objective-C are quite different in that regard.
Objective-C uses messaging to implement object method calling, which means that a method is resolved at run-time, allowing reflectivity and delegation.
C++ uses static typing, and V-tables to implement function calling in classes, meaning that functions are represented as pointers. It is not possible to dynamically determine whether a class implements a given method, because there are no method names in memory.
On the other hand, you can use RTTI to determine whether a given object belongs to a certain type.
void callFunc(generic_object * obj) {
specific_object * spec_obj = dynamic_cast<specific_object*>(obj);
if (spec_obj != NULL) {
spec_obj->method();
}
}
Edit:
As per nacho4d's demand, here is an example of dynamic invocation :
typedef void (specific_object::*ptr_to_func)();
void callFunc(generic_object * obj, ptr_to_func f) {
specific_object * spec_obj = dynamic_cast<specific_object*>(obj);
if (spec_obj != NULL) {
((*spec_obj).*f)();
}
}
Yes there is, and they are called "Function Pointers", you will get lots of results googling around,
http://www.newty.de/fpt/index.html
http://www.cprogramming.com/tutorial/function-pointers.html