summaryrefslogtreecommitdiff
path: root/src/T2DLL/LAttachable.cpp
blob: 7ec8afa106c0869725cdcb6bd8ab4fceb7e15422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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;
}