Skip to content
Snippets Groups Projects
Commit fe78a000 authored by Thomas Jahns's avatar Thomas Jahns :cartwheel:
Browse files

Fix condestruct pattern.

* Given how many lines documenting the confusing pattern and how many
  compiler warnings are eliminated by this change...
parent 1dbc1572
No related branches found
No related tags found
No related merge requests found
......@@ -15,8 +15,8 @@
struct CdiFallbackIterator {
CdiIterator super;
int streamId, vlistId, subtypeId;
char *path; //needed for clone() & serialize()
int streamId, vlistId, subtypeId;
int variableCount, curVariable;
int curLevelCount, curLevel;
......@@ -30,52 +30,45 @@ CdiIterator *cdiFallbackIterator_getSuper(CdiFallbackIterator *me)
}
//For more information on the condestruct() pattern, see comment in src/iterator_grib.c
static CdiFallbackIterator *cdiFallbackIterator_condestruct(CdiFallbackIterator *me, const char *path, int filetype)
CdiIterator *cdiFallbackIterator_new(const char *path, int filetype)
{
if(me) goto destruct;
me = (CdiFallbackIterator *) Malloc(sizeof(*me));
CdiFallbackIterator *me = (CdiFallbackIterator *)Malloc(sizeof(*me));
baseIterConstruct(&me->super, filetype);
me->streamId = streamOpenRead(path);
if(me->streamId == CDI_UNDEFID) goto destructSuper;
me->vlistId = streamInqVlist(me->streamId);
if(me->vlistId == CDI_UNDEFID) goto closeStream;
me->variableCount = vlistNvars(me->vlistId);
if(me->variableCount <= 0) goto closeStream;
me->subtypeId = CDI_UNDEFID; //Will be set in cdiFallbackIterator_nextField()
me->curSubtypeCount = -1; //Will be set in cdiFallbackIterator_nextField()
me->curLevelCount = -1; //Will be set in cdiFallbackIterator_nextField()
//These values are chosen so that the natural increment at the start of cdiFallbackIterator_nextField() will correctly position us at the first slice.
me->curTimestep = 0;
if(streamInqTimestep(me->streamId, me->curTimestep) <= 0) goto closeStream;
me->curVariable = -1;
me->curSubtype = -1;
me->curLevel = -1;
me->path = strdup(path);
if(!me->path) goto closeStream;
return me;
// ^ constructor code ^
// | |
// v destructor/error-cleanup code v
destruct:
Free(me->path);
closeStream:
streamClose(me->streamId);
destructSuper:
me->streamId = streamOpenRead(path);
if (me->streamId != CDI_UNDEFID)
{
me->vlistId = streamInqVlist(me->streamId);
if(me->vlistId != CDI_UNDEFID
&& (me->variableCount = vlistNvars(me->vlistId)) > 0
&& streamInqTimestep(me->streamId, me->curTimestep) > 0
&& (me->path = strdup(path)))
{
return (CdiIterator *)me;
}
Free(me->path);
streamClose(me->streamId);
}
baseIterDestruct(&me->super);
Free(me);
return NULL;
}
CdiIterator *cdiFallbackIterator_new(const char *path, int filetype)
void cdiFallbackIterator_delete(CdiIterator *super)
{
return &cdiFallbackIterator_condestruct(NULL, path, filetype)->super;
CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
Free(me->path);
streamClose(me->streamId);
baseIterDestruct(super);
Free(me);
}
//Fetches the info that is derived from the current variable. Most of this is published by the data members in the base class.
......@@ -99,20 +92,23 @@ CdiFallbackIterator *cdiFallbackIterator_clone(CdiIterator *super)
CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
//Make another stream for this file. This yields an unadvanced iterator.
CdiFallbackIterator *clone = cdiFallbackIterator_condestruct(NULL, me->path, me->super.filetype);
if(!clone) return NULL;
//Point the clone to the same position in the file.
clone->variableCount = me->variableCount;
clone->curVariable = me->curVariable;
clone->curLevelCount = me->curLevelCount;
clone->curLevel = me->curLevel;
clone->curSubtypeCount = me->curSubtypeCount;
clone->curSubtype = me->curSubtype;
clone->curTimestep = me->curTimestep;
clone->super.isAdvanced = super->isAdvanced;
if(super->isAdvanced) fetchVariableInfo(clone);
CdiFallbackIterator *clone
= (CdiFallbackIterator *)(void *)
cdiFallbackIterator_new(me->path, me->super.filetype);
if (clone)
{
//Point the clone to the same position in the file.
clone->variableCount = me->variableCount;
clone->curVariable = me->curVariable;
clone->curLevelCount = me->curLevelCount;
clone->curLevel = me->curLevel;
clone->curSubtypeCount = me->curSubtypeCount;
clone->curSubtype = me->curSubtype;
clone->curTimestep = me->curTimestep;
clone->super.isAdvanced = super->isAdvanced;
if(super->isAdvanced) fetchVariableInfo(clone);
}
return clone;
}
......@@ -391,12 +387,6 @@ void cdiFallbackIterator_readFieldF(CdiIterator *super, float *buffer, size_t *n
if(nmiss) *nmiss = (size_t)missingValues;
}
void cdiFallbackIterator_delete(CdiIterator *super)
{
CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
cdiFallbackIterator_condestruct(me, NULL, 0);
}
/*
* Local Variables:
* c-file-style: "Java"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment