YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

[sqrat] Binding of function pointers as functions
devlin
#1 Posted : Tuesday, October 20, 2015 2:42:44 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/21/2015(UTC)
Posts: 10
Location: Sweden

Thanks: 0 times
Was thanked: 1 time(s) in 1 post(s)
Greetings!

I'm wondering whether it's somehow possible to bind function pointers for use as functions inside f.e. a Table.

Binding them like usual only works if the function which the function-pointer is pointing to doesn't change after binding it.

F.e.
Code:

void dummy() { printf("hello"); }
void dummy2() { printf("world!"); }

void (*func)();
func = dummy;

RootTable().Bind("Test", Table()
.Func("Dummy", func)
);

func = dummy2;

// squirrel:
Test.Dummy();  // sadly prints "hello" instead of "world!"


If it's impossible to change the binding above to result in "world!" getting printed, I'm going to have to write a *lot* of router-functions. :)

Thanks in advance,
Johan
absence
#2 Posted : Wednesday, October 21, 2015 8:52:26 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 103
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 8 time(s) in 8 post(s)
I do not know SQRat, but from how Squirrel does things here I figure that rebinding might do the trick:

Simply bind AGAIN to the other function, but with the same name:

Code:

void dummy() { printf("hello"); }
void dummy2() { printf("world!"); }


RootTable().Bind("Test", Table()
.Func("Dummy", dummy)
);

//squirrel call Test.Dummy() now will print "hello"

RootTable().Bind("Test", Table()
.Func("Dummy", dummy2)
);

//and now the same call will print "world!"



A single named Slot in a table can hold exactly one object, a native closure in this case, and overwriting it by "re-binding" will automatically replace the old with the new closure properly.
While Squirrel is capable of doing this properly, as already said I'm a bit unsure about SQRats implementation here - it may or may not work properly. I guess it will do.
devlin
#3 Posted : Thursday, October 22, 2015 4:01:04 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/21/2015(UTC)
Posts: 10
Location: Sweden

Thanks: 0 times
Was thanked: 1 time(s) in 1 post(s)
Such an obvious answer - why didn't I think of that? (blaming old age here...)

Works brilliantly, and easily handled since all the function pointers for each system is updated at once. Thanks!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.055 seconds.