dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / UAS / Base / UAS_PtrList.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 // $XConsortium: UAS_PtrList.cc /main/5 1996/08/06 09:23:58 rcs $
24
25 #include "Managers/CatMgr.hh"
26 #include "Registration.hh"
27
28 template <class T>
29 UAS_PtrList<T>::UAS_PtrList() {
30     fItems = 0;
31     fNumItems = 0;
32     fListSize = 0;
33 }
34
35 template <class T>
36 UAS_PtrList<T>::UAS_PtrList (const UAS_PtrList<T> &l) {
37     fItems = 0;
38     fNumItems = l.fNumItems;
39     fListSize = l.fListSize;
40     if (fListSize > 0) {
41         fItems = new Tptr[fListSize];
42         for (int i = 0; i < fNumItems; i ++)
43             fItems[i] = l.fItems[i];
44     }
45 }
46
47 template <class T>
48 UAS_PtrList<T>::~UAS_PtrList () {
49     delete [] fItems;
50 }
51
52 template <class T>
53 UAS_PtrList<T> &
54 UAS_PtrList<T>::operator = (const UAS_PtrList<T> &l) {
55     if (this == &l)
56         return *this;
57     delete [] fItems;
58     fItems = 0;
59     fListSize = l.fListSize;
60     fNumItems = l.fNumItems;
61     if (fListSize > 0) {
62         fItems = new Tptr[fListSize];
63         for (int i = 0; i < fNumItems; i ++)
64             fItems[i] = l.fItems[i];
65     }
66     return *this;
67 }
68
69 template <class T>
70 T *
71 UAS_PtrList<T>::operator [] (int i) const {
72     return item(i);
73 }
74
75 template <class T>
76 T *
77 UAS_PtrList<T>::item (int i) const {
78     if (i < 0 || i >= fNumItems)
79         throw (UAS_Exception (
80             (char*)UAS_String(CATGETS(Set_UAS_Base, 4, "File a Bug"))));
81     return fItems[i];
82 }
83
84 template <class T>
85 int
86 UAS_PtrList<T>::numItems () const {
87     return fNumItems;
88 }
89
90 template <class T>
91 void
92 UAS_PtrList<T>::append (T *newItem) {
93     if (fNumItems == fListSize) {
94         T **newList = new Tptr[fListSize += 4];
95         for (int i = 0; i < fNumItems; i ++)
96             newList[i] = fItems[i];
97         delete [] fItems;
98         fItems = newList;
99     }
100     fItems[fNumItems ++] = newItem;
101 }
102
103 template <class T>
104 void
105 UAS_PtrList<T>::remove (T *oldItem) {
106     int i;
107     for (i = 0; i < fNumItems; i ++)
108         if (fItems[i] == oldItem)
109             break;
110     if (i >= fNumItems)
111         return;
112     for (i ++; i < fNumItems; i ++)
113         fItems[i-1] = fItems[i];
114     fNumItems --;
115 }
116
117 template <class T>
118 void
119 UAS_PtrList<T>::clear () {
120     delete [] fItems;
121     fItems = 0;
122     fNumItems = 0;
123     fListSize = 0;
124 }