v0.14.0
petsc_smart_ptr_objects.cpp
Go to the documentation of this file.
1 /** \file petsc_smart_ptr_objects.cpp
2  * \example petsc_smart_ptr_objects.cpp
3  * \brief Create and destroy PETSc objects with smart pointer
4  */
5 
6 
7 
8 #include <MoFEM.hpp>
9 
10 using namespace MoFEM;
11 
12 static char help[] = "...\n\n";
13 
14 int main(int argc, char *argv[]) {
15 
16  MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
17 
18  try {
19 
20  SmartPetscObj<Mat> m_ptr;
21 
22  // check is count is correct
23  auto check = [&](const int expected) {
25  if (m_ptr.use_count() != expected)
26  SETERRQ2(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
27  "use count should be %d but is %d", expected,
28  m_ptr.use_count());
30  };
31 
32  // check copy constructor
33  {
34  Mat m;
35  CHKERR MatCreate(PETSC_COMM_SELF, &m);
36  m_ptr = SmartPetscObj<Mat>(m);
37  }
38 
39  // Check casting on PetscObject
40  { std::ignore = static_cast<PetscObject>(m_ptr); }
41 
42  {
43  SmartPetscObj<Mat> n_ptr(m_ptr);
44  CHKERR check(2);
45  }
46 
47  CHKERR check(1);
48 
49  // check if casting works well
50  CHKERR MatSetSizes(m_ptr, 2, 2, 2, 2);
51 
52  {
53  // nesting once
54  SmartPetscObj<Mat> n_ptr = m_ptr;
55  CHKERR check(2);
56  {
57  // again
58  SmartPetscObj<Mat> i_ptr = m_ptr;
59  CHKERR check(3);
60  }
61  CHKERR check(2);
62  }
63 
64 
65  {
66  // nesting again
67  SmartPetscObj<Mat> n_ptr = m_ptr;
68  CHKERR check(2);
69  }
70 
71  // only one now
72  CHKERR check(1);
73 
74  // reset, i.e. delete matrix
75  m_ptr.reset();
76 
77  // counts should be zero now
78  CHKERR check(0);
79 
80  {
82  }
83 
84  }
86 
88  return 0;
89 }
help
static char help[]
Definition: petsc_smart_ptr_objects.cpp:12
MoFEM.hpp
MoFEM::CoreTmp< 0 >::Finalize
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112
main
int main(int argc, char *argv[])
Definition: petsc_smart_ptr_objects.cpp:14
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:535
MoFEM::SmartPetscObj::use_count
int use_count() const
Definition: PetscSmartObj.hpp:109
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::CoreTmp< 0 >::Initialize
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
CATCH_ERRORS
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:372
MOFEM_DATA_INCONSISTENCY
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
m
FTensor::Index< 'm', 3 > m
Definition: shallow_wave.cpp:80
MoFEM::SmartPetscObj< Mat >
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346