Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / dti_cc / CC_Slist.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $TOG: CC_Slist.C /main/5 1998/04/17 11:44:28 mgreess $
24 #ifndef _CC_Slist_cc
25 #define _CC_Slist_cc
26
27 #include "cc_exceptions.h"
28 #include "CC_Slist.h"
29
30 //-----------------------------------------------------------------------
31 template<class T>
32 CC_TPtrSlist<T>::CC_TPtrSlist(const CC_TPtrSlist<T>&slist) 
33 {
34
35   destructed = FALSE;
36   CC_TPtrSlistIterator<T> slist_iter( *(CC_TPtrSlist<T> *)&slist );
37   
38   while ( slist_iter() ) {
39     insert( slist_iter.key() );
40   }
41   
42 }
43
44 //------------------------------------------------------------------
45 template<class T>
46 T *CC_TPtrSlist<T>::removeAt(size_t pos) {
47
48   CC_TPtrSlistIterator<T> iter( *this );
49   for( size_t i = 0; i <= pos; i++ ) {
50     if ( !(++iter) ) {
51       throw(CASTCCBEXCEPT ccBoundaryException(0,0,i));
52     }
53   }
54
55   T *key_val = iter.key();
56   CC_Link<T> *elem = (CC_Link<T> *)CC_Listbase::remove( iter );
57   delete elem;
58
59   return key_val;
60
61 }
62
63 //------------------------------------------------------------------
64 template<class T>
65 T *CC_TPtrSlist<T>::find(const T* elem) const
66 {
67   CC_TPtrSlistIterator<T> iter( *(CC_TPtrSlist<T> *)this );
68
69   while ( iter() ) {
70     if ( *(iter.key()) == *elem ) 
71       return (iter.key());
72   }
73
74   return  ( NULL );
75 }
76
77 //------------------------------------------------------------------
78 template<class T>
79 T *CC_TPtrSlist<T>::find(CC_Boolean (*testFunc)(T*, void *),
80                          void *d) const
81 {
82   CC_TPtrSlistIterator<T> iter( *(CC_TPtrSlist<T> *)this );
83   while ( iter() ) {
84     if ( testFunc( iter.key(), d ) ) {
85       return ( iter.key() );
86     }
87   }
88
89   return ( NULL );
90 }
91  
92 //------------------------------------------------------------------  
93 template<class T>
94 CC_Boolean CC_TPtrSlist<T>::contains(const T *elem) const
95 {
96   CC_TPtrSlistIterator<T> iter( *(CC_TPtrSlist<T> *)this );
97   while (iter()) {
98     if ( *(iter.key()) == *elem )
99       return (TRUE);
100   }
101
102   return (FALSE);
103 }
104
105 //------------------------------------------------------------------  
106 template<class T>
107 T *CC_TPtrSlist<T>::remove(const T *elem)
108 {
109   CC_TPtrSlistIterator<T> iter( *this );
110   while (iter()) {
111     if ( *(iter.key()) == *elem ) {
112       CC_Link<T> *key_rec = (CC_Link<T> *)CC_Listbase::remove( iter );
113       T *ret = key_rec->f_element;
114       delete key_rec; /* since key_rec does not allocate any memory for
115                        * f_element, it is ok to delete key_rec
116                        */
117
118       return(ret);
119     }
120   }
121
122   return(NULL);
123 }
124
125 //---------------------------------------------------------------------
126 template <class T>
127 CC_TPtrSlist<T>::~CC_TPtrSlist()
128 {
129   if ( !destructed ) {
130     CC_TPtrSlistIterator<T> iter(*this);
131     if ( ++iter ) {
132       while (1) {
133         CC_Link<T> *elem = (CC_Link<T> *)CC_Listbase::remove( (CC_List_Iterator_base &)iter );
134         if ( elem ) {
135           delete elem;
136         }
137         else { break; }
138       }
139     }
140   }
141 }
142
143 //---------------------------------------------------------------------    
144 template <class T>
145 void CC_TPtrSlist<T>::clearAndDestroy()
146 {
147     destructed = TRUE;
148     CC_TPtrSlistIterator<T> iter(*this);
149     if ( ++iter ) {
150         while (1) {
151             CC_Link<T> *elem = (CC_Link<T> *)CC_Listbase::remove( (CC_List_Iterator_base &)iter );
152             if ( elem ) { 
153                 T *temp_elem = elem->f_element;
154                 delete temp_elem;
155                 elem->f_element = NULL; // prevent further destruction on the pointer
156                 delete elem; 
157             }
158             else { break; }
159         }
160     }
161 }
162
163 //---------------------------------------------------------------------
164 template<class T>
165 void CC_TPtrSlist<T>::clear()
166 {
167   CC_TPtrSlistIterator<T> iter(*this);
168   if ( ++iter ) {
169     while (1) {
170       CC_Link<T> *elem = (CC_Link<T> *)CC_Listbase::remove( (CC_List_Iterator_base &)iter );
171       if ( elem ) { 
172         elem->f_element = 0;
173         delete elem;
174       }
175       else { break; }
176     }
177   }
178 }
179
180 //---------------------------------------------------------------------
181 template<class T>
182 CC_TValSlist<T>::CC_TValSlist(const CC_TValSlist<T> &sval_list) 
183 {
184   CC_TValSlistIterator<T> slist_val_iter( *(CC_TValSlist<T> *)&sval_list );
185   while ( ++slist_val_iter ) {
186     append( slist_val_iter.key() );
187   }
188 }
189
190 //---------------------------------------------------------------------
191 template <class T>
192 CC_TValSlist<T>::~CC_TValSlist()
193 {
194     CC_TValSlistIterator<T> iter( *this );
195     if ( ++iter ) {
196         while (1) {
197             CC_Link<T> *elem = (CC_Link<T> *)CC_Listbase::remove( (CC_List_Iterator_base &)iter );
198             if ( elem ) {
199                 if ( elem->f_element ) { 
200                     T *temp_elem = elem->f_element;
201                     delete temp_elem;
202                 }
203                 delete elem;
204             }
205             else { 
206                 break; 
207             }
208         }
209     }
210 }
211   
212 //---------------------------------------------------------------------
213 template <class T>
214 T CC_TValSlistIterator<T>::key() const
215 {
216   CC_Link<T> *link_item = (CC_Link<T> *) CC_List_Iterator_base::item();
217   if ( link_item ) {
218     return ( *(link_item->f_element) );
219   }
220   else {
221     throw (CASTCCEXCEPT  ccException() );
222   }
223 }
224
225 #endif /* _CC_Slist_cc */
226 /* DO NOT ADD ANY LINES AFTER THIS #endif */