2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "ValueNodeContainer.h"
9 #include <AutoLocker.h>
11 #include "ValueNode.h"
14 // #pragma mark - ValueNodeContainer
17 ValueNodeContainer::ValueNodeContainer()
19 fLock("value node container")
24 ValueNodeContainer::~ValueNodeContainer()
31 ValueNodeContainer::Init()
33 return fLock
.InitCheck();
38 ValueNodeContainer::CountChildren() const
40 return fChildren
.CountItems();
45 ValueNodeContainer::ChildAt(int32 index
) const
47 return fChildren
.ItemAt(index
);
52 ValueNodeContainer::AddChild(ValueNodeChild
* child
)
54 AutoLocker
<ValueNodeContainer
> locker(this);
56 if (!fChildren
.AddItem(child
))
59 child
->AcquireReference();
60 child
->SetContainer(this);
67 ValueNodeContainer::RemoveChild(ValueNodeChild
* child
)
69 if (child
->Container() != this || !fChildren
.RemoveItem(child
))
72 child
->SetContainer(NULL
);
73 child
->ReleaseReference();
78 ValueNodeContainer::RemoveAllChildren()
80 for (int32 i
= 0; ValueNodeChild
* child
= ChildAt(i
); i
++) {
81 child
->SetContainer(NULL
);
82 child
->ReleaseReference();
85 fChildren
.MakeEmpty();
90 ValueNodeContainer::AddListener(Listener
* listener
)
92 return fListeners
.AddItem(listener
);
97 ValueNodeContainer::RemoveListener(Listener
* listener
)
99 fListeners
.RemoveItem(listener
);
104 ValueNodeContainer::NotifyValueNodeChanged(ValueNodeChild
* nodeChild
,
105 ValueNode
* oldNode
, ValueNode
* newNode
)
107 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--)
108 fListeners
.ItemAt(i
)->ValueNodeChanged(nodeChild
, oldNode
, newNode
);
113 ValueNodeContainer::NotifyValueNodeChildrenCreated(ValueNode
* node
)
115 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--)
116 fListeners
.ItemAt(i
)->ValueNodeChildrenCreated(node
);
121 ValueNodeContainer::NotifyValueNodeChildrenDeleted(ValueNode
* node
)
123 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--)
124 fListeners
.ItemAt(i
)->ValueNodeChildrenDeleted(node
);
129 ValueNodeContainer::NotifyValueNodeValueChanged(ValueNode
* node
)
131 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--)
132 fListeners
.ItemAt(i
)->ValueNodeValueChanged(node
);
136 // #pragma mark - ValueNodeContainer
139 ValueNodeContainer::Listener::~Listener()
145 ValueNodeContainer::Listener::ValueNodeChanged(ValueNodeChild
* nodeChild
,
146 ValueNode
* oldNode
, ValueNode
* newNode
)
152 ValueNodeContainer::Listener::ValueNodeChildrenCreated(ValueNode
* node
)
158 ValueNodeContainer::Listener::ValueNodeChildrenDeleted(ValueNode
* node
)
164 ValueNodeContainer::Listener::ValueNodeValueChanged(ValueNode
* node
)