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 17 ValueNodeContainer::ValueNodeContainer() 18 : 19 fLock("value node container"), 20 fChildren(20, false), 21 fListeners(20, false) 22 { 23 } 24 25 26 ValueNodeContainer::~ValueNodeContainer() 27 { 28 RemoveAllChildren(); 29 fListeners.MakeEmpty(); 30 } 31 32 33 status_t 34 ValueNodeContainer::Init() 35 { 36 return fLock.InitCheck(); 37 } 38 39 40 int32 41 ValueNodeContainer::CountChildren() const 42 { 43 return fChildren.CountItems(); 44 } 45 46 47 ValueNodeChild* 48 ValueNodeContainer::ChildAt(int32 index) const 49 { 50 return fChildren.ItemAt(index); 51 } 52 53 54 bool 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 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 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 95 ValueNodeContainer::AddListener(Listener* listener) 96 { 97 return fListeners.AddItem(listener); 98 } 99 100 101 void 102 ValueNodeContainer::RemoveListener(Listener* listener) 103 { 104 fListeners.RemoveItem(listener); 105 } 106 107 108 void 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 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 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 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 144 ValueNodeContainer::Listener::~Listener() 145 { 146 } 147 148 149 void 150 ValueNodeContainer::Listener::ValueNodeChanged(ValueNodeChild* nodeChild, 151 ValueNode* oldNode, ValueNode* newNode) 152 { 153 } 154 155 156 void 157 ValueNodeContainer::Listener::ValueNodeChildrenCreated(ValueNode* node) 158 { 159 } 160 161 162 void 163 ValueNodeContainer::Listener::ValueNodeChildrenDeleted(ValueNode* node) 164 { 165 } 166 167 168 void 169 ValueNodeContainer::Listener::ValueNodeValueChanged(ValueNode* node) 170 { 171 } 172