typed_io(3C++)


typed_io -- G2++ typed input/output

Synopsis

   // For each record type defined in
   // the corresponding .g file:
       typedef type_specifier T;
       istream& operator>>(istream& is,T& t);
       ostream& operator<<(ostream& os,const T& t);

Description

The G2++ compiler, g2++comp(1C++), must be used prior to compiling client code that uses G2++ typed input/output routines. g2++comp(1C++) reads a collection of G2++ record definitions from one or more .g files (each definition is itself a G2++ record) and writes a pair of files, one .h file and one .c file, for each .g file. The .c file must be compiled and linked with the rest of the application. The .h file must be included by client programs. The .h file contains a set of three declarations for each record type defined in the .g file. The three declarations are:

typedef type_specifier T; Used by the client to declare variables suitable as sources or targets of the typed I/O operators. T is the upper case version of the record name in the .g file. For simple records (records with no indented groups), type_specifier will be a simple type; otherwise, type_specifier will be a structure type.

istream& operator>>(istream& is,T& t); Stream extraction operator. Scans istream is until it encounters a G2++ record of type T and maps the record contents into t. If a record of type T cannot be found before the stream is exhausted, the stream will test as null after the operation and the contents of t will be undefined. The mapping obeys the following rules (see G2++(4C++) for definitions of italicized terms):

ostream& operator<<(ostream& os,const T& t); Stream insertion operator. Maps the values in t into a G2++ record and inserts the record into os. The mapping obeys the following rules:

Example

Using g2++comp(1C++) to compile the following person.g file:

           Time    USER
                   .header Time.h
                   .header Timeio.h
                   .null   Time::MIN
           person
                   id      *
                   age     SHORT
                   b_day   Time
                   hobbies
                           *       *

produces the following person.h file:

           #ifndef PERSONH
           #define PERSONH
           #include <g2++.h>
           #include <Vblock.h>
           #include <String.h>
           #include "Time.h"
           #include "Timeio.h"
           class istream;
           class ostream;
           typedef struct PERSON{
                   String id;
                   short   age;
                   Time    b_day;
                   Vblock<String> hobbies;  see Vblock(3C++)
                   PERSON();
           }PERSON;
           istream& operator>>(istream& is, PERSON& x);
           ostream& operator<<(ostream& os, const PERSON& x);
           #endif

The following "batch" update program adds 1 to the age of every person whose birthday is today.

           #include "person.h"
           #include <time.h>
           #include <Time.h>
           main(){
               PERSON p;
               Time today = make_time(time(0));
               while(cin >> p){
                   if(p.b_day==today){
                        p.age += 1;
                   }
                   cout << p;
               }
           }

Notes

Applications without a priori knowledge of record types must use untyped I/O (see untyped_io(3C++)).

References

G2++(4C++), g2++comp(1C++), G2++(3C++), String(3C++), untyped_io(3C++), Vblock(3C++)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004