xref: /petsc/src/sys/objects/destroy.c (revision 16bf3c38127ebc85ec7b0555e3aa5e69d51a6fbb)
1 #define PETSC_DLL
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include "petsc.h"  /*I   "petsc.h"    I*/
6 
7 PetscCookie PETSC_OBJECT_COOKIE = 0;
8 
9 struct _p_Object {
10   PETSCHEADER(int);
11 };
12 
13 PetscErrorCode PetscObjectDestroy_PetscObject(PetscObject obj)
14 {
15   PetscErrorCode ierr;
16   PetscFunctionBegin;
17   PetscValidHeader(obj,1);
18   if (--obj->refct > 0) PetscFunctionReturn(0);
19   ierr = PetscHeaderDestroy(obj);CHKERRQ(ierr);
20   PetscFunctionReturn(0);
21 }
22 
23 #undef __FUNCT__
24 #define __FUNCT__ "PetscObjectCreate"
25 /*@C
26    PetscObjectCreate - Creates a PetscObject
27 
28    Collective on PetscObject
29 
30    Input Parameter:
31 .  comm - An MPI communicator
32 
33    Output Parameter:
34 .  obj - The object
35 
36    Level: developer
37 
38    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject()
39           to make new object classes.
40 
41     Concepts: destroying object
42     Concepts: freeing object
43     Concepts: deleting object
44 
45 @*/
46 PetscErrorCode PETSC_DLLEXPORT PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
47 {
48   PetscObject    o;
49   PetscErrorCode ierr;
50 
51   PetscFunctionBegin;
52   PetscValidPointer(obj,2);
53 
54 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
55   ierr = PetscInitializePackage(PETSC_NULL);CHKERRQ(ierr);
56 #endif
57   ierr = PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_PetscObject,0);CHKERRQ(ierr);
58   /* records not yet defined in PetscObject
59   o->data        = 0;
60   o->setupcalled = 0;
61   */
62   *obj = o;
63   PetscFunctionReturn(0);
64 }
65 
66 #undef __FUNCT__
67 #define __FUNCT__ "PetscObjectDestroy"
68 /*@
69    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
70 
71    Collective on PetscObject
72 
73    Input Parameter:
74 .  obj - any PETSc object, for example a Vec, Mat or KSP.
75          This must be cast with a (PetscObject), for example,
76          PetscObjectDestroy((PetscObject)mat);
77 
78    Level: beginner
79 
80     Concepts: destroying object
81     Concepts: freeing object
82     Concepts: deleting object
83 
84 @*/
85 PetscErrorCode PETSC_DLLEXPORT PetscObjectDestroy(PetscObject obj)
86 {
87   PetscErrorCode ierr;
88 
89   PetscFunctionBegin;
90   PetscValidHeader(obj,1);
91 
92   if (obj->bops->destroy) {
93     ierr = (*obj->bops->destroy)(obj);CHKERRQ(ierr);
94   } else {
95     SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
96   }
97   PetscFunctionReturn(0);
98 }
99 
100 #undef __FUNCT__
101 #define __FUNCT__ "PetscObjectView"
102 /*@C
103    PetscObjectView - Views any PetscObject, regardless of the type.
104 
105    Collective on PetscObject
106 
107    Input Parameters:
108 +  obj - any PETSc object, for example a Vec, Mat or KSP.
109          This must be cast with a (PetscObject), for example,
110          PetscObjectView((PetscObject)mat,viewer);
111 -  viewer - any PETSc viewer
112 
113    Level: intermediate
114 
115 @*/
116 PetscErrorCode PETSC_DLLEXPORT PetscObjectView(PetscObject obj,PetscViewer viewer)
117 {
118   PetscErrorCode ierr;
119 
120   PetscFunctionBegin;
121   PetscValidHeader(obj,1);
122   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(obj->comm);
123   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
124 
125   if (obj->bops->view) {
126     ierr = (*obj->bops->view)(obj,viewer);CHKERRQ(ierr);
127   } else {
128     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
129   }
130   PetscFunctionReturn(0);
131 }
132 
133 #undef __FUNCT__
134 #define __FUNCT__ "PetscTypeCompare"
135 /*@C
136    PetscTypeCompare - Determines whether a PETSc object is of a particular type.
137 
138    Not Collective
139 
140    Input Parameters:
141 +  obj - any PETSc object, for example a Vec, Mat or KSP.
142          This must be cast with a (PetscObject), for example,
143          PetscObjectDestroy((PetscObject)mat);
144 -  type_name - string containing a type name
145 
146    Output Parameter:
147 .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
148 
149    Level: intermediate
150 
151 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
152 
153    Concepts: comparing^object types
154    Concepts: types^comparing
155    Concepts: object type^comparing
156 
157 @*/
158 PetscErrorCode PETSC_DLLEXPORT PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
159 {
160   PetscErrorCode ierr;
161 
162   PetscFunctionBegin;
163   if (!obj) {
164     *same = PETSC_FALSE;
165   } else if (!type_name && !obj->type_name) {
166     *same = PETSC_TRUE;
167   } else if (!type_name || !obj->type_name) {
168     *same = PETSC_FALSE;
169   } else {
170     PetscValidHeader(obj,1);
171     PetscValidCharPointer(type_name,2);
172     PetscValidPointer(same,3);
173     ierr = PetscStrcmp((char*)(obj->type_name),type_name,same);CHKERRQ(ierr);
174   }
175   PetscFunctionReturn(0);
176 }
177 
178 static int         PetscObjectRegisterDestroy_Count = 0;
179 static PetscObject PetscObjectRegisterDestroy_Objects[256];
180 
181 #undef __FUNCT__
182 #define __FUNCT__ "PetscObjectRegisterDestroy"
183 /*@C
184    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
185      PetscFinalize() is called.
186 
187    Collective on PetscObject
188 
189    Input Parameter:
190 .  obj - any PETSc object, for example a Vec, Mat or KSP.
191          This must be cast with a (PetscObject), for example,
192          PetscObjectRegisterDestroy((PetscObject)mat);
193 
194    Level: developer
195 
196    Notes:
197       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
198     when PETSc ends.
199 
200 .seealso: PetscObjectRegisterDestroyAll()
201 @*/
202 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroy(PetscObject obj)
203 {
204   PetscFunctionBegin;
205   PetscValidHeader(obj,1);
206   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
207   PetscFunctionReturn(0);
208 }
209 
210 #undef __FUNCT__
211 #define __FUNCT__ "PetscObjectRegisterDestroyAll"
212 /*@C
213    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
214      with PetscObjectRegisterDestroy(). Called by PetscFinalize()
215      PetscFinalize() is called.
216 
217    Collective on individual PetscObjects
218 
219    Level: developer
220 
221 .seealso: PetscObjectRegisterDestroy()
222 @*/
223 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroyAll(void)
224 {
225   PetscErrorCode ierr;
226   int i;
227 
228   PetscFunctionBegin;
229   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
230     ierr = PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);CHKERRQ(ierr);
231   }
232   PetscFunctionReturn(0);
233 }
234 
235 
236