home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / EXCEPT.CXX < prev    next >
C/C++ Source or Header  |  1992-12-03  |  6KB  |  218 lines

  1. //$$except.cxx                        Exception handler
  2.  
  3.  
  4.  
  5. #define WANT_STREAM                  // include.h will get stream fns
  6.  
  7.  
  8. #include "include.h"                 // include standard files
  9. #include "boolean.h"
  10.  
  11.  
  12. #include "except.h"                  // for exception handling
  13.  
  14.  
  15. void Throw()
  16. {
  17.    for (Janitor* jan = JumpBase::jl->janitor; jan; jan = jan->NextJanitor)
  18.       jan->CleanUp();
  19.    JumpBase::jl = JumpBase::jl->ji;
  20.    if ( ! JumpBase::jl ) Terminate();
  21.    Exception::last = JumpBase::jl->trace;
  22.    longjmp(JumpBase::jl->env, 1);
  23. }
  24.  
  25. void Throw(const Exception& exc) { JumpBase::type = exc.type(); Throw(); }
  26.  
  27.  
  28. void Exception::PrintTrace(Boolean)
  29. {
  30.    cout << "\n";
  31.    {
  32.       for (Tracer* et = last; et; et=et->previous)
  33.          cout << "  * " << et->entry << "\n";
  34.    }
  35. }
  36.  
  37. Exception::Exception(int action)
  38. {
  39.    if (action)
  40.    {
  41.       cout << "\nAn exception has occurred: call trace follows.";
  42.       PrintTrace();
  43.       if (action < 0) exit(1);
  44.    }
  45. }   
  46.  
  47. Janitor::Janitor()
  48. {
  49.    if (do_not_link) { do_not_link = FALSE; NextJanitor = 0; OnStack = FALSE; }
  50.    else
  51.    {
  52.       OnStack = TRUE;
  53.       NextJanitor = JumpBase::jl->janitor; JumpBase::jl->janitor=this;
  54.    }
  55. }
  56.  
  57. Janitor::~Janitor() { if (OnStack) JumpBase::jl->janitor = NextJanitor; }
  58.  
  59. JumpItem* JumpBase::jl = 0;
  60. long JumpBase::type;
  61. Tracer* Exception::last = 0;
  62. Boolean Janitor::do_not_link = FALSE;
  63.  
  64. static JumpItem JI;                  // need JumpItem at head of list
  65.  
  66.  
  67.  
  68. void Terminate()
  69. {
  70.    cout << "\nThere has been an exception with no handler - exiting\n";
  71.    exit(1);
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. #ifdef DO_FREE_CHECK
  80. // Routines for tracing whether new and delete calls are balanced
  81.  
  82. FreeCheckLink::FreeCheckLink() : next(FreeCheck::next)
  83.    { FreeCheck::next = this; }
  84.  
  85. FCLClass::FCLClass(void* t, char* name) : ClassName(name) { ClassStore=t; }
  86.    
  87. FCLRealArray::FCLRealArray(void* t, char* o, int s)
  88.   : Operation(o), size(s) { ClassStore=t; }
  89.  
  90. FCLIntArray::FCLIntArray(void* t, char* o, int s)
  91.   : Operation(o), size(s) { ClassStore=t; }
  92.  
  93. FreeCheckLink* FreeCheck::next = 0;
  94.  
  95. void FCLClass::Report()
  96. { cout << "   " << ClassName << "   " << (unsigned long)ClassStore << "\n"; }
  97.  
  98. void FCLRealArray::Report()
  99. {
  100.    cout << "   " << Operation << "   " << (unsigned long)ClassStore << 
  101.       "   " << size << "\n";
  102. }
  103.  
  104. void FCLIntArray::Report()
  105. {
  106.    cout << "   " << Operation << "   " << (unsigned long)ClassStore << 
  107.       "   " << size << "\n";
  108. }
  109.  
  110. void FreeCheck::Register(void* t, char* name)
  111. {
  112.    FCLClass* f = new FCLClass(t,name);
  113.    if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  114. //   cout << "Registering   " << name << "   " << (unsigned long)t << "\n";
  115. }
  116.  
  117. void FreeCheck::RegisterR(void* t, char* o, int s)
  118. {
  119.    FCLRealArray* f = new FCLRealArray(t,o,s);
  120.    if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  121. //   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  122. }
  123.  
  124. void FreeCheck::RegisterI(void* t, char* o, int s)
  125. {
  126.    FCLIntArray* f = new FCLIntArray(t,o,s);
  127.    if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  128. //   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  129. }
  130.  
  131. void FreeCheck::DeRegister(void* t, char* name)
  132. {
  133.    FreeCheckLink* last = 0;
  134. //   cout << "Deregistering " << name << "   " << (unsigned long)t << "\n";
  135.    for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  136.    {
  137.       if (fcl->ClassStore==t)
  138.       {
  139.      if (last) last->next = fcl->next; else next = fcl->next;
  140.      delete fcl; return;
  141.       }
  142.       last = fcl;
  143.    }
  144.    cout << "\nRequest to delete non-existent object of class and location:\n";
  145.    cout << "   " << name << "   " << (unsigned long)t << "\n";
  146.    Exception::PrintTrace(TRUE);
  147.    cout << "\n";
  148. }
  149.  
  150. void FreeCheck::DeRegisterR(void* t, char* o, int s)
  151. {
  152.    FreeCheckLink* last = 0;
  153. //   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  154.    for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  155.    {
  156.       if (fcl->ClassStore==t)
  157.       {
  158.      if (last) last->next = fcl->next; else next = fcl->next;
  159.      if (((FCLRealArray*)fcl)->size != s)
  160.      {
  161.         cout << "\nArray sizes don't agree:\n";
  162.         cout << "   " << o << "   " << (unsigned long)t
  163.            << "   " << s << "\n";
  164.         Exception::PrintTrace(TRUE);
  165.         cout << "\n";
  166.      }
  167.      delete fcl; return;
  168.       }
  169.       last = fcl;
  170.    }
  171.    cout << "\nRequest to delete non-existent real array:\n";
  172.    cout << "   " << o << "   " << (unsigned long)t << "   " << s << "\n";
  173.    Exception::PrintTrace(TRUE);
  174.    cout << "\n";
  175. }
  176.  
  177. void FreeCheck::DeRegisterI(void* t, char* o, int s)
  178. {
  179.    FreeCheckLink* last = 0;
  180. //   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  181.    for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  182.    {
  183.       if (fcl->ClassStore==t)
  184.       {
  185.      if (last) last->next = fcl->next; else next = fcl->next;
  186.      if (((FCLIntArray*)fcl)->size != s)
  187.      {
  188.         cout << "\nArray sizes don't agree:\n";
  189.         cout << "   " << o << "   " << (unsigned long)t
  190.            << "   " << s << "\n";
  191.         Exception::PrintTrace(TRUE);
  192.         cout << "\n";
  193.      }
  194.      delete fcl; return;
  195.       }
  196.       last = fcl;
  197.    }
  198.    cout << "\nRequest to delete non-existent int array:\n";
  199.    cout << "   " << o << "   " << (unsigned long)t << "   " << s << "\n";
  200.    Exception::PrintTrace(TRUE);
  201.    cout << "\n";
  202. }
  203.  
  204. void FreeCheck::Status()
  205. {
  206.    if (next)
  207.    {
  208.       cout << "\nObjects of the following classes remain undeleted:\n";
  209.       for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next) fcl->Report();
  210.       cout << "\n";
  211.    }
  212.    else cout << "\nNo objects remain undeleted\n";
  213. }
  214.  
  215. #endif
  216.  
  217.  
  218.