[ZOOM] Re: zoom.hh

Ashley Sanders zzaascs at irwell.mimas.ac.uk
Thu Nov 29 13:03:23 CET 2001


> >   try
> >     {
> >       SUTRSRecord sMyOwnCopy =
> >          dynamic_cast<const SUTRSRecord &>(sResultSet.getRecord (0));
> > 
> >       cout << sMyOwnCopy.render () << endl;
> >     }
> >   catch (bad_cast)
> >     {
> >       cerr << "oops, wasn't a SUTRS record" << endl;
> >     }
> 
> Hmm.  Speaking as a C programmer: Bleuuuururuuugughghchg!
> 
> > Which I think isn't too cumbersome to write.
> 
> Wow.

Honestly, it isn't. If the variable and class names weren't so
long it would look a lot nicer. We can also define operator[] as
an alias of getRecord(). Are the following any better? If
we check the type of the record first, then there's no need to
catch a bad_cast exception:

     if (sSet[0].recsyn () == ZOOM::record::SUTRS)
        {
          SUTRSRecord sRec = dynamic_cast<const SUTRSRecord &>(sSet[0]);
          cout << sRec.render () << endl;
        }
      else
        cerr << "oops, wasn't a SUTRS record" << endl;

or you can use a C type cast if dynamic_cast turns your
stomach:

     if (sSet[0].recsyn () == ZOOM::record::SUTRS)
        {
          SUTRSRecord sRec = (const SUTRSRecord &) sSet[0];
          cout << sRec.render () << endl;
        }
      else
        cerr << "oops, wasn't a SUTRS record" << endl;

So, you can use whatever style you wish, though IMO a dynamic_cast
makes it plainly obvious what you were doing.

> > But really, I can't see a reason why you would want your own
> > copy of a record. The resultSet object is already a handy
> > container for keeping them in.
> 
> Again, the reason you might want this is if the resultSet object is
> transient, so the record is supposed to live longer than it.

My answer to that of course is make sure your resultSet
objects last until you no longer need what they contain.

> For example, if you were writing a Zthes browsing application, you might
> want a function that returns the record with a specified termId:
> [WARNING: untested code ahead!]
> 
> 	record *recordByTermId(conncetion *conn, char *termId) {
> 	    char buf[100];
> 	    sprintf(buf, "@attr util 1=4 \"%s\"", termId);
> 	    resultSet rs(conn, RPNQuery(buf));
> 	    return rs.getRecord(0);
> 	    // ... and rs is automatically destructed
> 	}

Why not return the resultSet instead, then the user of your
recordByTermId function can check to see if zero or more than one
records were found (several of our libraries cannot supply unquie
record control numbers for instance.) So:

       resultSet *searchByTermId(conncetion *conn, char *termId) {
           char buf[100];
           sprintf(buf, "@attr util 1=4 \"%s\"", termId);
           return new resultSet (conn, RPNQuery(buf));
       }

> I have the sense that we're going around in circles a bit here!

Just trying to provoke some discussion.

Ashley.

-- 
Ashley Sanders                                a.sanders at mcc.ac.uk
COPAC: A public bibliographic database from MIMAS, funded by JISC
             http://copac.ac.uk/ - copac at mimas.ac.uk



More information about the ZOOM mailing list