casting pointer to template type
Hello.
I have another circular dependency issue, so was trying to handle by changing the variable access, to pointer type.
But the pointer is for templated class.
Basically i had
Code:
OnePixel<float> fraster(pfVal);
RenderFuncParams rfp;
rfp.displayDataType = eDDT;
rfp.pInstance = pInstance;
rfp.pfOut = &fraster;
when i got circular dependency issue. I tried the following
float val = 0.0;
OnePixel<float> * pfraster = &val;
obviously gives error. But
OnePixel<float> * pfraster = static_cast<(OnePixel<float> *)>(&val);
even gives error " error C2681: 'float *': invalid expression type for dynamic_cast
thanks a lot
pdk
Re: casting pointer to template type
You can't use static_cast on pointers. Use reinterpret_cast.
Re: casting pointer to template type
Thankyou very much kaud !!!. Yes it compiled like magic :)
I was some short break and now back to work and i feel like i have forgotten little bit c++ i knew..
thanks a lot again.. i am sorry for asking basic qs..but your patience is always appreciated.
Re: casting pointer to template type
Note though, that this isn't consider 'good practice'. Usually reinterpret_cast highlights some design issue and such code is often considered 'flakey' and 'fragile'. Use at own risk!
Re: casting pointer to template type
Thanks a lot kaud. Yes, i will check with my collegues about this.
But now, Im facing issue further on: somehow, the passed reinterpret cast pointer is not able to be written
Actually, i am casting to abstract class pointer , is it a issue ?
Code:
Function()
{
float val = 0;
// TODO_PRIYA_FWA: Following line needs 3gRaster.h file to be included. This results in circular dependency
// So use the pointer type (by forward declaring), to avoid this issue.
//OnePixel<float>* pfraster(pfVal);
SimOutputRaster<float> * pfraster = reinterpret_cast<SimOutputRaster<float> *>(&val);
RenderFuncParams rfp;
rfp.displayDataType = DISPLAYDATA_RSRP_5g;
rfp.pInstance = pInstanceSim;
rfp.pfOut = pfraster;
rfp.piOut = nullptr;
rfp.numPixels = 1;
rfp.SetUseMask(false);
rfp.SetAssignColours(false);
rfp.ulStartPoint = rFwaPoint.GetPixelIndex();
rfp.ulEndPoint = rFwaPoint.GetPixelIndex() + 1;
// Call RenderNewSimOutput, to get the required value at the FWA point
pOM->RenderNewSimOutput(rfp);
}
bool OutputManager::RenderNewSimOutput(RenderFuncParams& rfp)
{
const bool bSuccess = pSim->DoNewTerminalCalculation(i, pTermType, ¶ms, rfp.displayDataType,
bUseArraySettings, bIgnoreThresholds, inCellIndexWrtPixel, bSkipTrafficCheck,
dOut, iOut, outInfo, outCs, pPos);
if ( rfp.pfOut ) (*rfp.pfOut)[iOutIndex] = float(dOut);
}
I get the exception thrown at the red line.
I tried to chekc the pointer value , is coming as 0x00000000006fe6c4, before passing to the function.
At the exception, i tried to check, the pointer value is still the same .
But
Exception thrown : read access violation
rfp.pfOut-> was 0xFFFFFFFFFFFFFFFF
rfp.pfOut 0x00000000006fe6c4 {...}
rfp.pfOut->__vfptr 0xcccccccc00000000 {???, ???, ???, ???, ???, ???, ???, ???}
[0] = <Unable to read memory>
Actually, i checked somewhere in other places where they pass pointer of type "OnePixel" instead of SimOutputRaster
Actually the following is abstract class
template <class T>
class SimOutputRaster
{
public:
virtual T& operator[](unsigned __int64 idx) = 0;
//These have an implementation which won't be used
virtual T GetIndexFor(int iCellKey, int iAntKey = 0, int iCarrierKey = 0) {return T(); };
virtual void FillIndex(T /*iIndex*/, LPCTSTR /*pStr*/, COLORREF /*cr*/, int iCellSimIndex = 0, TgNetType netType = TGNETWORK_UNKNOWN, int iStaticEnumColourIndex = -1) { iCellSimIndex; };
virtual T GetNumEntries() const {return T();};
virtual void EndFillIndex() {};
virtual int GetCellKey(T /*iIndex*/) const { return 0; };
virtual int GetAntKey(T /*iIndex*/) const { return 0; };
virtual int GetCarrierKey(T /*iIndex*/) const { return 0; };
};
Re: casting pointer to template type
Simply, rfp.pfOut is a pointer to a float (val) which is a single variable and not an array. But you are treating rtp.pfOut as if it's an array pointer whn it's not. Hence the issue.
reinterpret_cast<> is the surest way of shooting yourself in your left foot whilst pointing the gun at your right foot...
Re: casting pointer to template type
Thanks a lot kaud..
Im seeing somewhere else in legacy code, (for getting int value)
int iVal = 0;
OnePixel<int> iraster(&iVal);
RenderFuncParams rfp;
rfp.displayDataType = eDDT;
rfp.pInstance = pInstance;
rfp.pfOut = nullptr;
rfp.piOut = &iraster;
where piOut is just single value, not an array
Re: casting pointer to template type
That just says that the pfOut pointer is set to nullptr - ie set to 0 ie not used/specified. Hence the test:
which checks for the nullptr condition.
Re: casting pointer to template type
i.e true, because output can be int or float..
if out is int, rfp.piOut is used
if float rfp.pfOut is used
in our case it is float..(not array)
Re: casting pointer to template type
Quote:
in our case it is float..(not array)
...but you're indexing rfp.pfOut via iOutIndex as if it's an array...
Re: casting pointer to template type
thanks a lot kaud. But the function im calling (RenderNewSimOutput) is legacy
in my case, iOutIndex will just be "0", as its just one pixel
Code:
bool OutputManager::RenderNewSimOutput(RenderFuncParams& rfp)
{
for (INT_PTR i = 0, iOutIndex = 0, iProgressIndex = rfp.ulStartPoint;
iProgressIndex < rfp.ulEndPoint; //End condition
iProgressIndex++) //Increment operation
{
const bool bSuccess = pSim->DoNewTerminalCalculation(i, pTermType, ¶ms, rfp.displayDataType,
bUseArraySettings, bIgnoreThresholds, inCellIndexWrtPixel, bSkipTrafficCheck,
dOut, iOut, outInfo, outCs, pPos);
if ( rfp.pfOut ) (*rfp.pfOut)[iOutIndex] = float(dOut);
}
Re: casting pointer to template type
Thanks a lot kaud, there was function i could call to get the output array. And that i passed and now it works ok !!.