Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/IStreamPoll.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: IStreamPoll.h,v 1.2 1999/11/23 20:37:32 bormotov Exp $
00003  */
00004 
00005 // Copyright (C) 1994 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Definition of istream helper class IStreamPoll.
00009 //
00010 //   IStreamPoll is a simple helper class to construct intermediate
00011 //   results from input operations, which are then automatically
00012 //   converted depending on usage.
00013 
00014 // Principal Author: Stephane Collart
00015 //
00016 // Usage:
00017 //
00018 //   Suppose a class A admits an
00019 //      istr& operator >> ( istream& i, A& a );
00020 //   with the purpose of reading in a value for a. The operator can be
00021 //   made to return a temporary of type IStreamPoll instead
00022 //      IStreamPoll >> ( istream& i, A& a ) {
00023 //         // read operations; a = ...; and so on as before
00024 //         Chars return_msg; // the message to pass back
00025 //         if (ok) return_msg = "";
00026 //         else return_msg = "failure to read value in istream& i >> A& a";
00027 //         return IStreamPoll(i,msg);
00028 //      }
00029 //   The return value is thus a temporary intermediate value of type
00030 //   IStreamPoll, which is automatically converted depending on how
00031 //   you use it. It can become
00032 //   - an int, to do
00033 //           A a[20]; int i = 0;
00034 //           while ( i < 20 && cin >> a[i]) i++;
00035 //   - a Chars, to poll an error message
00036 //           A a;
00037 //           Chars errMesg = cin >> a;
00038 //   - an istream, to concatenate in the usual manner
00039 //           A a,b;
00040 //           cin >> a >> b;
00041 //   
00042 // Status: Useable.
00043 //
00044 // Restrictions:
00045 //
00046 // * the conversion to int from IStreamPoll relies for its semantic
00047 //   correctness on the assumption that a read operation was succesful
00048 //   if and only if the return message is the empty string
00049 //
00050 // Extensions:
00051 //
00052 // * if desired, one could add an explicit flag
00053 //      bool successflag;
00054 //   to IStreamPoll, and a constructor
00055 //      IStreamPoll(istream&,bool status,Chars errmsg);
00056 //   to uncouple the status of the read operation from the contents of
00057 //   the message string
00058 //
00059 // Revision History:
00060 //
00061 
00062 #ifndef _READRESULT_H_
00063 #define _READRESULT_H_
00064 
00065 
00066 #include "Chars.h"
00067 
00068 
00069 class IStreamPoll {
00070   
00071 public :
00072   
00073   IStreamPoll( istream& is, Chars em = "" ) :
00074     istr(is), errMesg(em) { }
00075   // em must be passed by value to take actual arguments which
00076   // are local to an exiting function
00077   
00078   typedef istream& IstreamRef;
00079   // @stc what is this for?
00080   
00081   operator int( ) { return errMesg == Chars(""); }
00082   // to get a result condition: while (cin >> group);
00083 
00084   operator Chars( ) { return errMesg; }
00085   // to retrieve error message: Chars result = (cin >> group);
00086   
00087   operator IstreamRef( ) { return istr; }
00088   // to convert back to istream&
00089   
00090   
00091 private :
00092   
00093   istream& istr;
00094   
00095   Chars errMesg;
00096 
00097 };
00098 
00099 #endif

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001