TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_nd_types.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:22:32 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
31#pragma once
32
33#include <cstdint>
34#include <cstdio>
35#include <cstdlib>
36#include <cstring>
37#include <string>
38#include <type_traits>
39
40#include "tec/tec_def.hpp" // IWYU pragma: keep
41#include "tec/tec_memfile.hpp"
42#include "tec/tec_container.hpp"
43#include "tec/tec_serialize.hpp"
45
46
47namespace tec {
48
56struct NdTypes {
57
58
59 using ID = uint16_t;
60 using Tag = uint16_t;
61 using Size = uint32_t;
62 using Count = uint16_t;
63 using Bool = uint8_t;
64 using String = std::string;
65
73 struct Meta {
74
75 static constexpr Tag Scalar{(1 << 8)};
76 static constexpr Tag Float{(1 << 9)};
77 static constexpr Tag Signed{(1 << 10)};
78 static constexpr Tag Sequence{(1 << 11)};
79 };
80
85 struct Tags {
86 static constexpr Tag Unknown{0};
87
88 // Scalars
89 static constexpr Tag I8{(1 | Meta::Scalar)};
90 static constexpr Tag I16{(2 | Meta::Scalar)};
91 static constexpr Tag I32{(3 | Meta::Scalar)};
92 static constexpr Tag I64{(4 | Meta::Scalar)};
93 static constexpr Tag IBool{(5 | Meta::Scalar)};
94 static constexpr Tag F32{(6 | Meta::Scalar | Meta::Float)};
95 static constexpr Tag F64{(7 | Meta::Scalar | Meta::Float)};
96 static constexpr Tag F128{(8 | Meta::Scalar | Meta::Float)};
97
98 // Scalar sequences
99 static constexpr Tag SByte{('B' | Meta::Scalar | Meta::Sequence)};
100 static constexpr Tag SChar{('A' | Meta::Scalar | Meta::Sequence)};
101
102 // Containers
103 static constexpr Tag Container{'C'}; // 67 ///< Flat container (vector, array, etc.).
104 static constexpr Tag Map{'M'}; // 77 ///< Associative map/container.
105
106 // Object
107 static constexpr Tag Object{'O'}; // 79 ///< User-defined serializable object.
108 };
109
110#pragma pack(push, 1)
134 struct Header {
136 static constexpr uint32_t kMagic{0x00041b00};
137
139 static constexpr uint16_t kDefaultVersion{0x0100};
140
141 uint32_t magic;
142 uint32_t size;
143 uint16_t version;
144 uint16_t id;
145 int16_t status;
148 uint32_t reserved;
149
154 : magic(kMagic)
155 , size{0}
157 , id{0}
158 , status{0}
159 , compression_flags{CompressionParams::kNoCompression}
161 , reserved{0}
162 {}
163
168 inline constexpr bool is_valid() const {
169 return (magic == kMagic && version >= kDefaultVersion);
170 }
171
176 inline constexpr int get_compression() const {
177 return (0xF & compression_flags);
178 }
179
184 inline constexpr void set_compression(int comp_type) {
185 compression_flags |= (0xF & comp_type);
186 }
187
192 inline constexpr int get_compression_level() const {
193 return (compression_flags & 0xF0) >> 4;
194 }
195
200 inline constexpr void set_compression_level(int nlevel) {
201 compression_flags |= ((0xF & nlevel) << 4);
202 }
203 };
204
222 struct ElemHeader {
226
231 : tag{Tags::Unknown}
232 , size{0}
233 , count{0}
234 {}
235
239 ElemHeader(Tag _tag, Size _size, Count _count = 1)
240 : tag{_tag}
241 , size{_size}
242 , count{_count}
243 {}
244 };
245#pragma pack(pop)
246
252 inline constexpr Count to_count(size_t count) {
253 return ((count > __UINT16_MAX__) ? __UINT16_MAX__ : static_cast<Count>(count));
254 }
255
257 ElemHeader get_scalar_info(const char&) { return {Tags::I8 | Meta::Signed, 1}; }
258
260 ElemHeader get_scalar_info(const unsigned char&) { return {Tags::I8, 1}; }
261
263 ElemHeader get_scalar_info(const short&) { return {Tags::I16 | Meta::Signed, 2}; }
264
266 ElemHeader get_scalar_info(const unsigned short&) { return {Tags::I16, 2}; }
267
269 ElemHeader get_scalar_info(const int&) { return {Tags::I32 | Meta::Signed, 4}; }
270
272 ElemHeader get_scalar_info(const unsigned int&) { return {Tags::I32, 4}; }
273
275 ElemHeader get_scalar_info(const long&) { return {Tags::I32 | Meta::Signed, 4}; }
276
278 ElemHeader get_scalar_info(const unsigned long&) { return {Tags::I32, 4}; }
279
281 ElemHeader get_scalar_info(const long long&) { return {Tags::I64 | Meta::Signed, 8}; }
282
284 ElemHeader get_scalar_info(const unsigned long long&) { return {Tags::I64, 8}; }
285
287 ElemHeader get_scalar_info(const bool&) { return {Tags::IBool, sizeof(Bool)}; }
288
290 ElemHeader get_scalar_info(const float&) { return {Tags::F32 | Meta::Signed, 4}; }
291
293 ElemHeader get_scalar_info(const double&) { return {Tags::F64 | Meta::Signed, 8}; }
294
296 ElemHeader get_scalar_info(const long double&) { return {Tags::F128 | Meta::Signed, 16}; }
297
299 ElemHeader get_seq_info(const String& str) { return {Tags::SChar, static_cast<Size>(str.size())}; }
300
302 ElemHeader get_seq_info(const Blob& bytes) { return {Tags::SByte, static_cast<Size>(bytes.size())}; }
303
308 template <typename TContainer>
309 ElemHeader get_container_info(const TContainer& c) { return {Tags::Container, 0, to_count(c.size())}; }
310
315 template <typename TMap>
316 ElemHeader get_map_info(const TMap& m) { return {Tags::Map, 0, to_count(m.size())}; }
317
322 template <typename TObject>
323 ElemHeader get_object_info(const TObject&) { return {Tags::Object, 0}; }
324
334 template <typename T>
335 ElemHeader get_info(const T& val) {
336 if constexpr (is_serializable_v<T>) {
337 return get_object_info(val);
338 }
339 else if constexpr (std::is_arithmetic_v<T>) {
340 return get_scalar_info(val);
341 }
342 else if constexpr (
343 std::is_same_v<T, String> ||
344 std::is_same_v<T, Blob>
345 ) {
346 return get_seq_info(val);
347 }
348 else if constexpr (is_map_v<T>) {
349 return get_map_info(val);
350 }
351 else if constexpr (is_container_v<T>) {
352 return get_container_info(val);
353 }
354 else {
355 return {};
356 }
357 }
358
359}; // struct NdTypes
360
361} // namespace tec
362
363/*
364~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
365
366NOTE:
367
368 - float — single precision floating-point type. Usually IEEE-754
369 binary32 format.
370
371 - double — double precision floating-point type. Usually IEEE-754
372 binary64 format.
373
374 - long double — extended precision floating-point type. Does not
375 necessarily map to types mandated by IEEE-754.
376
377IEEE-754 binary128 format is used by some HP-UX, SPARC, MIPS, ARM64,
378and z/OS implementations.
379
380The most well known IEEE-754 binary64-extended format is x87 80-bit
381extended precision format. It is used by many x86 and x86-64
382implementations (a notable exception is MSVC, which implements long
383double in the same format as double, i.e. binary64).
384
385On Windows, sizeof(long double) == 8.
386On Linux, macOS, sizeof(long double) == 16.
387
388~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389*/
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
size_t size() const noexcept
Returns the logical size of the data in the buffer.
Definition tec_memfile.hpp:304
Definition tec_compression.hpp:34
Per-element header describing the following data item.
Definition tec_nd_types.hpp:222
Size size
Payload size in bytes (excluding this header).
Definition tec_nd_types.hpp:224
Count count
Number of elements (for containers/sequences; 1 for scalars).
Definition tec_nd_types.hpp:225
Tag tag
Type and property tag.
Definition tec_nd_types.hpp:223
ElemHeader()
Default constructor (unknown element).
Definition tec_nd_types.hpp:230
ElemHeader(Tag _tag, Size _size, Count _count=1)
Construct with explicit tag, size, and count.
Definition tec_nd_types.hpp:239
Global header placed at the start of every serialized buffer.
Definition tec_nd_types.hpp:134
uint16_t version
Protocol version.
Definition tec_nd_types.hpp:143
constexpr void set_compression_level(int nlevel)
Set compression level.
Definition tec_nd_types.hpp:200
static constexpr uint16_t kDefaultVersion
Default protocol version.
Definition tec_nd_types.hpp:139
constexpr bool is_valid() const
Check if the header appears valid.
Definition tec_nd_types.hpp:168
uint16_t compression_flags
Compression type and level (low 4 bits: type, high 4 bits: level).
Definition tec_nd_types.hpp:146
uint32_t size_uncompressed
Original uncompressed payload size (for verification).
Definition tec_nd_types.hpp:147
int16_t status
Status code (application-specific).
Definition tec_nd_types.hpp:145
static constexpr uint32_t kMagic
Magic constant identifying valid serialized data.
Definition tec_nd_types.hpp:136
constexpr void set_compression(int comp_type)
Set compression algorithm type.
Definition tec_nd_types.hpp:184
constexpr int get_compression() const
Extract compression algorithm type.
Definition tec_nd_types.hpp:176
Header()
Default constructor initializing a valid empty header.
Definition tec_nd_types.hpp:153
constexpr int get_compression_level() const
Extract compression level.
Definition tec_nd_types.hpp:192
uint32_t reserved
Reserved for future use (must be zero).
Definition tec_nd_types.hpp:148
uint16_t id
User-defined root object identifier.
Definition tec_nd_types.hpp:144
uint32_t magic
Magic identifier (must match kMagic).
Definition tec_nd_types.hpp:141
uint32_t size
Total size of the serialized payload excluding this header (bytes).
Definition tec_nd_types.hpp:142
Bit-flag definitions for interpreting the Tag field.
Definition tec_nd_types.hpp:73
static constexpr Tag Float
Indicates a floating-point scalar.
Definition tec_nd_types.hpp:76
static constexpr Tag Signed
Indicates a signed integer scalar.
Definition tec_nd_types.hpp:77
static constexpr Tag Sequence
Indicates a sequence of scalar elements (e.g., byte array, string).
Definition tec_nd_types.hpp:78
static constexpr Tag Scalar
Indicates a scalar value (single primitive).
Definition tec_nd_types.hpp:75
Concrete tag values for all supported data types.
Definition tec_nd_types.hpp:85
static constexpr Tag SByte
Sequence of raw bytes (Blob).
Definition tec_nd_types.hpp:99
static constexpr Tag SChar
Sequence of characters (std::string).
Definition tec_nd_types.hpp:100
static constexpr Tag I8
Signed 8-bit integer.
Definition tec_nd_types.hpp:89
static constexpr Tag IBool
Boolean value.
Definition tec_nd_types.hpp:93
static constexpr Tag F32
32-bit IEEE-754 float.
Definition tec_nd_types.hpp:94
static constexpr Tag Unknown
Unknown or uninitialized element.
Definition tec_nd_types.hpp:86
static constexpr Tag I64
Signed/unsigned 64-bit integer.
Definition tec_nd_types.hpp:92
static constexpr Tag I32
Signed/unsigned 32-bit integer.
Definition tec_nd_types.hpp:91
static constexpr Tag F128
128-bit floating point (platform-dependent).
Definition tec_nd_types.hpp:96
static constexpr Tag F64
64-bit IEEE-754 double.
Definition tec_nd_types.hpp:95
static constexpr Tag I16
Signed/unsigned 16-bit integer.
Definition tec_nd_types.hpp:90
Core type definitions and metadata for a lightweight binary serialization format.
Definition tec_nd_types.hpp:56
ElemHeader get_scalar_info(const bool &)
Get element info for bool.
Definition tec_nd_types.hpp:287
ElemHeader get_scalar_info(const short &)
Get element info for signed short.
Definition tec_nd_types.hpp:263
uint16_t ID
Unique identifier type for serialized objects/messages.
Definition tec_nd_types.hpp:59
ElemHeader get_container_info(const TContainer &c)
Get element info for flat containers (vector, array, etc.).
Definition tec_nd_types.hpp:309
ElemHeader get_scalar_info(const double &)
Get element info for double.
Definition tec_nd_types.hpp:293
ElemHeader get_map_info(const TMap &m)
Get element info for map-like containers.
Definition tec_nd_types.hpp:316
ElemHeader get_scalar_info(const unsigned long &)
Get element info for unsigned long.
Definition tec_nd_types.hpp:278
ElemHeader get_scalar_info(const unsigned long long &)
Get element info for unsigned long long.
Definition tec_nd_types.hpp:284
ElemHeader get_scalar_info(const int &)
Get element info for signed int.
Definition tec_nd_types.hpp:269
ElemHeader get_seq_info(const Blob &bytes)
Get element info for Blob (raw byte sequence).
Definition tec_nd_types.hpp:302
uint8_t Bool
Boolean storage type (always 1 byte).
Definition tec_nd_types.hpp:63
ElemHeader get_scalar_info(const long long &)
Get element info for signed long long.
Definition tec_nd_types.hpp:281
std::string String
String storage type.
Definition tec_nd_types.hpp:64
ElemHeader get_scalar_info(const long &)
Get element info for signed long.
Definition tec_nd_types.hpp:275
uint32_t Size
Size type for individual element payload (in bytes).
Definition tec_nd_types.hpp:61
ElemHeader get_scalar_info(const unsigned short &)
Get element info for unsigned short.
Definition tec_nd_types.hpp:266
ElemHeader get_object_info(const TObject &)
Get element info for user-defined serializable objects.
Definition tec_nd_types.hpp:323
constexpr Count to_count(size_t count)
Convert a size_t count to Count, clamping at maximum.
Definition tec_nd_types.hpp:252
ElemHeader get_scalar_info(const char &)
Get element info for signed char.
Definition tec_nd_types.hpp:257
uint16_t Count
Count type for number of elements in sequences/containers.
Definition tec_nd_types.hpp:62
ElemHeader get_scalar_info(const float &)
Get element info for float.
Definition tec_nd_types.hpp:290
ElemHeader get_scalar_info(const long double &)
Get element info for long double (platform-dependent size).
Definition tec_nd_types.hpp:296
ElemHeader get_seq_info(const String &str)
Get element info for std::string (character sequence).
Definition tec_nd_types.hpp:299
uint16_t Tag
Tag type used to encode element kind and properties.
Definition tec_nd_types.hpp:60
ElemHeader get_info(const T &val)
Generic dispatcher to determine ElemHeader for any supported type.
Definition tec_nd_types.hpp:335
ElemHeader get_scalar_info(const unsigned int &)
Get element info for unsigned int.
Definition tec_nd_types.hpp:272
ElemHeader get_scalar_info(const unsigned char &)
Get element info for unsigned char.
Definition tec_nd_types.hpp:260
Compression parameters.
Generic container and map traits.
Common definitions and utilities for the tec namespace.
A byte buffer class with stream-like read/write semantics.
The base interface for serializable objects.