LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Bun

C++ like Class templates for Labview classes

Status: New

It would be nice if we had a C++ like class template functionality for LabView classes.

 

Templates are a powerful concept. Being able to provide the type of control in a class at edit time would allow users to create reusable classes, like data structures. It would be nice if we had a way to select a control and make it part of a template, so that we could pass the data type of that control in at edit time without changing the base definition of the class, and thus be able to reuse the class in several instances and never have to rewrite any code.

 

A great example would be the node of a tree. If we were to have 2 classes Tree.lvclass & TreeNode.lvclass.  Assume that the Tree class contains an array of nodes and VIs to add, remove, insert, and recurse the nodes in the array. Assume that a node contains a numeric which is the index in the tree array to its parent and an array of numerics which are the indexes into the array of the nodes children. We also need a control in the node that stores our data. We will call it data, and it will be of type LogData.lvclass.

 

The tree works well and great for children of LogData.lvclass, but if we decide we now also need a tree to hold FilterData, we can't just swap out LogData.lvclass for FilterData.lvclass in the TreeNode.lvclass. Because then we wouldn't be able to put LogData classes into our tree node anymore.

 

Class Templates would allow us to tell the TreeNode Class that the type of the control data can be changed at edit time, and in fact is worthless unless we give it a type. Thus we can create several instances of the Tree class and feed the desired type down into the TreeNode, and we can in fact have 2 different trees containing to different types of data, while still reusing the same Tree code.

7 Comments
Mr.Mike
NI Employee (retired)

We do have something like templates in LabVIEW already, but it's very rough and not open to the public.  The idea has been suggested a few times before and the original idea is here.

 

Unless you feel your idea is significantly different than the above linked one, I'm going to request that this be marked as a duplicate of the other one..

-- Mike
Mr.Mike
NI Employee (retired)

P.S. You can see an example of the "template" VI here.  We call them "generic," but it actually behaves more like a template than an generic.  Unfortunately, "template" is already used for VIs.

-- Mike
Bun
Member
Member

From what I read in the link you posted, this is not the same thing. The link you posted deals with polymorphic VIs. This idea is specific to Labview Classes. Where as the other post was specific to polymorphic VIs.

 

The request isn't about being able to to change what type is getting passed in over a connector in a VI (the wire type would remain the same the original class) The added value is being able to make certain controls in the class of a generic type, of which can be an actual type can eventually be assigned at edit time. Not changing the actual wire type would allow you to use all of the accessor & dynamic dispatch VIs that you created for this class regardless of what type the controls that are marked generic are actually made.

 

While I agree this sounds somewhat similar to the link you posted, I don't think they are the same thing. One is about polymorphic VIs and the other is about providing template functionality for classes to allow code reuse.

Mr.Mike
NI Employee (retired)

The first post I linked is about making an easier way to implement polymorphic VIs -- they essentially want a VI with a type that automatically adapts to the input at edit time.  That is in essence having a VI with a control on it that adapts to the type wired into it.  I envision that classes would be templated based on the templated VIs within them.  So if you have Foo.lvlib with a VI that is templated on type T, the class would be typed as Foo.lvlib<T>.  And Foo.lvlib<T> would correspond to a private data control where some control class' private data control is type T (or not...).  I think if/when we get around to making "generics" public then we'll make it so classes can be "generic" too and their accessors will adapt to type (at edit time).

 

I think we're talking about the same things, but the solution (that I've linked) is an abstraction from both of them.

-- Mike
AristosQueue (NI)
NI Employee (retired)

Bun: Yes, you and Mike are indeed talking about the same thing. It is C++-esque template instantiation. What's shown in the article Mike linked to is a single VI, but the feature would obviously need to encompass all the LV library types as well (classes, libraries, xcontrols, state charts).

Bun
Member
Member

What do you guys think about rolling both posts into a new one that better describes the overall idea and then applies it to a few specific examples (like VI's & Libraries)?

 

This seems like something that people want, but it appears that people are comming at it from different angles.

AristosQueue (NI)
NI Employee (retired)

I'd leave the ideas as they stand. Multiple approaches suggest different ways that this could be handled, and the kudos might reflect which solution people would rather see.