Template Problem

Hi All Gurus,

I am facing one template realting problem.

void SetData(T data,long pos,VARENUM var)
{
SAFEARRAY * psa = rowset->parray;
VARIANT VarData;
VariantInit(&VarData);
VarData.vt = var;

VarData.lVal = data;

SafeArrayPutElement( psa, &pos, &VarData);
VariantClear(&VarData);
}

In the above code

VarData.lVal can be
VarData. bstrVal, for string
VarData.iVal for integer
etc.

So by using template How can I change Right hand side

Thanks for suggestion

Regards
[623 byte] By [sharda_arvind] at [2007-11-20 1:26:38]
# 1 Re: Template Problem
What do you want, actually?

Three things:
1. Use [CODE] tags when posting code.
2. Post relevant code to compile the stuff (or which is valid C++) that replicates your problem.
3. Be specific about your problem.

When you are using a template type - why do you want to put it into a VARIANT type?
exterminator at 2007-11-9 1:07:46 >
# 2 Re: Template Problem
Problem is I want to create a safearray of VARIANT.
It can contain Int,double,string and other type of variables.

So suppose I want to put an element into this safe array I will just pass the
position(where u want to put the element) and data (data that u want to store)to the function.

For this purpose previously i was using 3 different functions..like for Putting the integer function is

void SetInt(long pos, int data)
{
SAFEARRAY * psa = rowset->parray;
VARIANT VarData;
VariantInit(&VarData);
VarData.vt = VT_I4;
VarData.lVal = data;
SafeArrayPutElement( psa, &pos, &VarData);
VariantClear(&VarData);
}

For String

void SetString(long pos, String^ data)
{
SAFEARRAY * psa = rowset->parray;
VARIANT VarData;
VariantInit(&VarData);
VarData.vt = VT_BSTR;
VarData.bstrVal = String Value;
SafeArrayPutElement( psa, &pos, &VarData);
VariantClear(&VarData);
}

and similarly for other data types.
So I want to make template for these functions and now problem is

1. VarData.lVal = data;
2. VarData.bstrVal = String Value;

How can I change LHS ..As shown above in 1. .lVal and in 2. .bstrVal in my template.

Hope u understand the problem
sharda_arvind at 2007-11-9 1:08:52 >
# 3 Re: Template Problem
Stick with the overloading...
exterminator at 2007-11-9 1:09:47 >
# 4 Re: Template Problem
Overload VARIANT constructor(or factory if don't have access to VARIANT code) and use it inside templated method.
RoboTact at 2007-11-9 1:10:48 >
# 5 Re: Template Problem
Actually I have 8-9 functiions which are doing the same thing.Thats why I want to use Template..Overloading I am already using
sharda_arvind at 2007-11-9 1:11:55 >
# 6 Re: Template Problem
Actually I have 8-9 functiions which are doing the same thing.Thats why I want to use Template..Overloading I am already usingIf as I got from your code you have union+enumeration then anyway you have to specify somewhere which tag+union variant is to use with each type. What I suggest is extract this particular variation and code the rest in single place under template arguments.
RoboTact at 2007-11-9 1:12:59 >
# 7 Re: Template Problem
Actually I have 8-9 functiions which are doing the same thing.Thats why I want to use Template..Overloading I am already usingWell, if I get it right, the problem is that VARIANT is not a template... it is a union. So, there is no way to set the member VarData.vt to tell what the datatype is by getting type information from the T-typed data.

There can be hacks but it will make the code miserable. Try looking at boost::any and/or boost::variant.

A hack shown here:std::map<std::type_info, int> type_mappings;

//put the various members into it:
type_mappings[VT_BSTR] = typeid(std::string);
type_mappings[VY_I4] = typeid(int);
//etc - make sure the mapping are right - although I am not sure what type does BSTR would correspond to and if the
//MFC types have standard counter parts that you can use..

VARIANT helper_one()
{
SAFEARRAY * psa = rowset->parray;
VARIANT VarData;
VariantInit(&VarData);
return VarData;
}

void helper_two(long pos, VARIANT& VarData)
{
SafeArrayPutElement( psa, &pos, &VarData);
VariantClear(&VarData);
}

template<typename T>
void SetValue(long pos, const T& data)
{
helper_one();
VarData.vt = type_mappings[typeid(data)];
switch(type_mappings[typeid(data)])
{
case VT_STR:
VarData.bstrVal = data;
break;
case VT_I4:
VarData.IVal = data;
break:
//so on...
}
helper_two(pos);
}I think this is the best you can get out of the VARIANT type. About the map - I am very unsure how reliable it would be.. certainly this is not worth the way.. I think you are fine with the overload else re-think about using the VARIANT type itself.
exterminator at 2007-11-9 1:14:01 >
# 8 Re: Template Problem
I think this is not the solution because again I have to overload all these function.
sharda_arvind at 2007-11-9 1:14:57 >
# 9 Re: Template Problem
I think this is not the solution because again I have to overload all these function.Which functions?
RoboTact at 2007-11-9 1:15:54 >