GeeCON Prague 2014*
The moose likes Java in General and the fly likes Is there a Java version of C++ pointers to functions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there a Java version of C++ pointers to functions?" Watch "Is there a Java version of C++ pointers to functions?" New topic
Author

Is there a Java version of C++ pointers to functions?

Curtis Hoffmann
Greenhorn

Joined: Jul 14, 2012
Posts: 6
I'm writing an ADSR (attack/Decay/Sustain/Release)-style software synthesizer, and I've got all the sound stuff working, but the "circuit emulator" part is hard coded. Essentially, it looks like:



What I'd like to do is soft code this "wiring diagram" (oscillator 1 outputs to ADSR input; ADSR outputs to sound engine) so that the user can change it dynamically as the program runs (oscillator 1 inputs to oscillator 2, which then outputs to the ADSR).

e.g. -




In C++ there's a way to store pointers to functions in an array and call them from the array (essentially). Any suggestions for how to approach this in Java for dynamically specifying object input-out connections? (Actually, my code is farther along than the example shows, and both adsr1, osc1 and osc2 implement a class called "module" to unify things, if that makes a difference to the answer for this question.)

Thanks.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Java doesn't have pointers to functions. The closest equivalent would to be define an interface for the function you want a "pointer" to, and then you can have an array or collection of instances of that interface. These instances can be instances of named classes or anonymous inner classes, whichever is more convenient.

I'd give an example, but I'm not quite sure where that translates to your example - how you were planning to use the "pointer to function".
Curtis Hoffmann
Greenhorn

Joined: Jul 14, 2012
Posts: 6
Thanks for the fast reply. Basically, if I were brute-forcing things with an array, it would be something like:



The above code is just an example of what I'm trying to accomplish. Is there a better way to do this?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Curtis Hoffmann wrote:Thanks for the fast reply. Basically, if I were brute-forcing things with an array, it would be something like:

First, please don't put excessively long lines inside code blocks. It screws up the windowing and makes your thread difficult to read. I've done it for you this time. I suggest you re-read the UseCodeTags page thoroughly for details.

The above code is just an example of what I'm trying to accomplish. Is there a better way to do this?

Well, first off, module should be Module. By convention, all Java class and interface names start with a capital letter.

Second, that seems like an awful lot of procedure code to me. Just a couple of things off the top of my head:
  • The pin setting logic should probably be a method in your Module class; all that business about +6 and -1 looks like private implementation detail to me. Furthermore, if there are only a few pin settings you need to worry about, I'd probably have a public method for each one with a good meaningful name (eg, setPinsForInput()).
    Maybe even better, hide ALL of that stuff inside methods that describe the process rather than the mechanics, eg, Module.attachToInput(Amplifier), or possibly Amplifier.attachToInput(Oscillator) - I'm still not sure exactly what class actually contains all this code, but hopefully you get the idea. This article contains some very good tips about process-driven logic.
  • How many types of oscillator are there? Are they always the same? If the answers are: 'only a few', and 'yes', you might want to consider setting up an Oscillator enum that implements your Module interface and/or acts as a factory for different types of oscillator.

  • My suggestion, and it's the same for all coding problems:
    1. Stop coding.
    2. Get out a pencil and paper and describe WHAT needs to be done, not HOW you're going to do it. And do it in English (or your native language), not in Java-ese. Right now you seem to be obsessing about nuts and bolts when you don't have the big picture straight.
    3. When (and only when) you have that done, switch your computer back on and have a look at your code.

    HIH

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Andreas Hollmann
    Greenhorn

    Joined: Jan 06, 2010
    Posts: 27
    In java you can use to call all pointers to methods the observer-design pattern.
    I personally like delegates from C#, they have almost the same functionality as an array with function-pointers. For that purpose I implemented delegates in java. See the example below how a delegate can be used as an function pointer array.



    output:

     
    wood burning stoves
     
    subject: Is there a Java version of C++ pointers to functions?