We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › passing basic variables by reference
Page Index Toggle Pages: 1
passing basic variables by reference (Read 338 times)
passing basic variables by reference
Jul 28th, 2008, 1:04am
 
I'm building a little utility class on top of promidi, but ran into a problem. The functionality I would like is:

Code:

MSAMidi msaMidi = new MSAMidi(this);

//msaMidi.addController(varName, minValue, maxValue, deviceID, midiChannel);
msaMidi.addController("myVar1", 5, 100, 1, 1);
msaMidi.addController("myVar2", -10, 10, 1, 1);
etc.


So basically a very simple interface for being able to assign midi controllers to my variables, and then the variables get updated automatically as the midi controllers change (a bit like how controlP5 works - but with midi controllers).

I know how to use promidi and have a callback (plug) for controllerIn etc. but my problem is purely a Java syntax problem (i'm more of a C guy). How do I pass my variables to the class to be registered? I know objects are passed by reference, but integers and floats are passed by value.. can I change that? or I send as a string (like in controlP5), how can I access them later?

P.S. I was going to use a HashMap to store all the registered variables and associated midi info using (midiDeviceID<<11 + midiChannelNum<<7 + midiCC) as the key. If there is a better way please lemme know...

P.P.S. I have to pass 'this' to the constructor of MSAMidi because promidi needs access to the PApplet. Is there anyway MSAMidi can know the PApplet without me passing it?

Cheers,

Re: passing basic variables by reference
Reply #1 - Jul 28th, 2008, 10:13am
 
If you use Integer or Float instead of int or float, they'll then be passed by reference. However, even this won't help, since the Integer/Float classes don't have method for altering their values, and any assignment actually changes the reference so it's no longer linked.

The only way I can see to do what you want is to create your own wrapper classes. e.g.

Code:
class myInt
{
int val;
myInt(int i)
{
val=i;
}
void setVal(int j)
{
val=j
}
//etc..
}

And then you'd have to pass your objects, and have the callback functions able to cope with them.

Also no, there's no way for a class to be bale to guess what the PApplet is without you passing it in as "this".
Re: passing basic variables by reference
Reply #2 - Jul 28th, 2008, 11:03am
 
Basically, there is two ways (probably more, but it becomes convoluted...):

- the casher way is what JohnG did, to pass the reference to the class holding the primitive types (int, long, float...) and to set accessors to these (private) members: foo.setRate(10). It is fine for a small amount of variables.

- A variant is to still encapsulate the variables but leave them public, and change them directly (foo.rate = 10). Less OO purist (can't hide types, etc., close of global variable hell) but pragmatic: no need to create dozens of setters/getters.

- Another way if you have lot of primitive type variables is to store them in arrays: if you pass the array to a function, it is given by reference, as always, so altering the content of the array alters the original!

Mmm, that's already three ways, actually. Cheesy
Re: passing basic variables by reference
Reply #3 - Jul 28th, 2008, 12:42pm
 
Hi Guys, thanks for the replies.

RE: not being able to access the PApplet from within a class without passing it in is fine... i'll just carry on sending in 'this'...

For the registering variables, I'd rather change my app code as little as possible. In this particular app I have about 20 variables that I want to control with midi and I have other apps which I'd like to add the functionality as well with minimal change if possible.

If there were no other choice I would go for the encapsulating in an object or array approach, but the controlP5 library seems to be able to do it with just using normal ints, floats, booleans etc by sending in the variable name as a String (which makes integrating it very easy!). I'm looking through the source code and have found where they are registered in a HashTable, but haven't yet found the chunk of code which accesses those variables. Ultimately thats the kind of functionality I'm looking for...

Re: passing basic variables by reference
Reply #4 - Jul 28th, 2008, 12:56pm
 
I can be wrong but I think that controlP5 is using one of the "other" methods I wrote about, namely using introspection (analyzing at low (actually medium) level the Java bytecode) to access a class/member/field by name.
Not the most orthodox way, but useful for a foreign library like controlP5 or some other corner cases. Probably not very fast, though (much slower than the given alternatives).
Re: passing basic variables by reference
Reply #5 - Jul 28th, 2008, 3:11pm
 
Arrays are objects so they're also passed by reference. So if you need to move your 20 variables around, just pass an int[] or float[] array.

for instance, this code in c/c++:
something(int* p) {
}

will have the same semantics in java:
something(int[] p) {
}

meaning that you'll be modifying the values pointed to by 'p', not a copy of them.
Page Index Toggle Pages: 1