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) {
166:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
167:   }

170:   if (obj->bops->view) {
171:     (*obj->bops->view)(obj,viewer);
172:   } else {
173:     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
174:   }
175:   return(0);
176: }

180: /*@C
181:    PetscTypeCompare - Determines whether a PETSc object is of a particular type.

183:    Not Collective

185:    Input Parameters:
186: +  obj - any PETSc object, for example a Vec, Mat or KSP.
187:          This must be cast with a (PetscObject), for example, 
188:          PetscObjectDestroy((PetscObject)mat);
189: -  type_name - string containing a type name

191:    Output Parameter:
192: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
193:   
194:    Level: intermediate

196: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()

198:    Concepts: comparing^object types
199:    Concepts: types^comparing
200:    Concepts: object type^comparing

202: @*/
203: PetscErrorCode  PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
204: {

208:   if (!obj) {
209:     *same = PETSC_FALSE;
210:   } else if (!type_name && !obj->type_name) {
211:     *same = PETSC_TRUE;
212:   } else if (!type_name || !obj->type_name) {
213:     *same = PETSC_FALSE;
214:   } else {
218:     PetscStrcmp((char*)(obj->type_name),type_name,same);
219:   }
220:   return(0);
221: }

223: static int         PetscObjectRegisterDestroy_Count = 0;
224: static PetscObject PetscObjectRegisterDestroy_Objects[256];

228: /*@C
229:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
230:      PetscFinalize() is called.

232:    Collective on PetscObject

234:    Input Parameter:
235: .  obj - any PETSc object, for example a Vec, Mat or KSP.
236:          This must be cast with a (PetscObject), for example, 
237:          PetscObjectRegisterDestroy((PetscObject)mat);

239:    Level: developer

241:    Notes:
242:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
243:     when PETSc ends.

245: .seealso: PetscObjectRegisterDestroyAll()
246: @*/
247: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
248: {
251:   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
252:   return(0);
253: }

257: /*@C
258:    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
259:      with PetscObjectRegisterDestroy(). Called by PetscFinalize()
260:      PetscFinalize() is called.

262:    Collective on individual PetscObjects

264:    Level: developer

266: .seealso: PetscObjectRegisterDestroy()
267: @*/
268: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
269: {
271:   int i;

274:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
275:     PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);
276:   }
277:   return(0);
278: }