Add Closer, partial RAII class

Closer is a template using which C-style resources can be managed
in a C++ context where exceptions can occur. By placing a C resource
in a Closer instance, it is certain the resource will be freed when
the closing scope is exited irrespective of whether that occurs due
to the normal control flow, due to a return statement or an exception
having been thrown.

To be used with Closer, the CloserTraits template must be specialized
for the type in question. With this change specializations are provided
for FILE*, json_t*, pcre_code* and pcre2_match_data*.
This commit is contained in:
Johan Wikman
2016-12-29 14:43:53 +02:00
parent 482fbe6400
commit 0b4c379539
4 changed files with 306 additions and 0 deletions

View File

@ -0,0 +1,44 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cppdefs.hh>
#include <maxscale/jansson.h>
#include <maxscale/utils.hh>
namespace maxscale
{
/**
* @class CloserTraits<json_t*> jansson.hh <maxscale/jansson.hh>
*
* Specialization of @c CloserTraits for @c json_t*.
*/
template<>
struct CloserTraits<json_t*>
{
static void close_if(json_t* pJson)
{
if (pJson)
{
json_decref(pJson);
}
}
static void reset(json_t*& pJson)
{
pJson = NULL;
}
};
}