   [java] Function Pointers -> Work around -> Get your "Function Pointer" class here :) !
  Ok, I know that there are no function pointers in Java. So I have tried to work around this problem.Ok, so what I have now is a Dispatcher class where I define the function to be executed. (pointed to)Then I also made an ActionPool class that contains a list of Dispatcher objects, just for easier managing of my "function pointers".Now Sometimes the body of my "function pointer" needs access to some objects right?So, I added an object of the "Object" type to my Dispatcher class and a new constructor. So if the body of the functionpointer needs to access some specific object, you send it along the constructor.A simple version of what I told you looks like this:  //This is the dispatcher class, the dispatch() function//is the "function pointer", the one to be executedpublic abstract class JACDispatcher {public Object object = null;public JACDispatcher() {}public JACDispatcher(Object obj) {object = obj;}public void dispatch() {//here comes the body of the function...}}//This is how I would want to create a "function pointer":class TestClass {public int varInt = 0;public TestClass() {//This is how I want to create the functionpointer//This one is going to increment the varInt in//a given TestClass object...JACDispatcher disp = new JACDispatcher(this) {public void dispatch() {this.object = (TestClass)this.object;this.object.varInt++;}};JCActionPool.getInstance().registerAction("testClassInc",disp);}}The problem I'm getting is that the varInt variable isn't accessible in the dispatch() function. the "object" in the dispatch() function should now be of (TestClass) type and therefor it should have a varInt variable right?!But it isn't...First I send a TestClass reference with the constructor to the new "function pointer" - the Dispatcher class.Then the TestClass object is casted to the (Object) type, but it should still have memory allocated for a TestClass object, and thus later be possible to be casted back to (TestClass).So I don't understand why it doesn't work?thank you(The question has been solved and a rough "function pointer" class is at the bottom...)[edited by - FxMazter on February 9, 2004 1:58:07 PM][edited by - FxMazter on February 9, 2004 8:00:14 PM][edited by - FxMazter on February 9, 2004 8:01:01 PM]
  http://java.sun.com/docs/white/delegates.html ;  Hey, Trap - I understand that you didn''t read my long post Just anwsered a "standard" anwser to the probabilly often upcomming question about "function pointers".Now that article talks about exactly what I have done here...But the problem I''m having isn''t the delegate, but the "this" pointer that I''m sending along...Would really appreciate if you could read my post again and check if you can find any error there?thx! ;  Change this.object = (TestClass)this.object;this.object.varInt++;ToTestClass testClass = (TestClass)this.object;testClass.varInt++; ;  Thank you Per that works nice! ;  Ok, I got another problem now Now, lets say I would want to let the dispatch() function to take different amounts of arguments?I know I could make a couple of dispatch(...) functions with different amounts of arguments, but then I will have to remember how many arguments this Dispatcher has, and how many arguments that Dispatcher has... And I would have to choose the right one or the Dispatcher won't execute the right function...So is there any way of choosing to override only one of the multiple dispatch functions and kinda let the other ones "dissapear" when I do that?this is somewhat what I'm aiming for: public abstract class JACDispatcher {public Object object = null;public JACDispatcher() {}public JACDispatcher(Object obj) {object = obj;}//I want to be able to choose to overrride one of these//when i derive from this class, and let the rest of //them "dissapear"... so they aren't visible to the new //class anymore...public void dispatch() {}public void dispatch(Object arg1) {}public void dispatch(Object arg1, Object arg2) {}}//Something like this:public abstract class OneArgDispatch extends JACDispatcher {//I want to overrride the dispatch with one//argument, and I never want to see//the other dispatch functions anymore...public void dispatch(Object arg1) {}}//Now when I do like this:OneArgDispatch oneArgFunctionPointer = new OneArgDispatch() {public void dispatch(Object arg1) {//Here I want to do some calculations on arg1}}//And when I do like this, I want only the dispatch(Object arg1)//to be visible:oneArgFunctionPointer.dispatch("This is arg1");//But It isn't what happens... all the other dispatch() and//dispatch(Object arg1, Object arg2) are also visible, which is //NOT desired :(//So I could do like this, which won't do any good:oneArgFunctionPointer.dispatch("This is arg1", "and arg2");//which has an emty body... so therefor I want to disable it!Hopefully you have understood what I want to accomplish I have no clue on how to solve this, but I'm having my hopes to the Java 1.5 sdk that comes with generics, where I simply could do something like this: JACDispatcher<OneArg> functionPointerWithOneArgument;JACDispatcher<TwoArg> functionPointerWithTwoArguments;//I hope that definition like this will be possible:class JACDispatcher<NumOfArgs> extends NumOfArgs {//...}That would really be wonderfull, and probabilly java would save me from c++  since templates are in my opinion the ONLY plus that makes me choose C++ before Java. Optimized Java can ALMOST achieve the same speed as C++, and with future JVM's that will probabilly happen/if not even surpass C++ in speed. (There still is sooo much optimization to be done to the JVM...)Anyway... a bit off topic (A former C++, and DEATH to Java coder)  [edited by - FxMazter on February 9, 2004 3:43:09 PM];  Depends on what exactly you are trying to archieve. Would passing one Array of arguments instead do the job? Or in 1.5  varargs? ;  Nope, not an array What I want to do is something like a Functor in C++. I want to be able to store the functions as objects together with the functions arguments as data.So I want it to look as similar to a function as possible.I want to define the functions like this:OneArgDispatch oneArgFunctionPointer = new OneArgDispatch() {public void dispatch(Object arg1) {//Here I want to do some calculations on arg1}}  and I want to call it with a syntax like this:oneArgFunctionPointer.dispatch("This is arg1");twoArgFunctionPointer.dispatch("This is arg1", "and arg2");but in both of the cases I want the rest of the versions of the dispatch() functions inherited from JACDispatcher class not to be visible to OneArgDispatch respectively TwoArgDispatch objects...[edited by - FxMazter on February 9, 2004 5:57:55 PM] [edited by - FxMazter on February 9, 2004 5:58:12 PM] [edited by - FxMazter on February 9, 2004 5:58:32 PM] [edited by - FxMazter on February 9, 2004 5:59:15 PM] [edited by - FxMazter on February 9, 2004 5:59:31 PM];  What''s wrong with using interfaces for all this instead?Cas ;  Hum, interfaces you say?I'm not sure... but I haven't thought how to implement something like that with interfaces...But anyway, it was trivialy simple I don't know, but I got totally locked up with this shit...This is how it looks now : public class JCBaseDispatcher {Object object = null;public JCBaseDispatcher() {}public JCBaseDispatcher(Object obj) {object = obj;}public boolean testObject() {return (object != null);}public Object dispatch() {}}abstract public class JCOneArgDispatcher extends JCBaseDispatcher {Object argument1 = null;public JCOneArgDispatcher() {}public JCOneArgDispatcher(Object obj) {super(obj);}public void setArguments(Object arg1) {//Clone the value and assign it to //this.argument1}public Object dispatch() {dispatch(argument1);}abstract public dispatch(Object arg1) {}}abstract public class JCTwoArgDispatcher extends JCBaseDispatcher {Object argument1 = null;Object argument2 = null;public JCOneArgDispatcher() {}public JCOneArgDispatcher(Object obj) {super(obj);}public void setArgs(Object arg1, Object arg2){//Clone the values and assign them to //this.argument1//this.argument2}public Object dispatch() {dispatch(argument1, argument2);}abstract public dispatch(Object arg1, Object arg2) {}}//Now you can simply do like this when you want to create a "function pointer":JCTwoArgDispatcher functionPointerWithTwoArguments = new JCTwoArgDispatcher() {public dispatch(Object arg1, Object arg2) {//Here comes the bode of a function//Do whatever you feel here, maybe a function for//Shooting that deadly rocketlauncher? ;)}}//Now you can simply do like this when you want to create a "function pointer":JCOneArgDispatcher functionPointerWithOneArgument = new JCOneArgDispatcher() {public dispatch(Object arg1) {//Here comes the bode of a function//Do whatever you feel here, maybe a function for//Shooting that deadly rocketlauncher? ;)}}//This is how you would fire the functions:functionPointerWithTwoArguments.dispatch("This is the arg1", "and this is the arg2");//or like this:functionPointerWithTwoArguments.setArgs("This is the arg1", "and this is the arg2");//and later callback:functionPointerWithTwoArguments.dispatch();//Two ways of using the "Function pointer", both usefull in cases :)//And this is how the other would look:functionPointerWithOneArgument.dispatch("This is the only arg - arg1");//or like this:functionPointerWithOneArgument.setArgs("This is the only arg - arg1");//and later callback:functionPointerWithOneArgument.dispatch();//Two ways of using the "Function pointer", both usefull in cases :)//A little example would be:public class MyCustomButton {public MyCustomButton() {}//Instead of making normal function bodys, you make the dispatchers instead...public JCOneArgDispatcher doSomething1 = new JCOneArgDispatcher() {public dispatch(Object arg1) {//Here comes the bode of a function//Do whatever you feel here, maybe a function for//Shooting that deadly rocketlauncher? ;)}}//You could also send along some object that the function needs access to//Maybe its the Renderer or some other importat class in the Game class?//In this case we will just send along a pointer to this MyCustomButtonpublic JCTwoArgDispatcher doSomething2 = new JCTwoArgDispatcher(this) {public dispatch(Object arg1, Object arg2) {//Here comes the bode of a function//Do whatever you feel here, maybe a function for//Shooting that deadly rocketlauncher? ;)}}//Now you could register those "function pointers" in some other object//Maybe you want a button that is supposed to fire one of these//functions?? yeh... anyway...//If you want normal functions as well... then do it like this:public int doSomething1(int val1) {//If the returnvalue from the function pointer is an int...return ((int)doSomething1.dispatch(val1));}public void doSomething2(String str1, String str2) {doSomething2.dispatch(str1,str2);}}Ok, there you have a basic "function pointer" and a "function to member" pointer Hopefully someone will find it usefull, like me You prolly would want to add a couple of more variants of it with more arguments. A good value would prolly be having all variants up to 10 arguments... some shitty work, but hey! - you got em for ever ^^When generics will come later this can be done much better, and will have better compatibility for storing the function pointers in containers like lists or maps.For now this will have to be sufficent...   [edited by - FxMazter on February 9, 2004 7:59:11 PM]