| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <string> |
| 14 |
|
| 15 |
//------------------------------------------------------------------------------------------------------ |
| 16 |
|
| 17 |
// For type safety we want to use std::byte for the DataType "BYTE", but it is a C++17 feature. |
| 18 |
// Backport std::byte here without any operations |
| 19 |
#if __cplusplus >= 201703L |
| 20 |
#include <cstddef> |
| 21 |
#else |
| 22 |
#ifndef STD_BYTE_DEFINED |
| 23 |
#define STD_BYTE_DEFINED |
| 24 |
namespace std { |
| 25 |
#ifdef _CRAYC |
| 26 |
struct byte { |
| 27 |
unsigned char byte_; |
| 28 |
}; |
| 29 |
#else |
| 30 |
enum class byte : unsigned char |
| 31 |
{ |
| 32 |
}; |
| 33 |
#endif |
| 34 |
} // namespace std |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
|
| 38 |
//------------------------------------------------------------------------------------------------------ |
| 39 |
|
| 40 |
namespace atlas { |
| 41 |
namespace array { |
| 42 |
class DataType { |
| 43 |
public: |
| 44 |
typedef long kind_t; |
| 45 |
static constexpr kind_t KIND_BYTE = 1; |
| 46 |
static constexpr kind_t KIND_INT32 = -4; |
| 47 |
static constexpr kind_t KIND_INT64 = -8; |
| 48 |
static constexpr kind_t KIND_REAL32 = 4; |
| 49 |
static constexpr kind_t KIND_REAL64 = 8; |
| 50 |
static constexpr kind_t KIND_UINT32 = -15; |
| 51 |
static constexpr kind_t KIND_UINT64 = -16; |
| 52 |
|
| 53 |
template <typename DATATYPE> |
| 54 |
static DataType create(); |
| 55 |
|
| 56 |
static DataType byte() { return DataType(KIND_BYTE); } |
| 57 |
static DataType int32() { return DataType(KIND_INT32); } |
| 58 |
static DataType int64() { return DataType(KIND_INT64); } |
| 59 |
static DataType real32() { return DataType(KIND_REAL32); } |
| 60 |
static DataType real64() { return DataType(KIND_REAL64); } |
| 61 |
static DataType uint32() { return DataType(KIND_UINT32); } |
| 62 |
static DataType uint64() { return DataType(KIND_UINT64); } |
| 63 |
|
| 64 |
template <typename DATATYPE> |
| 65 |
static constexpr kind_t kind(); |
| 66 |
template <typename DATATYPE> |
| 67 |
static constexpr kind_t kind(const DATATYPE&); |
| 68 |
|
| 69 |
template <typename DATATYPE> |
| 70 |
static std::string str(); |
| 71 |
template <typename DATATYPE> |
| 72 |
static std::string str(const DATATYPE); |
| 73 |
|
| 74 |
static kind_t str_to_kind(const std::string&); |
| 75 |
static std::string kind_to_str(kind_t); |
| 76 |
static bool kind_valid(kind_t); |
| 77 |
|
| 78 |
private: |
| 79 |
static std::string byte_str() { return "byte"; } |
| 80 |
static std::string int32_str() { return "int32"; } |
| 81 |
static std::string int64_str() { return "int64"; } |
| 82 |
static std::string real32_str() { return "real32"; } |
| 83 |
static std::string real64_str() { return "real64"; } |
| 84 |
static std::string uint32_str() { return "uint32"; } |
| 85 |
static std::string uint64_str() { return "uint64"; } |
| 86 |
|
| 87 |
[[noreturn]] static void throw_not_recognised(kind_t); |
| 88 |
[[noreturn]] static void throw_not_recognised(std::string datatype); |
| 89 |
|
| 90 |
public: |
| 91 |
DataType(const std::string&); |
| 92 |
DataType(long); |
| 93 |
DataType(const DataType&); |
| 94 |
DataType& operator=(const DataType&); |
| 95 |
std::string str() const { return kind_to_str(kind_); } |
| 96 |
kind_t kind() const { return kind_; } |
| 97 |
size_t size() const { return (kind_ == KIND_UINT32) ? 4 : |
| 98 |
(kind_ == KIND_UINT64) ? 8 : |
| 99 |
std::abs(kind_); } |
| 100 |
|
| 101 |
friend bool operator==(DataType dt1, DataType dt2); |
| 102 |
friend bool operator!=(DataType dt1, DataType dt2); |
| 103 |
friend bool operator==(DataType dt, kind_t kind); |
| 104 |
friend bool operator!=(DataType dt, kind_t kind); |
| 105 |
friend bool operator==(kind_t kind, DataType dt); |
| 106 |
friend bool operator!=(kind_t kind, DataType dt2); |
| 107 |
|
| 108 |
private: |
| 109 |
kind_t kind_; |
| 110 |
}; |
| 111 |
|
| 112 |
template <> |
| 113 |
inline std::string DataType::str<std::byte>() { |
| 114 |
return byte_str(); |
| 115 |
} |
| 116 |
template <> |
| 117 |
inline std::string DataType::str<const std::byte>() { |
| 118 |
return byte_str(); |
| 119 |
} |
| 120 |
template <> |
| 121 |
inline std::string DataType::str<int>() { |
| 122 |
static_assert(sizeof(int) == 4, ""); |
| 123 |
return int32_str(); |
| 124 |
} |
| 125 |
template <> |
| 126 |
inline std::string DataType::str<const int>() { |
| 127 |
static_assert(sizeof(int) == 4, ""); |
| 128 |
return int32_str(); |
| 129 |
} |
| 130 |
template <> |
| 131 |
inline std::string DataType::str<long>() { |
| 132 |
static_assert(sizeof(long) == 8, ""); |
| 133 |
return int64_str(); |
| 134 |
} |
| 135 |
template <> |
| 136 |
inline std::string DataType::str<const long>() { |
| 137 |
static_assert(sizeof(long) == 8, ""); |
| 138 |
return int64_str(); |
| 139 |
} |
| 140 |
template <> |
| 141 |
inline std::string DataType::str<long long>() { |
| 142 |
static_assert(sizeof(long long) == 8, ""); |
| 143 |
return int64_str(); |
| 144 |
} |
| 145 |
template <> |
| 146 |
inline std::string DataType::str<const long long>() { |
| 147 |
static_assert(sizeof(long long) == 8, ""); |
| 148 |
return int64_str(); |
| 149 |
} |
| 150 |
template <> |
| 151 |
inline std::string DataType::str<float>() { |
| 152 |
static_assert(sizeof(float) == 4, ""); |
| 153 |
return real32_str(); |
| 154 |
} |
| 155 |
template <> |
| 156 |
inline std::string DataType::str<const float>() { |
| 157 |
static_assert(sizeof(float) == 4, ""); |
| 158 |
return real32_str(); |
| 159 |
} |
| 160 |
template <> |
| 161 |
inline std::string DataType::str<double>() { |
| 162 |
static_assert(sizeof(double) == 8, ""); |
| 163 |
return real64_str(); |
| 164 |
} |
| 165 |
template <> |
| 166 |
inline std::string DataType::str<const double>() { |
| 167 |
static_assert(sizeof(double) == 8, ""); |
| 168 |
return real64_str(); |
| 169 |
} |
| 170 |
template <> |
| 171 |
inline std::string DataType::str<unsigned int>() { |
| 172 |
static_assert(sizeof(unsigned int) == 4, ""); |
| 173 |
return uint32_str(); |
| 174 |
} |
| 175 |
template <> |
| 176 |
inline std::string DataType::str<const unsigned int>() { |
| 177 |
static_assert(sizeof(unsigned int) == 4, ""); |
| 178 |
return uint32_str(); |
| 179 |
} |
| 180 |
template <> |
| 181 |
inline std::string DataType::str<unsigned long>() { |
| 182 |
static_assert(sizeof(unsigned long) == 8, ""); |
| 183 |
return uint64_str(); |
| 184 |
} |
| 185 |
template <> |
| 186 |
inline std::string DataType::str<const unsigned long>() { |
| 187 |
static_assert(sizeof(unsigned long) == 8, ""); |
| 188 |
return uint64_str(); |
| 189 |
} |
| 190 |
|
| 191 |
template <> |
| 192 |
inline std::string DataType::str<unsigned long long>() { |
| 193 |
static_assert(sizeof(unsigned long long) == 8, ""); |
| 194 |
return uint64_str(); |
| 195 |
} |
| 196 |
template <> |
| 197 |
inline std::string DataType::str<const unsigned long long>() { |
| 198 |
static_assert(sizeof(unsigned long long) == 8, ""); |
| 199 |
return uint64_str(); |
| 200 |
} |
| 201 |
template <> |
| 202 |
inline std::string DataType::str(const int&) { |
| 203 |
return str<int>(); |
| 204 |
} |
| 205 |
template <> |
| 206 |
inline std::string DataType::str(const long&) { |
| 207 |
return str<long>(); |
| 208 |
} |
| 209 |
template <> |
| 210 |
inline std::string DataType::str(const long long&) { |
| 211 |
return str<long long>(); |
| 212 |
} |
| 213 |
template <> |
| 214 |
inline std::string DataType::str(const unsigned int&) { |
| 215 |
return str<unsigned int>(); |
| 216 |
} |
| 217 |
template <> |
| 218 |
inline std::string DataType::str(const unsigned long&) { |
| 219 |
return str<unsigned long>(); |
| 220 |
} |
| 221 |
template <> |
| 222 |
inline std::string DataType::str(const unsigned long long&) { |
| 223 |
return str<unsigned long>(); |
| 224 |
} |
| 225 |
template <> |
| 226 |
inline std::string DataType::str(const float&) { |
| 227 |
return str<float>(); |
| 228 |
} |
| 229 |
template <> |
| 230 |
inline std::string DataType::str(const double&) { |
| 231 |
return str<double>(); |
| 232 |
} |
| 233 |
template <> |
| 234 |
inline constexpr DataType::kind_t DataType::kind<std::byte>() { |
| 235 |
static_assert(sizeof(std::byte) == 1, ""); |
| 236 |
return KIND_BYTE; |
| 237 |
} |
| 238 |
template <> |
| 239 |
inline constexpr DataType::kind_t DataType::kind<const std::byte>() { |
| 240 |
static_assert(sizeof(std::byte) == 1, ""); |
| 241 |
return KIND_BYTE; |
| 242 |
} |
| 243 |
template <> |
| 244 |
inline constexpr DataType::kind_t DataType::kind<int>() { |
| 245 |
static_assert(sizeof(int) == 4, ""); |
| 246 |
return KIND_INT32; |
| 247 |
} |
| 248 |
template <> |
| 249 |
inline constexpr DataType::kind_t DataType::kind<const int>() { |
| 250 |
static_assert(sizeof(int) == 4, ""); |
| 251 |
return KIND_INT32; |
| 252 |
} |
| 253 |
template <> |
| 254 |
inline constexpr DataType::kind_t DataType::kind<long>() { |
| 255 |
static_assert(sizeof(long) == 8, ""); |
| 256 |
return KIND_INT64; |
| 257 |
} |
| 258 |
template <> |
| 259 |
inline constexpr DataType::kind_t DataType::kind<const long>() { |
| 260 |
static_assert(sizeof(long) == 8, ""); |
| 261 |
return KIND_INT64; |
| 262 |
} |
| 263 |
template <> |
| 264 |
inline constexpr DataType::kind_t DataType::kind<long long>() { |
| 265 |
static_assert(sizeof(long long) == 8, ""); |
| 266 |
return KIND_INT64; |
| 267 |
} |
| 268 |
template <> |
| 269 |
inline constexpr DataType::kind_t DataType::kind<const long long>() { |
| 270 |
static_assert(sizeof(long long) == 8, ""); |
| 271 |
return KIND_INT64; |
| 272 |
} |
| 273 |
template <> |
| 274 |
inline constexpr DataType::kind_t DataType::kind<unsigned int>() { |
| 275 |
static_assert(sizeof(unsigned int) == 4, ""); |
| 276 |
return KIND_UINT32; |
| 277 |
} |
| 278 |
template <> |
| 279 |
inline constexpr DataType::kind_t DataType::kind<const unsigned int>() { |
| 280 |
static_assert(sizeof(unsigned int) == 4, ""); |
| 281 |
return KIND_UINT32; |
| 282 |
} |
| 283 |
template <> |
| 284 |
inline constexpr DataType::kind_t DataType::kind<unsigned long>() { |
| 285 |
static_assert(sizeof(unsigned long) == 8, ""); |
| 286 |
return KIND_UINT64; |
| 287 |
} |
| 288 |
template <> |
| 289 |
inline constexpr DataType::kind_t DataType::kind<const unsigned long>() { |
| 290 |
static_assert(sizeof(unsigned long) == 8, ""); |
| 291 |
return KIND_UINT64; |
| 292 |
} |
| 293 |
template <> |
| 294 |
inline constexpr DataType::kind_t DataType::kind<unsigned long long>() { |
| 295 |
static_assert(sizeof(unsigned long long) == 8, ""); |
| 296 |
return KIND_UINT64; |
| 297 |
} |
| 298 |
template <> |
| 299 |
inline constexpr DataType::kind_t DataType::kind<const unsigned long long>() { |
| 300 |
static_assert(sizeof(unsigned long long) == 8, ""); |
| 301 |
return KIND_UINT64; |
| 302 |
} |
| 303 |
template <> |
| 304 |
inline constexpr DataType::kind_t DataType::kind<float>() { |
| 305 |
static_assert(sizeof(float) == 4, ""); |
| 306 |
return KIND_REAL32; |
| 307 |
} |
| 308 |
template <> |
| 309 |
inline constexpr DataType::kind_t DataType::kind<const float>() { |
| 310 |
static_assert(sizeof(float) == 4, ""); |
| 311 |
return KIND_REAL32; |
| 312 |
} |
| 313 |
template <> |
| 314 |
inline constexpr DataType::kind_t DataType::kind<double>() { |
| 315 |
static_assert(sizeof(double) == 8, ""); |
| 316 |
return KIND_REAL64; |
| 317 |
} |
| 318 |
template <> |
| 319 |
inline constexpr DataType::kind_t DataType::kind<const double>() { |
| 320 |
static_assert(sizeof(double) == 8, ""); |
| 321 |
return KIND_REAL64; |
| 322 |
} |
| 323 |
template <> |
| 324 |
inline constexpr DataType::kind_t DataType::kind(const int&) { |
| 325 |
return kind<int>(); |
| 326 |
} |
| 327 |
template <> |
| 328 |
inline constexpr DataType::kind_t DataType::kind(const long&) { |
| 329 |
return kind<long>(); |
| 330 |
} |
| 331 |
template <> |
| 332 |
inline constexpr DataType::kind_t DataType::kind(const unsigned long&) { |
| 333 |
return kind<unsigned long>(); |
| 334 |
} |
| 335 |
template <> |
| 336 |
inline constexpr DataType::kind_t DataType::kind(const unsigned int&) { |
| 337 |
return kind<unsigned int>(); |
| 338 |
} |
| 339 |
template <> |
| 340 |
inline constexpr DataType::kind_t DataType::kind(const float&) { |
| 341 |
return kind<float>(); |
| 342 |
} |
| 343 |
template <> |
| 344 |
inline constexpr DataType::kind_t DataType::kind(const double&) { |
| 345 |
return kind<double>(); |
| 346 |
} |
| 347 |
|
| 348 |
inline DataType::kind_t DataType::str_to_kind(const std::string& datatype) { |
| 349 |
if (datatype == "int32") |
| 350 |
return KIND_INT32; |
| 351 |
else if (datatype == "int64") |
| 352 |
return KIND_INT64; |
| 353 |
else if (datatype == "uint32") |
| 354 |
return KIND_UINT32; |
| 355 |
else if (datatype == "uint64") |
| 356 |
return KIND_UINT64; |
| 357 |
else if (datatype == "real32") |
| 358 |
return KIND_REAL32; |
| 359 |
else if (datatype == "real64") |
| 360 |
return KIND_REAL64; |
| 361 |
else if (datatype == "byte") { |
| 362 |
return KIND_BYTE; |
| 363 |
} |
| 364 |
else { |
| 365 |
throw_not_recognised(datatype); |
| 366 |
} |
| 367 |
} |
| 368 |
inline std::string DataType::kind_to_str(kind_t kind) { |
| 369 |
switch (kind) { |
| 370 |
case KIND_INT32: |
| 371 |
return int32_str(); |
| 372 |
case KIND_INT64: |
| 373 |
return int64_str(); |
| 374 |
case KIND_UINT32: |
| 375 |
return uint32_str(); |
| 376 |
case KIND_UINT64: |
| 377 |
return uint64_str(); |
| 378 |
case KIND_REAL32: |
| 379 |
return real32_str(); |
| 380 |
case KIND_REAL64: |
| 381 |
return real64_str(); |
| 382 |
case KIND_BYTE: |
| 383 |
return byte_str(); |
| 384 |
default: |
| 385 |
throw_not_recognised(kind); |
| 386 |
} |
| 387 |
} |
| 388 |
inline bool DataType::kind_valid(kind_t kind) { |
| 389 |
switch (kind) { |
| 390 |
case KIND_BYTE: |
| 391 |
case KIND_INT32: |
| 392 |
case KIND_INT64: |
| 393 |
case KIND_UINT32: |
| 394 |
case KIND_UINT64: |
| 395 |
case KIND_REAL32: |
| 396 |
case KIND_REAL64: |
| 397 |
return true; |
| 398 |
default: |
| 399 |
return false; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
inline DataType::DataType(const DataType& other): kind_(other.kind_) {} |
| 404 |
|
| 405 |
inline DataType& DataType::operator=(const DataType& other) { |
| 406 |
kind_ = other.kind_; |
| 407 |
return *this; |
| 408 |
} |
| 409 |
|
| 410 |
inline DataType::DataType(const std::string& datatype): kind_(str_to_kind(datatype)) {} |
| 411 |
|
| 412 |
inline DataType::DataType(long kind): kind_(kind) {} |
| 413 |
|
| 414 |
inline bool operator==(DataType dt1, DataType dt2) { |
| 415 |
return dt1.kind_ == dt2.kind_; |
| 416 |
} |
| 417 |
|
| 418 |
inline bool operator!=(DataType dt1, DataType dt2) { |
| 419 |
return dt1.kind_ != dt2.kind_; |
| 420 |
} |
| 421 |
|
| 422 |
inline bool operator==(DataType dt, DataType::kind_t kind) { |
| 423 |
return dt.kind_ == kind; |
| 424 |
} |
| 425 |
|
| 426 |
inline bool operator!=(DataType dt, DataType::kind_t kind) { |
| 427 |
return dt.kind_ != kind; |
| 428 |
} |
| 429 |
|
| 430 |
inline bool operator==(DataType::kind_t kind, DataType dt) { |
| 431 |
return dt.kind_ == kind; |
| 432 |
} |
| 433 |
|
| 434 |
inline bool operator!=(DataType::kind_t kind, DataType dt) { |
| 435 |
return dt.kind_ != kind; |
| 436 |
} |
| 437 |
|
| 438 |
template <typename DATATYPE> |
| 439 |
inline DataType DataType::create() { |
| 440 |
return DataType(DataType::kind<DATATYPE>()); |
| 441 |
} |
| 442 |
|
| 443 |
template <typename DATATYPE> |
| 444 |
inline DataType make_datatype() { |
| 445 |
return DataType(DataType::kind<DATATYPE>()); |
| 446 |
} |
| 447 |
|
| 448 |
//------------------------------------------------------------------------------------------------------ |
| 449 |
} // namespace array |
| 450 |
|
| 451 |
using DataType= array::DataType; |
| 452 |
|
| 453 |
template <typename DATATYPE> |
| 454 |
inline DataType make_datatype() { |
| 455 |
return DataType(DataType::kind<DATATYPE>()); |
| 456 |
} |
| 457 |
|
| 458 |
} // namespace atlas |