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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2