File: datatype.h

package info (click to toggle)
mrtrix3 3.0~rc3%2Bgit135-g2b8e7d0c2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 34,248 kB
  • sloc: cpp: 117,101; python: 6,472; sh: 638; makefile: 226; xml: 39; ansic: 20
file content (239 lines) | stat: -rw-r--r-- 7,736 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (c) 2008-2018 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/
 *
 * MRtrix3 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * For more details, see http://www.mrtrix.org/
 */


#ifndef __data_type_h__
#define __data_type_h__

#include "cmdline_option.h"

#ifdef Complex
# undef Complex
#endif

namespace MR
{

  class DataType { NOMEMALIGN
    public:
      DataType () noexcept : dt (DataType::Native) { }
      DataType (uint8_t type) noexcept : dt (type) { }
      DataType (const DataType&) noexcept = default;
      DataType (DataType&&) noexcept = default;
      DataType& operator= (const DataType&) noexcept = default;
      DataType& operator= (DataType&&) noexcept = default;

      bool undefined () const {
        return dt == Undefined;
      }
      uint8_t operator() () const {
        return dt;
      }
      bool operator== (uint8_t type) const {
        return dt == type;
      }
      bool operator!= (uint8_t type) const {
        return dt != type;
      }
      bool operator== (const DataType DT) const {
        return dt == DT.dt;
      }
      bool operator!= (const DataType DT) const {
        return dt != DT.dt;
      }

      bool is (uint8_t type) const {
        return dt == type;
      }
      bool is_complex () const {
        return dt & Complex;
      }
      bool is_signed () const {
        return dt & Signed;
      }
      bool is_little_endian () const {
        return dt & LittleEndian;
      }
      bool is_big_endian () const {
        return dt & BigEndian;
      }
      bool is_integer () const {
        const uint8_t type = dt & Type;
        return ((type == UInt8) || (type == UInt16) || (type == UInt32) || (type == UInt64));
      }
      bool is_floating_point () const {
        const uint8_t type = dt & Type;
        return ((type == Float32) || (type == Float64));
      }
      void set_byte_order_native () {
        if (dt != Bit && dt != Int8 && dt != UInt8) {
          if (!is_little_endian() && !is_big_endian()) {
#ifdef MRTRIX_BYTE_ORDER_BIG_ENDIAN
            dt |= BigEndian;
#else
            dt |= LittleEndian;
#endif
          }
        }
      }

      size_t bits () const;
      size_t bytes () const {
        return (bits() +7) /8;
      }
      const char*  description () const;
      const char*  specifier () const;

      void set_flag (uint8_t flag) {
        dt |= flag;
      }
      void unset_flag (uint8_t flag) {
        dt &= ~flag;
      }

      template <typename T>
        static DataType from () { return DataType::Undefined; }

      static DataType native (DataType dt) {
        dt.set_byte_order_native();
        return dt;
      }
      static DataType parse (const std::string& spec);
      static DataType from_command_line (DataType default_datatype = Undefined);


      static constexpr uint8_t     Attributes    = 0xF0U;
      static constexpr uint8_t     Type          = 0x0FU;

      static constexpr uint8_t     Complex       = 0x10U;
      static constexpr uint8_t     Signed        = 0x20U;
      static constexpr uint8_t     LittleEndian  = 0x40U;
      static constexpr uint8_t     BigEndian     = 0x80U;

      static constexpr uint8_t     Undefined     = 0x00U;
      static constexpr uint8_t     Bit           = 0x01U;
      static constexpr uint8_t     UInt8         = 0x02U;
      static constexpr uint8_t     UInt16        = 0x03U;
      static constexpr uint8_t     UInt32        = 0x04U;
      static constexpr uint8_t     UInt64        = 0x05U;
      static constexpr uint8_t     Float32       = 0x06U;
      static constexpr uint8_t     Float64       = 0x07U;


      static constexpr uint8_t     Int8          = UInt8  | Signed;
      static constexpr uint8_t     Int16         = UInt16 | Signed;
      static constexpr uint8_t     Int16LE       = UInt16 | Signed | LittleEndian;
      static constexpr uint8_t     UInt16LE      = UInt16 | LittleEndian;
      static constexpr uint8_t     Int16BE       = UInt16 | Signed | BigEndian;
      static constexpr uint8_t     UInt16BE      = UInt16 | BigEndian;
      static constexpr uint8_t     Int32         = UInt32 | Signed;
      static constexpr uint8_t     Int32LE       = UInt32 | Signed | LittleEndian;
      static constexpr uint8_t     UInt32LE      = UInt32 | LittleEndian;
      static constexpr uint8_t     Int32BE       = UInt32 | Signed | BigEndian;
      static constexpr uint8_t     UInt32BE      = UInt32 | BigEndian;
      static constexpr uint8_t     Int64         = UInt64 | Signed;
      static constexpr uint8_t     Int64LE       = UInt64 | Signed | LittleEndian;
      static constexpr uint8_t     UInt64LE      = UInt64 | LittleEndian;
      static constexpr uint8_t     Int64BE       = UInt64 | Signed | BigEndian;
      static constexpr uint8_t     UInt64BE      = UInt64 | BigEndian;
      static constexpr uint8_t     Float32LE     = Float32 | LittleEndian;
      static constexpr uint8_t     Float32BE     = Float32 | BigEndian;
      static constexpr uint8_t     Float64LE     = Float64 | LittleEndian;
      static constexpr uint8_t     Float64BE     = Float64 | BigEndian;
      static constexpr uint8_t     CFloat32      = Complex | Float32;
      static constexpr uint8_t     CFloat32LE    = Complex | Float32 | LittleEndian;
      static constexpr uint8_t     CFloat32BE    = Complex | Float32 | BigEndian;
      static constexpr uint8_t     CFloat64      = Complex | Float64;
      static constexpr uint8_t     CFloat64LE    = Complex | Float64 | LittleEndian;
      static constexpr uint8_t     CFloat64BE    = Complex | Float64 | BigEndian;

      static constexpr uint8_t     Native        = Float32 |
#ifdef MRTRIX_BYTE_ORDER_BIG_ENDIAN
          BigEndian;
#else
          LittleEndian;
#endif
      static App::OptionGroup options ();

      static const char* identifiers[];

      friend std::ostream& operator<< (std::ostream& stream, const DataType& dt) {
        stream << dt.specifier();
        return stream;
      }

    protected:
      uint8_t dt;

  };



  template <> inline DataType DataType::from<bool> ()
  {
    return DataType::Bit;
  }
  template <> inline DataType DataType::from<int8_t> ()
  {
    return DataType::Int8;
  }
  template <> inline DataType DataType::from<uint8_t> ()
  {
    return DataType::UInt8;
  }
  template <> inline DataType DataType::from<int16_t> ()
  {
    return DataType::native (DataType::Int16);
  }
  template <> inline DataType DataType::from<uint16_t> ()
  {
    return DataType::native (DataType::UInt16);
  }
  template <> inline DataType DataType::from<int32_t> ()
  {
    return DataType::native (DataType::Int32);
  }
  template <> inline DataType DataType::from<uint32_t> ()
  {
    return DataType::native (DataType::UInt32);
  }
  template <> inline DataType DataType::from<int64_t> ()
  {
    return DataType::native (DataType::Int64);
  }
  template <> inline DataType DataType::from<uint64_t> ()
  {
    return DataType::native (DataType::UInt64);
  }
  template <> inline DataType DataType::from<float> ()
  {
    return DataType::native (DataType::Float32);
  }
  template <> inline DataType DataType::from<double> ()
  {
    return DataType::native (DataType::Float64);
  }
  template <> inline DataType DataType::from<cfloat> ()
  {
    return DataType::native (DataType::CFloat32);
  }
  template <> inline DataType DataType::from<cdouble> ()
  {
    return DataType::native (DataType::CFloat64);
  }

}

#endif