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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2