coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
integer.h
Go to the documentation of this file.
1 #ifndef INTEGER_H
2 #define INTEGER_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 
40 #include <cstddef>
41 
46 enum integer_type {
51  INT_INT = (3 << 16) | (sizeof(int) << 8) | 1,
53  INT_UINT = (3 << 16) | (sizeof(int) << 8) | 2,
55  INT_LONG = (4 << 16) | (sizeof(long) << 8) | 1,
57  INT_ULONG = (4 << 16) | (sizeof(long) << 8) | 2,
59  INT_LLONG = (5 << 16) | (sizeof(long long) << 8) | 1,
61  INT_ULLONG = (5 << 16) | (sizeof(long long) << 8) | 2,
62 };
63 
65 struct integer {
66 
67  friend struct integer_constant;
68 
73  explicit integer(integer_type type = INT_INT, unsigned long long val = 0)
74  : _val(val),_type(type) {}
75 
77  integer_type type() const {
78  return _type;
79  }
81  bool good() const {
82  return _type != INT_UNDEF;
83  }
85  size_t size() const {
86  return (_type & 0xff00) >> 8;
87  }
89  bool is_signed() const {
90  return _type & 1;
91  }
93  unsigned long long raw() const {
94  return _val;
95  }
96 
98  char const * type_desc() const {
99  return type_desc(_type);
100  }
101 
108  int sign() const;
109 
114  integer operator<(integer const & rhs) const;
115 
120  integer operator<=(integer const & rhs) const;
121 
126  integer operator>(integer const & rhs) const;
127 
132  integer operator>=(integer const & rhs) const;
133 
138  integer operator==(integer const & rhs) const;
139 
144  integer operator!=(integer const & rhs) const;
149  integer operator&&(integer const & rhs) const;
154  integer operator||(integer const & rhs) const;
155 
160  integer operator&(integer const & rhs) const;
161 
166  integer operator|(integer const & rhs) const;
167 
173  integer operator^(integer const & rhs) const;
174 
178  integer operator!() const {
179  return good() ? integer(_type,!_val) : integer(INT_UNDEF);
180  }
181 
185  integer operator~() const;
186 
190  integer operator-() const;
191 
195  integer operator+() const {
196  return good() ? *this : integer(INT_UNDEF);
197  }
198 
203  integer operator+(integer const & rhs) const;
204 
209  integer operator-(integer const & rhs) const;
210 
215  integer operator*(integer const & rhs) const;
216 
221  integer operator/(integer const & rhs) const;
222 
227  integer operator%(integer const & rhs) const;
228 
233  integer operator<<(integer const & rhs) const;
234 
239  integer operator>>(integer const & rhs) const;
240 
241 private:
242 
248  return integer_type((type & ~3) | 2);
249  }
250 
256  return integer_type((type & ~3) | 1);
257  }
258 
263  static unsigned rank_of(integer_type type) {
264  return type >> 16;
265  }
266 
268  unsigned rank() const {
269  return rank_of(_type);
270  }
271 
273  void make_unsigned() {
275  }
276 
278  void make_signed() {
279  _type = to_signed(_type);
280  }
281 
297  static integer_type
298  result_type(integer const & lhs, integer const & rhs);
299 
300 
311  static bool
312  valid_shift(int direction, integer const & lhs, integer const & rhs);
313 
315  static char const * type_desc(integer_type it);
316 
318  unsigned long long _val;
321 };
322 
323 #endif /* EOF*/
static integer_type result_type(integer const &lhs, integer const &rhs)
Definition: integer.cpp:69
unsigned long long raw() const
Get the bits comprising the integer as an unsigned long long
Definition: integer.h:93
void make_unsigned()
Make the integer unsigned.
Definition: integer.h:273
Type long long int
Definition: integer.h:59
integer operator&&(integer const &rhs) const
Definition: integer.cpp:267
size_t size() const
Get the size of the integer
Definition: integer.h:85
integer operator>(integer const &rhs) const
Definition: integer.cpp:175
void make_signed()
Make the integer signed.
Definition: integer.h:278
integer operator<(integer const &rhs) const
Definition: integer.cpp:129
bool good() const
Say whether the integer is of valid type, not INT_UNDEF
Definition: integer.h:81
integer operator*(integer const &rhs) const
Definition: integer.cpp:468
static integer_type to_signed(integer_type type)
Definition: integer.h:255
integer_type _type
The type of the integer.
Definition: integer.h:320
Type unsigned long
Definition: integer.h:57
integer operator^(integer const &rhs) const
Definition: integer.cpp:359
Undetermined type or invalid.
Definition: integer.h:49
Class integer encapsulates an integer of some type.
Definition: integer.h:65
integer operator>=(integer const &rhs) const
Definition: integer.cpp:198
integer operator+() const
Definition: integer.h:195
Type int
Definition: integer.h:51
integer operator-() const
Definition: integer.cpp:382
int sign() const
Definition: integer.cpp:48
static bool valid_shift(int direction, integer const &lhs, integer const &rhs)
Definition: integer.cpp:105
Type long
Definition: integer.h:55
integer(integer_type type=INT_INT, unsigned long long val=0)
Definition: integer.h:73
char const * type_desc() const
Get the text descriptor of this integer's type.
Definition: integer.h:98
static unsigned rank_of(integer_type type)
Definition: integer.h:263
integer_type
Enumerated constants denoting integral types.
Definition: integer.h:47
struct integer_constant encapsulates an integer constant
integer operator/(integer const &rhs) const
Definition: integer.cpp:491
integer operator~() const
Definition: integer.cpp:402
integer operator==(integer const &rhs) const
Definition: integer.cpp:244
unsigned long long _val
The value of the integer
Definition: integer.h:318
integer operator<=(integer const &rhs) const
Definition: integer.cpp:152
unsigned rank() const
Get the conversion rank of an integer
Definition: integer.h:268
Type unsigned long long
Definition: integer.h:61
integer_type type() const
Get the type of the integer
Definition: integer.h:77
integer operator<<(integer const &rhs) const
Definition: integer.cpp:537
integer operator|(integer const &rhs) const
Definition: integer.cpp:336
integer operator&(integer const &rhs) const
Definition: integer.cpp:313
integer operator>>(integer const &rhs) const
Definition: integer.cpp:563
integer operator!=(integer const &rhs) const
Definition: integer.cpp:221
static integer_type to_unsigned(integer_type type)
Definition: integer.h:247
integer operator%(integer const &rhs) const
Definition: integer.cpp:514
Type unsigned
Definition: integer.h:53
integer operator!() const
Definition: integer.h:178
integer operator||(integer const &rhs) const
Definition: integer.cpp:290
bool is_signed() const
Say whether the integer is of signed type.
Definition: integer.h:89