Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Object.h

Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // Object.h
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef OBJECT_H
00010 #define OBJECT_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include "gtypes.h"
00021 #include "gmem.h"
00022 #include "GString.h"
00023 
00024 class XRef;
00025 class Array;
00026 class Dict;
00027 class Stream;
00028 
00029 //------------------------------------------------------------------------
00030 // Ref
00031 //------------------------------------------------------------------------
00032 
00033 struct Ref {
00034   int num;                      // object number
00035   int gen;                      // generation number
00036 };
00037 
00038 //------------------------------------------------------------------------
00039 // object types
00040 //------------------------------------------------------------------------
00041 
00042 enum ObjType {
00043   // simple objects
00044   objBool,                      // boolean
00045   objInt,                       // integer
00046   objReal,                      // real
00047   objString,                    // string
00048   objName,                      // name
00049   objNull,                      // null
00050 
00051   // complex objects
00052   objArray,                     // array
00053   objDict,                      // dictionary
00054   objStream,                    // stream
00055   objRef,                       // indirect reference
00056 
00057   // special objects
00058   objCmd,                       // command name
00059   objError,                     // error return from Lexer
00060   objEOF,                       // end of file return from Lexer
00061   objNone                       // uninitialized object
00062 };
00063 
00064 #define numObjTypes 14          // total number of object types
00065 
00066 //------------------------------------------------------------------------
00067 // Object
00068 //------------------------------------------------------------------------
00069 
00070 #ifdef DEBUG_MEM
00071 #define initObj(t) ++numAlloc[type = t]
00072 #else
00073 #define initObj(t) type = t
00074 #endif
00075 
00076 class Object {
00077 public:
00078 
00079   // Default constructor.
00080   Object():
00081     type(objNone) {}
00082 
00083   // Initialize an object.
00084   Object *initBool(GBool boolnA)
00085     { initObj(objBool); booln = boolnA; return this; }
00086   Object *initInt(int intgA)
00087     { initObj(objInt); intg = intgA; return this; }
00088   Object *initReal(double realA)
00089     { initObj(objReal); real = realA; return this; }
00090   Object *initString(GString *stringA)
00091     { initObj(objString); string = stringA; return this; }
00092   Object *initName(char *nameA)
00093     { initObj(objName); name = copyString(nameA); return this; }
00094   Object *initNull()
00095     { initObj(objNull); return this; }
00096   Object *initArray(XRef *xref);
00097   Object *initDict(XRef *xref);
00098   Object *initDict(Dict *dictA);
00099   Object *initStream(Stream *streamA);
00100   Object *initRef(int numA, int genA)
00101     { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
00102   Object *initCmd(char *cmdA)
00103     { initObj(objCmd); cmd = copyString(cmdA); return this; }
00104   Object *initError()
00105     { initObj(objError); return this; }
00106   Object *initEOF()
00107     { initObj(objEOF); return this; }
00108 
00109   // Copy an object.
00110   Object *copy(Object *obj);
00111 
00112   // If object is a Ref, fetch and return the referenced object.
00113   // Otherwise, return a copy of the object.
00114   Object *fetch(XRef *xref, Object *obj);
00115 
00116   // Free object contents.
00117   void free();
00118 
00119   // Type checking.
00120   ObjType getType() { return type; }
00121   GBool isBool() { return type == objBool; }
00122   GBool isInt() { return type == objInt; }
00123   GBool isReal() { return type == objReal; }
00124   GBool isNum() { return type == objInt || type == objReal; }
00125   GBool isString() { return type == objString; }
00126   GBool isName() { return type == objName; }
00127   GBool isNull() { return type == objNull; }
00128   GBool isArray() { return type == objArray; }
00129   GBool isDict() { return type == objDict; }
00130   GBool isStream() { return type == objStream; }
00131   GBool isRef() { return type == objRef; }
00132   GBool isCmd() { return type == objCmd; }
00133   GBool isError() { return type == objError; }
00134   GBool isEOF() { return type == objEOF; }
00135   GBool isNone() { return type == objNone; }
00136 
00137   // Special type checking.
00138   GBool isName(char *nameA)
00139     { return type == objName && !strcmp(name, nameA); }
00140   GBool isDict(char *dictType);
00141   GBool isStream(char *dictType);
00142   GBool isCmd(char *cmdA)
00143     { return type == objCmd && !strcmp(cmd, cmdA); }
00144 
00145   // Accessors.  NB: these assume object is of correct type.
00146   GBool getBool() { return booln; }
00147   int getInt() { return intg; }
00148   double getReal() { return real; }
00149   double getNum() { return type == objInt ? (double)intg : real; }
00150   GString *getString() { return string; }
00151   char *getName() { return name; }
00152   Array *getArray() { return array; }
00153   Dict *getDict() { return dict; }
00154   Stream *getStream() { return stream; }
00155   Ref getRef() { return ref; }
00156   int getRefNum() { return ref.num; }
00157   int getRefGen() { return ref.gen; }
00158   char *getCmd() { return cmd; }
00159 
00160   // Array accessors.
00161   int arrayGetLength();
00162   void arrayAdd(Object *elem);
00163   Object *arrayGet(int i, Object *obj);
00164   Object *arrayGetNF(int i, Object *obj);
00165 
00166   // Dict accessors.
00167   int dictGetLength();
00168   void dictAdd(char *key, Object *val);
00169   GBool dictIs(char *dictType);
00170   Object *dictLookup(char *key, Object *obj);
00171   Object *dictLookupNF(char *key, Object *obj);
00172   char *dictGetKey(int i);
00173   Object *dictGetVal(int i, Object *obj);
00174   Object *dictGetValNF(int i, Object *obj);
00175 
00176   // Stream accessors.
00177   GBool streamIs(char *dictType);
00178   void streamReset();
00179   void streamClose();
00180   int streamGetChar();
00181   int streamLookChar();
00182   char *streamGetLine(char *buf, int size);
00183   Guint streamGetPos();
00184   void streamSetPos(Guint pos, int dir = 0);
00185   Dict *streamGetDict();
00186 
00187   // Output.
00188   char *getTypeName();
00189   void print(FILE *f = stdout);
00190 
00191   // Memory testing.
00192   static void memCheck(FILE *f);
00193 
00194 private:
00195 
00196   ObjType type;                 // object type
00197   union {                       // value for each type:
00198     GBool booln;                //   boolean
00199     int intg;                   //   integer
00200     double real;                //   real
00201     GString *string;            //   string
00202     char *name;                 //   name
00203     Array *array;               //   array
00204     Dict *dict;                 //   dictionary
00205     Stream *stream;             //   stream
00206     Ref ref;                    //   indirect reference
00207     char *cmd;                  //   command
00208   };
00209 
00210 #ifdef DEBUG_MEM
00211   static int                    // number of each type of object
00212     numAlloc[numObjTypes];      //   currently allocated
00213 #endif
00214 };
00215 
00216 //------------------------------------------------------------------------
00217 // Array accessors.
00218 //------------------------------------------------------------------------
00219 
00220 #include "Array.h"
00221 
00222 inline int Object::arrayGetLength()
00223   { return array->getLength(); }
00224 
00225 inline void Object::arrayAdd(Object *elem)
00226   { array->add(elem); }
00227 
00228 inline Object *Object::arrayGet(int i, Object *obj)
00229   { return array->get(i, obj); }
00230 
00231 inline Object *Object::arrayGetNF(int i, Object *obj)
00232   { return array->getNF(i, obj); }
00233 
00234 //------------------------------------------------------------------------
00235 // Dict accessors.
00236 //------------------------------------------------------------------------
00237 
00238 #include "Dict.h"
00239 
00240 inline int Object::dictGetLength()
00241   { return dict->getLength(); }
00242 
00243 inline void Object::dictAdd(char *key, Object *val)
00244   { dict->add(key, val); }
00245 
00246 inline GBool Object::dictIs(char *dictType)
00247   { return dict->is(dictType); }
00248 
00249 inline GBool Object::isDict(char *dictType)
00250   { return type == objDict && dictIs(dictType); }
00251 
00252 inline Object *Object::dictLookup(char *key, Object *obj)
00253   { return dict->lookup(key, obj); }
00254 
00255 inline Object *Object::dictLookupNF(char *key, Object *obj)
00256   { return dict->lookupNF(key, obj); }
00257 
00258 inline char *Object::dictGetKey(int i)
00259   { return dict->getKey(i); }
00260 
00261 inline Object *Object::dictGetVal(int i, Object *obj)
00262   { return dict->getVal(i, obj); }
00263 
00264 inline Object *Object::dictGetValNF(int i, Object *obj)
00265   { return dict->getValNF(i, obj); }
00266 
00267 //------------------------------------------------------------------------
00268 // Stream accessors.
00269 //------------------------------------------------------------------------
00270 
00271 #include "Stream.h"
00272 
00273 inline GBool Object::streamIs(char *dictType)
00274   { return stream->getDict()->is(dictType); }
00275 
00276 inline GBool Object::isStream(char *dictType)
00277   { return type == objStream && streamIs(dictType); }
00278 
00279 inline void Object::streamReset()
00280   { stream->reset(); }
00281 
00282 inline void Object::streamClose()
00283   { stream->close(); }
00284 
00285 inline int Object::streamGetChar()
00286   { return stream->getChar(); }
00287 
00288 inline int Object::streamLookChar()
00289   { return stream->lookChar(); }
00290 
00291 inline char *Object::streamGetLine(char *buf, int size)
00292   { return stream->getLine(buf, size); }
00293 
00294 inline Guint Object::streamGetPos()
00295   { return stream->getPos(); }
00296 
00297 inline void Object::streamSetPos(Guint pos, int dir)
00298   { stream->setPos(pos, dir); }
00299 
00300 inline Dict *Object::streamGetDict()
00301   { return stream->getDict(); }
00302 
00303 #endif

Generated on Wed Nov 3 12:59:00 2004 for Lemur Toolkit by doxygen1.2.18