Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / lib / tttk / tttk2free.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 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: tttk2free.C /main/3 1995/10/23 10:33:09 rswiston $                                                  
28 /*
29  * @(#)tttk2free.C      1.3 93/09/07
30  *
31  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
32  */
33 #include "tttk/tttk2free.h"
34
35 _TttkItem2Free::_TttkItem2Free()
36 {
37         _type = NoItem;
38 }
39
40 _TttkItem2Free::_TttkItem2Free(
41         Tt_message msg
42 )
43 {
44         operator=( msg );
45 }
46
47 _TttkItem2Free::_TttkItem2Free(
48         Tt_pattern pat
49 )
50 {
51         operator=( pat );
52 }
53
54 _TttkItem2Free::_TttkItem2Free(
55         caddr_t ptr
56 )
57 {
58         operator=( ptr );
59 }
60
61 _TttkItem2Free::~_TttkItem2Free()
62 {
63         switch (_type) {
64             case Message:
65                 if (_msg != 0) {
66                         tttk_message_destroy( _msg );
67                 }
68                 break;
69             case Pattern:
70                 if (_pat != 0) {
71                         tt_pattern_destroy( _pat );
72                 }
73                 break;
74             case Pointer:
75                 if (_ptr != 0) {
76                         tt_free( _ptr );
77                 }
78                 break;
79             default:
80                 break;
81         }
82 }
83
84 Tt_message
85 _TttkItem2Free::operator =(
86         Tt_message msg
87 )
88 {
89         _type = Message;
90         _msg = msg;
91         return msg;
92 }
93
94 Tt_pattern
95 _TttkItem2Free::operator =(
96         Tt_pattern pat
97 )
98 {
99         _type = Pattern;
100         _pat = pat;
101         return pat;
102 }
103
104 caddr_t
105 _TttkItem2Free::operator =(
106         caddr_t ptr
107 )
108 {
109         _type = Pointer;
110         _ptr = ptr;
111         return ptr;
112 }
113
114 _TttkList2Free::_TttkList2Free(
115         unsigned int maxElems
116 )
117 {
118         _num = 0;
119         _max = maxElems;
120 #ifdef OPT_VECNEW
121         _items = new _TttkItem2Free[ maxElems ];
122         if (_items == 0) {
123                 _max = 0;
124         }
125 #else
126         _items = (_TttkItem2Free **)malloc( maxElems * sizeof(_TttkItem2Free *));
127         if (_items == 0) {
128                 _max = 0;
129         }
130         for (int i = 0; i < _max; i++) {
131                 _items[ i ] = new _TttkItem2Free();
132                 if (_items[ i ] == 0) {
133                         _destruct();
134                         _max = 0;
135                         break;
136                 }
137         }
138 #endif
139 }
140
141 _TttkList2Free::~_TttkList2Free()
142 {
143         _destruct();
144 }
145
146 Tt_message
147 _TttkList2Free::operator +=(
148         Tt_message msg
149 )
150 {
151         if (_num >= _max) {
152                 return msg;
153         }
154         // LIFO
155         _item( _max - _num - 1 ) = msg;
156         _num++;
157         return msg;
158 }
159
160 Tt_pattern
161 _TttkList2Free::operator +=(
162         Tt_pattern pat
163 )
164 {
165         if (_num >= _max) {
166                 return pat;
167         }
168         // LIFO
169         _item( _max - _num - 1 ) = pat;
170         _num++;
171         return pat;
172 }
173
174 caddr_t
175 _TttkList2Free::operator +=(
176         caddr_t ptr
177 )
178 {
179         if (_num >= _max) {
180                 return ptr;
181         }
182         // LIFO
183         _item( _max - _num - 1 ) = ptr;
184         _num++;
185         return ptr;
186 }
187
188 void
189 _TttkList2Free::flush()
190 {
191         for (int i = 0; i < _num; i++) {
192                 _item( i ) = (caddr_t)0;
193         }
194 }
195
196 void
197 _TttkList2Free::_destruct()
198 {
199         if (_items != 0) {
200 #ifdef OPT_VECNEW
201                 delete [] _items;
202 #else
203                 for (int i = 0; i < _max; i++) {
204                         if (_items[ i ] != 0) { 
205                                 delete _items[ i ];
206                                 _items[ i ] = 0;
207                         }
208                 }
209                 free( _items );
210                 _items = 0;
211 #endif
212         }
213 }
214
215 _TttkItem2Free &
216 _TttkList2Free::_item(
217         int i
218 )
219 {
220 #ifdef OPT_VECNEW
221         return _items[ i ];
222 #else
223         return *_items[ i ];
224 #endif
225 }