#include "StdAfx.h" #include "LArray.h" #include "LAttachable.h" #include "LAttachment.h" LAttachable::LAttachable() { mAttachments = NULL; SetDefaultAttachable(this); } LAttachable::LAttachable(const LAttachable& other) { mAttachments = NULL; SetDefaultAttachable(this); } /*virtual*/ LAttachable::~LAttachable() { RemoveAllAttachments(); } /*virtual*/ void LAttachable::AddAttachment(LAttachment* attachment, LAttachment* before, BOOL setAsOwner) { if (!mAttachments) mAttachments = new LArray; if (!before) { mAttachments->Add(&attachment); } else { int index = mAttachments->GetCount() + 1; if (before) { // bug? should this use &before? index = mAttachments->FetchIndexOf(before); if (index == 0) index = mAttachments->GetCount() + 1; } mAttachments->InsertItemsAt(1, index, &attachment); } if (setAsOwner) attachment->SetOwnerHost(this); } /*virtual*/ void LAttachable::RemoveAttachment(LAttachment* attachment) { if (mAttachments) { mAttachments->Remove(&attachment); if (attachment->GetOwnerHost() == this) attachment->SetOwnerHost(NULL); } } /*virtual*/ void LAttachable::RemoveAllAttachments() { if (mAttachments) { LArrayIterator iter(*mAttachments); LAttachment *theAttachment; while (iter.Next(&theAttachment)) { if (theAttachment->GetOwnerHost() == this) { delete theAttachment; iter.Reset(); } } delete mAttachments; mAttachments = NULL; } } /*virtual*/ BOOL LAttachable::ExecuteAttachments(unsigned int message, void* data) { BOOL result = true; if (mAttachments) { LArrayIterator iter(*mAttachments); LAttachment *theAttachment; while (iter.Next(&theAttachment)) { result = result & theAttachment->Execute(message, data); } } return result; } /*static*/ LAttachable* LAttachable::sDefaultAttachable; /*static*/ LAttachable* LAttachable::GetDefaultAttachable() { return sDefaultAttachable; } /*static*/ void LAttachable::SetDefaultAttachable(LAttachable* attachable) { sDefaultAttachable = attachable; }