Hello,
I have an application, which has a (Qt C++) singleton logger class. The GetInstance() implementation is:
if(m_Instance == NULL)
{
try
{
m_Instance = new Logger();
}
catch(...){}
}
return m_Instance;
Now I have following macro in the .h file: "#define LOG Logger::Instance()->Log"
Everything is fine as long as new() operation works. What is the best way to ensure that pointer has set (I was thinking some try-catch blocks to catch the std::bad_alloc, but I don't know how to implement that in a macro)? I have created a workaround, which seems to work but is not pretty:
"#define LOG if(Logger::Instance()) Logger::Instance()->Log"
In addition, I would like to know what if my object has a lot getters/setters (e.g. setPath(), getSize()...)? Currently, I have a macro:
"#define SET Settings::Instance()"
and in code I'm able to use SET->setPath("abc"); or SET->getSize();
In this case my ugly workaround does not work, because it requires separate macro for every method. Any tips how I can improve that?
Thanks for the answers.