Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] Bind Standalone Member Functions #15

Open
GoogleCodeExporter opened this issue Mar 16, 2015 · 2 comments
Open

[REQ] Bind Standalone Member Functions #15

GoogleCodeExporter opened this issue Mar 16, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

Would it be possible to add the ability to bind a member function? There is 
already the ability for a c function like:

m->set( "c_function", SLB::FuncCall::create(c_function));

But could we have something like:

struct Foo
{
    int Bar(int a, int b) { return a*b; }
};

Foo f;
m->set( "Bar", SLB::FuncCall::create(Foo::Bar), &f);

Original issue reported on code.google.com by [email protected] on 16 Feb 2012 at 4:51

@GoogleCodeExporter
Copy link
Author

I will consider it but there the problem of how SLB should treat the memory of 
object "f", for example:

{
   Foo f; 
   m->set("Bar", ... , &f);
}

after the block f is no longer a valid pointer...  but this, can lead to memory 
leaks:

{ 
   Foo *f = new Foo();
   m->set("Bar", ... , &f);
}

So, in the end its better to wrap Foo class with SLB and make some sort of 
wrapper around a given object instead. I will consider how to do it, but if you 
have any insight about the management of the memory it would be great to know.

Original comment by [email protected] on 19 Feb 2012 at 1:00

  • Changed state: Accepted

@GoogleCodeExporter
Copy link
Author

It is a tricky problem. I think the only options are to force the user to make 
sure they don't do silly things like that and hope they manage their memory 
correctly, OR do something like you have mentioned and have a wrapper that SLB 
uses to manage the lifetime of the object. The user then might do something 
like (if they wanted to manually remove the binding and clean the memory being 
used):

m->unset("Bar");

However, this doesn't stop them from doing silly things like this:

{
   Foo *f = new Foo();
   m->set("Bar", ... , &f);
   delete f;
   m->unset("Bar"); // potential crash
}

I don't think this is a major concern though as the user should still be 
responsible for overall management.

In any case, I think adding member function support would be really useful 
feature. I just moved over to SLB from luabind and I'm really liking it. The 
member function ability is the only thing I feel it is lacking at the moment. 
Nice work so far!

Original comment by [email protected] on 19 Feb 2012 at 9:59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant