home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- //
- // Object.h
- //
- // Copyright 1996 Derek B. Noonburg
- //
- //========================================================================
- //
- // Ported to EPOC by Sander van der Wal
- //
- // $Id: Object.h 1.2 2000-09-17 13:38:16+02 svdwal Exp svdwal $
-
- #ifndef OBJECT_H
- #define OBJECT_H
-
- #ifdef __GNUC__
- #pragma interface
- #endif
-
- #ifndef __E32BASE_H__
- #include <e32base.h>
- #endif
-
- #ifndef __F32FILE_H__
- #include <f32file.h>
- #endif
-
- // C Library
- #include <string.h>
-
- // --o GooLib
- #include "gtypes.h"
- #include "gmem.h"
- #include "GString.h"
-
- // --o PdfLib
- class Array;
- class Dict;
- class Stream;
-
- //------------------------------------------------------------------------
- // Ref
- //------------------------------------------------------------------------
-
- struct Ref {
- int num; // object number
- int gen; // generation number
- };
-
- //------------------------------------------------------------------------
- // object types
- //------------------------------------------------------------------------
-
- enum ObjType {
- // simple objects
- objBool, // boolean
- objInt, // integer
- objReal, // real
- objString, // string
- objName, // name
- objNull, // null
-
- // complex objects
- objArray, // array
- objDict, // dictionary
- objStream, // stream
- objRef, // indirect reference
-
- // special objects
- objCmd, // command name
- objError, // error return from Lexer
- objEOF, // end of file return from Lexer
- objNone // uninitialized object
- };
-
- #define numObjTypes 14 // total number of object types
-
- //------------------------------------------------------------------------
- // Object
- //------------------------------------------------------------------------
-
- #ifdef DEBUG_MEM
- #define initObj(t) ++numAlloc[type = t]
- #else
- #define initObj(t) type = t
- #endif
-
- class Object {
- public:
-
- // Default constructor.
- Object():
- type(objNone) {}
-
- // Initialize an object.
- Object *initBool(GBool booln1)
- { initObj(objBool); booln = booln1; return this; }
- Object *initInt(int intg1)
- { initObj(objInt); intg = intg1; return this; }
- Object *initReal(double real1)
- { initObj(objReal); real = real1; return this; }
- Object *initString(GString *string1)
- { initObj(objString); string = string1; return this; }
- Object *initNameL(char *name1);
- Object *initNull()
- { initObj(objNull); return this; }
- Object *initArrayL();
- Object *initDictL();
- Object *initDict(Dict *dict1)
- { initObj(objDict); dict = dict1; return this; }
- Object *initStream(Stream *stream1);
- Object *initRef(int num1, int gen1)
- { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
- Object *initCmdL(char *cmd1);
- Object *initError()
- { initObj(objError); return this; }
- Object *initEOF()
- { initObj(objEOF); return this; }
-
- // Copy an object.
- Object *copyL(Object *obj);
-
- // If object is a Ref, fetch and return the referenced object.
- // Otherwise, return a copy of the object.
- Object *fetchL(Object *obj);
-
- // Free object contents.
- void free();
-
- // Type checking.
- GBool isBool() const { return type == objBool; }
- GBool isInt() const { return type == objInt; }
- GBool isReal() const { return type == objReal; }
- GBool isNum() const { return type == objInt || type == objReal; }
- GBool isString() const { return type == objString; }
- GBool isName() const { return type == objName; }
- GBool isNull() const { return type == objNull; }
- GBool isArray() const { return type == objArray; }
- GBool isDict() const { return type == objDict; }
- GBool isStream() const { return type == objStream; }
- GBool isRef() const { return type == objRef; }
- GBool isCmd() const { return type == objCmd; }
- GBool isError() const { return type == objError; }
- GBool isEOF() const { return type == objEOF; }
- GBool isNone() const { return type == objNone; }
-
- // Special type checking.
- GBool isName(const char *name1) const
- { if (type != objName) return gFalse;
- return !strcmp(name, name1); }
- GBool isDict(char *dictType);
- GBool isStream(char *dictType);
- GBool isCmd(char *cmd1) const
- { if (type != objCmd) return gFalse;
- return !strcmp(cmd, cmd1); }
-
- // Accessors. NB: these assume object is of correct type.
- GBool getBool() const { return booln; }
- int getInt() const { return intg; }
- double getReal() const { return real; }
- double getNum() const { return type == objInt ? (double)intg : real; }
- GString *getString() const { return string; }
- char *getName() const { return name; }
- Array *getArray() const { return array; }
- Dict *getDict() const { return dict; }
- Stream *getStream() const { return stream; }
- Ref getRef() const { return ref; }
- int getRefNum() const { return ref.num; }
- int getRefGen() const { return ref.gen; }
-
- // Array accessors.
- int arrayGetLength();
- void arrayAddL(Object *elem);
- Object *arrayGetL(int i, Object *obj);
- Object *arrayGetNFL(int i, Object *obj);
-
- // Dict accessors.
- int dictGetLength();
- void dictAddL(char *key, Object *val);
- GBool dictIs(char *dictType);
- Object *dictLookupL(char *key, Object *obj);
- Object *dictLookupNFL(char *key, Object *obj);
- char *dictGetKey(int i);
- Object *dictGetValL(int i, Object *obj);
- Object *dictGetValNFL(int i, Object *obj);
-
- // Stream accessors.
- GBool streamIs(char *dictType);
- void streamReset();
- int streamGetChar();
- int streamLookChar();
- char *streamGetLine(char *buf, int size);
- int streamGetPos();
- void streamSetPos(int pos);
- RFile streamGetFile();
- Dict *streamGetDict();
-
- // Output.
- char *getTypeName();
-
- // Cleanup support
- operator TCleanupItem() { return TCleanupItem(Cleanup, this); }
-
- protected:
- static void Cleanup(TAny* aPtr);
-
- private:
-
- ObjType type; // object type
- union { // value for each type:
- GBool booln; // boolean
- int intg; // integer
- double real; // real
- GString *string; // string
- char *name; // name
- Array *array; // array
- Dict *dict; // dictionary
- Stream *stream; // stream
- Ref ref; // indirect reference
- char *cmd; // command
- };
-
- #ifdef DEBUG_MEM
- static int // number of each type of object
- numAlloc[numObjTypes]; // currently allocated
- #endif
-
- };
-
-
- // Objects on the stack need to be cleaned up. This construct allows this with minimal
- // adaption to the code: only the declarations need to be changed, not the use of these
- // Objects. The resulting class behaves very much like a normal R class.
- // This only works because:
- // 1 - the objects themselves are on the stack, so allocation cannot fail
- // 2 - Object::free() changes the type in such a way that deallocation
- // happens only once (the R class behaviour).
- // 3 - C++ destructors are called in the opposite order as C++ constructors
- // at the end of a block
- // 4 - this type itself isn't going to be derived from.
- class RAutoObject: public Object
- {
- public:
- RAutoObject(): Object() { CleanupStack::PushL(*this); }
- ~RAutoObject() { CleanupStack::PopAndDestroy(); }
- };
-
-
- //------------------------------------------------------------------------
- // Array accessors.
- //------------------------------------------------------------------------
-
- #include "Array.h"
-
- inline int Object::arrayGetLength()
- { return array->getLength(); }
-
- inline void Object::arrayAddL(Object *elem)
- { array->addL(elem); }
-
- inline Object *Object::arrayGetL(int i, Object *obj)
- { return array->getL(i, obj); }
-
- inline Object *Object::arrayGetNFL(int i, Object *obj)
- { return array->getNFL(i, obj); }
-
- //------------------------------------------------------------------------
- // Dict accessors.
- //------------------------------------------------------------------------
-
- #include "Dict.h"
-
- inline int Object::dictGetLength()
- { return dict->getLength(); }
-
- inline void Object::dictAddL(char *key, Object *val)
- { dict->addL(key, val); }
-
- inline GBool Object::dictIs(char *dictType)
- { return dict->is(dictType); }
-
- inline GBool Object::isDict(char *dictType)
- { return type == objDict && dictIs(dictType); }
-
- inline Object *Object::dictLookupL(char *key, Object *obj)
- { return dict->lookupL(key, obj); }
-
- inline Object *Object::dictLookupNFL(char *key, Object *obj)
- { return dict->lookupNFL(key, obj); }
-
- inline char *Object::dictGetKey(int i)
- { return dict->getKey(i); }
-
- inline Object *Object::dictGetValL(int i, Object *obj)
- { return dict->getValL(i, obj); }
-
- inline Object *Object::dictGetValNFL(int i, Object *obj)
- { return dict->getValNFL(i, obj); }
-
- //------------------------------------------------------------------------
- // Stream accessors.
- //------------------------------------------------------------------------
-
- #include "Stream.h"
-
- inline GBool Object::streamIs(char *dictType)
- { return stream->getDict()->is(dictType); }
-
- inline GBool Object::isStream(char *dictType)
- { return type == objStream && streamIs(dictType); }
-
- inline void Object::streamReset()
- { stream->reset(); }
-
- inline int Object::streamGetChar()
- { return stream->getChar(); }
-
- inline int Object::streamLookChar()
- { return stream->lookChar(); }
-
- inline char *Object::streamGetLine(char *buf, int size)
- { return stream->getLine(buf, size); }
-
- inline int Object::streamGetPos()
- { return stream->getPos(); }
-
- inline void Object::streamSetPos(int pos)
- { stream->setPos(pos); }
-
- inline RFile Object::streamGetFile()
- { return stream->getFile(); }
-
- inline Dict *Object::streamGetDict()
- { return stream->getDict(); }
-
- #endif
-