home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 720 / PDF090B4-SorceCode / pdf / Object.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  9.6 KB  |  337 lines

  1. //========================================================================
  2. //
  3. // Object.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8. //
  9. // Ported to EPOC by Sander van der Wal
  10. //
  11. // $Id: Object.h 1.2 2000-09-17 13:38:16+02 svdwal Exp svdwal $
  12.  
  13. #ifndef OBJECT_H
  14. #define OBJECT_H
  15.  
  16. #ifdef __GNUC__
  17. #pragma interface
  18. #endif
  19.  
  20. #ifndef __E32BASE_H__
  21. #include <e32base.h>
  22. #endif
  23.  
  24. #ifndef __F32FILE_H__
  25. #include <f32file.h>
  26. #endif
  27.  
  28. // C Library
  29. #include <string.h>
  30.  
  31. // --o GooLib
  32. #include "gtypes.h"
  33. #include "gmem.h"
  34. #include "GString.h"
  35.  
  36. // --o PdfLib
  37. class Array;
  38. class Dict;
  39. class Stream;
  40.  
  41. //------------------------------------------------------------------------
  42. // Ref
  43. //------------------------------------------------------------------------
  44.  
  45. struct Ref {
  46.   int num;            // object number
  47.   int gen;            // generation number
  48. };
  49.  
  50. //------------------------------------------------------------------------
  51. // object types
  52. //------------------------------------------------------------------------
  53.  
  54. enum ObjType {
  55.   // simple objects
  56.   objBool,            // boolean
  57.   objInt,            // integer
  58.   objReal,            // real
  59.   objString,            // string
  60.   objName,            // name
  61.   objNull,            // null
  62.  
  63.   // complex objects
  64.   objArray,            // array
  65.   objDict,            // dictionary
  66.   objStream,            // stream
  67.   objRef,            // indirect reference
  68.  
  69.   // special objects
  70.   objCmd,            // command name
  71.   objError,            // error return from Lexer
  72.   objEOF,            // end of file return from Lexer
  73.   objNone            // uninitialized object
  74. };
  75.  
  76. #define numObjTypes 14        // total number of object types
  77.  
  78. //------------------------------------------------------------------------
  79. // Object
  80. //------------------------------------------------------------------------
  81.  
  82. #ifdef DEBUG_MEM
  83. #define initObj(t) ++numAlloc[type = t]
  84. #else
  85. #define initObj(t) type = t
  86. #endif
  87.  
  88. class Object {
  89. public:
  90.  
  91.   // Default constructor.
  92.   Object():
  93.     type(objNone) {}
  94.  
  95.   // Initialize an object.
  96.   Object *initBool(GBool booln1)
  97.     { initObj(objBool); booln = booln1; return this; }
  98.   Object *initInt(int intg1)
  99.     { initObj(objInt); intg = intg1; return this; }
  100.   Object *initReal(double real1)
  101.     { initObj(objReal); real = real1; return this; }
  102.   Object *initString(GString *string1)
  103.     { initObj(objString); string = string1; return this; }
  104.   Object *initNameL(char *name1);
  105.   Object *initNull()
  106.     { initObj(objNull); return this; }
  107.   Object *initArrayL();
  108.   Object *initDictL();
  109.   Object *initDict(Dict *dict1)
  110.     { initObj(objDict); dict = dict1; return this; }
  111.   Object *initStream(Stream *stream1);
  112.   Object *initRef(int num1, int gen1)
  113.     { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
  114.   Object *initCmdL(char *cmd1);
  115.   Object *initError()
  116.     { initObj(objError); return this; }
  117.   Object *initEOF()
  118.     { initObj(objEOF); return this; }
  119.  
  120.   // Copy an object.
  121.   Object *copyL(Object *obj);
  122.  
  123.   // If object is a Ref, fetch and return the referenced object.
  124.   // Otherwise, return a copy of the object.
  125.   Object *fetchL(Object *obj);
  126.  
  127.   // Free object contents.
  128.   void free();
  129.  
  130.   // Type checking.
  131.   GBool isBool() const { return type == objBool; }
  132.   GBool isInt() const { return type == objInt; }
  133.   GBool isReal() const { return type == objReal; }
  134.   GBool isNum() const { return type == objInt || type == objReal; }
  135.   GBool isString() const { return type == objString; }
  136.   GBool isName() const { return type == objName; }
  137.   GBool isNull() const { return type == objNull; }
  138.   GBool isArray() const { return type == objArray; }
  139.   GBool isDict() const { return type == objDict; }
  140.   GBool isStream() const { return type == objStream; }
  141.   GBool isRef() const { return type == objRef; }
  142.   GBool isCmd() const { return type == objCmd; }
  143.   GBool isError() const { return type == objError; }
  144.   GBool isEOF() const { return type == objEOF; }
  145.   GBool isNone() const { return type == objNone; }
  146.  
  147.   // Special type checking.
  148.   GBool isName(const char *name1) const 
  149.     { if (type != objName) return gFalse;
  150.       return !strcmp(name, name1); }
  151.   GBool isDict(char *dictType);
  152.   GBool isStream(char *dictType);
  153.   GBool isCmd(char *cmd1) const 
  154.     { if (type != objCmd) return gFalse;
  155.       return !strcmp(cmd, cmd1); }
  156.  
  157.   // Accessors.  NB: these assume object is of correct type.
  158.   GBool getBool() const { return booln; }
  159.   int getInt() const { return intg; }
  160.   double getReal() const { return real; }
  161.   double getNum() const { return type == objInt ? (double)intg : real; }
  162.   GString *getString() const { return string; }
  163.   char *getName() const { return name; }
  164.   Array *getArray() const { return array; }
  165.   Dict *getDict() const { return dict; }
  166.   Stream *getStream() const { return stream; }
  167.   Ref getRef() const { return ref; }
  168.   int getRefNum() const { return ref.num; }
  169.   int getRefGen() const { return ref.gen; }
  170.  
  171.   // Array accessors.
  172.   int arrayGetLength();
  173.   void arrayAddL(Object *elem);
  174.   Object *arrayGetL(int i, Object *obj);
  175.   Object *arrayGetNFL(int i, Object *obj);
  176.  
  177.   // Dict accessors.
  178.   int dictGetLength();
  179.   void dictAddL(char *key, Object *val);
  180.   GBool dictIs(char *dictType);
  181.   Object *dictLookupL(char *key, Object *obj);
  182.   Object *dictLookupNFL(char *key, Object *obj);
  183.   char *dictGetKey(int i);
  184.   Object *dictGetValL(int i, Object *obj);
  185.   Object *dictGetValNFL(int i, Object *obj);
  186.  
  187.   // Stream accessors.
  188.   GBool streamIs(char *dictType);
  189.   void streamReset();
  190.   int streamGetChar();
  191.   int streamLookChar();
  192.   char *streamGetLine(char *buf, int size);
  193.   int streamGetPos();
  194.   void streamSetPos(int pos);
  195.   RFile streamGetFile();
  196.   Dict *streamGetDict();
  197.  
  198.   // Output.
  199.   char *getTypeName();
  200.  
  201.    // Cleanup support
  202.   operator TCleanupItem() { return TCleanupItem(Cleanup, this); }
  203.  
  204. protected:
  205.   static void Cleanup(TAny* aPtr);
  206.  
  207. private:
  208.  
  209.   ObjType type;            // object type
  210.   union {            // value for each type:
  211.     GBool booln;        //   boolean
  212.     int intg;            //   integer
  213.     double real;        //   real
  214.     GString *string;        //   string
  215.     char *name;            //   name
  216.     Array *array;        //   array
  217.     Dict *dict;            //   dictionary
  218.     Stream *stream;        //   stream
  219.     Ref ref;            //   indirect reference
  220.     char *cmd;            //   command
  221.   };
  222.  
  223. #ifdef DEBUG_MEM
  224.   static int            // number of each type of object
  225.     numAlloc[numObjTypes];    //   currently allocated
  226. #endif
  227.  
  228. };
  229.  
  230.  
  231. // Objects on the stack need to be cleaned up. This construct allows this with minimal
  232. // adaption to the code: only the declarations need to be changed, not the use of these 
  233. // Objects. The resulting class behaves very much like a normal R class.
  234. // This only works because:
  235. //   1 - the objects themselves are on the stack, so allocation cannot fail
  236. //   2 - Object::free() changes the type in such a way that deallocation
  237. //       happens only once (the R class behaviour).
  238. //   3 - C++ destructors are called in the opposite order as C++ constructors
  239. //       at the end of a block
  240. //   4 - this type itself isn't going to be derived from.
  241. class RAutoObject: public Object
  242. {
  243. public:
  244.   RAutoObject(): Object() { CleanupStack::PushL(*this); }
  245.   ~RAutoObject() { CleanupStack::PopAndDestroy(); }
  246. };
  247.  
  248.  
  249. //------------------------------------------------------------------------
  250. // Array accessors.
  251. //------------------------------------------------------------------------
  252.  
  253. #include "Array.h"
  254.  
  255. inline int Object::arrayGetLength()
  256.   { return array->getLength(); }
  257.  
  258. inline void Object::arrayAddL(Object *elem)
  259.   { array->addL(elem); }
  260.  
  261. inline Object *Object::arrayGetL(int i, Object *obj)
  262.   { return array->getL(i, obj); }
  263.  
  264. inline Object *Object::arrayGetNFL(int i, Object *obj)
  265.   { return array->getNFL(i, obj); }
  266.  
  267. //------------------------------------------------------------------------
  268. // Dict accessors.
  269. //------------------------------------------------------------------------
  270.  
  271. #include "Dict.h"
  272.  
  273. inline int Object::dictGetLength()
  274.   { return dict->getLength(); }
  275.  
  276. inline void Object::dictAddL(char *key, Object *val)
  277.   { dict->addL(key, val); }
  278.  
  279. inline GBool Object::dictIs(char *dictType)
  280.   { return dict->is(dictType); }
  281.  
  282. inline GBool Object::isDict(char *dictType)
  283.   { return type == objDict && dictIs(dictType); }
  284.  
  285. inline Object *Object::dictLookupL(char *key, Object *obj)
  286.   { return dict->lookupL(key, obj); }
  287.  
  288. inline Object *Object::dictLookupNFL(char *key, Object *obj)
  289.   { return dict->lookupNFL(key, obj); }
  290.  
  291. inline char *Object::dictGetKey(int i)
  292.   { return dict->getKey(i); }
  293.  
  294. inline Object *Object::dictGetValL(int i, Object *obj)
  295.   { return dict->getValL(i, obj); }
  296.  
  297. inline Object *Object::dictGetValNFL(int i, Object *obj)
  298.   { return dict->getValNFL(i, obj); }
  299.  
  300. //------------------------------------------------------------------------
  301. // Stream accessors.
  302. //------------------------------------------------------------------------
  303.  
  304. #include "Stream.h"
  305.  
  306. inline GBool Object::streamIs(char *dictType)
  307.   { return stream->getDict()->is(dictType); }
  308.  
  309. inline GBool Object::isStream(char *dictType)
  310.   { return type == objStream && streamIs(dictType); }
  311.  
  312. inline void Object::streamReset()
  313.   { stream->reset(); }
  314.  
  315. inline int Object::streamGetChar()
  316.   { return stream->getChar(); }
  317.  
  318. inline int Object::streamLookChar()
  319.   { return stream->lookChar(); }
  320.  
  321. inline char *Object::streamGetLine(char *buf, int size)
  322.   { return stream->getLine(buf, size); }
  323.  
  324. inline int Object::streamGetPos()
  325.   { return stream->getPos(); }
  326.  
  327. inline void Object::streamSetPos(int pos)
  328.   { stream->setPos(pos); }
  329.  
  330. inline RFile Object::streamGetFile()
  331.   { return stream->getFile(); }
  332.  
  333. inline Dict *Object::streamGetDict()
  334.   { return stream->getDict(); }
  335.  
  336. #endif
  337.