3

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?

David Müller
  • 5,291
  • 2
  • 29
  • 33

4 Answers4

11

Yes there most certainly is a more elegant way of doing this (... else if (typeid(T) == ...? Yuck!). You could write some operator <<()s for your structs. This makes your debugPrintMe() function nice and generic and also allows you to stream your structs to cout, cerr, a logger, an ostringstream, ...

Here's an example to get you started:

std::ostream& operator <<(std::ostream& os, const ReqCntrlT& r)
{
    os << "reqControl"
        << "\n\tconnectionID: " << r.connectionID 
        << "\n\tdbApplID: " << r.dbApplID 
        << "\n\tappDescr: " << r.appDescr
        << "\n\treqID: " << r.reqID
        << "\n\tresubmitFlag: " << r.resubmitFlag
        << "\n\tresubmitNo: " << r.resubmitNo
        << "\n\tVCIver: " << r.VCIver
        << "\n\tloginID: " << r.loginID
        << '\n';
    return os;
}

template <class T>
void debugPrintMe(const T& myvar)
{
    if (DEBUG)
    {
        std::cout << myvar << std::endl;
    }
}

int main()
{
    ReqCntrlT r;

    // [...]

    debugPrintMe(r);

    return 0;
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Thanks, that's a good idea. Just wondering if there's a possibility to automatically iterate over all members of the struct to avoid explicitly calling all of them? – David Müller Apr 15 '11 at 15:54
  • Alas there's no equivalent of Python's [`dir()`](http://www.python.org/doc//current/tutorial/modules.html#the-dir-function) in C++. – johnsyweb Apr 15 '11 at 23:25
3

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.

1

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:

void debugPrintMe(ReqCntrlT const& r){
  // ...
}

void debugPrintMe(CallBkAppDataT const& c){
  // ...
}

// others
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • The way he's doing it, he *does* need to cast it, otherwise the function won't compile, because he's handling all the types in the same function. – Benjamin Lindley Apr 15 '11 at 13:42
  • Actually, no. He already uses a template, so the function gets instantiated for every input type and most of the code inside the function is wasted anyways. – Xeo Apr 15 '11 at 13:57
  • Those aren't static ifs, if such a thing existed in C++. When he tries to instantiate the template, every statement will be looked at by the compiler, even the ones under if blocks that will never be executed. If `[T = ReqCntrlT*]`, and he doesn't cast but instead just uses myvar directly, he will get a compile error when it gets to `myvar->appRespBlockSize`, because `ReqCntrlT` does not have a member `appRespBlockSize`. – Benjamin Lindley Apr 15 '11 at 14:16
  • @Benjamin: I'm sorry, you're totally right, I forgot the code analysis done by the compiler. Thanks for reminding me again. :) – Xeo Apr 15 '11 at 14:28
0

Since you are already willing to use a template function that checks against typeid(T), you may wish to just specialize implementations of TheOneTrueDebugFunction:

template <class T>
void debugPrintMe(const T& myvar);

template <>
void debugPrintMe< ReqCntrlT* >(T)
{
   // implementation to print debug messages
}

template <>
void debugPrintMe< CallBkAppDataT* >(T)
{
   // implementation
}

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 is Struct, Struct*, const Struct *, and so on. You'll want to explore using more generic type traits to avoid much more duplicated code.

Andy Finkenstadt
  • 3,547
  • 1
  • 21
  • 25