source: trunk/poppler/mypoppler/poppler/Object.h @ 461

Last change on this file since 461 was 461, checked in by Silvan Scherrer, 11 years ago

poppler update to 0.14.2

File size: 11.4 KB
Line 
1//========================================================================
2//
3// Object.h
4//
5// Copyright 1996-2003 Glyph & Cog, LLC
6//
7//========================================================================
8
9//========================================================================
10//
11// Modified under the Poppler project - http://poppler.freedesktop.org
12//
13// All changes made under the Poppler project to this file are licensed
14// under GPL version 2 or later
15//
16// Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
17// Copyright (C) 2008 Kees Cook <kees@outflux.net>
18// Copyright (C) 2008, 2010 Albert Astals Cid <aacid@kde.org>
19// Copyright (C) 2009 Jakub Wilk <ubanus@users.sf.net>
20//
21// To see a description of the changes please see the Changelog file that
22// came with your tarball or type make ChangeLog if you are building from git
23//
24//========================================================================
25
26#ifndef OBJECT_H
27#define OBJECT_H
28
29#ifdef USE_GCC_PRAGMAS
30#pragma interface
31#endif
32
33#include <stdio.h>
34#include <string.h>
35#include "goo/gtypes.h"
36#include "goo/gmem.h"
37#include "goo/GooString.h"
38#include "Error.h"
39
40#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
41# define likely(x)      __builtin_expect((x), 1)
42# define unlikely(x)    __builtin_expect((x), 0)
43#else
44# define likely(x)      (x)
45# define unlikely(x)    (x)
46#endif
47
48#define OBJECT_TYPE_CHECK(wanted_type) \
49    if (unlikely(type != wanted_type)) { \
50        error(0, (char *) "Call to Object where the object was type %d, " \
51                 "not the expected type %d", type, wanted_type); \
52        abort(); \
53    }
54
55#define OBJECT_2TYPES_CHECK(wanted_type1, wanted_type2) \
56    if (unlikely(type != wanted_type1) && unlikely(type != wanted_type2)) { \
57        error(0, (char *) "Call to Object where the object was type %d, " \
58                 "not the expected type %d or %d", type, wanted_type1, wanted_type2); \
59        abort(); \
60    }
61
62class XRef;
63class Array;
64class Dict;
65class Stream;
66
67//------------------------------------------------------------------------
68// Ref
69//------------------------------------------------------------------------
70
71struct Ref {
72  int num;                      // object number
73  int gen;                      // generation number
74};
75
76//------------------------------------------------------------------------
77// object types
78//------------------------------------------------------------------------
79
80enum ObjType {
81  // simple objects
82  objBool,                      // boolean
83  objInt,                       // integer
84  objReal,                      // real
85  objString,                    // string
86  objName,                      // name
87  objNull,                      // null
88
89  // complex objects
90  objArray,                     // array
91  objDict,                      // dictionary
92  objStream,                    // stream
93  objRef,                       // indirect reference
94
95  // special objects
96  objCmd,                       // command name
97  objError,                     // error return from Lexer
98  objEOF,                       // end of file return from Lexer
99  objNone,                      // uninitialized object
100
101  // poppler-only objects
102  objUint                       // overflown integer that still fits in a unsigned integer
103};
104
105#define numObjTypes 15          // total number of object types
106
107//------------------------------------------------------------------------
108// Object
109//------------------------------------------------------------------------
110
111#ifdef DEBUG_MEM
112#define initObj(t) zeroUnion(); ++numAlloc[type = t]
113#else
114#define initObj(t) zeroUnion(); type = t
115#endif
116
117class Object {
118public:
119  // clear the anonymous union as best we can -- clear at least a pointer
120  void zeroUnion() { this->name = NULL; }
121
122  // Default constructor.
123  Object():
124    type(objNone) { zeroUnion(); }
125
126  // Initialize an object.
127  Object *initBool(GBool boolnA)
128    { initObj(objBool); booln = boolnA; return this; }
129  Object *initInt(int intgA)
130    { initObj(objInt); intg = intgA; return this; }
131  Object *initReal(double realA)
132    { initObj(objReal); real = realA; return this; }
133  Object *initString(GooString *stringA)
134    { initObj(objString); string = stringA; return this; }
135  Object *initName(char *nameA)
136    { initObj(objName); name = copyString(nameA); return this; }
137  Object *initNull()
138    { initObj(objNull); return this; }
139  Object *initArray(XRef *xref);
140  Object *initDict(XRef *xref);
141  Object *initDict(Dict *dictA);
142  Object *initStream(Stream *streamA);
143  Object *initRef(int numA, int genA)
144    { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
145  Object *initCmd(char *cmdA)
146    { initObj(objCmd); cmd = copyString(cmdA); return this; }
147  Object *initError()
148    { initObj(objError); return this; }
149  Object *initEOF()
150    { initObj(objEOF); return this; }
151  Object *initUint(unsigned int uintgA)
152    { initObj(objUint); uintg = uintgA; return this; }
153
154  // Copy an object.
155  Object *copy(Object *obj);
156  Object *shallowCopy(Object *obj) {
157    *obj = *this;
158    return obj;
159  }
160
161  // If object is a Ref, fetch and return the referenced object.
162  // Otherwise, return a copy of the object.
163  Object *fetch(XRef *xref, Object *obj);
164
165  // Free object contents.
166  void free();
167
168  // Type checking.
169  ObjType getType() { return type; }
170  GBool isBool() { return type == objBool; }
171  GBool isInt() { return type == objInt; }
172  GBool isReal() { return type == objReal; }
173  GBool isNum() { return type == objInt || type == objReal; }
174  GBool isString() { return type == objString; }
175  GBool isName() { return type == objName; }
176  GBool isNull() { return type == objNull; }
177  GBool isArray() { return type == objArray; }
178  GBool isDict() { return type == objDict; }
179  GBool isStream() { return type == objStream; }
180  GBool isRef() { return type == objRef; }
181  GBool isCmd() { return type == objCmd; }
182  GBool isError() { return type == objError; }
183  GBool isEOF() { return type == objEOF; }
184  GBool isNone() { return type == objNone; }
185  GBool isUint() { return type == objUint; }
186
187  // Special type checking.
188  GBool isName(char *nameA)
189    { return type == objName && !strcmp(name, nameA); }
190  GBool isDict(char *dictType);
191  GBool isStream(char *dictType);
192  GBool isCmd(char *cmdA)
193    { return type == objCmd && !strcmp(cmd, cmdA); }
194
195  // Accessors.
196  GBool getBool() { OBJECT_TYPE_CHECK(objBool); return booln; }
197  int getInt() { OBJECT_TYPE_CHECK(objInt); return intg; }
198  double getReal() { OBJECT_TYPE_CHECK(objReal); return real; }
199  double getNum() { OBJECT_2TYPES_CHECK(objInt, objReal); return type == objInt ? (double)intg : real; }
200  GooString *getString() { OBJECT_TYPE_CHECK(objString); return string; }
201  char *getName() { OBJECT_TYPE_CHECK(objName); return name; }
202  Array *getArray() { OBJECT_TYPE_CHECK(objArray); return array; }
203  Dict *getDict() { OBJECT_TYPE_CHECK(objDict); return dict; }
204  Stream *getStream() { OBJECT_TYPE_CHECK(objStream); return stream; }
205  Ref getRef() { OBJECT_TYPE_CHECK(objRef); return ref; }
206  int getRefNum() { OBJECT_TYPE_CHECK(objRef); return ref.num; }
207  int getRefGen() { OBJECT_TYPE_CHECK(objRef); return ref.gen; }
208  char *getCmd() { OBJECT_TYPE_CHECK(objCmd); return cmd; }
209  unsigned int getUint() { OBJECT_TYPE_CHECK(objUint); return uintg; }
210
211  // Array accessors.
212  int arrayGetLength();
213  void arrayAdd(Object *elem);
214  Object *arrayGet(int i, Object *obj);
215  Object *arrayGetNF(int i, Object *obj);
216
217  // Dict accessors.
218  int dictGetLength();
219  void dictAdd(char *key, Object *val);
220  void dictSet(char *key, Object *val);
221  GBool dictIs(char *dictType);
222  Object *dictLookup(char *key, Object *obj);
223  Object *dictLookupNF(char *key, Object *obj);
224  char *dictGetKey(int i);
225  Object *dictGetVal(int i, Object *obj);
226  Object *dictGetValNF(int i, Object *obj);
227
228  // Stream accessors.
229  GBool streamIs(char *dictType);
230  void streamReset();
231  void streamClose();
232  int streamGetChar();
233  int streamLookChar();
234  char *streamGetLine(char *buf, int size);
235  Guint streamGetPos();
236  void streamSetPos(Guint pos, int dir = 0);
237  Dict *streamGetDict();
238
239  // Output.
240  char *getTypeName();
241  void print(FILE *f = stdout);
242
243  // Memory testing.
244  static void memCheck(FILE *f);
245
246private:
247
248  ObjType type;                 // object type
249  union {                       // value for each type:
250    GBool booln;                //   boolean
251    int intg;                   //   integer
252    unsigned int uintg;         //   unsigned integer
253    double real;                //   real
254    GooString *string;          //   string
255    char *name;                 //   name
256    Array *array;               //   array
257    Dict *dict;                 //   dictionary
258    Stream *stream;             //   stream
259    Ref ref;                    //   indirect reference
260    char *cmd;                  //   command
261  };
262
263#ifdef DEBUG_MEM
264  static int                    // number of each type of object
265    numAlloc[numObjTypes];      //   currently allocated
266#endif
267};
268
269//------------------------------------------------------------------------
270// Array accessors.
271//------------------------------------------------------------------------
272
273#include "Array.h"
274
275inline int Object::arrayGetLength()
276  { OBJECT_TYPE_CHECK(objArray); return array->getLength(); }
277
278inline void Object::arrayAdd(Object *elem)
279  { OBJECT_TYPE_CHECK(objArray); array->add(elem); }
280
281inline Object *Object::arrayGet(int i, Object *obj)
282  { OBJECT_TYPE_CHECK(objArray); return array->get(i, obj); }
283
284inline Object *Object::arrayGetNF(int i, Object *obj)
285  { OBJECT_TYPE_CHECK(objArray); return array->getNF(i, obj); }
286
287//------------------------------------------------------------------------
288// Dict accessors.
289//------------------------------------------------------------------------
290
291#include "Dict.h"
292
293inline int Object::dictGetLength()
294  { OBJECT_TYPE_CHECK(objDict); return dict->getLength(); }
295
296inline void Object::dictAdd(char *key, Object *val)
297  { OBJECT_TYPE_CHECK(objDict); dict->add(key, val); }
298
299inline void Object::dictSet(char *key, Object *val)
300        { OBJECT_TYPE_CHECK(objDict); dict->set(key, val); }
301
302inline GBool Object::dictIs(char *dictType)
303  { OBJECT_TYPE_CHECK(objDict); return dict->is(dictType); }
304
305inline GBool Object::isDict(char *dictType)
306  { return type == objDict && dictIs(dictType); }
307
308inline Object *Object::dictLookup(char *key, Object *obj)
309  { OBJECT_TYPE_CHECK(objDict); return dict->lookup(key, obj); }
310
311inline Object *Object::dictLookupNF(char *key, Object *obj)
312  { OBJECT_TYPE_CHECK(objDict); return dict->lookupNF(key, obj); }
313
314inline char *Object::dictGetKey(int i)
315  { OBJECT_TYPE_CHECK(objDict); return dict->getKey(i); }
316
317inline Object *Object::dictGetVal(int i, Object *obj)
318  { OBJECT_TYPE_CHECK(objDict); return dict->getVal(i, obj); }
319
320inline Object *Object::dictGetValNF(int i, Object *obj)
321  { OBJECT_TYPE_CHECK(objDict); return dict->getValNF(i, obj); }
322
323//------------------------------------------------------------------------
324// Stream accessors.
325//------------------------------------------------------------------------
326
327#include "Stream.h"
328
329inline GBool Object::streamIs(char *dictType)
330  { OBJECT_TYPE_CHECK(objStream); return stream->getDict()->is(dictType); }
331
332inline GBool Object::isStream(char *dictType)
333  { return type == objStream && streamIs(dictType); }
334
335inline void Object::streamReset()
336  { OBJECT_TYPE_CHECK(objStream); stream->reset(); }
337
338inline void Object::streamClose()
339  { OBJECT_TYPE_CHECK(objStream); stream->close(); }
340
341inline int Object::streamGetChar()
342  { OBJECT_TYPE_CHECK(objStream); return stream->getChar(); }
343
344inline int Object::streamLookChar()
345  { OBJECT_TYPE_CHECK(objStream); return stream->lookChar(); }
346
347inline char *Object::streamGetLine(char *buf, int size)
348  { OBJECT_TYPE_CHECK(objStream); return stream->getLine(buf, size); }
349
350inline Guint Object::streamGetPos()
351  { OBJECT_TYPE_CHECK(objStream); return stream->getPos(); }
352
353inline void Object::streamSetPos(Guint pos, int dir)
354  { OBJECT_TYPE_CHECK(objStream); stream->setPos(pos, dir); }
355
356inline Dict *Object::streamGetDict()
357  { OBJECT_TYPE_CHECK(objStream); return stream->getDict(); }
358
359#endif
Note: See TracBrowser for help on using the repository browser.