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

Last change on this file since 257 was 257, checked in by Eugene Romanenko, 13 years ago

PDF plugin: Poppler library updated to version 0.10.0

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