home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Core Utilities / LocationInCode.cp < prev    next >
Encoding:
Text File  |  1998-04-01  |  5.8 KB  |  312 lines  |  [TEXT/CWIE]

  1. #define __NO_LOCATION_MACROS__
  2. #ifndef __EXCEPTIONS__
  3. #include "Exceptions.h"
  4. #endif
  5. #ifndef __LOCATIONINCODE__
  6. #include "LocationInCode.h"
  7. #endif
  8. #ifndef __DEBUGWRITE__
  9. #include "DebugWrite.h"
  10. #endif
  11. #ifndef __AEBUILD__
  12. #include "AEBuild.h"
  13. #endif
  14.  
  15. #ifndef __OSA__
  16. #include <OSA.h>
  17. #endif
  18.  
  19. //------------------------------------------------------------------------------
  20.  
  21. void LocationInCode::ThrowAEErr(
  22.     OSStatus                err,
  23.     const char*                format, ...) const
  24. {
  25.     StdException exc(*this, err);
  26.  
  27.     if (format != nil)
  28.     {
  29.         va_list arg; va_start(arg, format);
  30.         
  31.         LogIfErr(vAEBuild(&exc.GetErrorParams(), format, arg));
  32.         
  33.         va_end(arg);
  34.     }
  35.         
  36.     exc.AboutToThrow();
  37.     throw exc;
  38. }
  39.  
  40. //------------------------------------------------------------------------------
  41.  
  42. void LocationInCode::Throw(
  43.     OSStatus                err) const
  44. {
  45.     ThrowAEErr(err, nil);
  46. }
  47.  
  48. //------------------------------------------------------------------------------
  49.  
  50. void LocationInCode::Throw(
  51.     OSStatus                err,        // an error message
  52.     long                /*    message*/,    // MacApp message
  53.     AEKeyword                parameter,
  54.     DescType                paramType,
  55.     const void*                paramData,
  56.     Size                    paramSize) const
  57. {
  58.     if (err != noErr)
  59.     {
  60.         AEDesc desc;
  61.  
  62.         desc.descriptorType    = typeNull;
  63.         desc.dataHandle        = nil;
  64.  
  65.         if (parameter)
  66.         {
  67.             if (LogIfErr(AECreateList(nil, 0, true, &desc)) == noErr)
  68.             {
  69.                 LogIfErr(AEPutKeyPtr(&desc, parameter, paramType,
  70.                                      paramData, paramSize));
  71.             }
  72.         }
  73.         
  74.         Throw(err, &desc);
  75.     }
  76. }
  77.  
  78. //------------------------------------------------------------------------------
  79.  
  80. void LocationInCode::Throw(
  81.     OSStatus                err,
  82.     AEDesc*                    params) const
  83. {
  84.     StdException exc(*this, err);
  85.     
  86.     if (params != nil)
  87.     {
  88.         exc.GetErrorParams()    = *params;
  89.         params->descriptorType    = typeNull;
  90.         params->dataHandle        = nil;
  91.     }
  92.     
  93.     exc.AboutToThrow();
  94.     throw exc;
  95. }
  96.  
  97. //------------------------------------------------------------------------------
  98.  
  99. /*
  100.  
  101. void LocationInCode::Throw(
  102.     OSStatus                err,
  103.     char*                    message) const
  104. {
  105.     Throw(err, 0, keyErrorString, typeChar,
  106.          message, strlen(message));
  107. }
  108.  
  109. */
  110.  
  111. //------------------------------------------------------------------------------
  112.  
  113. void LocationInCode::ThrowTypeError(
  114.     OSStatus                err,
  115.     DescType                expectedType) const
  116. {
  117.     Throw(err, 0, kOSAErrorExpectedType, typeType,
  118.         &expectedType, sizeof(expectedType));
  119. }
  120.  
  121. //------------------------------------------------------------------------------
  122.  
  123. void LocationInCode::ThrowKeywordError(
  124.     OSStatus                err,            //
  125.     DescType                expectedType,    // In case of coercion failure, etc.
  126.     const AEDesc&        /*    desc*/,            // Where we were gitting data from
  127.     AEKeyword                key) const
  128. {
  129.     AEKeyword    parameter = 0;
  130.     DescType    paramType = 0;
  131.     void*        paramData = 0;
  132.     Size        paramSize = 0;
  133.  
  134. /// update this •••
  135.  
  136.     switch (err)
  137.     {
  138.     case noErr:
  139.         return;
  140.  
  141.     case errAECoercionFail:
  142.         parameter = kOSAErrorExpectedType;
  143.         paramType = typeType;
  144.         paramData = &expectedType;
  145.         paramSize = sizeof(expectedType);
  146.         break;
  147.  
  148.     case errAEDescNotFound:
  149.         parameter = kOSAErrorOffendingObject;
  150.         paramType = typeKeyword;
  151.         paramData = &key;
  152.         paramSize = sizeof(key);
  153.         break;
  154.  
  155.     }
  156.  
  157.     Throw(err, 0, parameter, paramType, paramData, paramSize);
  158. }
  159.  
  160. //------------------------------------------------------------------------------
  161.  
  162. void LocationInCode::ThrowNILResource() const
  163. {
  164.     OSErr err = ResError();
  165.  
  166.     Throw(err ? err : resNotFound);
  167. }
  168.  
  169. //------------------------------------------------------------------------------
  170.  
  171. void LocationInCode::ThrowNILResource(
  172.     ResType                /*    type    */,
  173.     short                /*    resID   */) const
  174. {
  175.     OSErr err = ResError();
  176.  
  177.     Throw(err ? err : resNotFound);
  178. }
  179.  
  180. //------------------------------------------------------------------------------
  181.  
  182.  
  183. /*
  184. void ThrowErrNum(const LocationInCode& where,
  185.         OSStatus        err,
  186.         DescType        expectedType,
  187.         DescType        actualType,
  188.         AEKeyword        key,
  189.         const AEDesc*    offendingObject)
  190. {
  191.     if (err != noErr)
  192.     {
  193.         StdException exc(where, err);
  194.         
  195.         if (expectedType || actualType)
  196.         {
  197.             exc.PutExpectedType(expectedType, actualType);
  198.         }
  199.         
  200.         if (offendingObject)
  201.         {
  202.             exc.PutOffendingObject(*offendingObject);
  203.         }
  204.         else if (key)
  205.         {
  206.             exc.PutOffendingParameter(key);
  207.         }
  208.         
  209.         exc.AboutToThrow();
  210.         throw exc;
  211.     }
  212. }
  213. */
  214.  
  215. //------------------------------------------------------------------------------
  216.  
  217. OSStatus LocationInCode::LogError(
  218.     OSStatus                err) const
  219. {
  220.     LogError(err, nil);
  221.  
  222.     return err;
  223. }
  224.  
  225. //------------------------------------------------------------------------------
  226.  
  227. OSStatus LocationInCode::LogError(
  228.     OSStatus                err,
  229.     const char*                msg) const
  230. {
  231.     Str255 message;
  232.  
  233.     message[0] = '\0';
  234.     AppendToString(message);
  235.  
  236.     DebugWrite("\perror ");
  237.     DebugWriteNum(err);
  238.     DebugWrite("\p, ");
  239.     if (msg)
  240.     {
  241.         DebugWritePtr(msg, strlen(msg));
  242.     }
  243.     DebugWriteLn(message);
  244.  
  245.     return err;
  246. }
  247.  
  248. //------------------------------------------------------------------------------
  249.  
  250. bool LocationInCode::DelegateAECall() const
  251. {
  252.     SilentException exc(GetLocationInCode(), errAEEventNotHandled);
  253.     exc.AboutToThrow();
  254.     throw exc;
  255.     return false;
  256. }    
  257.  
  258. //------------------------------------------------------------------------------
  259.  
  260. #if qDebug
  261. void LocationInCode::Warning(const char* message) const
  262. {
  263.     if (message)
  264.     {
  265.         DebugWrite("\pWarning (");
  266.         DebugWrite(message);
  267.         DebugWriteLn("\p).", true);
  268.     }
  269.     else
  270.     {
  271.         DebugWriteLn("\pWarning", true);
  272.     }
  273. }
  274.  
  275. void LocationInCode::AssertionFailed(const char* message) const
  276. {
  277.     if (message)
  278.     {
  279.         DebugWrite("\passertion failed (");
  280.         DebugWrite(message);
  281.         DebugWriteLn("\p).", true);
  282.     }
  283.     else
  284.     {
  285.         DebugWriteLn("\passertion failed.", true);
  286.     }
  287.  
  288.     Throw(eAssertionFailure);
  289. }
  290. #endif
  291.  
  292. //------------------------------------------------------------------------------
  293.  
  294. void LocationInCode::RequirementNotMet(const char* message) const
  295. {
  296.     if (message)
  297.     {
  298.         DebugWrite("\prequirement not met (");
  299.         DebugWrite(message);
  300.         DebugWriteLn("\p).", true);
  301.     }
  302.     else
  303.     {
  304.         DebugWriteLn("\prequirement not met.", true);
  305.     }
  306.     
  307.     Throw(eRequirementNotMet);
  308. }
  309.  
  310. //------------------------------------------------------------------------------
  311.  
  312.