Showing results for 
Search instead for 
Did you mean: 

C++ template like functionality with libraries (lvlib) and typedefs

I'd like to share you the following design pattern I have found very useful many times.

C++ language provides templates as a solution for code reusage. One can use templates to create a prototype of a function using variables with undefined data type. Later on one can use the template to create an implementation of the function with specific data type.

One can achive similar functionality in Labview with the following method. Create an empty Library (lvlib, not LLB). Different libraries can contain VIs with the same name without problems since libraries have internal namespace. Create one or more typedefs inside the library corresponding to yet undefined template variable types. Create one on more VIs inside the template and use the typedef you just created to define terminals and constants and arrays etc of your choise.

Now, to create a different type version of the exactly same library, select save as from the library and provide different name. Then open the newly created library and change the type of the typedef. No you have exatly same functionality for different data type.

An example:

Consider you need want to create an operation that takes in an array and sets the first element to 0. You want to create operations for 1D, 2D and 3D arrays of types DBL, SGL, CSG and CDB. You create an emtpy library (lvlib), and create a typedef called datatype.ctl for lets say DBL. You then create a VI for 1D version. Define the input and output terminal to be 1D array of typedef datatype.ctl. Create a similar VI and for 2D and 3D operations as well.

No the catch. Save the library. Then create a copy of the library using save as function. Open the copy and modify the datatype.ctl of the copy. Define it to be SGL. Now if you used only polymorphic SubVIs in your VIs, you just created a SGL version of the library. Copy the library again and do the same steps for CSG and CDB. In the end create one polymorphic VI that contains all 12 VIs in all four libraries you just created. That's it.

Edit: I think this works only in LV 8.0 and later...


Message Edited by Tomi M on 03-28-2006 10:45 AM

Message Edited by Tomi M on 03-28-2006 10:50 AM

Tomi Maila
Message 1 of 4

Why this thread doees not have even a single reply all these years?


Has it not hooked anybody till date... ?!

- Partha ( CLD until Oct 2024 🙂 )
0 Kudos
Message 2 of 4

Kudos to Harold Timmis! Smiley Happy

- Partha ( CLD until Oct 2024 🙂 )
0 Kudos
Message 3 of 4

parthabe wrote:

Why this thread doees not have even a single reply all these years?


Has it not hooked anybody till date... ?!


Well maybe we (at least me) did not understand it.


Could you please illustrate its usefulness?


It just a litle too abstract for this old brain.


Hint: Pictures help.



Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 4 of 4