The MacView

Virtual Instrumentation from a Mac perspective

Friday, December 23, 2005

Conditional Disable Structure

There is a great new feature in LabVIEW 8.0 for cross platform development: The Conditional Disable Structure. For those familiar with C, this is a #if. You can implement cross platform support in one VI, and the version for the platform it is on will be used. You can find the conditional disable structure in the functions palette under Programming -> Structures.

As an example, say you wanted to create a VI that will convert a Path into a String that could be used to build up a command for the System Exec.vi (Connectivity -> Libraries & Executables). You can just use Path To String primitive (Programming -> String -> Conversion) in most cases, but on the Mac, the command line takes UNIX style paths, and Path To String will produce a Classic style path. As it turns out, there is a VI, vi.lib/Platform/CFURL.llb/PathToUNIXPathString.vi, however this VI is only on the Mac.

If you wanted to make a VI that did the right thing for on every platform, before the Conditional Disable Structure existed, you would have a case structure that used the Property Node (Programming -> Application Control) with Application -> Target -> Operating System property to determine the OS at runtime. In the default case you would just use the Path To String primitive. In the Carbon (Mac OS X is Carbon, don't be fooled by the Mac [Classic] item) case, you would open a reference to PathToUNIXPathString.vi (Programming -> Application Control -> Open VI Reference), call the VI by reference (Programming -> Application Control -> Call by Reference Node) and then close the reference (Programming -> Application Control -> Close Reference). You would have to do this so the VI would not be broken on non-Mac platforms, since PathToUNIXPathString.vi is only there on the Mac.

There are a few problems with this method. The first is that you have to create a type specifier VI refnum to get the connector pane correct on the Call by Reference Node (passed to Open VI Reference). You also have to get the path to the VI to call correct (which may change if you build it into an Application or Shared Library). It is also a common mistake to forget to close the VI reference, and have a VI reference leak. Basically, the code is difficult to write, difficult to maintain and difficult to get right.

With the Conditional Disable Structure, you have two cases. The default case will just have the Path To String primitive. The second case will be TARGET_TYPE == Mac. In that case, just drop the PathToUNIXPathString.vi and wire it up. And there you have it.

Now there is a small annoyance with this method. On the non-Mac platforms, when the VI is loaded and needs to be re-compiled for any reason, it will do a quick search for PathToUNIXPathString.vi, but the VI will come up as runnable.

0 Comments:

Post a Comment

<< Home

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.