home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / oath.lha / oath / src / oathCore.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-08  |  5.3 KB  |  241 lines

  1. //***************************************************************************
  2. //             OATH :: Object-oriented Abstract Type Hierarchy
  3. //***************************************************************************
  4. //
  5. //  Copyright (C) 1991, 1990  Texas Instruments Incorporated
  6. //  Permission is granted to any individual or institution
  7. //  to use, copy, modify, and distribute this software,
  8. //  provided that this complete copyright and permission notice
  9. //  is maintained, intact, in all copies and supporting documentation.
  10. //
  11. //  Texas Instruments Incorporated provides this software "as is"
  12. //  without express or implied warranty.
  13. //
  14. //***************************************************************************
  15. //  oathCore (oathCoreA, oathCoreG)
  16. //
  17. //  History:
  18. //    06/92  Brian M Kennedy  fixed oathCoreG::Nil to be a const pointer
  19. //    07/91  Brian M Kennedy  import, export, typeRegister
  20. //    06/91  Brian M Kennedy  New macros & format; remove printDiagnostic
  21. //    10/90  Brian M Kennedy  Original
  22. //
  23. //***************************************************************************
  24.  
  25. #include "copyright.h"
  26.  
  27. #include <oath/oathCore.h>
  28.  
  29. #include <oath/obj.h>
  30.  
  31. #include <iostream.h>
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // oathCoreG Outline Definitions
  35.  
  36.     void oathCoreG::
  37. vtablePosition () const
  38.    {}
  39.  
  40. const char* oathCoreG::TypeName = registerType("", oathCoreG::import);
  41.  
  42. void (*oathCoreG::NextNewHandler) () = oathCoreG::setOathNewHandler();
  43.  
  44. oathCoreG* oathCoreG::AllObjs;
  45.  
  46. oathCoreG* oathCoreG::NilPtr;
  47.  
  48. oathCoreG* const oathCoreG::Nil = oathCoreG::nil();
  49.  
  50.  
  51.     void (* oathCoreG::
  52. setOathNewHandler ()) ()
  53.    {return set_new_handler(oathCoreG::oathNewHandler);}
  54.  
  55.     oathCoreG* oathCoreG::
  56. nil ()
  57.    {if(!NilPtr)
  58.         NilPtr = new oathCoreG(OATH_NIL, OATH_NIL);
  59.     return NilPtr;
  60.    }
  61.  
  62.  
  63. // Garbage Collection //////////
  64.  
  65. #if (OATH_GC == OATH_GC_COM)
  66.  
  67.     void oathCoreG::
  68. collectGarbage (int Quick) // Quick = FALSE)
  69.    {oathCoreG* Obj;
  70.     if(!Quick)
  71.        {// Pass 1: Dereference
  72.         Obj = AllObjs;
  73.     while(Obj)
  74.        {Obj->clearReferences();
  75.         Obj = Obj->Next;
  76.        }
  77.  
  78.         // Pass 2: Re-reference
  79.         Obj = AllObjs;
  80.     while(Obj)
  81.        {if(Obj->isRefd())
  82.             Obj->setReferences();
  83.         Obj = Obj->Next;
  84.        }
  85.        }
  86.  
  87.     // Pass 3: Collect garbage
  88.     Obj = AllObjs;
  89.     while(Obj)
  90.        {oathCoreG* Next = Obj->Next;
  91.         if(!Obj->isRefd())
  92.         delete Obj;
  93.     Obj = Next;
  94.        }
  95.    }
  96.  
  97. #elif (OATH_GC == OATH_GC_SAC)
  98.  
  99.     void oathCoreG::
  100. collectGarbage (int Quick) // Quick = FALSE)
  101.    {oathCoreG* Prev;
  102.     oathCoreG* Obj;
  103.     if(!Quick)
  104.        {// Pass 1: Dereference
  105.         Obj = AllObjs;
  106.     while(Obj)
  107.        {Obj->clearReferences();
  108.         Obj = Obj->Next;
  109.        }
  110.  
  111.         // Pass 2: Re-reference
  112.         Obj = AllObjs;
  113.     while(Obj)
  114.        {if(Obj->isRefd())
  115.             Obj->setReferences();
  116.         Obj = Obj->Next;
  117.        }
  118.        }
  119.  
  120.     // Pass 3: Collect garbage
  121.     Obj = AllObjs;
  122.     while(Obj && !Obj->isRefd())
  123.        {AllObjs = Obj->Next;
  124.         delete Obj;
  125.     Obj = AllObjs;
  126.        }
  127.     while(Obj)
  128.        {Prev = Obj;
  129.         Obj = Obj->Next;
  130.     while(Obj && !Obj->isRefd())
  131.        {Prev->Next = Obj->Next;
  132.         delete Obj;
  133.         Obj = Prev->Next;
  134.        }
  135.        }
  136.    }
  137.  
  138. #else
  139.  
  140.     void oathCoreG::
  141. collectGarbage (int) // Quick = FALSE)
  142.    {}
  143.  
  144. #endif
  145.  
  146.  
  147.     void oathCoreG::
  148. oathNewHandler ()
  149.    {collectGarbage();
  150.     if(NextNewHandler)
  151.         NextNewHandler();
  152.    }
  153.  
  154.  
  155. // Operations //////////
  156.  
  157.     void oathCoreG::
  158. export (exportP& X) const
  159.    {X.writeType(TypeName);}
  160.  
  161.     objA oathCoreG::
  162. import (importP&)
  163.    {return objA::Nil;}
  164.  
  165.     void oathCoreG::    //  clearMark();
  166. clearReferences()    //  for(each Obj this ref's)
  167.    {clearMark();}    //      Obj->deref();
  168.  
  169.     void oathCoreG::    //  if(!isMarked())
  170. setReferences()        //     {setMark();
  171.    {setMark();}        //      for(each Obj this ref's)
  172.             //         {Obj->ref();
  173.             //        Obj->setReferences();
  174.             //       }
  175.             //     }
  176.  
  177.  
  178. // Static Internal Operations //////////
  179.  
  180.     const char* oathCoreG::
  181. registerType (const char* Name, importF Import)
  182.    {return typeRegisterP::registerType(Name, Import);}
  183.  
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186. // oathCoreA Outline Definitions
  187.  
  188. // Export Operations //////////
  189.  
  190.     void oathCoreA::
  191. export (ostream& S) const
  192.    {exportP X (S);
  193.     export(X);
  194.    }
  195.  
  196.     void oathCoreA::
  197. export (exportP& X) const
  198.    {oidP OID;
  199.     if(X.registerObj(guts(), OID))
  200.         X.stream() << 'R' << OID << ' ';
  201.     else
  202.        {X.stream() << 'N' << OID << ' ';
  203.         guts()->export(X);
  204.        }
  205.    }
  206.  
  207.     objA oathCoreA::
  208. import (istream& S)
  209.    {importP M (S);
  210.     return import(M);
  211.    }
  212.  
  213.     objA oathCoreA::
  214. import (importP& M)
  215.    {oathCoreA ImportObj;
  216.     char C = M.stream().get();
  217.     oidP OID;
  218.     M.stream() >> OID;
  219.     M.stream().get();
  220.     switch(C)
  221.        {case 'R':
  222.             ImportObj = M.obj(OID);
  223.         break;
  224.     case 'N':
  225.        {const char* TypeName = M.readType();
  226.         typeRegisterP * TR = typeRegisterP::findRegister(TypeName);
  227.         ensure(TR, "throw: badImportData");
  228. //        ImportObj = TR->Import(M);  // compiler broken -- bad code
  229.         ImportObj = (*(importF)(TR->Import))(M);
  230.         M.registerObj(ImportObj.guts(), OID);
  231.        }
  232.         break;
  233.         default:
  234.         ensure(FALSE, "throw: badImportData");
  235.        }
  236.     return (const objA&)ImportObj;
  237.    }
  238.  
  239.  
  240. //***************************************************************************
  241.