Actual source code: destroy.c
1: #define PETSC_DLL
2: /*
3: Provides utility routines for manulating any type of PETSc object.
4: */
5: #include petsc.h
7: struct _p_Object {
8: PETSCHEADER(int);
9: };
11: PetscErrorCode PetscObjectDestroy_PetscObject(PetscObject obj)
12: {
16: if (--obj->refct > 0) return(0);
17: PetscHeaderDestroy(obj);
18: return(0);
19: }
23: /*@C
24: PetscObjectCreate - Creates a PetscObject
26: Collective on PetscObject
28: Input Parameter:
29: . comm - An MPI communicator
31: Output Parameter:
32: . obj - The object
34: Level: developer
36: Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject()
37: to make new object classes.
39: Concepts: destroying object
40: Concepts: freeing object
41: Concepts: deleting object
43: @*/
44: PetscErrorCode PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
45: {
46: PetscObject o;
52: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
53: PetscInitializePackage(PETSC_NULL);
54: #endif
55: PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_PetscObject,0);
56: /* records not yet defined in PetscObject
57: o->data = 0;
58: o->setupcalled = 0;
59: */
60: *obj = o;
61: return(0);
62: }
66: /*@C
67: PetscObjectCreateGeneric - Creates a PetscObject
69: Collective on PetscObject
71: Input Parameter:
72: + comm - An MPI communicator
73: . cookie - The class cookie
74: - name - The class name
76: Output Parameter:
77: . obj - The object
79: Level: developer
81: Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject()
82: to make new object classes.
84: Concepts: destroying object
85: Concepts: freeing object
86: Concepts: deleting object
88: @*/
89: PetscErrorCode PetscObjectCreateGeneric(MPI_Comm comm, PetscCookie cookie, const char name[], PetscObject *obj)
90: {
91: PetscObject o;
97: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
98: PetscInitializePackage(PETSC_NULL);
99: #endif
100: PetscHeaderCreate(o,_p_PetscObject,-1,cookie,0,name,comm,PetscObjectDestroy_PetscObject,0);
101: /* records not yet defined in PetscObject
102: o->data = 0;
103: o->setupcalled = 0;
104: */
105: *obj = o;
106: return(0);
107: }
111: /*@
112: PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
114: Collective on PetscObject
116: Input Parameter:
117: . obj - any PETSc object, for example a Vec, Mat or KSP.
118: This must be cast with a (PetscObject), for example,
119: PetscObjectDestroy((PetscObject)mat);
121: Level: beginner
123: Concepts: destroying object
124: Concepts: freeing object
125: Concepts: deleting object
127: @*/
128: PetscErrorCode PetscObjectDestroy(PetscObject obj)
129: {
135: if (obj->bops->destroy) {
136: (*obj->bops->destroy)(obj);
137: } else {
138: SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
139: }
140: return(0);
141: }
145: /*@C
146: PetscObjectView - Views any PetscObject, regardless of the type.
148: Collective on PetscObject
150: Input Parameters:
151: + obj - any PETSc object, for example a Vec, Mat or KSP.
152: This must be cast with a (PetscObject), for example,
153: PetscObjectView((PetscObject)mat,viewer);
154: - viewer - any PETSc viewer
156: Level: intermediate
158: @*/
159: PetscErrorCode PetscObjectView(PetscObject obj,PetscViewer viewer)
160: {
165: if (!viewer) viewer = PETSC_VIEWER_STDOUT_(obj->comm);
168: if (obj->bops->view) {
169: (*obj->bops->view)(obj,viewer);
170: } else {
171: SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
172: }
173: return(0);
174: }
178: /*@C
179: PetscTypeCompare - Determines whether a PETSc object is of a particular type.
181: Not Collective
183: Input Parameters:
184: + obj - any PETSc object, for example a Vec, Mat or KSP.
185: This must be cast with a (PetscObject), for example,
186: PetscObjectDestroy((PetscObject)mat);
187: - type_name - string containing a type name
189: Output Parameter:
190: . same - PETSC_TRUE if they are the same, else PETSC_FALSE
191:
192: Level: intermediate
194: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
196: Concepts: comparing^object types
197: Concepts: types^comparing
198: Concepts: object type^comparing
200: @*/
201: PetscErrorCode PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
202: {
206: if (!obj) {
207: *same = PETSC_FALSE;
208: } else if (!type_name && !obj->type_name) {
209: *same = PETSC_TRUE;
210: } else if (!type_name || !obj->type_name) {
211: *same = PETSC_FALSE;
212: } else {
216: PetscStrcmp((char*)(obj->type_name),type_name,same);
217: }
218: return(0);
219: }
221: static int PetscObjectRegisterDestroy_Count = 0;
222: static PetscObject PetscObjectRegisterDestroy_Objects[256];
226: /*@C
227: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
228: PetscFinalize() is called.
230: Collective on PetscObject
232: Input Parameter:
233: . obj - any PETSc object, for example a Vec, Mat or KSP.
234: This must be cast with a (PetscObject), for example,
235: PetscObjectRegisterDestroy((PetscObject)mat);
237: Level: developer
239: Notes:
240: This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
241: when PETSc ends.
243: .seealso: PetscObjectRegisterDestroyAll()
244: @*/
245: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
246: {
249: PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
250: return(0);
251: }
255: /*@C
256: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
257: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
258: PetscFinalize() is called.
260: Collective on individual PetscObjects
262: Level: developer
264: .seealso: PetscObjectRegisterDestroy()
265: @*/
266: PetscErrorCode PetscObjectRegisterDestroyAll(void)
267: {
269: int i;
272: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
273: PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);
274: }
275: return(0);
276: }