My class using an interface and require the information if an interface was used on object creation:
The interface:
class IServerSetup {
public:
virtual void ServerSetup () = 0;
}
The class:
class MyServer : public MqC, public IServerSetup {
public:
MyServer(MqS *tmpl) : MqC(tmpl) {};
private:
// service to serve all incomming requests for token "HLWO"
void MyFirstService () {
SendSTART();
SendC("Hello World");
SendRETURN();
}
// define a service as link between the token "HLWO" and the callback "MyFirstService"
void ServerSetup() {
ServiceCreate("HLWO", CallbackF(&MyServer::MyFirstService));
}
};
The constructor at MqC:
MqC::MqC (struct MqS *tmpl) {
if (tmpl && (*(int*)tmpl) != MQ_MqS_SIGNATURE) {
throw MqCSignatureException("MqS");
} else {
hdl = MqContextCreate(0, tmpl);
MqConfigSetSelf (hdl, this);
this->objInit(); <<<<<<<<<<<< This is the important part…
}
}
And now the objInit()
should detect the interface to proper config the object…
void MqC::objInit () {
// use "hdl->setup.Parent.fCreate" to ckeck in context was initialized
if (hdl->setup.Parent.fCreate != NULL) return;
hdl->setup.Parent.fCreate = MqLinkDefault;
hdl->setup.Child.fCreate = MqLinkDefault;
// init the server interface
IServerSetup * const iSetup = dynamic_cast<IServerSetup*const>(this);
if (iSetup != NULL) {
struct ProcCallS * ptr = (struct ProcCallS *) MqSysMalloc(MQ_ERROR_PANIC, sizeof(*ptr));
ptr->type = ProcCallS::PC_IServerSetup;
ptr->call.ServerSetup = iSetup;
MqConfigSetServerSetup (hdl, ProcCall, static_cast<MQ_PTR>(ptr), ProcFree, ProcCopy);
}
...
To make it short… The line:
IServerSetup * const iSetup = dynamic_cast<IServerSetup*const>(this);
Does not work in a constructor (return always NULL
)… So I need to call objInit()
later… this is not good.
UPDATE
If I use the objInit
in the toplevel constructor… This works…
→ But is there any possibility to avoid this (always repeating objInit()
)… And get the top level object in MqC constructor?
MyServer(MqS *tmpl) : MqC(tmpl) {
objInit();
};