Allium  0.1.0
petsc_object_ptr.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_PETSC_OBJECT_PTR_HPP
16 #define ALLIUM_LA_PETSC_OBJECT_PTR_HPP
17 
18 #include <allium/config.hpp>
19 
20 #ifdef ALLIUM_USE_PETSC
21 
22 #include <petscmat.h>
23 #include <petscvec.h>
24 #include <petscksp.h>
25 #include <petscpc.h>
26 #include <petscts.h>
27 #include <petscdm.h>
28 
29 #ifdef USE_SLEPC
30 #include <slepceps.h>
31 #endif
32 
33 namespace allium {
34 
36 
37 // make a list of objects that can be casted to PetscObject such that we do not
38 // accidentally cast other operators
39 template <typename T>
40 struct is_petsc_object : std::false_type {};
41 
42 template<> struct is_petsc_object<Mat> : std::true_type {};
43 template<> struct is_petsc_object<Vec> : std::true_type {};
44 template<> struct is_petsc_object<KSP> : std::true_type {};
45 template<> struct is_petsc_object<PC> : std::true_type {};
46 template<> struct is_petsc_object<TS> : std::true_type {};
47 template<> struct is_petsc_object<DM> : std::true_type {};
48 #ifdef USE_SLEPC
49 template<> struct is_petsc_object<EPS> : std::true_type {};
50 #endif
51 
52 template <typename T>
53 std::enable_if_t<is_petsc_object<T>::value, PetscObject>
54 petsc_object_cast(T value) {
55  return reinterpret_cast<PetscObject>(value);
56 }
57 
59 
65 template <typename T>
66 class PetscObjectPtr final {
67  public:
68  static_assert(is_petsc_object<T>::value,
69  "PetscObjectPtr can only hold PETSc objects.");
70 
71  PetscObjectPtr() {
72  value = nullptr;
73  }
74 
75  PetscObjectPtr(const PetscObjectPtr& other) {
76  PetscObjectReference(petsc_object_cast(other.value));
77  this->value = other.value;
78  }
79 
81  this->value = other.value;
82  other.value = nullptr;
83  }
84 
85  PetscObjectPtr(T new_value, bool adopt) {
86  value = nullptr;
87  set(new_value, adopt);
88  }
89 
90  ~PetscObjectPtr() {
91  release();
92  }
93 
94  operator T () const {
95  return get();
96  }
97 
98  PetscObjectPtr& operator= (const PetscObjectPtr& other) {
99  set(other.value, false);
100  return *this;
101  }
102 
105  void release() {
106  if (value != nullptr) {
107  PetscObjectDereference(petsc_object_cast(value));
108  value = nullptr;
109  }
110  }
111 
114  T abandon() {
115  T result = value;
116  value = nullptr;
117  return result;
118  }
119 
120  void set(T new_value, bool adopt) {
121  if (!adopt) {
122  // It is important to increase the reference count first, otherwise we
123  // might free new_value by directly or indirectly releasing it.
124  PetscObjectReference(petsc_object_cast(new_value));
125  }
126  release();
127  value = new_value;
128  }
129 
136  release();
137  return &value;
138  }
139 
140  T get() const {
141  return value;
142  }
143 
144  friend void swap(PetscObjectPtr<T> &a, PetscObjectPtr<T> &b){
145  T v = a.value;
146  a.value = b.value;
147  b.value = v;
148  }
149 
150  protected:
151  T value;
152 };
153 
154 }
155 
156 #endif
157 #endif
PETSc wrapper object for memory management.
Definition: petsc_object_ptr.hpp:66
void release()
Definition: petsc_object_ptr.hpp:105
T * writable_ptr()
Definition: petsc_object_ptr.hpp:135
T abandon()
Definition: petsc_object_ptr.hpp:114