queue/stack blahs

gtoledo3's picture

I'm having a time envisioning how queueing working internally in QC, or more so...

What is the best way to program something so that everytime value a given input port value changes, you create a queue in a custom plugin that retains all values?

I mean, if I make objects, and retain them, this works... I'm not sure the best way to "contract the queue", as would work in javascript, for example. Does one release the object(s) when the retain count is over "whatever"?... or should I be using some kind of "remove object ___" method?

I've looked for remove object stuff (like removeAllObjects) in the NS Object Class reference, but I'm not finding it offhand.

What I'm trying to actually achieve are two separate things:

1- I wish to emulate the basic function of a fifo queue with a max count. This is so I can plug something like a mouse directly into a rendering patch and keep track of the last X values of mouse x and y without using the QC queue.

2- Particle system stuff.

psonice's picture
Re: queue/stack blahs

Assuming you're going down the obj-c/cocoa route, use an NSMutableArray.

What you do is: you get an incoming object of whatever type. You add it to the array with:

[array insertObject:object atIndex:0]; [object release]; // you only need to release if you've retained it somewhere

That sticks the object at the start of the array. The array retains the object, so you can release it if you've retained it for any reason. If not, don't release it.

You can then remove the last object with:

if([array count] > maxItems) [array removeLastObject];

That will remove it from the array, and also release it (if they object is not used anywhere else it shouldn't be retained anymore, and it will get deallocated automatically).

gtoledo3's picture
Re: queue/stack blahs

Thanks, that removeLastObject was eluding me. I haven't tried it, but that makes sense to me... I may end up doing something slightly different, but you got me over the hump for sure.

I can't find where the remove object stuff is in the NSObject Class Reference... I was just typing "remove___" and trying to let Xcode give me some hints with the autocomplete, as lame of a last resort as that is (which did at least result in my finding removeAllObjects).

Poking around some more, I realize now that there are also remove member for key and at index methods for QC Structure (as well as replaceMemberAtIndex, add member, set member, etc.)

psonice's picture
Re: queue/stack blahs

Yeah... NSObject is just a general abstract object class. It doesn't have any functionality for handling lists and the like. You need to look at the NSMutableArray object documentation (combined with NSArray, and perhaps NSObject in very rare circumstances).

Think of NSMutableArray as a list that you can edit. It just has editing functionality, not much more. But it's a sub-class of NSArray, which is a fixed, non-editable list. Mutable arrays inherit all the general list handling functionality from NSArray. And they're both objects of course, so they inherit their basic object handling stuff from NSObject (like retain/release, that kind of thing).

gtoledo3's picture
Re: queue/stack blahs

Oh I see, it totally makes sense that wouldn't be listed in the NSObject reference, as it's part of NSMutableArray, and NSObject is the root class.

I understood that NSMutableArray had those characteristics, but I hadn't taken time to think about it in the greater context of everything and that it's those methods that it's based on.

dust's picture
Re: queue/stack blahs

george you can use a qc patch controller to observe and get notified when a value changes in your composition. this is from the dev docs

/*
    This starts observing value changes on the input
*/
[theController addObserver:self forKeyPath:@"patch.Foo.value" options:0 context:NULL];
 
/*
    The observer (here self) must implement this method which will be called
    by KVO whenever the value on the input changes
*/
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object
    change:(NSDictionary*)change context:(void*)context
{
    //Do something here
}
 
/*
    This stops observing value changes on the input
*/
[theController removeObserver:self forKeyPath:@"patch.Foo.value"];

you can also use the KVO method

didChangeValueForKey:

like chris is saying using a mutable array is best. so one you would want to do a conditional like if input value changed then set object or add the object to your mutable array. if you keep adding to the array it will shift your objects automatically for you.

then you would want another conditional something like if mutable array count equals queue size then remove all objects and start adding again.

you could also keep track of the indexes you have added and do an set object or add object to index as well as remove object at index. then reverse the order or what ever depending on how you want the structured output.

gtoledo3's picture
Re: queue/stack blahs

That's not what my question was, which has been answered. I understood all of that, my question was about capping the object count at a specific value, and best methods, not observing when a port updates. anyway, thanks....:)