11-23-2014 07:08 AM
One should be able to use a case structure around a dequeue, with a constant case selector set to the empty case, and the tunnel out of the queue element set to "use default if unwired". I would expect the compiler to constant-fold that into just the element default type, and so this method should have no overhead.
11-23-2014 01:33 PM
@drjdpowell wrote:
One should be able to use a case structure around a dequeue, with a constant case selector set to the empty case, and the tunnel out of the queue element set to "use default if unwired". I would expect the compiler to constant-fold that into just the element default type, and so this method should have no overhead.
... and, as the Snippet shows, this does, indeed, work! Even simpler ... Note that the Queue is actually empty!
BS
11-24-2014 06:51 AM
Yes, all of these work. But they are way more complicated than just using a simple Type Def constant. You have to know your data type anyways. So you might as well just bind it to a type def.
11-24-2014 07:42 AM
A very good point. Even in my snippets illustrating some of the "other" ways, there's the TypeDef prominently displayed in creating the Queue ...
BS
11-24-2014 11:08 AM
Yes, a type def....but because it wasn't mentioned and because they're so...much...fun... don't forget the variant functions, specifically vi.lib\Utility\VariantDataType\GetRefnumInfo.vi.
From that you can get the type of your data and manually reconstruct it.
Or...use a type def 😞
11-25-2014 03:56 AM
omg so many answers .. i had a long weekend so i didnt see all the replies yet.
thank you all for posting .. ill have a look at all the suggestions now.
regards j
11-25-2014 04:21 AM
@tst & @Bob_Shor thx for the tip .. i thought this was only possible if elemnts were in the Q, ill try it.
@everyoneelse thx for the tips and discussion.
i understand that best practice would be the type def,
but i have some code with too many cables and was trying for a way with less long cables.
will decide how to go about this in production code,
but now at least i have more options 😉
cheers to you all
11-25-2014 05:07 AM
If that's the case then I would definitely suggest typedefs. All the things we were talking about here were ways to achieve what you were asking for, but there are only a few cases where something like this might be useful and I don't think that yours is one of those. Here's how a typedef could remove the wires:
Once you have a typedef you can also pass the queue reference into subVIs and do some of the work in there, so that each piece of code becomes simpler.
There are many other options for improving code, but this thread isn't the place for discussing them. You might want to try reading up on software engineering.
11-25-2014 05:43 AM - edited 11-25-2014 05:44 AM
this question just came up in my mind.
i do know a little about software engineering (am cs)
but applying all my code-based principles with the labview tools is quite troublesome.
for example i have not yet mastered (really tried) the object-oriented approach.
using type defs as best practice is what i took away,
but i was happy to find that my intuition of getting the element data type somehow from the queue itself was not false 😉
j
edit: formatting
11-25-2014 01:13 PM
For me, a (minor) downside of using Queues is all those wires everywhere! So I tend to use what I call Wireless Queues, which have been discussed fairly recently in the Forum (talking about speed/efficiency). Basically, you build an Action Engine for a Queue with all of the Queue functions (Obtain, Enqueue, Enqueue Lossy, ... Release) except Dequeue as the "Action" (I use an Enum that I call Queue Action to hold these choices). Since in most cases none of the Queue actions (except Dequeue) will take appreciable time and prevent other Actions from being called. there should be no problem having them in the same VI and being called from multiple places in your code.
Dequeue, however, typically "waits forever" for something to appear in the Queue. If Dequeue was present in this Action Engine, and you called for a Dequeue Action before you put something on the Queue, you'd be stuck -- the first call to this VI, Dequeue, would "wait forever" for something to appear, blocking the second call that was trying to fill the Queue! [True story -- I made this stupid mistake at least twice before I "learned my lesson" ...].
I've attached a Snippet of one such Queue, whose input is a Cluster. I'm illustrating the Status Action, which I use for three things -- in this particular example, I'm using it to give me a copy of the entire Queue by setting "Return Elements?" to True (normally I don't use this), am returning the number of elements on the Queue (useful to tell when the Queue is empty or getting full), and also to return the Queue, itself. I use this as a "trick" when I want to Dequeue something (typically inside a While Loop) -- I put Status outside the loop, bring the Queue into the loop, and connect it to the Dequeue. Very quick (and you only need to do it once) and otherwise doesn't affect the Queue.
Bob Schor