(file) Return to List.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Listener

  1 tony  1.1 //%/////////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
  4           // The Open Group, Tivoli Systems
  5           //
  6           // Permission is hereby granted, free of charge, to any person obtaining a copy
  7           // of this software and associated documentation files (the "Software"), to
  8           // deal in the Software without restriction, including without limitation the
  9           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10           // sell copies of the Software, and to permit persons to whom the Software is
 11           // furnished to do so, subject to the following conditions:
 12           // 
 13           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21           //
 22 tony  1.1 //==============================================================================
 23           //
 24           // Author: Dong Xiang, EMC Corporation (xiang_dong@emc.com)
 25           //
 26           // Modified By:
 27           //
 28           //%/////////////////////////////////////////////////////////////////////////////
 29           
 30           #include "List.h"
 31           
 32           #include <Pegasus/Common/Exception.h>
 33           
 34           
 35           PEGASUS_NAMESPACE_BEGIN
 36           
 37           /////////////////////////////////////////////////////////////////////////////
 38           // ListNode
 39           /////////////////////////////////////////////////////////////////////////////
 40           class ListNode
 41           {
 42           public:
 43 tony  1.1 	ListNode(void* element);
 44           	~ListNode();
 45           
 46           	ListNode* getNext() const;
 47           	void setNext(ListNode* next);
 48           
 49           	ListNode* getPrevious() const;
 50           	void setPrevious(ListNode* previous);
 51           
 52           	void* getElement() const;
 53           
 54           private:
 55           	void*			_element;
 56           	ListNode*	_next;
 57           	ListNode* _previous;
 58           };
 59           
 60           ListNode::ListNode(void* element)
 61           :_element(element),_next(NULL),_previous(NULL)
 62           {
 63           }
 64 tony  1.1 ListNode::~ListNode()
 65           {
 66           }
 67           
 68           ListNode* ListNode::getNext() const
 69           {
 70           	return _next;
 71           }
 72           void ListNode::setNext(ListNode* next)
 73           {
 74           	_next = next;
 75           }
 76           
 77           ListNode* ListNode::getPrevious() const
 78           {
 79           	return _previous;
 80           }
 81           void ListNode::setPrevious(ListNode* previous)
 82           {
 83           	_previous = previous;
 84           }
 85 tony  1.1 
 86           void* ListNode::getElement() const
 87           {
 88           	return _element;
 89           }
 90           
 91           /////////////////////////////////////////////////////////////////////////////
 92           // PtrListIterator
 93           /////////////////////////////////////////////////////////////////////////////
 94           class PtrListIterator : public Iterator
 95           {
 96           public:
 97           	PtrListIterator(ListNode* first);
 98             ~PtrListIterator();
 99           
100           	Boolean hasNext();
101           	void*		next();
102           	void		remove();
103           
104           private:
105           	ListNode* _lead;
106 tony  1.1 };
107           
108           PtrListIterator::PtrListIterator(ListNode* lead)
109           :_lead(lead)
110           {
111           }
112           PtrListIterator::~PtrListIterator()
113           {
114           }
115           Boolean PtrListIterator::hasNext()
116           {
117           	return (_lead!=NULL)?true:false;
118           }
119           void* PtrListIterator::next()
120           {
121           	if(_lead==NULL)
122           		throw IndexOutOfBoundsException();
123           
124           	void* element = _lead->getElement();
125           	_lead = _lead->getNext();
126           
127 tony  1.1 	return element;
128           }
129           void PtrListIterator::remove()
130           {
131           	throw Exception("Not Supported feature");
132           }
133           
134           
135           /////////////////////////////////////////////////////////////////////////////
136           // PtrListRep
137           /////////////////////////////////////////////////////////////////////////////
138           class PtrListRep
139           {
140           public:
141           	PtrListRep();
142             ~PtrListRep();
143           	
144           	void add(void* element);
145           	void remove(void* element);
146           
147           	Iterator* iterator();
148 tony  1.1 
149           private:
150           	ListNode* _first;
151           	ListNode* _last;
152           };
153           
154           PtrListRep::PtrListRep()
155           :_first(NULL),_last(NULL)
156           {
157           }
158           PtrListRep::~PtrListRep()
159           {
160           	// reemove all the nodes
161           	while(_last!=NULL)
162           	{
163           		ListNode* n = _last->getPrevious();
164           		delete _last;
165           		_last = n;
166           	}
167           }
168           void PtrListRep::add(void* element)
169 tony  1.1 {
170           	ListNode* n = new ListNode(element);
171           	
172           	if(_last==NULL)
173           	{ // this is the very first one
174           		_first = _last = n;
175           	}
176           	else { // append to the end
177           		_last->setNext(n);
178           		n->setPrevious(_last);
179           		_last = n;
180           	}
181           }
182           void PtrListRep::remove(void* element)
183           {
184           	if(element!=NULL && _first!=NULL)
185           	{
186           		for(ListNode* n=_first; n!=NULL; n=n->getNext())
187           		{
188           			void* el = n->getElement();
189           			if(el==element)
190 tony  1.1 			{ // remove the node
191           				ListNode* prev = n->getPrevious();
192           				ListNode* next = n->getNext();
193           
194           				if(prev!=NULL)
195           					prev->setNext(next);
196           				else // the node is the very first
197           					_first = next;
198           
199           				if(next!=NULL)
200           					next->setPrevious(prev);
201           				else // the node is the last
202           					_last = prev;
203           
204           				delete n;
205           				break;
206           			}
207           		}
208           	}
209           }
210           Iterator* PtrListRep::iterator()
211 tony  1.1 {
212           	return new PtrListIterator(_first);
213           }
214           
215           /////////////////////////////////////////////////////////////////////////////
216           // List
217           /////////////////////////////////////////////////////////////////////////////
218           PtrList::PtrList()
219           :_rep(new PtrListRep())
220           {
221           }
222           PtrList::~PtrList()
223           {
224           	if(_rep!=NULL)
225           		delete _rep;
226           	_rep=NULL;
227           }
228           	
229           void PtrList::add(void* element)
230           {
231           	static_cast<PtrListRep*>(_rep)->add(element);
232 tony  1.1 }
233           
234           void PtrList::remove(void* element)
235           {
236           	static_cast<PtrListRep*>(_rep)->remove(element);
237           }
238           
239           Iterator* PtrList::iterator()
240           {
241           	return static_cast<PtrListRep*>(_rep)->iterator();
242           }
243           
244           
245           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2