Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Question How to Serialize a "Function Pointer"?

    Hi,

    I want to do the equivalent of serializing a "function pointer" in Java and I am trying to find the most efficient method to do so. What I want to do is to take a "static method pointer" and then assign it to a field of a class instance that gets serialized (let say this class is called SerClass). I want the "static method pointer" itself to get serialized, but I don't want the class where that static method is defined to get instantiated or an instance of it to get serialized through the serialization mechanism of SerClass. Basically what I want is to be able to call the pointed static method after an instance of SerClass gets unserialized. Of course the "pointer" value has to be dynamic.

    So let say that the signature of the method I want to pass is double f(double x). What I had thought to do is to declare an interface like this:

    Java Code:
    public interface FPtrI extends serializable {
      public double f(double x);
    }
    and then to use a class defined like the following

    Java Code:
    public class FPtrC extends FPtrI {
     public FPtrC(){}
     public double f(double x){return MyClass.f(x);} //MyClass.f(double) is a static method
    }
    as a way to transmit the "pointer". So this way the "pointer" to MyClass.f would get serialized without having to instantiate MyClass or serialize an instance of it. I could define FPtrC as a static sub-class of MyClass and instantiate it when passing the "pointer" to SerClass. I could thus define a subclass FPtrC for every class where I want the pointer to potentially point. Is it the most efficient way of performing what I am trying to do (from a serialization point of view, but also to have an efficient way to call MyClass.f without having to use reflection)?

    Thanks!
    Last edited by kreyszig; 08-11-2011 at 02:09 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    What I want to do is to pass a "pointer" to a static method
    By pointer I assume you mean a reference to an object.
    There should be no problem with that.

    <CLASSNAME>.staticMethod(new Object()); // pass a pointer to an object to a static method in CLASSNAME

    Not sure what you are trying to do or why.

  3. #3
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    By pointer I assume you mean a reference to an object.
    There should be no problem with that.

    <CLASSNAME>.staticMethod(new Object()); // pass a pointer to an object to a static method in CLASSNAME

    Not sure what you are trying to do or why.
    Sorry, I don't want to pass a pointer to a static method. I want to pass a "static method pointer" to another method. (I changed the quoted sentence in my original post)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Is this it: You want to have a reference(pointer) to a class that has a static method so you can pass that reference to another method without creating a new instance of that class. Sounds like a job for reflection.

  5. #5
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is this it: You want to have a reference(pointer) to a class that has a static method so you can pass that reference to another method without creating a new instance of that class. Sounds like a job for reflection.
    Yes I think this is what you describe, and in addition I want the pointer to be serializable (without instantiating/serializing the class where the static method is defined). Sorry I was having a hard time to explain it. I edited me original post again to explain it more in details. I want to avoid reflection as much as possible since it is probably not the most efficient option. The pointed static methods will get called millions of times...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,568
    Blog Entries
    7
    Rep Power
    21

    Default

    Java doesn't have method pointers; C# has; aren't you mixing up those two languages?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    In your OP example MyClass won't get serilialsed anyway. Objects get serilised, not Classes.
    Indeed, it's the data in the objects that gets serilialised.
    The receiving system of that serialised data needs to have access to the same version of that Class in order to reconstitute the object.
    So your stuff in the OP is doing nothing at all, since there is no (useful) data in that FPtrC class.

    You've also got your extends and implements back to front.

  8. #8
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    In your OP example MyClass won't get serilialsed anyway. Objects get serilised, not Classes.
    Indeed, it's the data in the objects that gets serilialised.
    The receiving system of that serialised data needs to have access to the same version of that Class in order to reconstitute the object.
    So your stuff in the OP is doing nothing at all, since there is no (useful) data in that FPtrC class.

    You've also got your extends and implements back to front.
    I know that instances get serialised (I edited my original post to make it more exact, although harder to read) and that the receiving system must have access to the class... But this is not true to say that my example is not doing anything, because the SerClass instance holds a pointer to a FPtrC instance. When the pointer of the SerClass instance gets serialised, the information regarding the fact that it points to an instance of MyClass.FPtrC, instead of let say, an instance of MyOtherClass.FPtrC, will get serialized as well, right?
    Last edited by kreyszig; 08-11-2011 at 02:13 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    So, just to clarify:
    Java Code:
    public class SerClass implements Serlializable {
        private FPtrI myFunction;
    .. setters etc ...
    }
    ...
    public class FPtrC extends FPtrI {
     public FPtrC(){}
     public double f(double x){return MyClass.f(x);} //MyClass.f(double) is a static method
    }
    ...
    public class FPtr2 extends FPtrI {
        public FPtr2(){}
        public double f(double x) {
            return MyClass.g(x);
        }
    }
    So you would set myFunction to some concrete implementation, each of which would do something different (in the above, run MyClass.f() in one, and MyClass.g() in the other)?

    Yes, that would work.

    Not sure what you achieve via this, but it would work.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Still not sure what you are trying to do. Your questions are more about how to implement something a specific way rather than looking for a solution to a problem. Perhaps there are other solutions.
    In java all methods belong to a class.
    Question about C## Can you code a function without a class?
    In java pointers are references to instances of an object created by the new statement or other methods.
    The serialization process saves the contents of variables in a class. The way you are describing what you want to do, there is no data to be saved. Then you should not need to serialize the class.
    The class file will contain the byte code for the method you want to call.
    It seems what you are looking for is provided by the use of an interface. That will allow you to have many objects of the same type each of which can have a single method that does its thing.
    Last edited by Norm; 08-11-2011 at 03:58 PM.

  11. #11
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So, just to clarify:
    ...
    So you would set myFunction to some concrete implementation, each of which would do something different (in the above, run MyClass.f() in one, and MyClass.g() in the other)?

    Yes, that would work.

    Not sure what you achieve via this, but it would work.
    Yep, you got it pretty right. SerClass instances are used to pass messages in a parallel computing environment. They transmit a message between a class that represents a model and another class that needs to retrieve information from that model. Since the message can be transmitted across the network, it needs to be serialisable and what I am trying to do is some kind of serialisable callback mechanism. So what I have is:

    Java Code:
    public interface FPtrI extends Serializable {
      public double f(double param, Data data);
    }
    Java Code:
    public class MyClass {
      private static FPtrI pointer=new FPtrC();
      private Data myData=...
    
    
      public MyClass() {
        SerClass message=new SerClass();
        message.setModel(pointer);
        message.SetData(myData);
        Network.post(message);
      }
    
      public static double f(double param, Data data) {
        //Implementation of the model for this class
      }
    
      static class FPtrC extends FPtrI {
        public FPtrC(){}
        public double f(double param, Data data){return MyClass.f(param,data);}
      }
    }
    [And other models are getting implemented, similarly to the what is shown above for MyClass]

    Java Code:
    public class SerClass implements Serializable {
        private FPtrI myPointer;
        private Data myData;
    
        public void setModel(FPtrI pointer){myPointer=pointer;}
        public FPtrI getModel()}{return myPointer;}
        public void setData(Data data){myData=data;}
        public Data getData(){return myData;}
    }
    And then the class that plays with the models. In reality, models are attached to entities whose existence depends on dynamic parameters. Things are obviously simplified here:

    Java Code:
    public class Analyzer {
      public void analyze() {
        SerClass message;
        double[] params={...};
        FPtrI modelPtr;
        Data data;
        double value;
    
        while((message=Network.get())!=NULL) {
          modelPtr=message.getModel();
          data=message.getData();
    
          for(double param: params) {
            value=modelPtr.f(param, data);
            ...
          }
        }
      }
    }
    Would there be a more efficient way to perform that?
    Last edited by kreyszig; 08-11-2011 at 04:48 PM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Not sure, but I've got db locking issues so can't really spend the time on it.
    Now that it's spelled out (and what it's for) someone else might have a better solution for this sort of thing in a Java environment, but if it works for you and makes sense in your head then maybe that's not important.

  13. #13
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Still not sure what you are trying to do. Your questions are more about how to implement something a specific way rather than looking for a solution to a problem. Perhaps there are other solutions.
    I am constrained by the fact that I have to use a single message passing class (such as SerClass).

    In java all methods belong to a class.
    Question about C## Can you code a function without a class?
    Sorry I don't know C##...

    In java pointers are references to instances of an object created by the new statement or other methods.
    I know that

    The serialization process saves the contents of variables in a class. The way you are describing what you want to do, there is no data to be saved. Then you should not need to serialize the class.
    I don't think the above statement is right. The serialization mechanism also saves identifiers for the classes whose instances are getting saved. So even if the classes *.FPtrC do not contain any fields, tags that identify them get saved.

    The class file will contain the byte code for the method you want to call.
    It seems what you are looking for is provided by the use of an interface. That will allow you to have many objects of the same type each of which can have a single method that does its thing.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    The serialization mechanism also saves identifiers for the classes whose instances are getting saved
    Of course, the serialized data must identify what instance of what class was saved.

    I have to use a single message passing class (such as SerClass).
    What does that mean wrto the reference to a method to be called?
    The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
    The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    The serialization mechanism also saves identifiers for the classes whose instances are getting saved
    Of course, the serialized data must identify what instance of what class was saved.

    I have to use a single message passing class (such as SerClass).
    What does that mean wrto the reference to a method to be called?
    The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
    The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?

  16. #16
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Of course, the serialized data must identify what instance of what class was saved.


    What does that mean wrto the reference to a method to be called?
    The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
    The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?
    When SerClass gets serialised, the instance pointed by SerClass.myPointer will also get serialised (it is serialisable). The class for that instance does not have any field, but nonetheless the serialisation mechanism will take care of identifying the specific FPtrC class which is referenced by SerClass.myPointer

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Then is that the answer you want? Will that do it?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Essentially it's a mechanism for telling the receiving system to "run this method on this class".

    You could, of course, subclass SerClass to include this method, but I'm not sure that wouldn't actually be more clunky.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Time to write a simple test program with several classes and see what happens.

  20. #20
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Essentially it's a mechanism for telling the receiving system to "run this method on this class".

    You could, of course, subclass SerClass to include this method, but I'm not sure that wouldn't actually be more clunky.
    Yes I could potentially subclass SerClass instead... I can't think of any better method of doing what I want...

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 07-27-2011, 01:07 PM
  2. Replies: 3
    Last Post: 04-29-2011, 01:08 AM
  3. Can I Serialize "this"??
    By Sandia_man in forum New To Java
    Replies: 8
    Last Post: 08-29-2010, 03:36 AM
  4. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 12:46 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •