I believe that we should leverage RAII for those objects in order to make programming easier and safer. Basically one needs some sort of unique_ptr<FourSpinorBlock[]> with a custom deleter, namely geom.free(). The following is a simple implementation:
/**
RAII container for checkerboarded four spinors.
The four spinors are C-style arrays with no length information and most
importantly no cleanup. This wrapper enables automatic cleanup using the
QPhiX::Geometry object.
Perhaps it would be even better to use `std::vector` with a custom allocator
for this structure.
*/
template <typename FT, int veclen, int soalen, bool compress12>
class FourSpinorCBWrapper {
public:
typedef
typename ::QPhiX::Geometry<FT, veclen, soalen, compress12>::FourSpinorBlock FourSpinorBlock;
FourSpinorCBWrapper(::QPhiX::Geometry<FT, veclen, soalen, compress12> &geom_)
: geom(geom_), spinor(geom.allocCBFourSpinor()) {}
~FourSpinorCBWrapper() { geom.free(spinor); }
FourSpinorBlock const *data() const { return spinor; }
FourSpinorBlock *data() { return spinor; }
size_t num_blocks() const { return geom.get_num_blocks(); }
private:
::QPhiX::Geometry<FT, veclen, soalen, compress12> &geom;
FourSpinorBlock *const spinor;
};
There are a couple of types that could need such a RAII wrapper:
FourSpinorBlock
SU3MatrixBlock
CloverBlock
FullCloverBlock (for twisted mass)
Perhaps one wants to implement the operator*() and operator[] in order to use it just like a raw pointer. Perhaps the factory functions in the Geometry class could return such a handle instead of a raw owning pointer?
In the QPhiX testing code, there a lot of manually managed resources, like this:
At the end of the function, there are cleanup calls:
I believe that we should leverage RAII for those objects in order to make programming easier and safer. Basically one needs some sort of
unique_ptr<FourSpinorBlock[]>
with a custom deleter, namelygeom.free()
. The following is a simple implementation:There are a couple of types that could need such a RAII wrapper:
FourSpinorBlock
SU3MatrixBlock
CloverBlock
FullCloverBlock
(for twisted mass)Perhaps one wants to implement the
operator*()
andoperator[]
in order to use it just like a raw pointer. Perhaps the factory functions in theGeometry
class could return such a handle instead of a raw owning pointer?