class instance changed in callback method comparing to the main instance - ios

in cpp:
void Character::jump(CCLayer *layer){
if (this->isAnimationPlaying) return;
up_or_down = UP;
body->runAction(CCSequence::actions(
CCMoveBy::actionWithDuration(0.5, ccp(0, 50)),
CCCallFuncND::actionWithTarget(body, callfuncND_selector(Character::upDownDone), this),
// CCCallFuncN::actionWithTarget(body, callfuncN_selector(Character::upDownDone)),
NULL));
this->isAnimationPlaying = true;
}
void Character::upDownDone(CCNode *node, CCObject *ob){
this->isAnimationPlaying = false; // *this is different from the this(class instance) in jump method, seems this in upDownDone is a new created instance*
}
So How can I get the class instance in a callback method? And can I make the this same for the main class instance and the callback's class instance?
EDIT:
Character is a class which has no parent class, and body is a member variable which is an instance of CCSprite.
Thanks.

because you are using body to call the function Character::upDownDone.
you should use this to call it.
CCCallFuncND* callFunc = CCCallFuncND::actionWithTarget(first_arg, secend_arg, third_arg);
body->runAction(callFunc);
assume your secend_arg is callfuncND_selector(Character::upDownDone)
then,
the first_arg is the caller, ie. the class instance who calls this function, in your code is body. but actually it should be this, or any instance of Charactor class
the CCNode* node (the first para that is been passed to your calling function) is the action runner, ie. body in your code. because you are using body->runAction()
the CCObject* obj (the second para that is been passed to your calling function) is a void pointer which is exactly the same with third_arg.
another way is use
void Character::upDownDone(CCNode *node, void *ob){
(Character*)ob->isAnimationPlaying = false;
}

Seems like you call the Character::upDownDone method using the instance "body" instead of this .May be you want this:
CCCallFuncND::actionWithTarget(this, callfuncND_selector(Character::upDownDone), body),

Related

Any way around "instance member can't be accessed in an initialized"? [duplicate]

This question already has answers here:
Error: The instance member ... can't be accessed in an initializer
(4 answers)
Closed 6 months ago.
I would like a member of a class to received a listner from its owner class. So, something like this:
class Member {
Member({required this.listener});
final VoidCallback listener;
}
class Owner {
final member = Member(listener: saySomething); // <- error here
void saySomething() {
debugPrint('hello');
}
}
But I get the error on listener: saySomething that reads instance member can't be accessed in an initializer. My understanding is that it's because the compiler builds the Member instance first and then the Owner instance, so doesn't have the locations in memory yet.
I know I can do this in two steps. E.g., instantiate member and then assign its listener in my constructor or wherever, but it would be really nice if I could assign listener when member is instantiated.
I'm pretty sure that's not possible but am hoping to be proven wrong?
You can either pass the function directly into the constructor, or you can make saySomething into a static method, rather than an instance method:
// Directly passing the function
class Owner {
final member = Member(listener: () {
debugPrint('hello');
});
}
// Using a static method
class Owner {
final member = Member(listener: saySomething);
static void saySomething() {
debugPrint('hello');
}
}

Dart create class instance by string with class name

I want to invoke functions of a class by their names inside a string. I know my best option are Mirrors.
var ref = reflect(new TestClass());
ref.invoke(Symbol("test"), []);
It works fine, I can call the function test by a string. But I also want to put "TestClass" inside a string. Is it possible somehow ?
var ref = reflect("TestClass");
ref.invoke(Symbol("test"), []);
Jonas
You can do something like this:
import 'dart:mirrors';
class MyClass {
static void myMethod() {
print('Hello World');
}
}
void main() {
callStaticMethodOnClass('MyClass', 'myMethod'); // Hello World
}
void callStaticMethodOnClass(String className, String methodName) {
final classSymbol = Symbol(className);
final methodSymbol = Symbol(methodName);
(currentMirrorSystem().isolate.rootLibrary.declarations[classSymbol]
as ClassMirror)
.invoke(methodSymbol, <dynamic>[]);
}
Note, that this implementation does require that myMethod is static since we are never creating any object but only operate directly on the class itself. You can create new objects from the class by calling newInstance on the ClassMirror but you will then need to call the constructor.
But I hope this is enough. If not, please ask and I can try add some more examples.

iOS. Passing block to block as parameter

I am going around blocks and try to discover the ways that they can be used.
So I am wondering is it possible to pass block to block like parameter?
Here is some sample code:
//declaration
static id (^someBlock)(id) = ^(id someClass) {
// do some stuff to obtain class some class instance
// check if class instance respond to #selector
// if yes - perform selector
}
//usage
+ (instancetype)someMethod {
someBlock(SomeClass.class);
// do additional work and return some instance type
}
This works fine, but is not good enough, because we obligate caller to respond to selector if caller want to do some additional stuff when someBlock is completed.
So my question is how I can invoke someBlock block with parameter block which I want to be executed when someBlock is completed.
Some like:
//declaration
static id (^someBlock)(id, <b>^otherBlock</b>) = ^(id someClass, <b>????</b>) {
// do some stuff to obtain class some class instance
otherBlock();
}
Any advice?
PS: Please note that the question is not about passing block to method as parameter.
Thanks,
Venelin
Is this what you are looking for?
static id (^someBlock)(id, void (^otherBlock)()) = ^id (id someClass, void (^otherBlock)()) {
otherBlock();
return nil; // just because you declares a `id` return type
};
And call it like
someBlock(someClass, ^() {
NSLog(#"other stuff");
});

iOS jailbreak get object based on class name

I have a dylib which has a object of class "mConWifi". I have the main app which loads this dylib and executes following code
Class klass = objc_getClass("mConWifi");
SEL sel = sel_getUid("ListAllWifi:");
if ( [klass respondsToSelector:sel] )
objc_msgSend(klass, sel);
When above code is called, object of class mConWifi is already created in Memory.
My objective is to get object based on class name and then invoke a method. With above code I am not able to as respondsToSelector fails. I have already tried "ListAllWifi" and "ListAllWifi:"
Any ideas how to get object of a class based on class name?
Thanks in advance.
I think your problem is that you are trying to test a method of class (which are declared with +), but in fact you have an instance method, declared with -.
Try this:
Class klass = objc_getClass("mConWifi");
SEL sel = sel_getUid("ListAllWifi:");
if ( [klass instancesRespondToSelector:sel] ) {
id object = [[klass alloc] init];
objc_msgSend(object, sel);
}

What does Cannot create delegate without target for instance method or closure mean

I am using vala.
This is the source code that gives that compile time bug :
private Gee.HashMap<string,VoidFunc> fill_actions()
{
var actions = new Gee.HashMap<string,VoidFunc>();
MainWindow win = window;
actions["t"] = () => _puts(win.title);
return actions;
}
First I tried to access this.window directly but that gave another error so I tried this with a local scope variable.
Error when doing directly this.window :
This access invalid outside of instance methods
It sounds like VoidFunc is declared with [CCode (has_target = false)]. What that means is that no context information is passed to it, and AFAIK that is the only way delegates work as generic type arguments. The reason for this is limitations in C, so assuming VoidFunc looks like this:
[CCode (has_target = false)]
public delegate void VoidFunc ();
What you'll get in C is something like this:
typedef void (*VoidFunc)();
As opposed to something like this if you didn't have the [CCode (has_target = false)]:
typedef void (*VoidFunc)(gpointer user_data);
When you pass around callbacks in C you generally do so with between one and three arguments. Something with all three would look like this:
void foo (VoidFunc void_func, gpointer user_data, GDestroyNotify notify);
The first parameter is the actual function. The second parameter is the value to pass as user_data to the callback, and is what Vala uses to pass context information to the callback (which is what allows it to act as an instance method, or even a closure). The third parameter is used to specify a function to free user_data when it is no longer needed.
What [CCode (has_target = false)] means is that the delegate doesn't have a user_data argument, and therefore cannot be used as a closure or instance method.
The reason this is necessary with a generic argument is that generics look something like this at the C level:
void foo_bar (gpointer data, GDestroyNotify notify);
The first parameter is the data that you want to use as a generic value, the second is actually only added if the generic argument is owned (as it is in the case of the set methods in Gee), and is called with user_data as an argument when user_data is no longer needed.
As you can see, when trying to use a delegate as a generic, there is nowhere to put the user_data argument, which is why Vala only allows delegates without targets to be generic arguments.
The solution is basically to wrap the delegate in a class:
public delegate void VoidFunc ();
public class YourClass {
private class VoidFuncData {
public VoidFunc func;
public VoidFuncData (owned VoidFunc func) {
this.func = (owned) func;
}
}
private Gee.HashMap<string,VoidFuncData> fill_actions() {
var actions = new Gee.HashMap<string,VoidFuncData>();
string win = "win";
actions["t"] = new VoidFuncData (() => GLib.debug (win));
return actions;
}
}

Resources