Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / lib / tttk / tttk2free.h
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.h /main/3 1995/10/23 10:33:17 rswiston $                                                   */
28 /*
29  * @(#)tttk2free.h      1.3 93/09/07
30  *
31  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
32  */
33 #ifndef tttk2free_h
34 #define tttk2free_h
35
36 #include "api/c/tt_c.h"
37 #include "util/tt_new.h"
38 #include "tttk/tttk.h"
39
40 //
41 // An instance of this class will automatically destroy() a message or
42 // tt_free() a pointer when the instance goes out of scope.  This lets
43 // you avoid having to repetitively code the destroy() or free() right
44 // before every possible return from the scope. Typical usage:
45 //
46 //      TtDtItem2Free item;
47 //      Tt_message msg = tt_message_create();
48 //      item = msg;
49 //      ...
50 //      if (error) return error;        // msg is automatically destroyed here
51 //      ...
52 //      item = 0;                       // now, msg won't be destroyed on return
53 //      return TT_OK;
54 //
55 // This class actually uses ttDtDestroy() instead of tt_message_destroy().
56 //
57 class _TttkItem2Free : public _Tt_allocated {
58     public:
59                                 _TttkItem2Free();
60                                 _TttkItem2Free(
61                                         Tt_message msg
62                                 );
63                                 _TttkItem2Free(
64                                         Tt_pattern pat
65                                 );
66                                 _TttkItem2Free(
67                                         caddr_t    ptr
68                                 );
69                                 ~_TttkItem2Free();
70
71         Tt_message              operator =(
72                                         Tt_message msg
73                                 );
74         Tt_pattern              operator =(
75                                         Tt_pattern pat
76                                 );
77         caddr_t                 operator =(
78                                         caddr_t    ptr
79                                 );
80     private:
81         enum {
82                 NoItem,
83                 Message,
84                 Pattern,
85                 Pointer
86         }                       _type;
87         union {
88                 Tt_message      _msg;
89                 Tt_pattern      _pat;
90                 caddr_t         _ptr;
91         };
92 };
93
94 //
95 // This class is an array of _TttkItem2Free's.  Its main benefit is
96 // that the ::flush() method lets you defuse all the items at once
97 // (typically, right before returning successfully from a scope).
98 //
99 // A malloc() is only done at constructor-time, so that you need not
100 // check if the +=() method failed due to malloc() failure.  Thus,
101 // you have to know the max size of your list at constructor-time.
102 // Overflow items are ignored, and are thus potential memory leaks.
103 //
104 class _TttkList2Free : public _Tt_allocated {
105     public:
106                         _TttkList2Free(
107                                 unsigned int maxElems
108                         );
109                         ~_TttkList2Free();
110
111         Tt_message      operator +=(
112                                 Tt_message   msg2Destroy
113                         );
114         Tt_pattern      operator +=(
115                                 Tt_pattern   pat2Destroy
116                         );
117         caddr_t         operator +=(
118                                 caddr_t      ptr2tt_free
119                         );
120         void            flush();
121     private:
122         void            _destruct();
123         _TttkItem2Free  &_item(
124                                 int          i
125                         );
126
127         unsigned int    _num;
128         unsigned int    _max;
129 #ifdef OPT_VECNEW
130         _TttkItem2Free  *_items;
131 #else
132         _TttkItem2Free **_items;
133 #endif
134 };
135
136 #endif