From 37e364b2c6cc7487a1c888d256a73e5337bb7189 Mon Sep 17 00:00:00 2001 From: Ash Wolf Date: Wed, 14 Jun 2023 00:50:34 +0100 Subject: initial commit --- src/T2DLL/LAttachable.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/T2DLL/LAttachable.cpp (limited to 'src/T2DLL/LAttachable.cpp') diff --git a/src/T2DLL/LAttachable.cpp b/src/T2DLL/LAttachable.cpp new file mode 100644 index 0000000..9700c4d --- /dev/null +++ b/src/T2DLL/LAttachable.cpp @@ -0,0 +1,86 @@ +#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 *attachment; + + while (iter.Next(&attachment)) { + if (attachment->GetOwnerHost() == this) { + delete attachment; + iter.Reset(); + } + } + + delete mAttachments; + mAttachments = NULL; + } +} + +/*virtual*/ BOOL LAttachable::ExecuteAttachments(unsigned int message, void* data) { + BOOL result = true; + if (mAttachments) { + LArrayIterator iter(*mAttachments); + LAttachment *attachment; + + while (iter.Next(&attachment)) { + result = result & attachment->Execute(message, data); + } + } + return result; +} + +/*static*/ LAttachable* LAttachable::sDefaultAttachable; + +/*static*/ LAttachable* LAttachable::GetDefaultAttachable() { + return sDefaultAttachable; +} + +/*static*/ void LAttachable::SetDefaultAttachable(LAttachable* attachable) { + sDefaultAttachable = attachable; +} -- cgit v1.2.3