Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Object.cc

Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // Object.cc
00004 //
00005 // Copyright 1996-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifdef __GNUC__
00010 #pragma implementation
00011 #endif
00012 
00013 #include <aconf.h>
00014 #include <stddef.h>
00015 #include "Object.h"
00016 #include "Array.h"
00017 #include "Dict.h"
00018 #include "Error.h"
00019 #include "Stream.h"
00020 #include "XRef.h"
00021 
00022 //------------------------------------------------------------------------
00023 // Object
00024 //------------------------------------------------------------------------
00025 
00026 char *objTypeNames[numObjTypes] = {
00027   "boolean",
00028   "integer",
00029   "real",
00030   "string",
00031   "name",
00032   "null",
00033   "array",
00034   "dictionary",
00035   "stream",
00036   "ref",
00037   "cmd",
00038   "error",
00039   "eof",
00040   "none"
00041 };
00042 
00043 #ifdef DEBUG_MEM
00044 int Object::numAlloc[numObjTypes] =
00045   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
00046 #endif
00047 
00048 Object *Object::initArray(XRef *xref) {
00049   initObj(objArray);
00050   array = new Array(xref);
00051   return this;
00052 }
00053 
00054 Object *Object::initDict(XRef *xref) {
00055   initObj(objDict);
00056   dict = new Dict(xref);
00057   return this;
00058 }
00059 
00060 Object *Object::initStream(Stream *streamA) {
00061   initObj(objStream);
00062   stream = streamA;
00063   return this;
00064 }
00065 
00066 Object *Object::copy(Object *obj) {
00067   *obj = *this;
00068   switch (type) {
00069   case objString:
00070     obj->string = string->copy();
00071     break;
00072   case objName:
00073     obj->name = copyString(name);
00074     break;
00075   case objArray:
00076     array->incRef();
00077     break;
00078   case objDict:
00079     dict->incRef();
00080     break;
00081   case objStream:
00082     stream->incRef();
00083     break;
00084   case objCmd:
00085     obj->cmd = copyString(cmd);
00086     break;
00087   default:
00088     break;
00089   }
00090 #ifdef DEBUG_MEM
00091   ++numAlloc[type];
00092 #endif
00093   return obj;
00094 }
00095 
00096 Object *Object::fetch(XRef *xref, Object *obj) {
00097   return (type == objRef && xref) ?
00098          xref->fetch(ref.num, ref.gen, obj) : copy(obj);
00099 }
00100 
00101 void Object::free() {
00102   switch (type) {
00103   case objString:
00104     delete string;
00105     break;
00106   case objName:
00107     gfree(name);
00108     break;
00109   case objArray:
00110     if (!array->decRef()) {
00111       delete array;
00112     }
00113     break;
00114   case objDict:
00115     if (!dict->decRef()) {
00116       delete dict;
00117     }
00118     break;
00119   case objStream:
00120     if (!stream->decRef()) {
00121       delete stream;
00122     }
00123     break;
00124   case objCmd:
00125     gfree(cmd);
00126     break;
00127   default:
00128     break;
00129   }
00130 #ifdef DEBUG_MEM
00131   --numAlloc[type];
00132 #endif
00133   type = objNone;
00134 }
00135 
00136 char *Object::getTypeName() {
00137   return objTypeNames[type];
00138 }
00139 
00140 void Object::print(FILE *f) {
00141   Object obj;
00142   int i;
00143 
00144   switch (type) {
00145   case objBool:
00146     fprintf(f, "%s", booln ? "true" : "false");
00147     break;
00148   case objInt:
00149     fprintf(f, "%d", intg);
00150     break;
00151   case objReal:
00152     fprintf(f, "%g", static_cast<double>(real));
00153     break;
00154   case objString:
00155     fprintf(f, "(");
00156     fwrite(string->getCString(), 1, string->getLength(), stdout);
00157     fprintf(f, ")");
00158     break;
00159   case objName:
00160     fprintf(f, "/%s", name);
00161     break;
00162   case objNull:
00163     fprintf(f, "null");
00164     break;
00165   case objArray:
00166     fprintf(f, "[");
00167     for (i = 0; i < arrayGetLength(); ++i) {
00168       if (i > 0)
00169         fprintf(f, " ");
00170       arrayGetNF(i, &obj);
00171       obj.print(f);
00172       obj.free();
00173     }
00174     fprintf(f, "]");
00175     break;
00176   case objDict:
00177     fprintf(f, "<<");
00178     for (i = 0; i < dictGetLength(); ++i) {
00179       fprintf(f, " /%s ", dictGetKey(i));
00180       dictGetValNF(i, &obj);
00181       obj.print(f);
00182       obj.free();
00183     }
00184     fprintf(f, " >>");
00185     break;
00186   case objStream:
00187     fprintf(f, "<stream>");
00188     break;
00189   case objRef:
00190     fprintf(f, "%d %d R", ref.num, ref.gen);
00191     break;
00192   case objCmd:
00193     fprintf(f, "%s", cmd);
00194     break;
00195   case objError:
00196     fprintf(f, "<error>");
00197     break;
00198   case objEOF:
00199     fprintf(f, "<EOF>");
00200     break;
00201   case objNone:
00202     fprintf(f, "<none>");
00203     break;
00204   }
00205 }
00206 
00207 void Object::memCheck(FILE *f) {
00208 #ifdef DEBUG_MEM
00209   int i;
00210   int t;
00211 
00212   t = 0;
00213   for (i = 0; i < numObjTypes; ++i)
00214     t += numAlloc[i];
00215   if (t > 0) {
00216     fprintf(f, "Allocated objects:\n");
00217     for (i = 0; i < numObjTypes; ++i) {
00218       if (numAlloc[i] > 0)
00219         fprintf(f, "  %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
00220     }
00221   }
00222 #endif
00223 }

Generated on Sat Nov 5 16:18:15 2005 for OPIE by  doxygen 1.4.2