home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / AEA / Source / Sources / Descriptors / AEADesc.cc next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  3.5 KB  |  204 lines

  1. /*    ==========
  2.  *    AEADesc.cc
  3.  *    ==========
  4.  */
  5.  
  6. #include "AEADebugging.h"
  7.  
  8. #include <string.h>
  9.  
  10. #include <AppleEvents.h>
  11. #include <Errors.h>
  12.  
  13. #include "AEADesc.hh"
  14.  
  15. static AEDesc sNullDesc = {typeNull, NULL};
  16.  
  17. AEADescX::AEADescX()
  18. : mAEDesc(sNullDesc), refCount(1), owned(true)
  19. {
  20. }
  21.  
  22. AEADescX::AEADescX(AEDesc inAEDesc)
  23. : mAEDesc(inAEDesc), refCount(1), owned(false)
  24. {
  25. }
  26.  
  27. AEADescX::~AEADescX()
  28. {
  29.     if (owned) {
  30.         OSErr err = ::AEDisposeDesc(&mAEDesc);
  31.     }
  32. }
  33.  
  34. void
  35. AEADescX::Retain()
  36. {
  37.     refCount++;
  38. }
  39.  
  40. void
  41. AEADescX::Release()
  42. {
  43.     if (--refCount == 0) {
  44.         delete this;
  45.     }
  46. }
  47.  
  48.  
  49. AEADesc::AEADesc()
  50. : mX(*new AEADescX())
  51. {
  52. }
  53.  
  54. AEADesc::AEADesc(AEDesc inAEDesc)
  55. : mX(*new AEADescX(inAEDesc))
  56. {
  57. }
  58.  
  59. // Copy constructor
  60. AEADesc::AEADesc(AEADesc &inDesc)
  61. : mX(inDesc.mX)
  62. {
  63.     mX.Retain();
  64. }
  65.  
  66. AEADesc::~AEADesc()
  67. {
  68.     mX.Release();
  69. }
  70.  
  71. AEDesc &
  72. AEADesc::Ref()
  73. {
  74.     return mX.mAEDesc;
  75. }
  76.  
  77. const AEDesc &
  78. AEADesc::Ref() const
  79. {
  80.     return mX.mAEDesc;
  81. }
  82.  
  83. void
  84. AEADesc::Reset()
  85. {
  86.     mX.mAEDesc = sNullDesc;
  87. }
  88.  
  89. DescType
  90. AEADesc::DescriptorType() const
  91. {
  92.     return mX.mAEDesc.descriptorType;
  93. }
  94.  
  95. Boolean
  96. AEADesc::Equals(const AEDesc &inAEDesc) const
  97. {
  98.     Size size;
  99.     
  100.     // It would be nice if this did some coercion, but it works for now...
  101.     return (mX.mAEDesc.descriptorType == inAEDesc.descriptorType)
  102.         && ((size = ::GetHandleSize(mX.mAEDesc.dataHandle)) == ::GetHandleSize(inAEDesc.dataHandle))
  103.         && (memcmp(*mX.mAEDesc.dataHandle, *inAEDesc.dataHandle, size) == 0);
  104. }
  105.  
  106. void
  107. AEADesc::Create(DescType inTypeCode, const void *inDataPtr, Size inDataSize)
  108. {
  109.     //EnsureDesc();
  110.     OSErr err = ::AECreateDesc(inTypeCode, inDataPtr, inDataSize, &mX.mAEDesc);
  111.     ThrowIfOSErr_(err);
  112. }
  113.  
  114. const AEDesc &
  115. AEADesc::operator=(const AEDesc &inAEDesc)
  116. {
  117.     Duplicate(inAEDesc);
  118.     return inAEDesc;
  119. }
  120.  
  121. void
  122. AEADesc::Duplicate(const AEDesc &inAEDesc)
  123. {
  124.     //EnsureDesc();
  125.     OSErr err = ::AEDuplicateDesc(&inAEDesc, &mX.mAEDesc);
  126.     ThrowIfOSErr_(err);
  127. }
  128.  
  129. void
  130. AEADesc::Dispose()
  131. {
  132.     OSErr err = ::AEDisposeDesc(&mX.mAEDesc);  // this resets the AEDesc
  133.     ThrowIfOSErr_(err);
  134. }
  135.  
  136. void
  137. AEADesc::Get(DescType &outType) const
  138. {
  139.     // This should really be using Apple's coercion routines. Too bad for now.
  140.     if (/*mAEDesc.descriptorType != typeLongInteger ||*/ mX.mAEDesc.dataHandle == NULL) {
  141.         ThrowOSErr_(errAECoercionFail);
  142.     } else {
  143.         outType = **(DescType **)mX.mAEDesc.dataHandle;
  144.     }
  145. }
  146.  
  147. void
  148. AEADesc::Get(long &outLongInt) const
  149. {
  150.     //VerifyDesc();
  151.     // This should really be using Apple's coercion routines. Too bad for now.
  152.     if (/*mAEDesc.descriptorType != typeLongInteger ||*/ mX.mAEDesc.dataHandle == NULL) {
  153.         ThrowOSErr_(errAECoercionFail);
  154.     } else {
  155.         if (mX.mAEDesc.descriptorType == typeShortInteger)
  156.             outLongInt = (long)**(short **)mX.mAEDesc.dataHandle;
  157.         else
  158.             outLongInt = **(long **)mX.mAEDesc.dataHandle;
  159.     }
  160. }
  161.  
  162. void
  163. AEADesc::Get(Str255 &outString) const
  164. {
  165.     // This should really be using Apple's coercion routines. Too bad for now.
  166.     if (mX.mAEDesc.descriptorType != typeChar || mX.mAEDesc.dataHandle == NULL) {
  167.         outString[0] = 0;
  168.         ThrowOSErr_(errAECoercionFail);
  169.     } else {
  170.         Size len = GetHandleSize(mX.mAEDesc.dataHandle);
  171.         if (len > 255)
  172.             len = 255;
  173.         outString[0] = len;
  174.         memcpy(outString + 1, *mX.mAEDesc.dataHandle, len);
  175.     }
  176. }
  177.  
  178.  
  179. #if 0
  180. AEADesc::AEADesc(const AEDesc &inAEDesc)
  181. : mCallAEDisposeDesc(false)
  182. {
  183.     mAEDesc = new AEDesc(inAEDesc);
  184.     mDeleteStruct = true;
  185. }
  186.  
  187. AEADesc::AEADesc(DescType inTypeCode, const void *inDataPtr, Size inDataSize)
  188. : mAEDesc(NULL)
  189. {
  190.     Create(inTypeCode, inDataPtr, inDataSize);
  191. }
  192.  
  193. AEADesc::~AEADesc()
  194. {
  195.     if (mCallAEDisposeDesc)
  196.         Dispose();
  197.     if (mDeleteStruct) {
  198.         delete mAEDesc;
  199.         mAEDesc = NULL;
  200.     }
  201. }
  202.  
  203. #endif
  204.