Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Object.h

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

Generated on Sat Nov 5 16:18:16 2005 for OPIE by  doxygen 1.4.2