1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
26 //------------------------------------------------------------------------
28 //------------------------------------------------------------------------
31 int num; // object number
32 int gen; // generation number
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
50 objDict, // dictionary
52 objRef, // indirect reference
55 objCmd, // command name
56 objError, // error return from Lexer
57 objEOF, // end of file return from Lexer
58 objNone // uninitialized object
61 #define numObjTypes 14 // total number of object types
63 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
68 #define initObj(t) ++numAlloc[type = t]
70 #define initObj(t) type = t
76 // Default constructor.
80 // Initialize an object.
81 Object *initBool(GBool booln1)
82 { initObj(objBool); booln = booln1; return this; }
83 Object *initInt(int intg1)
84 { initObj(objInt); intg = intg1; return this; }
85 Object *initReal(double real1)
86 { initObj(objReal); real = real1; return this; }
87 Object *initString(GString *string1)
88 { initObj(objString); string = string1; return this; }
89 Object *initName(char *name1)
90 { initObj(objName); name = copyString(name1); return this; }
92 { initObj(objNull); return this; }
95 Object *initDict(Dict *dict1)
96 { initObj(objDict); dict = dict1; return this; }
97 Object *initStream(Stream *stream1);
98 Object *initRef(int num1, int gen1)
99 { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
100 Object *initCmd(char *cmd1)
101 { initObj(objCmd); cmd = copyString(cmd1); return this; }
103 { initObj(objError); return this; }
105 { initObj(objEOF); return this; }
108 Object *copy(Object *obj);
110 // If object is a Ref, fetch and return the referenced object.
111 // Otherwise, return a copy of the object.
112 Object *fetch(Object *obj);
114 // Free object contents.
118 ObjType getType() { return type; }
119 GBool isBool() { return type == objBool; }
120 GBool isInt() { return type == objInt; }
121 GBool isReal() { return type == objReal; }
122 GBool isNum() { return type == objInt || type == objReal; }
123 GBool isString() { return type == objString; }
124 GBool isName() { return type == objName; }
125 GBool isNull() { return type == objNull; }
126 GBool isArray() { return type == objArray; }
127 GBool isDict() { return type == objDict; }
128 GBool isStream() { return type == objStream; }
129 GBool isRef() { return type == objRef; }
130 GBool isCmd() { return type == objCmd; }
131 GBool isError() { return type == objError; }
132 GBool isEOF() { return type == objEOF; }
133 GBool isNone() { return type == objNone; }
135 // Special type checking.
136 GBool isName(char *name1)
137 { return type == objName && !strcmp(name, name1); }
138 GBool isDict(char *dictType);
139 GBool isStream(char *dictType);
140 GBool isCmd(char *cmd1)
141 { return type == objCmd && !strcmp(cmd, cmd1); }
143 // Accessors. NB: these assume object is of correct type.
144 GBool getBool() { return booln; }
145 int getInt() { return intg; }
146 double getReal() { return real; }
147 double getNum() { return type == objInt ? (double)intg : real; }
148 GString *getString() { return string; }
149 char *getName() { return name; }
150 Array *getArray() { return array; }
151 Dict *getDict() { return dict; }
152 Stream *getStream() { return stream; }
153 Ref getRef() { return ref; }
154 int getRefNum() { return ref.num; }
155 int getRefGen() { return ref.gen; }
158 int arrayGetLength();
159 void arrayAdd(Object *elem);
160 Object *arrayGet(int i, Object *obj);
161 Object *arrayGetNF(int i, Object *obj);
165 void dictAdd(char *key, Object *val);
166 GBool dictIs(char *dictType);
167 Object *dictLookup(char *key, Object *obj);
168 Object *dictLookupNF(char *key, Object *obj);
169 char *dictGetKey(int i);
170 Object *dictGetVal(int i, Object *obj);
171 Object *dictGetValNF(int i, Object *obj);
174 GBool streamIs(char *dictType);
178 int streamLookChar();
179 char *streamGetLine(char *buf, int size);
181 void streamSetPos(int pos);
182 Dict *streamGetDict();
186 void print(FILE *f = stdout);
189 static void memCheck(FILE *f);
193 ObjType type; // object type
194 union { // value for each type:
195 GBool booln; // boolean
198 GString *string; // string
200 Array *array; // array
201 Dict *dict; // dictionary
202 Stream *stream; // stream
203 Ref ref; // indirect reference
204 char *cmd; // command
208 static int // number of each type of object
209 numAlloc[numObjTypes]; // currently allocated
213 //------------------------------------------------------------------------
215 //------------------------------------------------------------------------
219 inline int Object::arrayGetLength()
220 { return array->getLength(); }
222 inline void Object::arrayAdd(Object *elem)
223 { array->add(elem); }
225 inline Object *Object::arrayGet(int i, Object *obj)
226 { return array->get(i, obj); }
228 inline Object *Object::arrayGetNF(int i, Object *obj)
229 { return array->getNF(i, obj); }
231 //------------------------------------------------------------------------
233 //------------------------------------------------------------------------
237 inline int Object::dictGetLength()
238 { return dict->getLength(); }
240 inline void Object::dictAdd(char *key, Object *val)
241 { dict->add(key, val); }
243 inline GBool Object::dictIs(char *dictType)
244 { return dict->is(dictType); }
246 inline GBool Object::isDict(char *dictType)
247 { return type == objDict && dictIs(dictType); }
249 inline Object *Object::dictLookup(char *key, Object *obj)
250 { return dict->lookup(key, obj); }
252 inline Object *Object::dictLookupNF(char *key, Object *obj)
253 { return dict->lookupNF(key, obj); }
255 inline char *Object::dictGetKey(int i)
256 { return dict->getKey(i); }
258 inline Object *Object::dictGetVal(int i, Object *obj)
259 { return dict->getVal(i, obj); }
261 inline Object *Object::dictGetValNF(int i, Object *obj)
262 { return dict->getValNF(i, obj); }
264 //------------------------------------------------------------------------
266 //------------------------------------------------------------------------
270 inline GBool Object::streamIs(char *dictType)
271 { return stream->getDict()->is(dictType); }
273 inline GBool Object::isStream(char *dictType)
274 { return type == objStream && streamIs(dictType); }
276 inline void Object::streamReset()
279 inline void Object::streamClose()
282 inline int Object::streamGetChar()
283 { return stream->getChar(); }
285 inline int Object::streamLookChar()
286 { return stream->lookChar(); }
288 inline char *Object::streamGetLine(char *buf, int size)
289 { return stream->getLine(buf, size); }
291 inline int Object::streamGetPos()
292 { return stream->getPos(); }
294 inline void Object::streamSetPos(int pos)
295 { stream->setPos(pos); }
297 inline Dict *Object::streamGetDict()
298 { return stream->getDict(); }