-
Notifications
You must be signed in to change notification settings - Fork 341
C plus plus
From 2.2 onwards MaxScale will be built as C++. That does not mean that MaxScale will be rewritten in C++ but that C++ can be used were appropriate.
That is what is available on all platforms officially supported by MaxScale.
The corollary of that is that the reserving of some resources can fail, then it cannot be done in the constructor. In practice that means that if the creation of an object may fail, then there must be a separate create
-function that reserves the resources and once reserved, creates the object or returns NULL if the creation fails.
class Heavy
{
public:
static Heavy* create(...)
{
Heavy* pThis = NULL;
auto_ptr<Resource>*sResource(...);
if (sResource.get())
{
pThis = new Heavy(sResource);
}
return pThis;
}
private:
Heavy(auto_ptr<Resource> sResource)
: m_sResource(sResource)
{
}
};
Note that although MaxScale code itself must not throw exceptions, the runtime-library may do so (e.g. new
if the allocation fails).
Even though MaxScale code itself must not throw exceptions, new C++ code should be exception safe. That is, the code should be written so that even if an exception occurs, all resources are freed appropriately and MaxScale can continue to run. Guard objects should be used for ensuring the release of resources in preference of try-catch
blocks.
Under no circumstances must exceptions be allowed to cross module boundaries. That is handled automatically if the templates maxscale::Filter
and maxscale::Router
are used.