I'm in the situation that i have several different structs around in my code, that i want to print to the console.
Three examples (of a few hundred):
typedef struct ReqCntrlT /* Request control record */
{
int connectionID;
int dbApplID;
char appDescr[MAX_APPDSCR];
int reqID;
int resubmitFlag;
unsigned int resubmitNo;
char VCIver[MAX_VCIVER];
int loginID;
} ReqCntrlT;
//---------------------------------------------
typedef struct /* Connection request data block */
{
char userID[MAX_USRID];
char password[MAX_PWDID];
} CnctReqDataT;
//---------------------------------------------
typedef struct {
char userID[LOGIN_MAX_USERID];
char closure;
int applVersion;
int authorizationDataLength;
void *authorizationData; } LoginReqDataT;
So what i want to have, is a debug function that simply takes a struct as Parameter and puts out all members of the struct, as so:
LoginReqDataT* foo = new LoginReqDataT;
foo->applVersion = 123;
//...
debugPrintMe(foo);
CnctReqDataT* bar = new CnctReqDataT;
strcpy(bar->userID, "123");
strcpy(bar->password, "mypwd");
debugPrintMe(bar);
What I currently have, is an endless function which is doing stuff like this:
template <class T>
void debugPrintMe(T myvar)
{
if (!DEBUG) return;
if (typeid(T) == typeid(ReqCntrlT*))
{
ReqCntrlT* r = (ReqCntrlT*)myvar;
cout << "reqControl: " << endl
<< "\tconnectionID: " << r->connectionID << endl
<< "\tdbApplID: " << r->dbApplID << endl
//...
<< "\tloginID: " << r->loginID << endl << endl;
}
else if (typeid(T) == typeid(CallBkAppDataT*))
{
CallBkAppDataT* c = (CallBkAppDataT*)myvar;
cout << "appData: " << endl
<< "\tappRespBlockSize " << c->appRespBlockSize << endl
//...
<< "\tstreamType: " << c->streamType << endl << endl;
}
//... and so on
}
Is there a more elegant way to do this?
Rather than branching on the
typeid
, I'd use a very basic C++ feature that doesn't have any runtime overhead: function overloading! Since you're writing the code to print the function anyways, just seperate it into seperate functions:Since you are already willing to use a template function that checks against typeid(T), you may wish to just specialize implementations of TheOneTrueDebugFunction:
As you noticed, you have to play games with
typeid(T)==typeid(XYZ*)
to get it to print the correct types in response to whether the type isStruct
,Struct*
,const Struct *
, and so on. You'll want to explore using more generic type traits to avoid much more duplicated code.Yes there most certainly is a more elegant way of doing this (
... else if (typeid(T) == ...
? Yuck!). You could write someoperator <<()
s for yourstruct
s. This makes yourdebugPrintMe()
function nice and generic and also allows you to stream your structs tocout
,cerr
, a logger, anostringstream
, ...Here's an example to get you started:
I don't think this is easily doable in a language with no builtin introspection, so you're probably better off just overloading
operator<<
for each of your structs to print thme to an ostream.