Why is DECLARE_DYNAMIC & IMPLEMENT_DYNAMIC nessary for DYNAMIC_DOWNCAST? - c++

I have two classes:
/*Switch.h*/
class CSwitch : public CDeviceEntity {}
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity {}
but when I use:
CDeviceEntity* dev = NULL;
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL ) {}
"DYNAMIC_DOWNCAST" always returns not NULL while dev is kind of class CEndSystem or class CSwitch.
If use:
/*Switch.h*/
class CSwitch : public CDeviceEntity { DECLARE_DYNAMIC(CSwitch) }
and
/*Switch.cpp*/
IMPLEMENT_DYNAMIC(CSwitch, CDeviceEntity)
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity { DECLARE_DYNAMIC(CEndSystem) }
and
/*EndSystem.cpp*/
IMPLEMENT_DYNAMIC(CEndSystem, CDeviceEntity)
"DYNAMIC_DOWNCAST" returns NULL or not NULL according to class CEndSystem or class CSwitch.
Why "DECLARE_DYNAMIC" and "IMPLEMENT_DYNAMIC" are nessary for "DYNAMIC_DOWNCAST"?
/*Algorithm.h*/
static int getESNum();
/*Algorithm.cpp*/
int CAlgorithm::getESNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL )
{
count++;
}
}
return count;
}
/*Algorithm.h*/
static int getSWNum();
/*Algorithm.cpp*/
int CAlgorithm::getSWNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST(CSwitch, dev) != NULL )
{
count++;
}
}
return count;
}
And the functions are called in serialization when saving the document.

DYNAMIC_DOWNCAST is a throw back to how you used to have to do dynamic casting before RTTI information was available from the compiler. The casting information is created using the macros DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC which use the class CRuntimeClass to decide if the cast is valid.
DYNAMIC_DOWNCAST simply does this:
CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject)
{
if (pObject != NULL && pObject->IsKindOf(pClass))
return pObject;
else
return NULL;
}
The DECLARE_DYNAMIC macro adds this code:
#define DECLARE_DYNAMIC(class_name) \
protected: \
static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
static const CRuntimeClass class##class_name; \
static CRuntimeClass* PASCAL GetThisClass(); \
virtual CRuntimeClass* GetRuntimeClass() const; \
Add IMPLEMENT_DYNAMIC adds this code:
#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew, class_init) \
CRuntimeClass* PASCAL class_name::_GetBaseClass() \
{ return RUNTIME_CLASS(base_class_name); } \
AFX_COMDAT const CRuntimeClass class_name::class##class_name = { \
#class_name, sizeof(class class_name), wSchema, pfnNew, \
&class_name::_GetBaseClass, NULL, class_init }; \
CRuntimeClass* PASCAL class_name::GetThisClass() \
{ return _RUNTIME_CLASS(class_name); } \
CRuntimeClass* class_name::GetRuntimeClass() const \
{ return _RUNTIME_CLASS(class_name); }
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL, NULL)
I imagine few people still use this for new projects, instead preferring the C++ standard dynamic_cast<> call (along with static_cast and reinterpret_cast).

Related

Passing a TForm as an argument to a function

I have an application with several Forms. Two of them are quite similar, they have features in the form of VCL objects (labels, images, etc...) in common, which I named the same.
I want to have a function in a specific class which can accept one of these two Form as a parameter in order to modify the parameters that they have in common. The solution I came around does not seem to work.
As my application is quite big and complicated, I replicated the problem using a small example.
First, below is an example of my MainForm :
And an example of one subForm (they are all arranged in a similar way)
I have an additionnal class which is used to fill in the Edits on the subForms. The code for this class is the following:
#pragma hdrstop
#include "master_class.h"
#include "sub_Form2.h"
#include "sub_Form3.h"
#include "sub_Form4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
Master::Master(void)
{
}
Master::~Master(void)
{
}
void Master::WriteToForm(TForm* Form)
{
TForm2* curForm = static_cast<TForm2*>(Form);
TForm3* self = dynamic_cast<TForm3*>(Form);
TForm2* self2 = dynamic_cast<TForm2*>(Form);
if (self != NULL && self2 == NULL) {
TForm3* curForm = static_cast<TForm3*>(Form);
}
else if (self == NULL && self2 != NULL) {
TForm2* curForm = static_cast<TForm2*>(Form);
}
curForm -> Edit1 -> Text = "blablabla_1";
curForm -> Edit2 -> Text = "blablabla_2";
}
And in the MainForm, the code for the "Fill Form2" button is the following:
Master1 -> WriteToForm(Form2);
where Master1 is just an object of the Master class.
This works very well for Form2 :
But for Form3, which is filled up using Master1 -> WriteToForm(Form3), here is what I get, which the same pb than in my real application:
So what should go to the Edit, is misplaced. I think the main pb comes from the fact that I did not create every label, edit, etc... on the same order. I did that on purpose to mimic my real application. To verify this, I created a 3rd subForm, where this time the VCL objects were created in the same order as my first subForm, and this works:
So I would suspect that this comes from the initial cast
TForm2* curForm = static_cast<TForm2*>(Form);
When I pass Form3 as an argument, Form3 is somewhat casted into the "shape" of Form2, which is not defined in the same order. Maybe this could be corrected by modifying directly the DFM file, but it is not a realistic approach for my main app.
I do this initial cast otherwise I get a compilation error saying that curForm is not known at the first line
curForm -> Edit1 -> Text = "blablabla_1";
So, is there a better way to pass the Form as an argument to the WriteToForm function?
Just because two types are similar does not mean they are related. Your code does not work because your two Form classes are not related to each other in any way. You can't just cast one to the other arbitrarily.
To solve this, you have several options:
code for both Form classes separately, eg:
void Master::WriteToForm(TForm* Form)
{
TForm2* curForm2 = dynamic_cast<TForm2*>(Form);
TForm3* curForm3 = dynamic_cast<TForm3*>(Form);
if (curForm2)
{
curForm2->Edit1->Text = _D("blablabla_1");
curForm2->Edit2->Text = _D("blablabla_2");
}
else if (curForm3)
{
curForm3->Edit1->Text = _D("blablabla_1");
curForm3->Edit2->Text = _D("blablabla_2");
}
}
Or:
void WriteToForm(TForm2* Form);
void WriteToForm(TForm3* Form);
...
void Master::WriteToForm(TForm2* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
void Master::WriteToForm(TForm3* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
Make your function use a template (however, be aware of this: Why can templates only be implemented in the header file?):
template<typename T>
void WriteToForm(T* Form);
...
void Master::WriteToForm<T>(T* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
make the two Form classes derive from a common base class or interface, eg:
class TBaseForm : public TForm
{
public:
inline __fastcall TBaseForm(TComponent *Owner) : TForm(Owner) {}
virtual void SetEdit1(const String &Text) = 0;
virtual void SetEdit2(const String &Text) = 0;
};
...
class TForm2 : public TBaseForm
{
...
public:
__fastcall TForm2(TComponent *Owner);
...
void SetEdit1(const String &NewText);
void SetEdit2(const String &NewText);
};
__fastcall TForm2::TForm2(TComponent *Owner)
: TBaseForm(Owner)
{
...
}
void TForm2::SetEdit1(const String &NewText)
{
Edit1->Text = NewText;
}
void TForm2::SetEdit2(const String &NewText)
{
Edit2->Text = NewText;
}
...
repeat for TForm3...
...
void Master::WriteToForm(TBaseForm* Form)
{
Form->SetEdit1(_D("blablabla_1"));
Form->SetEdit2(_D("blablabla_2"));
}
Or:
__interface INTERFACE_UUID("{E900785E-0151-480F-A33A-1F1452A431D2}")
IMyIntf : public IInterface
{
public:
virtual void SetEdit1(const String &Text) = 0;
virtual void SetEdit2(const String &Text) = 0;
};
...
class TForm2 : public TForm, public IMyIntf
{
...
public:
__fastcall TForm2(TComponent *Owner);
...
void SetEdit1(const String &NewText);
void SetEdit2(const String &NewText);
};
__fastcall TForm2::TForm2(TComponent *Owner)
: TForm(Owner)
{
...
}
void TForm2::SetEdit1(const String &NewText)
{
Edit1->Text = NewText;
}
void TForm2::SetEdit2(const String &NewText)
{
Edit2->Text = NewText;
}
...
repeat for TForm3...
...
void Master::WriteToForm(IMyIntf* Intf)
{
Intf->SetEdit1(_D("blablabla_1"));
Intf->SetEdit2(_D("blablabla_2"));
}
use RTTI to access the fields, eg:
#include <System.Rtti.hpp>
void Master::WriteToForm(TForm* Form)
{
TRttiContext Ctx;
TRttiType *FormType = Ctx.GetType(Form->ClassType());
TRttiField *Field = FormType->GetField(_D("Edit1"));
if (Field)
{
TValue value = Field->GetValue(Form);
if( (!value.Empty) && (value.IsObject()) )
{
TObject *Obj = value.AsObject();
// Either:
static_cast<TEdit*>(Obj)->Text = _D("blablabla_1");
// Or:
TRttiProperty *Prop = Ctx.GetType(Obj->ClassType())->GetProperty(_D("Text"));
if (Prop) Prop->SetValue(Obj, String(_D("blablabla_1")));
}
}
Field = FormType->GetField(_D("Edit2"));
if (Field)
{
TValue value = Field->GetValue(Form);
if( (!value.Empty) && (value.IsObject()) )
{
TObject *Obj = value.AsObject();
// Either:
static_cast<TEdit*>(Obj)->Text = _D("blablabla_2");
// Or:
TRttiProperty *Prop = Ctx.GetType(Obj->ClassType())->GetProperty(_D("Text"));
if (Prop) Prop->SetValue(Obj, String(_D("blablabla_2")));
}
}
}

Hooking virtual method on Win32 (return object larger than ptr crashes)?

So trying to hook a OpenVR method "ITrackedDeviceServerDriver::GetPose" but i'm getting access violations when my custom hooked method returns out (I think something is wrong with calling convention but idk enough about what x64 asm expects).
All other methods I hook in "ITrackedDeviceServerDriver" work fine. Only "GetPose" fails.
Class thats being hooked looks like:
class ITrackedDeviceServerDriver
{
public:
virtual EVRInitError Activate( uint32_t unObjectId ) = 0;
virtual void Deactivate() = 0;
virtual void EnterStandby() = 0;
virtual void *GetComponent( const char *pchComponentNameAndVersion ) = 0;
virtual void DebugRequest( const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize ) = 0;
virtual DriverPose_t GetPose() = 0;
};
Object thats larger than ptr & gets returned from method that crashes
struct DriverPose_t
{
double poseTimeOffset;
vr::HmdQuaternion_t qWorldFromDriverRotation;
double vecWorldFromDriverTranslation[ 3 ];
vr::HmdQuaternion_t qDriverFromHeadRotation;
double vecDriverFromHeadTranslation[ 3 ];
double vecPosition[ 3 ];
double vecVelocity[ 3 ];
double vecAcceleration[ 3 ];
vr::HmdQuaternion_t qRotation;
double vecAngularVelocity[ 3 ];
double vecAngularAcceleration[ 3 ];
ETrackingResult result;
bool poseIsValid;
bool willDriftInYaw;
bool shouldApplyHeadModel;
bool deviceIsConnected;
};
Example of how I'm hooking the virtual methods:
typedef DriverPose_t(__thiscall* GetPose_Org)(ITrackedDeviceServerDriver* thisptr);
GetPose_Org GetPose_Ptr = nullptr;
DriverPose_t __fastcall GetPose_Hook(ITrackedDeviceServerDriver* thisptr)
{
DriverPose_t result = GetPose_Ptr(thisptr);// works
//result.deviceIsConnected = true;
return result;// after return I get access violation crash
}
void TestHook(ITrackedDeviceServerDriver *pDriver)
{
MEMORY_BASIC_INFORMATION mbi;
ZeroMemory(&mbi, sizeof(MEMORY_BASIC_INFORMATION));
void** vTable = *(void***)(pDriver);
VirtualQuery((LPCVOID)vTable, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
VirtualProtect(mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect);// unlock
Activate_Ptr = (Activate_Org)vTable[0];
vTable[0] = &Activate_Hook;// Hook!
Deactivate_Ptr = (Deactivate_Org)vTable[1];
vTable[1] = &Deactivate_Hook;// Hook!
EnterStandby_Ptr = (EnterStandby_Org)vTable[2];
vTable[2] = &EnterStandby_Hook;// Hook!
GetPose_Ptr = (GetPose_Org)vTable[5];
vTable[5] = &GetPose_Hook;// Hook!
VirtualProtect(mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect);// lock
}
So why is "GetPose" the only method that fails after it returns out?
How can I fix the calling convention on "GetPose" to work with registers correctly?
as already noted in comments
__thiscall and __fastcall are not the same calling convention
even for x64. for x86 this is obvious - __thiscall - pass first parameter (this) via Ecx register, and next parameters via stack (i skip now float/double parameters case). __fastcall - 1-first via Ecx, 2-second via Edx, and next via stack
in case x64 (again let assume that no float/double parameters) - first 4 parameters via Rcx, Rdx, R8, R9 and next in stack. so look like x64 have single and common calling convention (no difference between __stdcall, __fastcall, __cdecl, __thiscall)
but exist question about implicit parameters. say in case __thiscall - the first parameter - this is implicit parameter, always passed as first parameter via Ecx/Rcx
and now case when function try "return" object. really on any platform function can return something only via cpu registers. it finite count, and only several of it can be used for return value store.
say on x86(x64) - for return value can be used: AL, AX, EAX, RAX, DAX:EAX, RDX:RAX
so function can return only something that have size - 1, 2, 4, 8 (and 16 for x64)
if function try "return" object with another size - this is impossible do direct, compiler need transform your code. for example if you write function:
DriverPose_t GetPose(ITrackedDeviceServerDriver* thisptr)
{
DriverPose_t pos = ...;
return pos;
}
and call
DriverPose_t pos = GetPose(thisptr);
compiler (of course this already implementation details, different compilers can do this in different ways !) can transform this to
void GetPose(DriverPose_t *ppos, ITrackedDeviceServerDriver* thisptr)
{
DriverPose_t pos = ...;
*ppos = pos;
}
and call to
DriverPose_t pos;
GetPose(&pos, thisptr);
so here pointer to object passed to function as first ,hidden/implicit, parameter. but in case member function, still exist another hidden/implicit, parameter - this, which always passed as first, as result hidden/implicit, parameter for return value moved to second place !
struct ITrackedDeviceServerDriver
{
DriverPose_t GetPose()
{
DriverPose_t pos = ...;
return pos;
}
};
and call
ITrackedDeviceServerDriver obj;
DriverPose_t pos = obj.GetPose();
can be transformed to
struct ITrackedDeviceServerDriver
{
void GetPose(DriverPose_t *ppos)
{
DriverPose_t pos = ...;
*ppos = pos;
}
};
and call to
ITrackedDeviceServerDriver obj;
DriverPose_t pos;
obj.GetPose(&pos);
so real signature of function, if "uncover" this parameter is
void ITrackedDeviceServerDriver::GetPose(ITrackedDeviceServerDriver* this, DriverPose_t *ppos)
{
DriverPose_t pos = ...;
*ppos = pos;
}
and call
ITrackedDeviceServerDriver obj;
DriverPose_t pos;
GetPose(&obj, &pos);
so compare
void ITrackedDeviceServerDriver::GetPose(ITrackedDeviceServerDriver* this, DriverPose_t *ppos);
and
void GetPose(DriverPose_t *ppos, ITrackedDeviceServerDriver* thisptr);
you implement
DriverPose_t GetPose_Hook(ITrackedDeviceServerDriver* thisptr);
which will be transformed by compiler to
void GetPose_Hook(DriverPose_t* ,ITrackedDeviceServerDriver* thisptr);
but really you need implement next hook:
void GetPose_Hook(ITrackedDeviceServerDriver* thisptr, DriverPose_t* );
you confuse first and second parameters. however i think not need change object vtable at all, instead need return proxy object to client. possible and very generic implementation for COM interface hooks, but it too long for post here. for your case can be done next example of hook:
struct DriverPose_t
{
int x, y, z;
};
struct __declspec(novtable) IDemoInterface
{
virtual DriverPose_t GetPose() = 0;
virtual ULONG GetComponent( const char * pchComponentNameAndVersion ) = 0;
virtual void Delete() = 0;
};
class DemoObject : public IDemoInterface
{
ULONG v;
DriverPose_t pos;
virtual DriverPose_t GetPose()
{
DbgPrint("%s<%p>\n", __FUNCTION__, this);
return pos;
}
virtual ULONG GetComponent( const char * pchComponentNameAndVersion )
{
DbgPrint("%s<%p>(%s)\n", __FUNCTION__, this, pchComponentNameAndVersion);
return v;
}
virtual void Delete()
{
delete this;
}
public:
DemoObject(ULONG v, int x, int y, int z) : v(v)
{
pos.x = x, pos.y = y, pos.z = z;
DbgPrint("%s<%p>\n", __FUNCTION__, this);
}
virtual ~DemoObject()
{
DbgPrint("%s<%p>\n", __FUNCTION__, this);
}
};
class Hook_DemoInterface : public IDemoInterface
{
IDemoInterface* pItf;
virtual DriverPose_t GetPose()
{
DriverPose_t pos = pItf->GetPose();
DbgPrint("%s<%p>=<%d, %d, %d>\n", __FUNCTION__, this, pos.x, pos.y, pos.z);
return pos;
}
virtual ULONG GetComponent( const char * pchComponentNameAndVersion )
{
ULONG v = pItf->GetComponent(pchComponentNameAndVersion);
DbgPrint("%s<%p>(%s)=%u\n", __FUNCTION__, this, pchComponentNameAndVersion);
return v;
}
virtual void Delete()
{
delete this;
}
public:
Hook_DemoInterface(IDemoInterface* pItf) : pItf(pItf)
{
DbgPrint("%s<%p>\n", __FUNCTION__, this);
}
~Hook_DemoInterface()
{
pItf->Delete();
DbgPrint("%s<%p>\n", __FUNCTION__, this);
}
};
BOOL CreateDemoIface(IDemoInterface** ppItf)
{
if (DemoObject* pObj = new DemoObject (1, -1, 2, 3))
{
*ppItf = pObj;
return TRUE;
}
return FALSE;
}
BOOL Hook_CreateDemoIface(IDemoInterface** ppItf)
{
IDemoInterface* pItf;
if (CreateDemoIface(&pItf))
{
if (Hook_DemoInterface* pObj = new Hook_DemoInterface(pItf))
{
*ppItf = pObj;
return TRUE;
}
*ppItf = pItf;
return TRUE;
}
return FALSE;
}
void UseIface(IDemoInterface* pItf)
{
ULONG v = pItf->GetComponent("some string");
DriverPose_t pos = pItf->GetPose();
DbgPrint("v=%u, pos<%d, %d, %d>\n", v, pos.x, pos.y, pos.z);
}
void test()
{
IDemoInterface* pItf;
if (CreateDemoIface(&pItf))
{
UseIface(pItf);
pItf->Delete();
}
if (Hook_CreateDemoIface(&pItf))
{
UseIface(pItf);
pItf->Delete();
}
}
instead hook object vtable - hook (if possible) object creation CreateDemoIface - replace it to self Hook_CreateDemoIface which return proxy object.
debug output
DemoObject::DemoObject<0000012C31E08F70>
DemoObject::GetComponent<0000012C31E08F70>(some string)
DemoObject::GetPose<0000012C31E08F70>
v=1, pos<-1, 2, 3>
DemoObject::~DemoObject<0000012C31E08F70>
DemoObject::DemoObject<0000012C31E08F70>
Hook_DemoInterface::Hook_DemoInterface<0000012C31DFAA10>
DemoObject::GetComponent<0000012C31E08F70>(some string)
Hook_DemoInterface::GetComponent<0000012C31DFAA10>(some string)=1
DemoObject::GetPose<0000012C31E08F70>
Hook_DemoInterface::GetPose<0000012C31DFAA10>=<-1, 2, 3>
v=1, pos<-1, 2, 3>
DemoObject::~DemoObject<0000012C31E08F70>
Hook_DemoInterface::~Hook_DemoInterface<0000012C31DFAA10>

Getting the address of a class member fuction

How do I get the memory address of a class member function, I'm using a library statically that was previously setup dynamically, as the platform I ported the game to does not support dynamic libraries. It works well except for one issue.
When restoring a saved game NPCs become static as they don't continue to run the functions that were active when the game was saved. It did this by looking up the function address to get the name in the global offset table and symbol table when saving, when restoring it got the address using the name from the GOT and ST.
As this will not work with static linking I'm trying to figure out how to get the address of the exported functions to store it in a std::map with the address and a unique name when the application first starts.
The following doesn't seems to work for me for some reason. Any help would be much appreciated :).
Storing the address for the save game file in the base class using the DEFINE_FIELD macro:
// Global Savedata for Delay
TYPEDESCRIPTION CBaseEntity::m_SaveData[] =
{
DEFINE_FIELD( CBaseEntity, m_pfnThink, FIELD_FUNCTION )
};
Derived class export from when it was using dynamic linking:
extern "C" _declspec( dllexport ) void CMultiManager( entvars_t *pev );
Derived class where ManagerThink() was exported previously when it used dynamic linking:
class CMultiManager : public CBaseEntity
{
public:
CMultiManager();
void /*EXPORT*/ ManagerThink ( void );
}
My new class constructor in the derived class to try and get the address of the member function (so I can store it in std:map with a name). Is it possible to this in the global scope rather than a constructor?
CMultiManager::CMultiManager()
{
//Try the ManagerThink function directly
void (CBaseEntity ::*ptrTemp)(void);
ptrTemp = static_cast <void (CBaseEntity::*)(void)> (ManagerThink);
void* iMemoryAddressFunc = &ptrTemp;
ALERT( at_error, "__TEST__ FUCNTION __ EXPORT ADDRESS: CMultiManager::ManagerThink (%08lx)\n", (unsigned long)iMemoryAddressFunc );
//-------------------------------------------------------------------------------
//Try the inherited m_pfnThink variable ManagerThink() is stored in as well.
void* iMemoryAddressVar = &m_pfnThink;
ALERT( at_error, "__TEST__ M_PFNTHINK __ EXPORT ADDRESS:
CMultiManager::ManagerThink (%08lx)\n", (unsigned long)iMemoryAddressVar );
}
Function check method called before restoring the game, it's using the function address stored in the save game data in the base class (function above).
void FunctionCheck( void *pFunction, char *name )
{
if (pFunction && !NAME_FOR_FUNCTION((unsigned long)(pFunction)) )
ALERT( at_error, "No EXPORT: %s:%s (%08lx)\n", STRING(pev->classname), name, (unsigned long)pFunction );
}
Debug log when running the game:
Obviously it's not going to find the export as I haven't setup anything yet as I need the address to store first, but why do the addresses not all match up? ManagerThink() has been assigned to m_pfnThink as well, so why are the first two in the log not at the same address at least?
From CMultiManager():
__TEST__ FUCNTION __ EXPORT ADDRESS: CMultiManager::ManagerThink (d008759c)
__TEST__ M_PFNTHINK __ EXPORT ADDRESS: CMultiManager::ManagerThink (01eb0e08)
From FunctionCheck();
No EXPORT: multi_manager:ManagerThink (00226c0a)
The ManagerThink() Function:
// Designers were using this to fire targets that may or may not exist --
// so I changed it to use the standard target fire code, made it a little simpler.
void CMultiManager :: ManagerThink ( void )
{
float time;
time = gpGlobals->time - m_startTime;
while ( m_index < m_cTargets && m_flTargetDelay[ m_index ] <= time )
{
FireTargets( STRING( m_iTargetName[ m_index ] ), m_hActivator, this, USE_TOGGLE, 0 );
m_index++;
}
if ( m_index >= m_cTargets )// have we fired all targets?
{
SetThink( NULL );
if ( IsClone() )
{
UTIL_Remove( this );
return;
}
SetUse ( ManagerUse );// allow manager re-use
}
else
pev->nextthink = m_startTime + m_flTargetDelay[ m_index ];
}
I'm also confused how it knew when setup dynamically which EXPORT ManagerThink ( void ); function to call for each instance of the object created, because it would have just stored the one EXPORT address in the GOT/symbol table right?
Any help/suggestions/advise would be great. Thank you :)
EDIT:
Thanks for the replies. I've managed to find a work around the issue.
I looked at a newer version of game where they updated the code to not use the GOT and ST as they needed the game on platforms that don't support dlls and were faced with the same issue. They solved it using the following macros to declare a structure and store defined pointers to member functions in the structure.
//-----------------------------------------------------------------------------
//
// Macros used to implement datadescs
//
#define DECLARE_SIMPLE_DATADESC() \
static datamap_t m_DataMap; \
static datamap_t *GetBaseMap(); \
template <typename T> friend void DataMapAccess(T *, datamap_t **p); \
template <typename T> friend datamap_t *DataMapInit(T *);
#define BEGIN_SIMPLE_DATADESC( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetBaseMap() { return NULL; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_SIMPLE_DATADESC_( className, BaseClass ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((BaseClass *)NULL, &pResult); return pResult; } \
BEGIN_DATADESC_GUTS( className )
#define DECLARE_DATADESC() \
DECLARE_SIMPLE_DATADESC() \
virtual datamap_t *GetDataDescMap( void );
#define BEGIN_DATADESC_NO_BASE( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
datamap_t *className::GetBaseMap() { return NULL; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_DATADESC( className ) \
datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((CBaseEntity *)NULL, &pResult); return pResult; } \
BEGIN_DATADESC_GUTS( className )
#define BEGIN_DATADESC_GUTS( className ) \
template <typename T> datamap_t *DataMapInit(T *); \
template <> datamap_t *DataMapInit<className>( className * ); \
namespace className##_DataDescInit \
{ \
datamap_t *g_DataMapHolder = DataMapInit( (className *)NULL ); /* This can/will be used for some clean up duties later */ \
} \
\
template <> datamap_t *DataMapInit<className>( className * ) \
{ \
typedef className classNameTypedef; \
static CDatadescGeneratedNameHolder nameHolder(#className); \
className::m_DataMap.baseMap = className::GetBaseMap(); \
static typedescription_t dataDesc[] = \
{ \
{ FIELD_VOID, 0, 0, 0, 0 }, /* so you can define "empty" tables */
#define END_DATADESC() \
}; \
\
if ( sizeof( dataDesc ) > sizeof( dataDesc[0] ) ) \
{ \
classNameTypedef::m_DataMap.dataNumFields = SIZE_OF_ARRAY( dataDesc ) - 1; \
classNameTypedef::m_DataMap.dataDesc = &dataDesc[1]; \
} \
else \
{ \
classNameTypedef::m_DataMap.dataNumFields = 1; \
classNameTypedef::m_DataMap.dataDesc = dataDesc; \
} \
return &classNameTypedef::m_DataMap; \
}
// replaces EXPORT table for portability and non-DLL based systems (xbox)
#define DEFINE_FUNCTION_RAW( function, func_type ) { FIELD_VOID, nameHolder.GenerateName(#function), /*{ NULL, NULL },*/ 1, FTYPEDESC_FUNCTIONTABLE, /*NULL, NULL,*/ (inputfunc_t)((func_type)(&classNameTypedef::function))},
//------------------------------------------------------------------------------

Integration of a singly linked tail queue support in C++

I'm currently trying to integrate an ASN.1 decoder code written in C to the C++ Builder XE6 environment. I've encountered some problems while using the Singly linked tail queue support provided by the library, with the following call :
asn1p_wsyntx_chunk_t *wc;
while((wc = TQ_REMOVE(&(wx->chunks), next)))
asn1p_wsyntx_chunk_free(wc);
The error that I get is :
member reference type 'int' is not a pointer
The definitions of asn1p_wsyntx_chunk_t (wc) and asn1p_wsyntx_t (wx) are :
typedef struct asn1p_wsyntx_chunk_s {
enum {
WC_LITERAL,
WC_WHITESPACE,
WC_FIELD,
WC_OPTIONALGROUP
} type;
/*
* WC_LITERAL -> {token}
* WC_WHITESPACE -> {token}
* WC_FIELD -> {token}
* WC_OPTIONALGROUP -> {syntax}
*/
union {
char *token;
struct asn1p_wsyntx_s *syntax;
} content;
TQ_ENTRY(struct asn1p_wsyntx_chunk_s) next;
} asn1p_wsyntx_chunk_t;
typedef struct asn1p_wsyntx_s {
TQ_HEAD(struct asn1p_wsyntx_chunk_s) chunks;
} asn1p_wsyntx_t;
The code of the Singly linked tail is this one :
#define TQ_HEAD(type) \
struct { \
type *tq_head; \
type**tq_tail; \
}
#define TQ_MOVE(to, from) do { \
if(&(TQ_FIRST(from)) == (from)->tq_tail) { \
TQ_INIT(to); \
} else { \
(to)->tq_head = (from)->tq_head; \
(to)->tq_tail = (from)->tq_tail; \
} \
TQ_INIT(from); \
} while(0)
#define TQ_ENTRY(type) \
struct { \
type *tq_next; \
}
#define TQ_FIRST(headp) ((headp)->tq_head)
#define TQ_NEXT(el, field) ((el)->field.tq_next)
#define TQ_INIT(head) do { \
TQ_FIRST((head)) = 0; \
(head)->tq_tail = &TQ_FIRST((head)); \
} while(0)
#define TQ_FOR(var, head, field) \
for((var) = TQ_FIRST((head)); \
(var); (var) = TQ_NEXT((var), field))
/* MSVC does not have decltype(), cannot prevent side effects! */
#define TQ_ADD(head, xel, field) do { \
decltype(xel) __el = (xel); \
assert(TQ_NEXT((__el), field) == 0); \
*(head)->tq_tail = (__el); \
(head)->tq_tail = &TQ_NEXT((__el), field); \
} while(0)
#define TQ_CONCAT(head1, head2, field) do { \
if(TQ_FIRST(head2)) { \
*(head1)->tq_tail = (head2)->tq_head; \
(head1)->tq_tail = (head2)->tq_tail; \
TQ_INIT(head2); \
} \
} while(0)
/*
* Remove the first element and return it.
*/
#define TQ_REMOVE(head, field) ({ \
auto __fel = TQ_FIRST((head)); \
if(__fel == 0 \
|| (TQ_FIRST((head)) = TQ_NEXT(__fel, field)) \
== 0) { \
(head)->tq_tail = &TQ_FIRST((head)); \
} else { \
TQ_NEXT(__fel, field) = 0; \
} \
__fel; })
I've tried different casts but without success.
Thank you for your help !
EDIT :
Here is the code that I get for this call once preprocessed :
asn1p_wsyntx_chunk_t *wc;
while((wc = ({ auto __fel = (((&(wx->chunks)))->tq_head); if(__fel == 0 ||
((((&(wx->chunks)))->tq_head) = ((__fel)->next.tq_next)) == 0)
{ (&(wx->chunks))->tq_tail = &(((&(wx->chunks)))->tq_head); }
else { ((__fel)->next.tq_next) = 0; } __fel; })))
asn1p_wsyntx_chunk_free(wc);

Why I couldn't get gravity value in Cocos2d-x 3.2

I'm new with cocos2d-x. I was trying to use cocos2d-x 3.2 to build a simple physics game, but I got a problem.
At the beginning, I followed the tutorial and added these in HelloWorld.h:
private:
PhysicsWorld* m_world;
public:
void setPhyWorld(PhysicsWorld* world){ m_world = world; }
Then, I added these in HelloWorld.cpp:
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
auto layer = HelloWorld::create();
layer->setPhyWorld(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
Then, I tried to get the gravity value in function init() like this:
bool HelloWorld::init()
{
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}
Then, I ran it, but the program stopped at "Vect g=m_world->getGravity();"
It said that "0x00C53B44 had an Unhandled Exceptions", and I had to interrupt it.
Did anybody have the same problem before? I really appreciate if anyone can help me.
Please observe your code
auto layer = HelloWorld::create(); //Calling init Method of Hello World Layer
layer->setPhyWorld(scene->getPhysicsWorld()); // Setting PhysicsWorld instance to the layer
the init() method is called first and then you are setting setPhyWorld(scene->getPhysicsWorld()) so m_world = null.
If you really want physicsWorld instance in the init() method, you should customize the create & init method of HelloWorld layer and send physicsWorld instance with create() method.
//This code above header class of HelloWorld
#define CUSTOM_CREATE_FUNC(__TYPE__) \
static __TYPE__* create(PhysicsWorld* world) \
{ \
__TYPE__ *pRet = new __TYPE__(); \
if (pRet && pRet->init(world)) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
and then
CUSTOM_CREATE_FUNC(HelloWorld); // in the header class, instead of CREATE_FUNC(HelloWorld)
virtual bool init(PhysicsWorld* world);
and
auto layer = HelloWorld::create(scene->getPhysicsWorld()); // in the createScene() Method
and finally
bool HelloWorld::init(PhysicsWorld* world)
{
m_world = world;
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}