home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 13.7 KB | 571 lines | [TEXT/MPS ] |
- /* UObject.cp */
- /* Copyright © 1984-1991 by Apple Computer, Inc. All rights reserved. */
-
- #ifndef __UOBJECT__
- #include "UObject.h"
- #endif
-
- #ifndef __STDIO__
- #include "StdIo.h"
- #endif
-
- #ifndef __UDEPENDENCIES__
- #include "UDependencies.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- // StripLong etc.
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- // SetPermHandleSize, etc.
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #if qDebug
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
- #endif
-
- #if qInspector
- #ifndef __UINSPECTOR__
- #include "UInspector.h"
- #endif
- #endif
-
- #ifndef qMacApp
- #define qMacApp False
- #endif
-
-
- // • Move the following to UObject.h when I get it generated.
-
- typedef pascal void(* DoToSubClassType)(ObjClassID theClass,
- void* localScope);
- typedef pascal void(* DoToSuperClassType)(ObjClassID theClass,
- void* localScope);
-
- //!!! Also Defined in UList
- typedef pascal void(* DoToObjectType)(TObject* item,
- void* staticLink);
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::Changed(ChangeID theChange,
- TObject* changedBy)
- {
- this->MarkDependents(true);
- this->UpdateDependents(theChange, this, changedBy);
- this->MarkDependents(false);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::DoUpdate(ChangeID& theChange,
- TObject* changedObject,
- TObject* changedBy)
-
- {
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::AddDependent(TObject* dependent)
-
- {
- AddDependentOf(this, dependent, kNoLabel);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::RemoveDependent(TObject* dependent)
-
- {
- RemoveDependentOf(this, dependent, kNoLabel);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::RemoveAllDependencies(void)
-
- {
- RemoveDependencies(this); // Call the routine in UDependencies
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::EachDependent(DoToObjectType DoToDependent,
- void* staticLink)
- {
- EachDependentOf(this, DoToDependent, staticLink);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::EachNotifier(DoToObjectType DoToNotifier,
- void* staticLink)
- {
- EachNotifierOf(this, DoToNotifier, staticLink);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::HandleUpdate(ChangeID theChange,
- TObject* changedObject,
- TObject* changedBy)
- {
- if (this->IsMarked())
- {
- this->SetMark(false);
- this->UpdateNotifiers(theChange, changedObject, changedBy);
- this->DoUpdate(theChange, changedObject, changedBy);
- if (theChange != kNoChange)
- this->UpdateDependents(theChange, changedObject, changedBy);
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Boolean TObject::IsMarked(void)
- {
- return IsObjectMarked(this);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::MarkRecursively(Boolean state)
- {
- if (state != this->IsMarked())
- {
- this->SetMark(state);
- this->MarkDependents(state);
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- class CMarkDependent
- {
- Boolean fState;
- public:
- CMarkDependent(Boolean state) :
- fState(state)
- {
- }
-
- pascal void MarkDependent(TObject* aDependent);
- };
-
- #pragma segment MAObjectRes
- pascal void CMarkDependent::MarkDependent(TObject* aDependent)
- {
- aDependent->MarkRecursively(fState);
- }
-
- #pragma segment MAObjectRes
- pascal void TObject::MarkDependents(Boolean state)
- {
- CMarkDependent scopeLink(state);
- this->EachDependent((DoToObjectType) & CMarkDependent::MarkDependent, &scopeLink);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::SetMark(Boolean state)
- {
- MarkObject(this, state);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- class CUpdateDependent
- {
- ChangeID& fChange;
- TObject* fChangedObject;
- TObject* fChangedBy;
- public:
- CUpdateDependent(ChangeID& theChange,
- TObject* changedObject,
- TObject* changedBy) :
- fChange(theChange),
- fChangedObject(changedObject),
- fChangedBy(changedBy)
- {
- }
-
- pascal void UpdateDependent(TObject* aDependent);
- };
-
- #pragma segment MAObjectRes
- pascal void CUpdateDependent::UpdateDependent(TObject* aDependent)
- {
- aDependent->HandleUpdate(fChange, fChangedObject, fChangedBy);
- }
-
- #pragma segment MAObjectRes
- pascal void TObject::UpdateDependents(ChangeID theChange,
- TObject* changedObject,
- TObject* changedBy)
- {
- CUpdateDependent scopeLink(theChange, changedObject, changedBy);
- this->EachDependent((DoToObjectType) & CUpdateDependent::UpdateDependent, &scopeLink);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- class CUpdateNotifier
- {
- ChangeID& fChange;
- TObject* fChangedObject;
- TObject* fChangedBy;
- public:
- CUpdateNotifier(ChangeID& theChange,
- TObject* changedObject,
- TObject* changedBy) :
- fChange(theChange),
- fChangedObject(changedObject),
- fChangedBy(changedBy)
- {
- }
- pascal void UpdateNotifier(TObject* aNotifier);
- };
-
- #pragma segment MAObjectRes
- pascal void CUpdateNotifier::UpdateNotifier(TObject* aNotifier)
- {
- aNotifier->HandleUpdate(fChange, fChangedObject, fChangedBy);
- }
-
- #pragma segment MAObjectRes
- pascal void TObject::UpdateNotifiers(ChangeID theChange,
- TObject* changedObject,
- TObject* changedBy)
- {
- CUpdateNotifier scopeLink(theChange, changedObject, changedBy);
- this->EachNotifier((DoToObjectType) & CUpdateNotifier::UpdateNotifier, &scopeLink);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal TObject* TObject::Clone(void)
-
- {
- return this->ShallowClone();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamNever
-
- pascal void TObject::ReadFrom(TStream* /* aStream */)
- {
- this->SubClassResponsibility();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamNever
-
- pascal void TObject::WriteTo(TStream* /* aStream */)
- {
- this->SubClassResponsibility();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TObject::DynamicFields(TObject* obj)
-
- {
- // ??? Would a nicer default be a formatted memory dump?
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TObject::DoToField(const Str255& ,
- void* ,
- short)
-
- {
- this->SubClassResponsibility();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TObject::Fields(TObject* obj)
- {
- static const short bObjClassID = bInteger;
- ObjClassID myID;
- Size mySize;
-
- myID = this->GetClass();
- mySize = this->GetClassSize() + this->GetDynamicSize();
-
- obj->DoToField("TObject", (Ptr)NULL, bClass);
- obj->DoToField("ObjClassID", (Ptr) & myID, bObjClassID);
- obj->DoToField("Size in bytes", &mySize, bLongInt);
-
- this->DynamicFields(obj); // Get the dynamic part inspected
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::ForAllSubClassesDo(DoToSubClassType DoToSubClass,
- void* localScope)
- {
- EachSubClassDo(this->GetClass(), DoToSubClass, localScope);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::ForAllSuperClassesDo(DoToSuperClassType DoToSuperClass,
- void* localScope)
- {
- EachSuperClassDo(this->GetClass(), DoToSuperClass, localScope);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::Free(void)
-
- {
- this->RemoveAllDependencies();
- this->ShallowFree();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::GetClassName(MAName& clName)
- {
- GetClassNameFromID(this->GetClass(), clName);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal ObjClassID TObject::GetClass(void)
- {
- return GetClassID(this);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Size TObject::GetClassSize(void)
- {
- return GetClassSizeFromID(this->GetClass());
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Ptr TObject::GetDynamicPtr(void)
- {
- Size classSize = this->GetClassSize();
-
- if (GetHandleSize((Handle)this) - classSize > 0)
- return (Ptr)(StripLong(*((Handle)this)) + classSize);
- else
- return NULL;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAInspector
-
- pascal void TObject::GetInspectorName(Str255& inspectorName)
- {
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Size TObject::GetDynamicSize(void)
- {
- return GetHandleSize((Handle)this) - this->GetClassSize();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal ObjClassID TObject::GetSuperClass(void)
- {
- return GetSuperClassID(this->GetClass());
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::Initialize(void)
- {
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::IObject(void)
- {
- this->Initialize();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Boolean TObject::IsSameClass(ObjClassID testClass)
- {
- return this->GetClass() == testClass;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Boolean TObject::IsMemberClass(ObjClassID testClass)
- {
- return IsClassIDMemberClass(this->GetClass(), testClass);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal Boolean TObject::Lock(Boolean lockIt)
- {
- Boolean wasLocked;
-
- wasLocked = IsHandleLocked((Handle)this);
- if (wasLocked != lockIt)
- {
- if (lockIt)
- HLock((Handle)this);
- else
- HUnlock((Handle)this);
- }
- return wasLocked;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::SetDynamicSize(Size newSize)
- {
- Boolean objectsFromPerm = AllocateObjectsFromPerm(true);
-
- AllocateObjectsFromPerm(objectsFromPerm);
-
- if (qMacApp && objectsFromPerm)
- SetPermHandleSize((Handle)this, newSize + this->GetClassSize());
- else
- SetHandleSize((Handle)this, newSize + this->GetClassSize());
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal TObject* TObject::ShallowClone(void)
- {
- TObject * result;
- Boolean oldPerm;
- OSErr err;
-
- #if qDebugMsg
- MAName s;
- #endif
-
- #if qDebugMsg
- if (gAskAboutAlloc)
- {
- GetCallersMethodName(s);
-
- if (s == "TOBJECT.CLONE") /* report about caller of TObject->Clone(),
- instead */
- GetMethodName(*((LongIntPtr)GetCurStackFramePtr()) + 4, s);
-
- fprintf(stderr, "Within %s: trying to clone a: '", (char *) s);
- this->GetClassName(s);
- fprintf(stderr, "%s.", (char *) s);
-
- #ifdef qDebug
- if (ReadYesNo(" Return NULL (Y or N) [N]? "))
- return NULL;
- #endif
- }
- #endif
-
- result = this;
-
- #if qMacApp
- oldPerm = PermAllocation(true);
- #endif
-
- err = HandToHand(*((Handle*)&result));
-
- #if qMacApp
- oldPerm = PermAllocation(oldPerm);
- #endif
-
- if (err != noErr)
- result = NULL;
- FailNIL(result);
-
- if (qInspector)
- AddObjectToInspector(result);
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::ShallowFree(void)
-
- {
- delete this;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- pascal void TObject::SubClassResponsibility(void)
- {
- #if qDebug
- MAName s;
- #endif
-
- #if qDebugMsg
- GetCallersMethodName(s);
-
- fprintf(stderr, "%s: must be overridden!", (char *) s);
- ProgramBreak("");
- #endif
-
- Failure(minErr, 0); // assign an error message
- }
-
-