00001
00002
00003
00004
00005
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
00029
00030
00031 struct Ref {
00032 int num;
00033 int gen;
00034 };
00035
00036
00037
00038
00039
00040 enum ObjType {
00041
00042 objBool,
00043 objInt,
00044 objReal,
00045 objString,
00046 objName,
00047 objNull,
00048
00049
00050 objArray,
00051 objDict,
00052 objStream,
00053 objRef,
00054
00055
00056 objCmd,
00057 objError,
00058 objEOF,
00059 objNone
00060 };
00061
00062 #define numObjTypes 14 // total number of object types
00063
00064
00065
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
00078 Object():
00079 type(objNone) {}
00080
00081
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
00107 Object *copy(Object *obj);
00108
00109
00110
00111 Object *fetch(XRef *xref, Object *obj);
00112
00113
00114 void free();
00115
00116
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
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
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
00157 int arrayGetLength();
00158 void arrayAdd(Object *elem);
00159 Object *arrayGet(int i, Object *obj);
00160 Object *arrayGetNF(int i, Object *obj);
00161
00162
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
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
00184 char *getTypeName();
00185 void print(FILE *f = stdout);
00186
00187
00188 static void memCheck(FILE *f);
00189
00190 private:
00191
00192 ObjType type;
00193 fouble real;
00194 union {
00195 GBool booln;
00196 int intg;
00197 GString *string;
00198 char *name;
00199 Array *array;
00200 Dict *dict;
00201 Stream *stream;
00202 Ref ref;
00203 char *cmd;
00204 };
00205
00206 #ifdef DEBUG_MEM
00207 static int
00208 numAlloc[numObjTypes];
00209 #endif
00210 };
00211
00212
00213
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
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
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