BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using templates with VCL objects

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Kostya
Guest





PostPosted: Wed Sep 27, 2006 3:36 am    Post subject: using templates with VCL objects Reply with quote



Is it possible at all to use templates with VCL objects ?

For example:
if I design some form with bunch of VCL objects like TToolBar, TImage, etc..
and then I change it to:

template<class Type> class TImgForm : public TForm{
****
TArray Arr<Type>;
****
};

The code compiles, no problem. But, when I try to run program it gives me
error "Resource TImgForm not found."
My guess the problem is that during run time the program does not know how
to make a connection between the
resource : USEFORM("Img_Form.cpp", ImgForm); and the template object I made.
Does anybody know how to solve this problem?
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Sep 27, 2006 3:43 am    Post subject: Re: using templates with VCL objects Reply with quote



"Kostya" <pichuginky (AT) hotmail (DOT) com> wrote in message
news:4519abce$1 (AT) newsgroups (DOT) borland.com...

Quote:
Is it possible at all to use templates with VCL objects ?

That depends on the particular object, and whether the object has to
interface with the Delphi VCL at all.

Quote:
template<class Type> class TImgForm : public TForm{
****
TArray Arr<Type>;
****
};

That will not work. You cannot apply a template to any object that has to
deal with the DFM, such as forms. The class names will not match, and the
DFM will thus not be able to stream properly.

Quote:
when I try to run program it gives me error "Resource TImgForm not found."

Exactly, for the reason mentioned above. In your code, the form's class
name may be "TImgForm", and that will be the name that the Pascal side of
the VCL will be expecting, but that will not be the actual class name when
compiled.

Quote:
My guess the problem is that during run time the program does not
know how to make a connection between the resource :
USEFORM("Img_Form.cpp", ImgForm); and the template object
I made.

That is not the problem. In fact, if you are using BCB 6 or later, USEFORM
has no effect at all anymore, since the file references are in the BPR file
itself now.

Quote:
Does anybody know how to solve this problem?

You can't use templates with forms.


Gambit
Back to top
Kostya
Guest





PostPosted: Wed Sep 27, 2006 4:40 am    Post subject: Re: using templates with VCL objects Reply with quote



Ok. I got it. Thank you Gambit!

I am sort of a newbie in C++. Could you suggest me a solution how to deal
with a template members of a TForm child?

If I were writing in C style the solution would be:

typedef enum {INT,LONG,FLOAT,DOUBLE} TDataType;

void *Arr;
TDataType DataType;

In C++ I am tempting to use templates. Is there any analog void* for
template pointers?
I understand I could make a parent for TArray

class TBaseArr{

******

};

template <class T> TArray : public TBaseArr{
******
T operator [](int idx);
******
};

Then TBaseArr *Arr; will point to any TArray<int>, TArray<double>, etc...
The problem is that I have to do dynamic_cast<>() every time I need to use
[] function.


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4519ad9e$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Kostya" <pichuginky (AT) hotmail (DOT) com> wrote in message
news:4519abce$1 (AT) newsgroups (DOT) borland.com...

Is it possible at all to use templates with VCL objects ?

That depends on the particular object, and whether the object has to
interface with the Delphi VCL at all.

template<class Type> class TImgForm : public TForm{
****
TArray Arr<Type>;
****
};

That will not work. You cannot apply a template to any object that has to
deal with the DFM, such as forms. The class names will not match, and the
DFM will thus not be able to stream properly.

when I try to run program it gives me error "Resource TImgForm not
found."

Exactly, for the reason mentioned above. In your code, the form's class
name may be "TImgForm", and that will be the name that the Pascal side of
the VCL will be expecting, but that will not be the actual class name when
compiled.

My guess the problem is that during run time the program does not
know how to make a connection between the resource :
USEFORM("Img_Form.cpp", ImgForm); and the template object
I made.

That is not the problem. In fact, if you are using BCB 6 or later,
USEFORM
has no effect at all anymore, since the file references are in the BPR
file
itself now.

Does anybody know how to solve this problem?

You can't use templates with forms.


Gambit

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Sep 27, 2006 10:25 pm    Post subject: Re: using templates with VCL objects Reply with quote

"Kostya" <pichuginky (AT) hotmail (DOT) com> wrote in message
news:4519bade$1 (AT) newsgroups (DOT) borland.com...

Quote:
I am sort of a newbie in C++.

This is not a C++ problem. If you were coding in straight C++ only, then
you could use the template just fine. But you are trying to apply the
template to a VCL TForm, and the VCL is written in Delphi Pascal, which
means the TForm has to be compatible with the Delphi side of the VCL. The
template will not allow you to do easily. About the only way to make it
work at all is to never call TApplication::CreateForm() (which you have to
do at least once to establish the TApplication::MainForm property, which
means the MainForm can't be templated), and to use the dummy constructor of
TForm so that you can bypass the native DFM streaming and then manually
stream in the DFM yourself. That way, you can bypass the class name
restriction on it.

Quote:
If I were writing in C style the solution would be:

The simpliest solution to this problem would be to just keep using that
approach. Otherwise, you would have to re-design your code so that the
TArray is not a direct member of the TForm class so that it would not have
to be templated anymore. Maybe make the TImgForm class be a base class for
additional descendants, each one with their own specialization of the TArray
class as their member. Then TImgForm could have virtual methods that the
descendant forms override when working with the array.

Quote:
In C++ I am tempting to use templates. Is there any analog
void* for template pointers?

No, there is not. Since you can't apply a template to a TForm itself, you
cannot have data members that need that template, as there would be no way
to specify the template parameter. You can have templated methods, though.

Quote:
Then TBaseArr *Arr; will point to any TArray<int>, TArray<double>,
etc... The problem is that I have to do dynamic_cast<>() every time I
need to use [] function.

You should be using static_cast instead of dynamic_cast. The code knows
which TArray type is allocted, so it can cast directly to that type when
accessing the array.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.