STIR 6.4.0
NumericInfo.h
Go to the documentation of this file.
1#ifndef __NUMERICINFO_H_
2#define __NUMERICINFO_H_
3//
4//
15/*
16 Copyright (C) 2000 PARAPET partners
17 Copyright (C) 2000- 2009, Hammersmith Imanet Ltd
18 This file is part of STIR.
19
20 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
21
22 See STIR/LICENSE.txt for details
23*/
24// TODO some of these members could be made static
25
26/*
27 History :
28 first version by Kris Thielemans
29
30 KT 15/12/99
31 reordered member function declarations to allow easier inlining
32 moved NumericType and ByteOrder to separate files
33
34 KT 18/02/2000 use new syntax for template specialisations: template<> {class blabla};
35 */
36
37#include "stir/NumericType.h"
38
39// this code uses #define's as specified by the ANSI C standard
40#include <climits>
41#include <cfloat>
42#ifndef __MSL__
43# include <sys/types.h>
44#else
45# include <size_t.h>
46#endif
47
48START_NAMESPACE_STIR
49
66template <class NUMBER>
68{
69public:
70 inline bool signed_type() const;
71
72 inline bool integer_type() const;
74 inline size_t size_in_bytes() const;
75 inline size_t size_in_bits() const;
76
77 inline NUMBER max_value() const;
78 inline NUMBER min_value() const;
79
80 inline NumericType type_id() const;
81};
82
83// Below are the actual details filled in.
84
86template <>
87class NumericInfo<signed char>
88{
89 typedef signed char type;
90
91public:
92 bool signed_type() const { return true; }
93 bool integer_type() const { return true; }
94 size_t size_in_bytes() const { return sizeof(type); }
95 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
96 type max_value() const { return SCHAR_MAX; }
97 type min_value() const { return SCHAR_MIN; }
98 NumericType type_id() const { return NumericType::SCHAR; }
99};
100
102template <>
103class NumericInfo<unsigned char>
104{
105 typedef unsigned char type;
106
107public:
108 bool signed_type() const { return false; }
109 bool integer_type() const { return true; }
110 size_t size_in_bytes() const { return sizeof(type); }
111 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
112 type max_value() const { return UCHAR_MAX; }
113 type min_value() const { return 0; }
114 NumericType type_id() const { return NumericType::UCHAR; }
115};
116
118template <>
119class NumericInfo<signed short>
120{
121 typedef signed short type;
122
123public:
124 bool signed_type() const { return true; }
125 bool integer_type() const { return true; }
126 size_t size_in_bytes() const { return sizeof(type); }
127 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
128 type max_value() const { return SHRT_MAX; }
129 type min_value() const { return SHRT_MIN; }
130 NumericType type_id() const { return NumericType::SHORT; }
131};
132
134template <>
135class NumericInfo<unsigned short>
136{
137 typedef unsigned short type;
138
139public:
140 bool signed_type() const { return false; }
141 bool integer_type() const { return true; }
142 size_t size_in_bytes() const { return sizeof(type); }
143 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
144 type max_value() const { return USHRT_MAX; }
145 type min_value() const { return 0; }
146 NumericType type_id() const { return NumericType::USHORT; }
147};
148
150template <>
151class NumericInfo<signed int>
152{
153 typedef signed int type;
154
155public:
156 bool signed_type() const { return true; }
157 bool integer_type() const { return true; }
158 size_t size_in_bytes() const { return sizeof(type); }
159 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
160 type max_value() const { return INT_MAX; }
161 type min_value() const { return INT_MIN; }
162 NumericType type_id() const { return NumericType::INT; }
163};
164
166template <>
167class NumericInfo<unsigned int>
168{
169 typedef unsigned int type;
170
171public:
172 bool signed_type() const { return false; }
173 bool integer_type() const { return true; }
174 size_t size_in_bytes() const { return sizeof(type); }
175 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
176 type max_value() const { return UINT_MAX; }
177 type min_value() const { return 0; }
178 NumericType type_id() const { return NumericType::UINT; }
179};
180
182template <>
183class NumericInfo<signed long>
184{
185 typedef signed long type;
186
187public:
188 bool signed_type() const { return true; }
189 bool integer_type() const { return true; }
190 size_t size_in_bytes() const { return sizeof(type); }
191 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
192 type max_value() const { return LONG_MAX; }
193 type min_value() const { return LONG_MIN; }
194 NumericType type_id() const { return NumericType::LONG; }
195};
196
198template <>
199class NumericInfo<unsigned long>
200{
201 typedef unsigned long type;
202
203public:
204 bool signed_type() const { return false; }
205 bool integer_type() const { return true; }
206 size_t size_in_bytes() const { return sizeof(type); }
207 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
208 type max_value() const { return ULONG_MAX; }
209 type min_value() const { return 0; }
210 NumericType type_id() const { return NumericType::ULONG; }
211};
212
214template <>
215class NumericInfo<float>
216{
217 typedef float type;
218
219public:
220 bool signed_type() const { return true; }
221 bool integer_type() const { return false; }
222 size_t size_in_bytes() const { return sizeof(type); }
223 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
224 type max_value() const { return FLT_MAX; }
225 type min_value() const { return -FLT_MAX; }
226 NumericType type_id() const { return NumericType::FLOAT; }
227};
228
230template <>
231class NumericInfo<double>
232{
233 typedef double type;
234
235public:
236 bool signed_type() const { return true; }
237 bool integer_type() const { return false; }
238 size_t size_in_bytes() const { return sizeof(type); }
239 size_t size_in_bits() { return CHAR_BIT * size_in_bytes(); }
240 type max_value() const { return DBL_MAX; }
241 type min_value() const { return -DBL_MAX; }
242 NumericType type_id() const { return NumericType::DOUBLE; }
243};
244
245END_NAMESPACE_STIR
246
247#endif
This file declares the stir::NumericType class.
class NumericInfo<NUMBER> defines properties for the type NUMBER.
Definition NumericInfo.h:68
size_t size_in_bytes() const
Size in 'bytes' as reported by sizeof(), so really size_in_sizeof_char.
provides names for some numeric types and methods for finding their properties.
Definition NumericType.h:55