Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Basic / BitField.hh
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 libraries 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 /*
24  * $TOG: BitField.hh /main/5 1998/04/17 11:35:57 mgreess $
25  *
26  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49 #if defined(SVR4) || defined(hpux) || defined(__linux__) || defined(__NetBSD__)
50 #include <limits.h>
51 #else
52 #include <sys/limits.h>
53 #endif
54
55 const int bytes_per_word = sizeof(unsigned long);
56 const int bits_per_byte  = CHAR_BIT ;
57 const int bits_per_word = bytes_per_word * bits_per_byte ;
58
59 typedef unsigned long BitHandle ;
60
61 class BitField 
62 {
63 public:
64   BitField(unsigned long initial_bits = CHAR_BIT * sizeof(unsigned long) - 1) ; 
65   ~BitField();
66
67   void          return_bit(unsigned long bit); // give back previously
68                                                // allocated bit
69   BitHandle     get_handle();                  // allocate a bit for future use
70   void          free_handle(BitHandle);        // return bit for other use
71
72   /* -------- bit operations -------- */
73   bool  is_set(BitHandle);
74   bool  set(BitHandle);
75   bool  unset(BitHandle);
76
77
78   void          save();
79
80 #ifdef DEBUG
81   bool  is_used(BitHandle);
82   ostream      &dump(ostream &);
83 #endif
84
85 private:
86   /* -------- internal functions that do the real work -------- */
87   void check_allocation (unsigned long word);
88   bool  i_is_set(BitHandle);
89   bool  i_set(BitHandle);
90   bool  i_unset(BitHandle);
91
92
93   /* -------- suplementary (inline) functions -------- */
94   bool  word_has_zero_bit(unsigned long);
95   bool  byte_has_zero_bit(unsigned char byte);
96   unsigned char get_byte(int byte, unsigned long word);
97
98
99 private:
100   unsigned long         f_num_words ; // number of allocated machine words
101   unsigned long        *f_words ;     // array of words for bits
102   unsigned long        *f_bits_in_use ; // maps bits already allocated
103 };
104
105 // the assert(is_used(handle))  is commented out as OutlineLists do not
106 // allocate separate data handles for thier child lists - jbm
107
108 inline bool
109 BitField::is_set(BitHandle handle)
110 {
111   assert(handle);
112 //  assert(is_used(handle));
113   return i_is_set(handle);
114 }
115
116 inline bool
117 BitField::set(BitHandle handle)
118 {
119   assert(handle);
120 //  assert(is_used(handle));
121   return i_set(handle);
122 }
123
124 inline bool
125 BitField::unset(BitHandle handle)
126 {
127   assert(handle);
128 //  assert(is_used(handle));
129   return i_unset(handle);
130 }
131
132
133 inline bool
134 BitField::word_has_zero_bit(unsigned long word)
135 {
136   return !((word & ULONG_MAX) == ULONG_MAX);
137 }
138
139 inline bool
140 BitField::byte_has_zero_bit(unsigned char byte)
141 {
142   return !((byte & UCHAR_MAX) == UCHAR_MAX) ;
143 }
144
145 // return a byte within a word
146
147 inline unsigned char
148 BitField::get_byte(int byte, unsigned long word)
149 {
150   return (word >> (byte * bits_per_byte)) & UCHAR_MAX ;
151 }
152
153 #ifdef DEBUG
154 inline
155 ostream &
156 operator << (ostream &o, BitField &bf)
157 {
158   return bf.dump(o);
159 }
160
161 #endif