Developer documentation
Version 3.0.3-105-gd3941f44
datatype.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __data_type_h__
18#define __data_type_h__
19
20#include "cmdline_option.h"
21
22#ifdef Complex
23# undef Complex
24#endif
25
26namespace MR
27{
28
30 public:
31 DataType () noexcept : dt (DataType::Native) { }
32 DataType (uint8_t type) noexcept : dt (type) { }
33 DataType (const DataType&) noexcept = default;
34 DataType (DataType&&) noexcept = default;
35 DataType& operator= (const DataType&) noexcept = default;
36 DataType& operator= (DataType&&) noexcept = default;
37
38 bool undefined () const {
39 return dt == Undefined;
40 }
41 uint8_t operator() () const {
42 return dt;
43 }
44 bool operator== (uint8_t type) const {
45 return dt == type;
46 }
47 bool operator!= (uint8_t type) const {
48 return dt != type;
49 }
50 bool operator== (const DataType DT) const {
51 return dt == DT.dt;
52 }
53 bool operator!= (const DataType DT) const {
54 return dt != DT.dt;
55 }
56
57 bool is (uint8_t type) const {
58 return dt == type;
59 }
60 bool is_complex () const {
61 return dt & Complex;
62 }
63 bool is_signed () const {
64 return dt & Signed;
65 }
67 if (bits() <= 8)
68 return true;
70 throw Exception ("byte order not set!");
71#ifdef MRTRIX_BYTE_ORDER_BIG_ENDIAN
72 return is_big_endian();
73#else
74 return is_little_endian();
75#endif
76 }
77 bool is_little_endian () const {
78 return dt & LittleEndian;
79 }
80 bool is_big_endian () const {
81 return dt & BigEndian;
82 }
83 bool is_integer () const {
84 const uint8_t type = dt & Type;
85 return ((type == UInt8) || (type == UInt16) || (type == UInt32) || (type == UInt64));
86 }
87 bool is_floating_point () const {
88 const uint8_t type = dt & Type;
89 return ((type == Float32) || (type == Float64));
90 }
92 if (!is_floating_point()) {
93 dt = Native;
94 }
95 }
97 if (dt != Bit && dt != Int8 && dt != UInt8) {
98 if (!is_little_endian() && !is_big_endian()) {
99#ifdef MRTRIX_BYTE_ORDER_BIG_ENDIAN
100 dt |= BigEndian;
101#else
102 dt |= LittleEndian;
103#endif
104 }
105 }
106 }
107
108 size_t bits () const;
109 size_t bytes () const {
110 return (bits() +7) /8;
111 }
112 const char* description () const;
113 const char* specifier () const;
114
115 void set_flag (uint8_t flag) {
116 dt |= flag;
117 }
118 void unset_flag (uint8_t flag) {
119 dt &= ~flag;
120 }
121
122 template <typename T>
123 static DataType from () { return DataType::Undefined; }
124
126 dt.set_byte_order_native();
127 return dt;
128 }
129 static DataType parse (const std::string& spec);
130 static DataType from_command_line (DataType default_datatype = Undefined);
131
132
133 static constexpr uint8_t Attributes = 0xF0U;
134 static constexpr uint8_t Type = 0x0FU;
135
136 static constexpr uint8_t Complex = 0x10U;
137 static constexpr uint8_t Signed = 0x20U;
138 static constexpr uint8_t LittleEndian = 0x40U;
139 static constexpr uint8_t BigEndian = 0x80U;
140
141 static constexpr uint8_t Undefined = 0x00U;
142 static constexpr uint8_t Bit = 0x01U;
143 static constexpr uint8_t UInt8 = 0x02U;
144 static constexpr uint8_t UInt16 = 0x03U;
145 static constexpr uint8_t UInt32 = 0x04U;
146 static constexpr uint8_t UInt64 = 0x05U;
147 static constexpr uint8_t Float32 = 0x06U;
148 static constexpr uint8_t Float64 = 0x07U;
149
150
151 static constexpr uint8_t Int8 = UInt8 | Signed;
152 static constexpr uint8_t Int16 = UInt16 | Signed;
153 static constexpr uint8_t Int16LE = UInt16 | Signed | LittleEndian;
154 static constexpr uint8_t UInt16LE = UInt16 | LittleEndian;
155 static constexpr uint8_t Int16BE = UInt16 | Signed | BigEndian;
156 static constexpr uint8_t UInt16BE = UInt16 | BigEndian;
157 static constexpr uint8_t Int32 = UInt32 | Signed;
158 static constexpr uint8_t Int32LE = UInt32 | Signed | LittleEndian;
159 static constexpr uint8_t UInt32LE = UInt32 | LittleEndian;
160 static constexpr uint8_t Int32BE = UInt32 | Signed | BigEndian;
161 static constexpr uint8_t UInt32BE = UInt32 | BigEndian;
162 static constexpr uint8_t Int64 = UInt64 | Signed;
163 static constexpr uint8_t Int64LE = UInt64 | Signed | LittleEndian;
164 static constexpr uint8_t UInt64LE = UInt64 | LittleEndian;
165 static constexpr uint8_t Int64BE = UInt64 | Signed | BigEndian;
166 static constexpr uint8_t UInt64BE = UInt64 | BigEndian;
167 static constexpr uint8_t Float32LE = Float32 | LittleEndian;
168 static constexpr uint8_t Float32BE = Float32 | BigEndian;
169 static constexpr uint8_t Float64LE = Float64 | LittleEndian;
170 static constexpr uint8_t Float64BE = Float64 | BigEndian;
171 static constexpr uint8_t CFloat32 = Complex | Float32;
172 static constexpr uint8_t CFloat32LE = Complex | Float32 | LittleEndian;
173 static constexpr uint8_t CFloat32BE = Complex | Float32 | BigEndian;
174 static constexpr uint8_t CFloat64 = Complex | Float64;
175 static constexpr uint8_t CFloat64LE = Complex | Float64 | LittleEndian;
176 static constexpr uint8_t CFloat64BE = Complex | Float64 | BigEndian;
177
178 static constexpr uint8_t Native = Float32 |
179#ifdef MRTRIX_BYTE_ORDER_BIG_ENDIAN
180 BigEndian;
181#else
183#endif
185
186 static const char* identifiers[];
187
188 friend std::ostream& operator<< (std::ostream& stream, const DataType& dt) {
189 stream << dt.specifier();
190 return stream;
191 }
192
193 protected:
194 uint8_t dt;
195
196 };
197
198
199
200 template <> inline DataType DataType::from<bool> ()
201 {
202 return DataType::Bit;
203 }
204 template <> inline DataType DataType::from<int8_t> ()
205 {
206 return DataType::Int8;
207 }
208 template <> inline DataType DataType::from<uint8_t> ()
209 {
210 return DataType::UInt8;
211 }
212 template <> inline DataType DataType::from<int16_t> ()
213 {
215 }
216 template <> inline DataType DataType::from<uint16_t> ()
217 {
219 }
220 template <> inline DataType DataType::from<int32_t> ()
221 {
223 }
224 template <> inline DataType DataType::from<uint32_t> ()
225 {
227 }
228 template <> inline DataType DataType::from<int64_t> ()
229 {
231 }
232 template <> inline DataType DataType::from<uint64_t> ()
233 {
235 }
236 template <> inline DataType DataType::from<float> ()
237 {
239 }
240 template <> inline DataType DataType::from<double> ()
241 {
243 }
244 template <> inline DataType DataType::from<cfloat> ()
245 {
247 }
248 template <> inline DataType DataType::from<cdouble> ()
249 {
251 }
252
253}
254
255#endif
256
a class to hold a named list of Option's
static constexpr uint8_t Int16LE
Definition: datatype.h:153
size_t bytes() const
Definition: datatype.h:109
bool is_floating_point() const
Definition: datatype.h:87
static constexpr uint8_t Float32
Definition: datatype.h:147
static constexpr uint8_t UInt64LE
Definition: datatype.h:164
static DataType from()
Definition: datatype.h:123
static constexpr uint8_t UInt32BE
Definition: datatype.h:161
static constexpr uint8_t Float64LE
Definition: datatype.h:169
static constexpr uint8_t CFloat32BE
Definition: datatype.h:173
DataType(uint8_t type) noexcept
Definition: datatype.h:32
friend std::ostream & operator<<(std::ostream &stream, const DataType &dt)
Definition: datatype.h:188
static constexpr uint8_t LittleEndian
Definition: datatype.h:138
static constexpr uint8_t Int16
Definition: datatype.h:152
bool is_byte_order_native()
Definition: datatype.h:66
static constexpr uint8_t Int16BE
Definition: datatype.h:155
const char * description() const
bool operator!=(uint8_t type) const
Definition: datatype.h:47
bool is_integer() const
Definition: datatype.h:83
static constexpr uint8_t CFloat64LE
Definition: datatype.h:175
static constexpr uint8_t Int32
Definition: datatype.h:157
static constexpr uint8_t Float32BE
Definition: datatype.h:168
static constexpr uint8_t BigEndian
Definition: datatype.h:139
static DataType parse(const std::string &spec)
static constexpr uint8_t CFloat32LE
Definition: datatype.h:172
static DataType native(DataType dt)
Definition: datatype.h:125
static constexpr uint8_t UInt16BE
Definition: datatype.h:156
bool operator==(uint8_t type) const
Definition: datatype.h:44
bool is_little_endian() const
Definition: datatype.h:77
static constexpr uint8_t UInt16LE
Definition: datatype.h:154
static constexpr uint8_t UInt64BE
Definition: datatype.h:166
static constexpr uint8_t Float64
Definition: datatype.h:148
static constexpr uint8_t UInt32
Definition: datatype.h:145
static constexpr uint8_t Native
Definition: datatype.h:178
static constexpr uint8_t Int64
Definition: datatype.h:162
static constexpr uint8_t UInt16
Definition: datatype.h:144
static constexpr uint8_t Complex
Definition: datatype.h:136
size_t bits() const
static constexpr uint8_t Int8
Definition: datatype.h:151
static constexpr uint8_t Signed
Definition: datatype.h:137
void set_floating_point()
Definition: datatype.h:91
static constexpr uint8_t Int64BE
Definition: datatype.h:165
void set_flag(uint8_t flag)
Definition: datatype.h:115
static constexpr uint8_t Int32BE
Definition: datatype.h:160
static DataType from_command_line(DataType default_datatype=Undefined)
bool is_complex() const
Definition: datatype.h:60
static constexpr uint8_t CFloat64
Definition: datatype.h:174
static App::OptionGroup options()
DataType(const DataType &) noexcept=default
static constexpr uint8_t Int64LE
Definition: datatype.h:163
bool is_big_endian() const
Definition: datatype.h:80
bool is_signed() const
Definition: datatype.h:63
static constexpr uint8_t Float32LE
Definition: datatype.h:167
static constexpr uint8_t Undefined
Definition: datatype.h:141
const char * specifier() const
uint8_t dt
Definition: datatype.h:194
void set_byte_order_native()
Definition: datatype.h:96
static constexpr uint8_t UInt32LE
Definition: datatype.h:159
static constexpr uint8_t CFloat32
Definition: datatype.h:171
static const char * identifiers[]
Definition: datatype.h:186
void unset_flag(uint8_t flag)
Definition: datatype.h:118
static constexpr uint8_t CFloat64BE
Definition: datatype.h:176
static constexpr uint8_t UInt64
Definition: datatype.h:146
bool is(uint8_t type) const
Definition: datatype.h:57
static constexpr uint8_t Int32LE
Definition: datatype.h:158
static constexpr uint8_t Float64BE
Definition: datatype.h:170
DataType() noexcept
Definition: datatype.h:31
bool undefined() const
Definition: datatype.h:38
uint8_t operator()() const
Definition: datatype.h:41
static constexpr uint8_t Type
Definition: datatype.h:134
DataType(DataType &&) noexcept=default
static constexpr uint8_t UInt8
Definition: datatype.h:143
static constexpr uint8_t Bit
Definition: datatype.h:142
static constexpr uint8_t Attributes
Definition: datatype.h:133
#define NOMEMALIGN
Definition: memory.h:22
Definition: base.h:24