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