libzarr
Header-only C++17 Zarr v2/v3, WASM-compatible
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#ifndef LIBZARR_TYPES_HPP
4#define LIBZARR_TYPES_HPP
5
6#include <cassert>
7#include <cstdint>
8#include <stdexcept>
9#include <vector>
10
14
15// Macros, not constants: consumers need these preprocessor-testable.
16// NOLINTBEGIN(modernize-macro-to-enum,cppcoreguidelines-macro-to-enum)
18#define LIBZARR_VERSION_MAJOR 1
20#define LIBZARR_VERSION_MINOR 0
22#define LIBZARR_VERSION_PATCH 0
23// NOLINTEND(modernize-macro-to-enum,cppcoreguidelines-macro-to-enum)
24
28#define LIBZARR_DEPRECATED(msg) [[deprecated(msg)]]
29
30namespace zarr {
31
36class error : public std::runtime_error {
37 public:
38 using std::runtime_error::runtime_error;
39};
40
42using Bytes = std::vector<std::uint8_t>;
43
45enum class ZarrFormat : std::uint8_t {
46 v2 = 2,
47 v3 = 3,
48};
49
52enum class DType : std::uint8_t {
53 boolean,
54 int8,
55 int16,
56 int32,
57 int64,
58 uint8,
59 uint16,
60 uint32,
61 uint64,
62 float16,
63 float32,
64 float64,
65 complex64,
66 complex128,
67 raw,
68};
69
71[[nodiscard]] constexpr bool is_float(DType kind) {
72 return kind == DType::float16 || kind == DType::float32 || kind == DType::float64;
73}
74
76[[nodiscard]] constexpr bool is_signed_int(DType kind) {
77 return kind == DType::int8 || kind == DType::int16 || kind == DType::int32 ||
78 kind == DType::int64;
79}
80
82[[nodiscard]] constexpr bool is_unsigned_int(DType kind) {
83 return kind == DType::uint8 || kind == DType::uint16 || kind == DType::uint32 ||
84 kind == DType::uint64;
85}
86
88[[nodiscard]] constexpr bool is_complex(DType kind) {
89 return kind == DType::complex64 || kind == DType::complex128;
90}
91
94[[nodiscard]] constexpr std::uint32_t fixed_itemsize(DType kind) {
95 switch (kind) {
96 case DType::boolean:
97 case DType::int8:
98 case DType::uint8:
99 return 1;
100 case DType::int16:
101 case DType::uint16:
102 case DType::float16:
103 return 2;
104 case DType::int32:
105 case DType::uint32:
106 case DType::float32:
107 return 4;
108 case DType::int64:
109 case DType::uint64:
110 case DType::float64:
111 case DType::complex64:
112 return 8;
113 case DType::complex128:
114 return 16;
115 case DType::raw:
116 return 0;
117 }
118 return 0; // unreachable; keeps compilers satisfied
119}
120
122struct DataType {
124 DType kind = DType::uint8;
126 std::uint32_t itemsize = 1;
127
131 [[nodiscard]] static constexpr DataType of(DType kind) {
132 if (kind == DType::raw) {
133 throw error("DataType::of(DType::raw): raw has no fixed size, use DataType::raw_bytes()");
134 }
136 }
137
139 [[nodiscard]] static constexpr DataType raw_bytes(std::uint32_t size) {
140 return DataType{DType::raw, size};
141 }
142
144 [[nodiscard]] friend constexpr bool operator==(DataType a, DataType b) {
145 return a.kind == b.kind && a.itemsize == b.itemsize;
146 }
148 [[nodiscard]] friend constexpr bool operator!=(DataType a, DataType b) { return !(a == b); }
149};
150
151} // namespace zarr
152
153#endif // LIBZARR_TYPES_HPP
Definition types.hpp:36
A concrete element type: kind plus size (the size only varies for raw).
Definition types.hpp:122
friend constexpr bool operator!=(DataType a, DataType b)
Differing kind or size.
Definition types.hpp:148
std::uint32_t itemsize
Element size in bytes.
Definition types.hpp:126
static constexpr DataType raw_bytes(std::uint32_t size)
A raw byte-string type of size bytes (v2 |V<size>).
Definition types.hpp:139
DType kind
Element type kind.
Definition types.hpp:124
static constexpr DataType of(DType kind)
Definition types.hpp:131
friend constexpr bool operator==(DataType a, DataType b)
Equal kind and size.
Definition types.hpp:144
constexpr bool is_float(DType kind)
True for float16/float32/float64.
Definition types.hpp:71
constexpr bool is_unsigned_int(DType kind)
True for uint8..uint64.
Definition types.hpp:82
DType
Definition types.hpp:52
constexpr bool is_signed_int(DType kind)
True for int8..int64.
Definition types.hpp:76
constexpr std::uint32_t fixed_itemsize(DType kind)
Definition types.hpp:94
std::vector< std::uint8_t > Bytes
Owned byte buffer used throughout the value-based public API.
Definition types.hpp:42
ZarrFormat
Zarr storage format version.
Definition types.hpp:45
constexpr bool is_complex(DType kind)
True for complex64/complex128.
Definition types.hpp:88