// interface (C-style)
int NI_Print(const std::string & text);
int NI_PrintSL(const QStringList & text);
int NI_Print(const std::string & text)
{
int hr = NOERROR;
int xi = 0;
do {
try
{
hr = g_rcfclnt->Print(text);
}
catch (const RCF::Exception & e)
{
hr = E_FAIL;
if (!xi && e.getErrorId() != RCF::RcfError_ClientReadFail) xi++; // xi = 1;
xi++;
if (xi > 1) NI_SetLastErrorMsg(e);
}
if (xi > 1) break; // when an error number 31, try running the query again
} while (xi > 0);
return hr;
}
int NI_PrintSL(const QStringList & text)
{
int hr = NOERROR;
int xi = 0;
do {
try
{
hr = g_rcfclnt->PrintSL(text);
}
catch (const RCF::Exception & e)
{
hr = E_FAIL;
if (!xi && e.getErrorId() != RCF::RcfError_ClientReadFail) xi++; // xi = 1;
xi++;
if (xi > 1) NI_SetLastErrorMsg(e);
}
if (xi > 1) break; // when an error number 31, try running the query again
} while (xi > 0);
return hr;
}
How to hide the same code?
I'm not good in the C++, boost, BOOSTPP and other.
It's not so easy in C++ to factor out try/catch wrappers like you are wanting to do here. It can be done by wrapping the RCF call with boost::function<> and boost::bind, and then passing that as a parameter to a common try/catch wrapper. However wrapping arguments with boost::bind can be pretty intricate, if you haven't used it before.
A simpler way to factor out at least part of the try/catch is the following:
You will still have some duplicated code, but it's fairly limited and only contains boilerplate. Also I wouldn't bother with any macros. Although they can save you a couple of minutes when you're writing the code, they are likely to cost you much more time than that when you are maintaining the code later on.