methods

Dynamic Function Creation in Java

余生颓废 提交于 2019-12-05 13:05:17
So I'm trying to figure out if there is some method to dynamically create/assign a method to a class in Java. If it were C, I would just do it as follows using pointers: public class Foo { void bar(void *ptr) {....} }; int main() { Foo f = new Foo(); f.bar({"my function" ...}) } However, Java of course has no pointers, so is there any way to get a similar functionality out of a Java application? In Java, you would normally declare an interface with a method to be called. For example, if your function simply wants to execute some code, you would declare a Runnable and implement its run method.

Passing a method's code as an argument in a typesafe way

会有一股神秘感。 提交于 2019-12-05 12:37:26
Passing a method as an argument is not a problem: type TSomething = class Msg: string; procedure Show; end; procedure TSomething.Show; begin ShowMessage(Msg); end; type TProc = procedure of object; procedure Test(Proc: TProc); begin Proc; end; procedure TForm1.Button1Click(Sender: TObject); var Smth: TSomething; begin Smth:= TSomething.Create; Smth.Msg:= 'Hello'; Test(Smth.Show); end; I need something tricky - to pass only a code part of a method. I know I can do it: procedure Test2(Code: Pointer); var Smth: TSomething; Meth: TMethod; begin Smth:= TSomething.Create; Smth.Msg:= 'Hello Hack';

iOS: What is the processing overhead in invoking an Objective-C method?

守給你的承諾、 提交于 2019-12-05 12:23:40
I am writing some real-time audio processing code, which is to be executed in an audio unit's render callback. This thread is at the highest priority level the system recognises. Apple instructs to minimise the amount of processing that goes on in this call. One of their recommendations is to avoid Objective-C method invocation. But why? What happens when an Objective-C method is invoked? what is the actual overhead? Objective-C method resolution is dynamic. In other languages such as C or C++, a function call is set at compile time, essentially as a jump to the address that contains the

Running method while destroying the object

寵の児 提交于 2019-12-05 12:20:13
A few days ago my friend told me about the situation, they had in their project. Someone decided, that it would be good to destroy the object of NotVerySafeClass in parallel thread (like asynchronously). It was implemented some time ago. Now they get crashes, because some method is called in main thread, while object is destroyed. Some workaround was created to handle the situation. Ofcourse, this is just an example of not very good solution, but still the question: Is there some way to prevent the situation internally in NotVerySafeClass (deny running the methods , if destructor was called

ES6 getter/method without curly braces

放肆的年华 提交于 2019-12-05 12:14:38
I have some classes which consist of many short getters/methods. Example: get jQuery() { return this.pageConfig.jQuery || jQuery; } An arrow function with similar content may be written as follows: () => this.pageConfig.jQuery || jQuery; Which is a one-liner and thus consumes only 1/3 of vertical space. But it is not a getter nor a method. Is there a recommended way of writing getters/methods in the form of a one-liner? (if possible without curly braces and without the return keyword) My getters and methods need not modify the object. They are just reading. For a one liner, just go with this:

Calling methods on value types

半城伤御伤魂 提交于 2019-12-05 12:08:11
Stop me if I make a mistake here. If I understand correctly, when I call a method on an instance of a class, the JIT compiler locates the type object corresponding to the type of the instance and then locates a reference therein to the actual method code. My question is how does this work for value types? I was under the impression that value types did not have a type object pointer like reference types do. If this is the case, how does the CLR manage to navigate to the method code when one is called? Consider an example, suppose we have the following structure: public struct Test { public

How to use operator= with anonymous objects in C++?

自作多情 提交于 2019-12-05 11:49:57
I have a class with an overloaded operator: IPAddress& IPAddress::operator=(IPAddress &other) { if (this != &other) { delete data; this->init(other.getVersion()); other.toArray(this->data); } return *this; } When I try to compile this: IPAddress x; x = IPAddress(IPV4, "192.168.2.10"); I get the following error: main.cc: In function ‘int main()’: main.cc:43:39: error: no match for ‘operator=’ in ‘x = IPAddress(4, ((const std::string&)(& std::basic_string<char>(((const char*)"192.168.2.10"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))))’ IPAddress.h

Force the order of functions in the virtual method table?

别等时光非礼了梦想. 提交于 2019-12-05 11:19:42
How can I control the order of virtual functions in the virtual table? Are they laid out in the same order that they are declared in? When inheriting a class with a virtual table, is the virtual table of the inherited class an extension of the base class, or is an entirely new virtual table created with only the inherited classes virtual functions. (i.e. is the virtual table still at index +0x0 of the class?) (a) As far as the standard is concerned, you can't, (in fact you can't even assume that vtables exist). (b) Probably, but what are the circumstances in which you need to control the order

Meaning of <T, U extends T> in java function declaration

本秂侑毒 提交于 2019-12-05 11:17:00
I am seeing this: public static <T,U extends T> AutoBean<T> getAutoBean(U delegate) I know the input class is of U type and AutoBean class is of T type and U extends T is the boundary. But what does <T, mean here? Also, if I am going to write a function to accept the output of getAutoBean, how would you write the function declaration? (i.e. myFunction(getAutoBean(...)), what will the function declaration of myFunction() be?) Thank you! It just declares the types that your method deals with. That is to say, that it basically has to first declare the generic type names, and only then use them in