v0.13.1
petsc_smart_ptr_objects.cpp

Create and destroy PETSc objects with smart pointer.

Create and destroy PETSc objects with smart pointer

/** \file petsc_smart_ptr_objects.cpp
* \example petsc_smart_ptr_objects.cpp
* \brief Create and destroy PETSc objects with smart pointer
*/
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "...\n\n";
int main(int argc, char *argv[]) {
MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
try {
// check is count is correct
auto check = [&](const int expected) {
if (m_ptr.use_count() != expected)
SETERRQ2(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
"use count should be %d but is %d", expected,
m_ptr.use_count());
};
// check copy constructor
{
Mat m;
CHKERR MatCreate(PETSC_COMM_SELF, &m);
}
// Check casting on PetscObject
{ PetscObject obj = static_cast<PetscObject>(m_ptr); }
{
SmartPetscObj<Mat> n_ptr(m_ptr);
CHKERR check(2);
}
CHKERR check(1);
// check if casting works well
CHKERR MatSetSizes(m_ptr, 2, 2, 2, 2);
{
// nesting once
SmartPetscObj<Mat> n_ptr = m_ptr;
CHKERR check(2);
{
// again
SmartPetscObj<Mat> i_ptr = m_ptr;
CHKERR check(3);
}
CHKERR check(2);
}
{
// nesting again
SmartPetscObj<Mat> n_ptr = m_ptr;
CHKERR check(2);
}
// only one now
CHKERR check(1);
// reset, i.e. delete matrix
m_ptr.reset();
// counts should be zero now
CHKERR check(0);
{
}
}
return 0;
}
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:372
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
#define CHKERR
Inline error check.
Definition: definitions.h:535
FTensor::Index< 'm', SPACE_DIM > m
implementation of Data Operators for Forces and Sources
Definition: MoFEM.hpp:24
int main(int argc, char *argv[])
static char help[]
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition: Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112