templated classes in fluid?

33 views
Skip to first unread message

ed.vi...@gmail.com

unread,
Mar 29, 2022, 7:08:17 AM3/29/22
to fltk.general
Hello

Can I define a templated class using fluid?

I can define templated functions within classes but have not found a way for the class itself.

I tried preceding it with a declaration but that did not work. I cannot add template before the name since fluid precedes the name with "class".

Cheers
Ed

imm

unread,
Mar 29, 2022, 8:11:20 AM3/29/22
to General FLTK
TBH, I do not think that there is.
Fluid is *old*, probably older than templates existed in C++...!

Matthias Melcher

unread,
Mar 29, 2022, 3:31:35 PM3/29/22
to fltk.general
Yep, Fluid is older than templates ;-). OP, could you give me a sample line how your code looks. I'll check if there is an easy fix for that. 

ed.vi...@gmail.com

unread,
Mar 30, 2022, 5:30:34 AM3/30/22
to fltk.general
HI

I would like to generate something like

template <class T>
class CurveInfo {
private:
  T _b;
public:
  CurveInfo( float a, T b ):_b(b) {...}
  someFunction(){ _b.fcn(); }
};

If I put a declaration with the template line right before the class, it gets moved earlier in the generated code and a semicolon appended to it.

Cheers
Ed

Greg Ercolano

unread,
Mar 30, 2022, 11:21:58 AM3/30/22
to fltkg...@googlegroups.com
On 3/30/22 02:30, ed.vi...@gmail.com wrote:
HI

I would like to generate something like

template <class T>
class CurveInfo {
private:
  T _b;
public:
  CurveInfo( float a, T b ):_b(b) {...}
  someFunction(){ _b.fcn(); }
};

Perhaps I'm missing something, but you can just define that template in a
my-template.h file using a text editor, then #include it from your fluid project
using: New -> Code -> Declaration -> #include "my-template.h"

When doing so, be sure to set the pulldown menu option for the Declaration to:
"in header file only", so it gets added to your fluid project's .H file when fluid's
code generator runs:


ed.vi...@gmail.com

unread,
Mar 31, 2022, 6:14:17 AM3/31/22
to fltk.general
Maybe I am missing something but the templated class has a window and widgets so I want to define it with fluid.

Ian MacArthur

unread,
Mar 31, 2022, 7:13:52 AM3/31/22
to fltk.general
On Thursday, 31 March 2022 at 11:14:17 UTC+1 Ed wrote:
Maybe I am missing something but the templated class has a window and widgets so I want to define it with fluid

Hm, OK - I think Greg was just offering a workaround that (I think, anyway) should work with the template sample that you posted.
It is not obvious that the template you showed has any window, or widgets, in it. Can you show a more concrete example?

And... what is it you are actually trying to do? 

Is it a fltk widget type you want to template, or some other type? Since pretty much all fltk widgets derive from Fl_Widget you can generally use Fl_widget as a generic type to access any widget if you do not know it's actual type, for most things, so templating widgets is often not actually all that useful.
Of course, if it's some other type you are templating (not a fltk widget type) then this comment is irrelevant.



 

Bill Spitzak

unread,
Mar 31, 2022, 12:47:20 PM3/31/22
to fltkg...@googlegroups.com
I think what is wanted here is to define a Fl_Widget subclass in fluid that is a template. Fluid already allows subclasses to be defined.

Most likely this could be done by fixing fluid to not choke if the class name has angle brackets in it. And I forget what rules fluid had about arguments to the widget constructor, that may need changes.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/327282d9-0479-4891-91d4-69997a6e0f0fn%40googlegroups.com.

ed.vi...@gmail.com

unread,
Apr 1, 2022, 8:02:19 AM4/1/22
to fltk.general
Hello

It is nothing special. Subclasses do not matter. Below is an example showing fluid. I want the class Curveinfo to be templated. I have indicated a templated argument for the constructor and a templated variable for the class. I cannot template the class declaration like I want:

template<class T> CurveInfo{...}:

Yes there are workarounds and I appreciate the suggestions (I have one) but this would be a cleaner solution.

Cheers
Ed

template.png

Matthias Melcher

unread,
Apr 1, 2022, 4:44:31 PM4/1/22
to fltk.general
I have good access to Git at the moment. You can take a look at teh Fluid source code yourself though. In the file `Fl_Function_Type.cxx`, look for `void Fl_Class_Type::write_code1()`. I think we can add a string comparison there. If the class description starts with `template`, Fluid simply does not generate the `class` keyword anymore. I am no particular fan of this, but then again, Fluid does similar stuff everywhere (see `void Fl_Decl_Type::write_code1()` etc.).

If this solves your issue, and if the other devs are ok with it, I will addd this as soon as I have full access again.

 - Matthias

ed.vi...@gmail.com

unread,
Apr 2, 2022, 2:53:58 AM4/2/22
to fltk.general
If you are suggesting that in the "Name:" field of the class dialogue (a) you check for an initial template keyword, that would work.

You could also
b) check for a template declaration  immediately preceding a class
c) add a template input field to the class dialogue

IMHO
a) is the simplest to implement but is not immediately obvious to the user
c) is immediately understandable by the user but would require more work by the developers to save and restore the field
b) is between the two

Ed


Bill Spitzak

unread,
Apr 2, 2022, 3:48:37 PM4/2/22
to fltkg...@googlegroups.com
I am not very clear on what suggestion "b" is saying. However "a" is what I was imagining the solution to be. It might be best to look for various patterns, possibly as simple as having a space in the name of the class, and use that to stop it from putting "class " in front of the output. That would allow insertion of __attrbute__ and other such stuff.


--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.

ed.vi...@gmail.com

unread,
Apr 2, 2022, 4:49:36 PM4/2/22
to fltk.general
In fluid, you would have a declaration on one line

temple <class T>

and then the next line is the class

Reply all
Reply to author
Forward
0 new messages