1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
27 //------------------------------------------------------------------------
29 //------------------------------------------------------------------------
32 int num; // object number
33 int gen; // generation number
36 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
51 objDict, // dictionary
53 objRef, // indirect reference
56 objCmd, // command name
57 objError, // error return from Lexer
58 objEOF, // end of file return from Lexer
59 objNone // uninitialized object
62 #define numObjTypes 14 // total number of object types
64 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
69 #define initObj(t) ++numAlloc[type = t]
71 #define initObj(t) type = t
77 // Default constructor.
81 // Initialize an object.
82 Object *initBool(GBool booln1)
83 { initObj(objBool); booln = booln1; return this; }
84 Object *initInt(int intg1)
85 { initObj(objInt); intg = intg1; return this; }
86 Object *initReal(double real1)
87 { initObj(objReal); real = real1; return this; }
88 Object *initString(GString *string1)
89 { initObj(objString); string = string1; return this; }
90 Object *initName(char *name1)
91 { initObj(objName); name = copyString(name1); return this; }
93 { initObj(objNull); return this; }
96 Object *initDict(Dict *dict1)
97 { initObj(objDict); dict = dict1; return this; }
98 Object *initStream(Stream *stream1);
99 Object *initRef(int num1, int gen1)
100 { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
101 Object *initCmd(char *cmd1)
102 { initObj(objCmd); cmd = copyString(cmd1); return this; }
104 { initObj(objError); return this; }
106 { initObj(objEOF); return this; }
109 Object *copy(Object *obj);
111 // If object is a Ref, fetch and return the referenced object.
112 // Otherwise, return a copy of the object.
113 Object *fetch(Object *obj);
115 // Free object contents.
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);
177 int streamLookChar();
178 char *streamGetLine(char *buf, int size);
180 void streamSetPos(int pos);
181 BaseFile streamGetFile();
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 int Object::streamGetChar()
280 { return stream->getChar(); }
282 inline int Object::streamLookChar()
283 { return stream->lookChar(); }
285 inline char *Object::streamGetLine(char *buf, int size)
286 { return stream->getLine(buf, size); }
288 inline int Object::streamGetPos()
289 { return stream->getPos(); }
291 inline void Object::streamSetPos(int pos)
292 { stream->setPos(pos); }
294 inline BaseFile Object::streamGetFile()
295 { return stream->getFile(); }
297 inline Dict *Object::streamGetDict()
298 { return stream->getDict(); }