home *** CD-ROM | disk | FTP | other *** search
- /* ==========
- * AEADesc.cc
- * ==========
- */
-
- #include "AEADebugging.h"
-
- #include <string.h>
-
- #include <AppleEvents.h>
- #include <Errors.h>
-
- #include "AEADesc.hh"
-
- static AEDesc sNullDesc = {typeNull, NULL};
-
- AEADescX::AEADescX()
- : mAEDesc(sNullDesc), refCount(1), owned(true)
- {
- }
-
- AEADescX::AEADescX(AEDesc inAEDesc)
- : mAEDesc(inAEDesc), refCount(1), owned(false)
- {
- }
-
- AEADescX::~AEADescX()
- {
- if (owned) {
- OSErr err = ::AEDisposeDesc(&mAEDesc);
- }
- }
-
- void
- AEADescX::Retain()
- {
- refCount++;
- }
-
- void
- AEADescX::Release()
- {
- if (--refCount == 0) {
- delete this;
- }
- }
-
-
- AEADesc::AEADesc()
- : mX(*new AEADescX())
- {
- }
-
- AEADesc::AEADesc(AEDesc inAEDesc)
- : mX(*new AEADescX(inAEDesc))
- {
- }
-
- // Copy constructor
- AEADesc::AEADesc(AEADesc &inDesc)
- : mX(inDesc.mX)
- {
- mX.Retain();
- }
-
- AEADesc::~AEADesc()
- {
- mX.Release();
- }
-
- AEDesc &
- AEADesc::Ref()
- {
- return mX.mAEDesc;
- }
-
- const AEDesc &
- AEADesc::Ref() const
- {
- return mX.mAEDesc;
- }
-
- void
- AEADesc::Reset()
- {
- mX.mAEDesc = sNullDesc;
- }
-
- DescType
- AEADesc::DescriptorType() const
- {
- return mX.mAEDesc.descriptorType;
- }
-
- Boolean
- AEADesc::Equals(const AEDesc &inAEDesc) const
- {
- Size size;
-
- // It would be nice if this did some coercion, but it works for now...
- return (mX.mAEDesc.descriptorType == inAEDesc.descriptorType)
- && ((size = ::GetHandleSize(mX.mAEDesc.dataHandle)) == ::GetHandleSize(inAEDesc.dataHandle))
- && (memcmp(*mX.mAEDesc.dataHandle, *inAEDesc.dataHandle, size) == 0);
- }
-
- void
- AEADesc::Create(DescType inTypeCode, const void *inDataPtr, Size inDataSize)
- {
- //EnsureDesc();
- OSErr err = ::AECreateDesc(inTypeCode, inDataPtr, inDataSize, &mX.mAEDesc);
- ThrowIfOSErr_(err);
- }
-
- const AEDesc &
- AEADesc::operator=(const AEDesc &inAEDesc)
- {
- Duplicate(inAEDesc);
- return inAEDesc;
- }
-
- void
- AEADesc::Duplicate(const AEDesc &inAEDesc)
- {
- //EnsureDesc();
- OSErr err = ::AEDuplicateDesc(&inAEDesc, &mX.mAEDesc);
- ThrowIfOSErr_(err);
- }
-
- void
- AEADesc::Dispose()
- {
- OSErr err = ::AEDisposeDesc(&mX.mAEDesc); // this resets the AEDesc
- ThrowIfOSErr_(err);
- }
-
- void
- AEADesc::Get(DescType &outType) const
- {
- // This should really be using Apple's coercion routines. Too bad for now.
- if (/*mAEDesc.descriptorType != typeLongInteger ||*/ mX.mAEDesc.dataHandle == NULL) {
- ThrowOSErr_(errAECoercionFail);
- } else {
- outType = **(DescType **)mX.mAEDesc.dataHandle;
- }
- }
-
- void
- AEADesc::Get(long &outLongInt) const
- {
- //VerifyDesc();
- // This should really be using Apple's coercion routines. Too bad for now.
- if (/*mAEDesc.descriptorType != typeLongInteger ||*/ mX.mAEDesc.dataHandle == NULL) {
- ThrowOSErr_(errAECoercionFail);
- } else {
- if (mX.mAEDesc.descriptorType == typeShortInteger)
- outLongInt = (long)**(short **)mX.mAEDesc.dataHandle;
- else
- outLongInt = **(long **)mX.mAEDesc.dataHandle;
- }
- }
-
- void
- AEADesc::Get(Str255 &outString) const
- {
- // This should really be using Apple's coercion routines. Too bad for now.
- if (mX.mAEDesc.descriptorType != typeChar || mX.mAEDesc.dataHandle == NULL) {
- outString[0] = 0;
- ThrowOSErr_(errAECoercionFail);
- } else {
- Size len = GetHandleSize(mX.mAEDesc.dataHandle);
- if (len > 255)
- len = 255;
- outString[0] = len;
- memcpy(outString + 1, *mX.mAEDesc.dataHandle, len);
- }
- }
-
-
- #if 0
- AEADesc::AEADesc(const AEDesc &inAEDesc)
- : mCallAEDisposeDesc(false)
- {
- mAEDesc = new AEDesc(inAEDesc);
- mDeleteStruct = true;
- }
-
- AEADesc::AEADesc(DescType inTypeCode, const void *inDataPtr, Size inDataSize)
- : mAEDesc(NULL)
- {
- Create(inTypeCode, inDataPtr, inDataSize);
- }
-
- AEADesc::~AEADesc()
- {
- if (mCallAEDisposeDesc)
- Dispose();
- if (mDeleteStruct) {
- delete mAEDesc;
- mAEDesc = NULL;
- }
- }
-
- #endif
-