1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "ValueNodeContainer.h"
8
9 #include <AutoLocker.h>
10
11 #include "ValueNode.h"
12
13
14 // #pragma mark - ValueNodeContainer
15
16
ValueNodeContainer()17 ValueNodeContainer::ValueNodeContainer()
18 :
19 fLock("value node container"),
20 fChildren(20, false),
21 fListeners(20, false)
22 {
23 }
24
25
~ValueNodeContainer()26 ValueNodeContainer::~ValueNodeContainer()
27 {
28 RemoveAllChildren();
29 fListeners.MakeEmpty();
30 }
31
32
33 status_t
Init()34 ValueNodeContainer::Init()
35 {
36 return fLock.InitCheck();
37 }
38
39
40 int32
CountChildren() const41 ValueNodeContainer::CountChildren() const
42 {
43 return fChildren.CountItems();
44 }
45
46
47 ValueNodeChild*
ChildAt(int32 index) const48 ValueNodeContainer::ChildAt(int32 index) const
49 {
50 return fChildren.ItemAt(index);
51 }
52
53
54 bool
AddChild(ValueNodeChild * child)55 ValueNodeContainer::AddChild(ValueNodeChild* child)
56 {
57 AutoLocker<ValueNodeContainer> locker(this);
58
59 if (!fChildren.AddItem(child))
60 return false;
61
62 child->AcquireReference();
63 child->SetContainer(this);
64
65 return true;
66 }
67
68
69 void
RemoveChild(ValueNodeChild * child)70 ValueNodeContainer::RemoveChild(ValueNodeChild* child)
71 {
72 if (child->Container() != this || !fChildren.RemoveItem(child))
73 return;
74
75 child->SetNode(NULL);
76 child->SetContainer(NULL);
77 child->ReleaseReference();
78 }
79
80
81 void
RemoveAllChildren()82 ValueNodeContainer::RemoveAllChildren()
83 {
84 for (int32 i = 0; ValueNodeChild* child = ChildAt(i); i++) {
85 child->SetNode(NULL);
86 child->SetContainer(NULL);
87 child->ReleaseReference();
88 }
89
90 fChildren.MakeEmpty();
91 }
92
93
94 bool
AddListener(Listener * listener)95 ValueNodeContainer::AddListener(Listener* listener)
96 {
97 return fListeners.AddItem(listener);
98 }
99
100
101 void
RemoveListener(Listener * listener)102 ValueNodeContainer::RemoveListener(Listener* listener)
103 {
104 fListeners.RemoveItem(listener);
105 }
106
107
108 void
NotifyValueNodeChanged(ValueNodeChild * nodeChild,ValueNode * oldNode,ValueNode * newNode)109 ValueNodeContainer::NotifyValueNodeChanged(ValueNodeChild* nodeChild,
110 ValueNode* oldNode, ValueNode* newNode)
111 {
112 for (int32 i = fListeners.CountItems() - 1; i >= 0; i--)
113 fListeners.ItemAt(i)->ValueNodeChanged(nodeChild, oldNode, newNode);
114 }
115
116
117 void
NotifyValueNodeChildrenCreated(ValueNode * node)118 ValueNodeContainer::NotifyValueNodeChildrenCreated(ValueNode* node)
119 {
120 for (int32 i = fListeners.CountItems() - 1; i >= 0; i--)
121 fListeners.ItemAt(i)->ValueNodeChildrenCreated(node);
122 }
123
124
125 void
NotifyValueNodeChildrenDeleted(ValueNode * node)126 ValueNodeContainer::NotifyValueNodeChildrenDeleted(ValueNode* node)
127 {
128 for (int32 i = fListeners.CountItems() - 1; i >= 0; i--)
129 fListeners.ItemAt(i)->ValueNodeChildrenDeleted(node);
130 }
131
132
133 void
NotifyValueNodeValueChanged(ValueNode * node)134 ValueNodeContainer::NotifyValueNodeValueChanged(ValueNode* node)
135 {
136 for (int32 i = fListeners.CountItems() - 1; i >= 0; i--)
137 fListeners.ItemAt(i)->ValueNodeValueChanged(node);
138 }
139
140
141 // #pragma mark - ValueNodeContainer
142
143
~Listener()144 ValueNodeContainer::Listener::~Listener()
145 {
146 }
147
148
149 void
ValueNodeChanged(ValueNodeChild * nodeChild,ValueNode * oldNode,ValueNode * newNode)150 ValueNodeContainer::Listener::ValueNodeChanged(ValueNodeChild* nodeChild,
151 ValueNode* oldNode, ValueNode* newNode)
152 {
153 }
154
155
156 void
ValueNodeChildrenCreated(ValueNode * node)157 ValueNodeContainer::Listener::ValueNodeChildrenCreated(ValueNode* node)
158 {
159 }
160
161
162 void
ValueNodeChildrenDeleted(ValueNode * node)163 ValueNodeContainer::Listener::ValueNodeChildrenDeleted(ValueNode* node)
164 {
165 }
166
167
168 void
ValueNodeValueChanged(ValueNode * node)169 ValueNodeContainer::Listener::ValueNodeValueChanged(ValueNode* node)
170 {
171 }
172