libMesh
wrapped_petsc.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_WRAPPED_PETSC_H
19 #define LIBMESH_WRAPPED_PETSC_H
20 
21 #include "libmesh/libmesh_config.h"
22 
23 #ifdef LIBMESH_HAVE_PETSC
24 
25 // C++ includes
26 #include <utility> // std::swap, std::move
27 
28 namespace libMesh
29 {
30 
31 // Template class which wraps PETSc objects and specializes the
32 // destructor to call the appropriate "XXXDestroy()" routine.
33 template <typename T>
35 {
43  WrappedPetsc() : obj() {}
44 
52  WrappedPetsc(T obj_in) : obj(obj_in) {}
53 
58  {
59  destroy();
60  }
61 
71  {
72  destroy();
73  obj = nullptr;
74  }
75 
82  WrappedPetsc(const WrappedPetsc & other) = delete;
83  WrappedPetsc & operator= (const WrappedPetsc &) = delete;
84 
91  WrappedPetsc(WrappedPetsc && other) noexcept
92  : obj(other.obj)
93  {
94  other.obj = nullptr;
95  }
96 
102  WrappedPetsc & operator= (WrappedPetsc && other) noexcept
103  {
104  WrappedPetsc tmp(std::move(other));
105  std::swap(tmp, *this);
106  return *this;
107  }
108 
117  T * get() { return &obj; }
118 
125  operator T() const { return obj; }
126 
136  T & operator*() { return obj; }
137 
147  operator bool() const { return obj != nullptr; }
148 
172  void destroy();
173 
174 private:
175  T obj;
176 };
177 
178 } // namespace libMesh
179 
180 #endif // LIBMESH_HAVE_PETSC
181 
182 #endif
void reset_to_zero()
Calls destroy() and sets the managed object to nullptr.
Definition: wrapped_petsc.h:70
WrappedPetsc()
Default constructor.
Definition: wrapped_petsc.h:43
The libMesh namespace provides an interface to certain functionality in the library.
~WrappedPetsc()
Destructor.
Definition: wrapped_petsc.h:57
WrappedPetsc & operator=(const WrappedPetsc &)=delete
T & operator*()
The "dereferencing" operator.
WrappedPetsc(WrappedPetsc &&other) noexcept
Move constructor.
Definition: wrapped_petsc.h:91
WrappedPetsc(T obj_in)
Constructor which initializes obj to a specific passed-in value.
Definition: wrapped_petsc.h:52
void destroy()
Must be specialized to call the appropriate XXXDestroy() routine in order for a WrappedPetsc<T> objec...