w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Object.h
Go to the documentation of this file.
1 //========================================================================
2 //
3 // Object.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 #ifndef OBJECT_H
10 #define OBJECT_H
11 
12 #include <aconf.h>
13 
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include "gtypes.h"
21 #include "gmem.h"
22 #include "gfile.h"
23 #include "GString.h"
24 #if MULTITHREADED
25 #include "GMutex.h"
26 #endif
27 
28 class XRef;
29 class Array;
30 class Dict;
31 class Stream;
32 
33 //------------------------------------------------------------------------
34 // Ref
35 //------------------------------------------------------------------------
36 
37 struct Ref {
38  int num; // object number
39  int gen; // generation number
40 };
41 
42 //------------------------------------------------------------------------
43 // object types
44 //------------------------------------------------------------------------
45 
46 enum ObjType {
47  // simple objects
48  objBool, // boolean
49  objInt, // integer
50  objReal, // real
51  objString, // string
52  objName, // name
53  objNull, // null
54 
55  // complex objects
56  objArray, // array
57  objDict, // dictionary
58  objStream, // stream
59  objRef, // indirect reference
60 
61  // special objects
62  objCmd, // command name
63  objError, // error return from Lexer
64  objEOF, // end of file return from Lexer
65  objNone // uninitialized object
66 };
67 
68 #define numObjTypes 14 // total number of object types
69 
70 //------------------------------------------------------------------------
71 // Object
72 //------------------------------------------------------------------------
73 
74 #ifdef DEBUG_MEM
75 # if MULTITHREADED
76 # define initObj(t) gAtomicIncrement(&numAlloc[type = t])
77 # else
78 # define initObj(t) ++numAlloc[type = t]
79 # endif
80 #else
81 #define initObj(t) type = t
82 #endif
83 
84 class Object {
85 public:
86 
87  // Default constructor.
88  Object():
89  type(objNone) {}
90 
91  // Initialize an object.
93  { initObj(objBool); booln = boolnA; return this; }
94  Object *initInt(int intgA)
95  { initObj(objInt); intg = intgA; return this; }
96  Object *initReal(double realA)
97  { initObj(objReal); real = realA; return this; }
99  { initObj(objString); string = stringA; return this; }
100  Object *initName(const char *nameA)
101  { initObj(objName); name = copyString(nameA); return this; }
103  { initObj(objNull); return this; }
106  Object *initDict(Dict *dictA);
107  Object *initStream(Stream *streamA);
108  Object *initRef(int numA, int genA)
109  { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
110  Object *initCmd(char *cmdA)
111  { initObj(objCmd); cmd = copyString(cmdA); return this; }
113  { initObj(objError); return this; }
115  { initObj(objEOF); return this; }
116 
117  // Copy an object.
118  Object *copy(Object *obj);
119 
120  // If object is a Ref, fetch and return the referenced object.
121  // Otherwise, return a copy of the object.
122  Object *fetch(XRef *xref, Object *obj, int recursion = 0);
123 
124  // Free object contents.
125  void free();
126 
127  // Type checking.
128  ObjType getType() { return type; }
129  GBool isBool() { return type == objBool; }
130  GBool isInt() { return type == objInt; }
131  GBool isReal() { return type == objReal; }
132  GBool isNum() { return type == objInt || type == objReal; }
133  GBool isString() { return type == objString; }
134  GBool isName() { return type == objName; }
135  GBool isNull() { return type == objNull; }
136  GBool isArray() { return type == objArray; }
137  GBool isDict() { return type == objDict; }
138  GBool isStream() { return type == objStream; }
139  GBool isRef() { return type == objRef; }
140  GBool isCmd() { return type == objCmd; }
141  GBool isError() { return type == objError; }
142  GBool isEOF() { return type == objEOF; }
143  GBool isNone() { return type == objNone; }
144 
145  // Special type checking.
146  GBool isName(const char *nameA)
147  { return type == objName && !strcmp(name, nameA); }
148  GBool isDict(const char *dictType);
149  GBool isStream(char *dictType);
150  GBool isCmd(const char *cmdA)
151  { return type == objCmd && !strcmp(cmd, cmdA); }
152 
153  // Accessors. NB: these assume object is of correct type.
154  GBool getBool() { return booln; }
155  int getInt() { return intg; }
156  double getReal() { return real; }
157  double getNum() { return type == objInt ? (double)intg : real; }
158  GString *getString() { return string; }
159  char *getName() { return name; }
160  Array *getArray() { return array; }
161  Dict *getDict() { return dict; }
162  Stream *getStream() { return stream; }
163  Ref getRef() { return ref; }
164  int getRefNum() { return ref.num; }
165  int getRefGen() { return ref.gen; }
166  char *getCmd() { return cmd; }
167 
168  // Array accessors.
169  int arrayGetLength();
170  void arrayAdd(Object *elem);
171  Object *arrayGet(int i, Object *obj, int recursion = 0);
172  Object *arrayGetNF(int i, Object *obj);
173 
174  // Dict accessors.
175  int dictGetLength();
176  void dictAdd(char *key, Object *val);
177  GBool dictIs(const char *dictType);
178  Object *dictLookup(const char *key, Object *obj, int recursion = 0);
179  Object *dictLookupNF(const char *key, Object *obj);
180  char *dictGetKey(int i);
181  Object *dictGetVal(int i, Object *obj);
182  Object *dictGetValNF(int i, Object *obj);
183 
184  // Stream accessors.
185  GBool streamIs(char *dictType);
186  void streamReset();
187  void streamClose();
188  int streamGetChar();
189  int streamLookChar();
190  int streamGetBlock(char *blk, int size);
191  char *streamGetLine(char *buf, int size);
193  void streamSetPos(GFileOffset pos, int dir = 0);
194  Dict *streamGetDict();
195 
196  // Output.
197  const char *getTypeName();
198  void print(FILE *f = stdout);
199 
200  // Memory testing.
201  static void memCheck(FILE *f);
202 
203 private:
204 
205  ObjType type; // object type
206  union { // value for each type:
207  GBool booln; // boolean
208  int intg; // integer
209  double real; // real
210  GString *string; // string
211  char *name; // name
212  Array *array; // array
213  Dict *dict; // dictionary
214  Stream *stream; // stream
215  Ref ref; // indirect reference
216  char *cmd; // command
217  };
218 
219 #ifdef DEBUG_MEM
220 #if MULTITHREADED
221  static GAtomicCounter // number of each type of object
222  numAlloc[numObjTypes]; // currently allocated
223 #else
224  static int // number of each type of object
225  numAlloc[numObjTypes]; // currently allocated
226 #endif
227 #endif
228 };
229 
230 //------------------------------------------------------------------------
231 // Array accessors.
232 //------------------------------------------------------------------------
233 
234 #include "Array.h"
235 
237  { return array->getLength(); }
238 
239 inline void Object::arrayAdd(Object *elem)
240  { array->add(elem); }
241 
242 inline Object *Object::arrayGet(int i, Object *obj, int recursion)
243  { return array->get(i, obj, recursion); }
244 
245 inline Object *Object::arrayGetNF(int i, Object *obj)
246  { return array->getNF(i, obj); }
247 
248 //------------------------------------------------------------------------
249 // Dict accessors.
250 //------------------------------------------------------------------------
251 
252 #include "Dict.h"
253 
255  { return dict->getLength(); }
256 
257 inline void Object::dictAdd(char *key, Object *val)
258  { dict->add(key, val); }
259 
260 inline GBool Object::dictIs(const char *dictType)
261  { return dict->is(dictType); }
262 
263 inline GBool Object::isDict(const char *dictType)
264  { return type == objDict && dictIs(dictType); }
265 
266 inline Object *Object::dictLookup(const char *key, Object *obj, int recursion)
267  { return dict->lookup(key, obj, recursion); }
268 
269 inline Object *Object::dictLookupNF(const char *key, Object *obj)
270  { return dict->lookupNF(key, obj); }
271 
272 inline char *Object::dictGetKey(int i)
273  { return dict->getKey(i); }
274 
275 inline Object *Object::dictGetVal(int i, Object *obj)
276  { return dict->getVal(i, obj); }
277 
279  { return dict->getValNF(i, obj); }
280 
281 //------------------------------------------------------------------------
282 // Stream accessors.
283 //------------------------------------------------------------------------
284 
285 #include "Stream.h"
286 
287 inline GBool Object::streamIs(char *dictType)
288  { return stream->getDict()->is(dictType); }
289 
290 inline GBool Object::isStream(char *dictType)
291  { return type == objStream && streamIs(dictType); }
292 
293 inline void Object::streamReset()
294  { stream->reset(); }
295 
296 inline void Object::streamClose()
297  { stream->close(); }
298 
300  { return stream->getChar(); }
301 
303  { return stream->lookChar(); }
304 
305 inline int Object::streamGetBlock(char *blk, int size)
306  { return stream->getBlock(blk, size); }
307 
308 inline char *Object::streamGetLine(char *buf, int size)
309  { return stream->getLine(buf, size); }
310 
312  { return stream->getPos(); }
313 
315  { stream->setPos(pos, dir); }
316 
318  { return stream->getDict(); }
319 
320 #endif
long GAtomicCounter
Definition: GMutex.h:63
Definition: Array.h:29
Definition: Dict.h:29
char * getKey(int i)
Definition: Dict.cc:135
void add(char *key, Object *val)
Definition: Dict.cc:56
GBool is(const char *type)
Definition: Dict.cc:116
int getLength()
Definition: Dict.h:48
Object * lookupNF(const char *key, Object *obj)
Definition: Dict.cc:129
Object * getVal(int i, Object *obj)
Definition: Dict.cc:139
Object * lookup(const char *key, Object *obj, int recursion=0)
Definition: Dict.cc:122
Object * getValNF(int i, Object *obj)
Definition: Dict.cc:143
Definition: Object.h:84
GFileOffset streamGetPos()
Definition: Object.h:311
Ref getRef()
Definition: Object.h:163
Object copy() const
Definition: Object.cc:42
GBool isDict()
Definition: Object.h:137
Object * dictLookup(const char *key, Object *obj, int recursion=0)
Definition: Object.h:266
Stream * stream
Definition: Object.h:214
double getReal()
Definition: Object.h:156
Object * initCmd(char *cmdA)
Definition: Object.h:110
char * name
Definition: Object.h:211
Stream * getStream()
Definition: Object.h:162
char * getCmd()
Definition: Object.h:166
void print(FILE *f=stdout)
Definition: Object.cc:160
Object * initBool(GBool boolnA)
Definition: Object.h:92
GBool dictIs(const char *dictType)
Definition: Object.h:260
GBool isNum()
Definition: Object.h:132
int intg
Definition: Object.h:208
Array * getArray()
Definition: Object.h:160
Object * initName(const char *nameA)
Definition: Object.h:100
char * cmd
Definition: Object.h:216
Object()
Definition: Object.h:88
void streamClose()
Definition: Object.h:296
GBool booln
Definition: Object.h:207
GBool isReal()
Definition: Object.h:131
Object * initReal(double realA)
Definition: Object.h:96
double getNum()
Definition: Object.h:157
Object * arrayGetNF(int i, Object *obj)
Definition: Object.h:245
GBool isName()
Definition: Object.h:134
const char * getTypeName()
Definition: Object.cc:156
void free()
Definition: Object.cc:119
GBool isStream()
Definition: Object.h:138
GBool isRef()
Definition: Object.h:139
GBool getBool()
Definition: Object.h:154
int getRefGen()
Definition: Object.h:165
Object * fetch(XRef *xref, Object *obj, int recursion=0)
Definition: Object.cc:114
void arrayAdd(Object *elem)
Definition: Object.h:239
int dictGetLength()
Definition: Object.h:254
GBool streamIs(char *dictType)
Definition: Object.h:287
char * streamGetLine(char *buf, int size)
Definition: Object.h:308
char * getName()
Definition: Object.h:159
ObjType type
Definition: Object.h:205
void dictAdd(char *key, Object *val)
Definition: Object.h:257
int getInt()
Definition: Object.h:155
GBool isNone()
Definition: Object.h:143
GBool isInt()
Definition: Object.h:130
Dict * streamGetDict()
Definition: Object.h:317
Object * initArray(XRef *xref)
Definition: Object.cc:55
Object * initNull()
Definition: Object.h:102
ObjType getType()
Definition: Object.h:128
GBool isEOF()
Definition: Object.h:142
GBool isCmd()
Definition: Object.h:140
int arrayGetLength()
Definition: Object.h:236
GString * string
Definition: Object.h:210
Ref ref
Definition: Object.h:215
GBool isName(const char *nameA)
Definition: Object.h:146
static void memCheck(FILE *f)
Definition: Object.cc:227
GBool isArray()
Definition: Object.h:136
GBool isCmd(const char *cmdA)
Definition: Object.h:150
Dict * dict
Definition: Object.h:213
void streamReset()
Definition: Object.h:293
Object * initString(GString *stringA)
Definition: Object.h:98
int streamGetBlock(char *blk, int size)
Definition: Object.h:305
int streamGetChar()
Definition: Object.h:299
Object * initDict(XRef *xref)
Definition: Object.cc:61
Object * initRef(int numA, int genA)
Definition: Object.h:108
GBool isString()
Definition: Object.h:133
void streamSetPos(GFileOffset pos, int dir=0)
Definition: Object.h:314
GString * getString()
Definition: Object.h:158
GBool isError()
Definition: Object.h:141
double real
Definition: Object.h:209
int streamLookChar()
Definition: Object.h:302
Dict * getDict()
Definition: Object.h:161
char * dictGetKey(int i)
Definition: Object.h:272
Object * dictLookupNF(const char *key, Object *obj)
Definition: Object.h:269
Object * initInt(int intgA)
Definition: Object.h:94
Object * dictGetVal(int i, Object *obj)
Definition: Object.h:275
Object * initStream(Stream *streamA)
Definition: Object.cc:74
GBool isNull()
Definition: Object.h:135
int getRefNum()
Definition: Object.h:164
Object * dictGetValNF(int i, Object *obj)
Definition: Object.h:278
Array * array
Definition: Object.h:212
Object * initEOF()
Definition: Object.h:114
Object * initError()
Definition: Object.h:112
Object * arrayGet(int i, Object *obj, int recursion=0)
Definition: Object.h:242
GBool isBool()
Definition: Object.h:129
Definition: Stream.h:67
Definition: XRef.h:58
double real
Definition: dvips.h:66
int strcmp()
Definition: coll.cpp:143
mpz_t * f
Definition: gen-fib.c:34
char * copyString(const char *s)
Definition: gmem.cc:390
int GBool
Definition: gtypes.h:16
small capitals from c petite p scientific i
Definition: afcover.h:80
long GFileOffset
Definition: gfile.h:115
ObjType
Definition: Object.h:46
@ objCmd
Definition: Object.h:62
@ objBool
Definition: Object.h:48
@ objNone
Definition: Object.h:65
@ objNull
Definition: Object.h:53
@ objArray
Definition: Object.h:56
@ objString
Definition: Object.h:51
@ objDict
Definition: Object.h:57
@ objStream
Definition: Object.h:58
@ objError
Definition: Object.h:63
@ objRef
Definition: Object.h:59
@ objInt
Definition: Object.h:49
@ objReal
Definition: Object.h:50
@ objName
Definition: Object.h:52
@ objEOF
Definition: Object.h:64
#define numObjTypes
Definition: Object.h:68
#define initObj(t)
Definition: Object.h:81
#define buf
const int * pos
Definition: combiners.h:905
static int size
Definition: ppmlabel.c:24
struct stream_s stream
Definition: pts_fax.h:93
#define dir
Definition: Object.h:37
int gen
Definition: Object.h:39
int num
Definition: Object.h:38
Definition: strexpr.c:21
#define FILE
Definition: t1stdio.h:34
#define xref
Definition: tex2xindy.c:758
#define key
Definition: tex2xindy.c:753