dataclass-native 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataclass_native/__init__.py +60 -0
- dataclass_native/__main__.py +5 -0
- dataclass_native/_arrow.py +822 -0
- dataclass_native/_json.py +783 -0
- dataclass_native/_mode.py +57 -0
- dataclass_native/_msgpack.py +207 -0
- dataclass_native/_native.py +234 -0
- dataclass_native/_runtime.py +607 -0
- dataclass_native/_version.py +24 -0
- dataclass_native/_wire.py +901 -0
- dataclass_native/arrays.py +13 -0
- dataclass_native/backends/__init__.py +0 -0
- dataclass_native/backends/bindings.py +74 -0
- dataclass_native/backends/cpp.py +574 -0
- dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
- dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
- dataclass_native/backends/templates/header.hpp.j2 +75 -0
- dataclass_native/cli.py +125 -0
- dataclass_native/cmake/dataclass_native-config.cmake +67 -0
- dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
- dataclass_native/include/dataclass_native/json.hpp +1903 -0
- dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
- dataclass_native/include/dataclass_native/python.hpp +594 -0
- dataclass_native/include/dataclass_native/reflect.hpp +63 -0
- dataclass_native/include/dataclass_native/types.hpp +417 -0
- dataclass_native/introspect.py +811 -0
- dataclass_native/py.typed +0 -0
- dataclass_native/schema.py +553 -0
- dataclass_native/types.py +103 -0
- dataclass_native-1.0.0.dist-info/METADATA +339 -0
- dataclass_native-1.0.0.dist-info/RECORD +35 -0
- dataclass_native-1.0.0.dist-info/WHEEL +5 -0
- dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
- dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
- dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// Types used by the C++ generated by dataclass-native
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <array>
|
|
6
|
+
#include <chrono>
|
|
7
|
+
#include <compare>
|
|
8
|
+
#include <concepts>
|
|
9
|
+
#include <cstddef>
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <optional>
|
|
13
|
+
#include <span>
|
|
14
|
+
#include <stdexcept>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <string_view>
|
|
17
|
+
#include <type_traits>
|
|
18
|
+
#include <utility>
|
|
19
|
+
#include <variant>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
namespace dataclass_native
|
|
23
|
+
{
|
|
24
|
+
using Bytes = std::vector<std::uint8_t>;
|
|
25
|
+
|
|
26
|
+
// datetime.date
|
|
27
|
+
using Date = std::chrono::sys_days;
|
|
28
|
+
|
|
29
|
+
// A timezone-aware datetime.datetime: an instant, as microseconds since the epoch, UTC
|
|
30
|
+
using DateTime = std::chrono::sys_time<std::chrono::microseconds>;
|
|
31
|
+
|
|
32
|
+
// A datetime.datetime with no timezone: a date and a time of day, as microseconds since 1970-01-01T00:00
|
|
33
|
+
using NaiveDateTime = std::chrono::local_time<std::chrono::microseconds>;
|
|
34
|
+
|
|
35
|
+
// datetime.timedelta
|
|
36
|
+
using TimeDelta = std::chrono::microseconds;
|
|
37
|
+
|
|
38
|
+
// datetime.time, without a timezone (a type of its own, as serialised differently from a TimeDelta)
|
|
39
|
+
struct TimeOfDay
|
|
40
|
+
{
|
|
41
|
+
std::chrono::microseconds since_midnight{};
|
|
42
|
+
|
|
43
|
+
friend auto operator<=>(const TimeOfDay&, const TimeOfDay&) = default;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// uuid.UUID: its 16 bytes, in the order in which it is written as text, which is that it is ordered by
|
|
47
|
+
struct Uuid
|
|
48
|
+
{
|
|
49
|
+
std::array<std::uint8_t, 16> bytes{};
|
|
50
|
+
|
|
51
|
+
friend auto operator<=>(const Uuid&, const Uuid&) = default;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// decimal.Decimal, of a fixed number of decimal places: the integer which is that many places of it, so 1.50 of
|
|
55
|
+
// two places is 150. Added, subtracted and compared as that integer is.
|
|
56
|
+
template <int Places>
|
|
57
|
+
struct Decimal
|
|
58
|
+
{
|
|
59
|
+
static constexpr std::int64_t limit = 1'000'000'000'000'000'000;
|
|
60
|
+
|
|
61
|
+
std::int64_t unscaled{};
|
|
62
|
+
|
|
63
|
+
// Whether it is of no more than 18 digits: one of more is not written
|
|
64
|
+
bool fits() const
|
|
65
|
+
{
|
|
66
|
+
return unscaled > -limit && unscaled < limit;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Its digits, with a point before the last Places of them: as it is written
|
|
70
|
+
std::string str() const
|
|
71
|
+
{
|
|
72
|
+
const auto magnitude = static_cast<std::uint64_t>(unscaled);
|
|
73
|
+
std::string digits = std::to_string(unscaled < 0 ? 0 - magnitude : magnitude);
|
|
74
|
+
if (digits.size() <= static_cast<std::size_t>(Places))
|
|
75
|
+
digits.insert(0, static_cast<std::size_t>(Places) + 1 - digits.size(), '0');
|
|
76
|
+
if (Places)
|
|
77
|
+
digits.insert(digits.size() - static_cast<std::size_t>(Places), 1, '.');
|
|
78
|
+
return unscaled < 0 ? "-" + digits : digits;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// That of digits alone, with a point or not: of no more places than its own but for zeros, nor more than
|
|
82
|
+
// 18 digits
|
|
83
|
+
static std::optional<Decimal> parse(std::string_view text)
|
|
84
|
+
{
|
|
85
|
+
const bool negative = text.starts_with('-');
|
|
86
|
+
text.remove_prefix(negative ? 1 : 0);
|
|
87
|
+
const std::size_t point = text.find('.');
|
|
88
|
+
const std::string_view whole = text.substr(0, point);
|
|
89
|
+
const std::string_view fraction = point == text.npos ? whole.substr(0, 0) : text.substr(point + 1);
|
|
90
|
+
if (whole.empty() || (point != text.npos && fraction.empty()))
|
|
91
|
+
return std::nullopt;
|
|
92
|
+
|
|
93
|
+
std::int64_t unscaled = 0;
|
|
94
|
+
const auto digit = [&](char c)
|
|
95
|
+
{
|
|
96
|
+
// Before it is of another digit, which may be more than an integer holds
|
|
97
|
+
if (c < '0' || c > '9' || unscaled >= limit / 10)
|
|
98
|
+
return false;
|
|
99
|
+
unscaled = unscaled * 10 + (c - '0');
|
|
100
|
+
return true;
|
|
101
|
+
};
|
|
102
|
+
for (const char c : whole)
|
|
103
|
+
if (!digit(c))
|
|
104
|
+
return std::nullopt;
|
|
105
|
+
for (std::size_t place = 0; place != static_cast<std::size_t>(Places); ++place)
|
|
106
|
+
if (!digit(place < fraction.size() ? fraction[place] : '0'))
|
|
107
|
+
return std::nullopt;
|
|
108
|
+
for (std::size_t place = static_cast<std::size_t>(Places); place < fraction.size(); ++place)
|
|
109
|
+
if (fraction[place] != '0')
|
|
110
|
+
return std::nullopt;
|
|
111
|
+
return Decimal{negative ? -unscaled : unscaled};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
friend auto operator<=>(const Decimal&, const Decimal&) = default;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// Shared[T]: an immutable value, shared rather than copied, and compared by value (as Rust's Arc<T>).
|
|
118
|
+
// Only a default-constructed or moved-from Shared is empty.
|
|
119
|
+
template <class T>
|
|
120
|
+
class Shared
|
|
121
|
+
{
|
|
122
|
+
public:
|
|
123
|
+
Shared() = default;
|
|
124
|
+
|
|
125
|
+
Shared(T value) : ptr_(std::make_shared<const T>(std::move(value)))
|
|
126
|
+
{
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
explicit Shared(std::shared_ptr<const T> ptr) : ptr_(std::move(ptr))
|
|
130
|
+
{
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const T& operator*() const
|
|
134
|
+
{
|
|
135
|
+
return *get();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const T* operator->() const
|
|
139
|
+
{
|
|
140
|
+
return get();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const T* get() const
|
|
144
|
+
{
|
|
145
|
+
if (!ptr_)
|
|
146
|
+
throw std::logic_error("dataclass_native::Shared is empty");
|
|
147
|
+
return ptr_.get();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const std::shared_ptr<const T>& ptr() const
|
|
151
|
+
{
|
|
152
|
+
return ptr_;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
explicit operator bool() const
|
|
156
|
+
{
|
|
157
|
+
return static_cast<bool>(ptr_);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
friend bool operator==(const Shared& lhs, const Shared& rhs)
|
|
161
|
+
{
|
|
162
|
+
if (lhs.ptr_ == rhs.ptr_)
|
|
163
|
+
return true;
|
|
164
|
+
if (!lhs.ptr_ || !rhs.ptr_)
|
|
165
|
+
return false;
|
|
166
|
+
return *lhs.ptr_ == *rhs.ptr_;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private:
|
|
170
|
+
std::shared_ptr<const T> ptr_;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// An object of a class which is not frozen: held by reference, as Python holds it, so that what holds it sees
|
|
174
|
+
// what is set on it, and copies of what holds it share it. Compared by value. Empty only if default-constructed
|
|
175
|
+
// or moved from.
|
|
176
|
+
template <class T>
|
|
177
|
+
class Mutable
|
|
178
|
+
{
|
|
179
|
+
public:
|
|
180
|
+
Mutable() = default;
|
|
181
|
+
|
|
182
|
+
Mutable(T value) : ptr_(std::make_shared<T>(std::move(value)))
|
|
183
|
+
{
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
explicit Mutable(std::shared_ptr<T> ptr) : ptr_(std::move(ptr))
|
|
187
|
+
{
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
T& operator*() const
|
|
191
|
+
{
|
|
192
|
+
return *get();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
T* operator->() const
|
|
196
|
+
{
|
|
197
|
+
return get();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
T* get() const
|
|
201
|
+
{
|
|
202
|
+
if (!ptr_)
|
|
203
|
+
throw std::logic_error("dataclass_native::Mutable is empty");
|
|
204
|
+
return ptr_.get();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const std::shared_ptr<T>& ptr() const
|
|
208
|
+
{
|
|
209
|
+
return ptr_;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
explicit operator bool() const
|
|
213
|
+
{
|
|
214
|
+
return static_cast<bool>(ptr_);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
friend bool operator==(const Mutable& lhs, const Mutable& rhs)
|
|
218
|
+
{
|
|
219
|
+
if (lhs.ptr_ == rhs.ptr_)
|
|
220
|
+
return true;
|
|
221
|
+
if (!lhs.ptr_ || !rhs.ptr_)
|
|
222
|
+
return false;
|
|
223
|
+
return *lhs.ptr_ == *rhs.ptr_;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private:
|
|
227
|
+
std::shared_ptr<T> ptr_;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// A numpy array of numbers, of N dimensions: shared rather than copied, by what holds it and by the arrays which
|
|
231
|
+
// Python reads it as. Compared by value. *array is its numbers, the last of its dimensions varying fastest (as
|
|
232
|
+
// numpy's C order), and array(i, j) is one of them. It is immutable unless Writeable, as is that of a class which
|
|
233
|
+
// is not frozen: then its numbers may be written, by C++ and by Python, though there are always as many of them.
|
|
234
|
+
template <class T, std::size_t N = 1, bool Writeable = false>
|
|
235
|
+
class Array
|
|
236
|
+
{
|
|
237
|
+
public:
|
|
238
|
+
using Shape = std::array<std::size_t, N>;
|
|
239
|
+
using Element = std::conditional_t<Writeable, T, const T>;
|
|
240
|
+
|
|
241
|
+
Array() = default;
|
|
242
|
+
|
|
243
|
+
Array(std::vector<T> values)
|
|
244
|
+
requires(N == 1)
|
|
245
|
+
: shape_{values.size()}, ptr_(std::make_shared<std::vector<T>>(std::move(values)))
|
|
246
|
+
{
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Throws std::invalid_argument unless the shape is that of the numbers
|
|
250
|
+
Array(std::vector<T> values, const Shape& shape)
|
|
251
|
+
: shape_(shape), ptr_(std::make_shared<std::vector<T>>(std::move(values)))
|
|
252
|
+
{
|
|
253
|
+
if (!fits(shape, ptr_->size()))
|
|
254
|
+
throw std::invalid_argument("dataclass_native::Array is not of its shape");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Whether a shape is that of so many numbers
|
|
258
|
+
static bool fits(const Shape& shape, std::size_t count)
|
|
259
|
+
{
|
|
260
|
+
if (std::ranges::find(shape, std::size_t{0}) != shape.end())
|
|
261
|
+
return count == 0;
|
|
262
|
+
std::size_t product = 1;
|
|
263
|
+
for (const std::size_t size : shape)
|
|
264
|
+
{
|
|
265
|
+
// So that the product does not overflow
|
|
266
|
+
if (product > count / size)
|
|
267
|
+
return false;
|
|
268
|
+
product *= size;
|
|
269
|
+
}
|
|
270
|
+
return product == count;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const Shape& shape() const
|
|
274
|
+
{
|
|
275
|
+
return shape_;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
std::size_t size() const
|
|
279
|
+
{
|
|
280
|
+
return ptr_ ? ptr_->size() : 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// The number at an index of each dimension, which is not checked
|
|
284
|
+
template <std::convertible_to<std::size_t>... I>
|
|
285
|
+
requires(sizeof...(I) == N)
|
|
286
|
+
Element& operator()(I... index) const
|
|
287
|
+
{
|
|
288
|
+
std::size_t at = 0;
|
|
289
|
+
std::size_t dimension = 0;
|
|
290
|
+
((at = at * shape_[dimension++] + static_cast<std::size_t>(index)), ...);
|
|
291
|
+
return (*ptr_)[at];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
std::span<Element> operator*() const
|
|
295
|
+
{
|
|
296
|
+
return ptr_ ? std::span<Element>(*ptr_) : std::span<Element>();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// What holds its numbers, which those who read them share
|
|
300
|
+
const std::shared_ptr<std::vector<T>>& ptr() const
|
|
301
|
+
{
|
|
302
|
+
return ptr_;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
friend bool operator==(const Array& lhs, const Array& rhs)
|
|
306
|
+
{
|
|
307
|
+
return lhs.shape_ == rhs.shape_ && (lhs.ptr_ == rhs.ptr_ || std::ranges::equal(*lhs, *rhs));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private:
|
|
311
|
+
Shape shape_{};
|
|
312
|
+
std::shared_ptr<std::vector<T>> ptr_;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// The array of a class which is not frozen
|
|
316
|
+
template <class T, std::size_t N = 1>
|
|
317
|
+
using MutableArray = Array<T, N, true>;
|
|
318
|
+
|
|
319
|
+
// A value stored out of line, so that recursive types are possible: copied deeply and compared by value (as
|
|
320
|
+
// C++26's std::indirect, or Rust's Box<T>). Unlike std::indirect, it is empty when default-constructed, so that
|
|
321
|
+
// default-constructing a recursive type terminates.
|
|
322
|
+
template <class T>
|
|
323
|
+
class Indirect
|
|
324
|
+
{
|
|
325
|
+
public:
|
|
326
|
+
Indirect() = default;
|
|
327
|
+
|
|
328
|
+
Indirect(T value) : ptr_(std::make_unique<T>(std::move(value)))
|
|
329
|
+
{
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
Indirect(const Indirect& other) : ptr_(other.ptr_ ? std::make_unique<T>(*other.ptr_) : nullptr)
|
|
333
|
+
{
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
Indirect(Indirect&&) noexcept = default;
|
|
337
|
+
|
|
338
|
+
Indirect& operator=(const Indirect& other)
|
|
339
|
+
{
|
|
340
|
+
if (this != &other)
|
|
341
|
+
ptr_ = other.ptr_ ? std::make_unique<T>(*other.ptr_) : nullptr;
|
|
342
|
+
return *this;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
Indirect& operator=(Indirect&&) noexcept = default;
|
|
346
|
+
|
|
347
|
+
~Indirect() = default;
|
|
348
|
+
|
|
349
|
+
T& operator*()
|
|
350
|
+
{
|
|
351
|
+
return *get();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const T& operator*() const
|
|
355
|
+
{
|
|
356
|
+
return *get();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
T* operator->()
|
|
360
|
+
{
|
|
361
|
+
return get();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const T* operator->() const
|
|
365
|
+
{
|
|
366
|
+
return get();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
T* get() const
|
|
370
|
+
{
|
|
371
|
+
if (!ptr_)
|
|
372
|
+
throw std::logic_error("dataclass_native::Indirect is empty");
|
|
373
|
+
return ptr_.get();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
explicit operator bool() const
|
|
377
|
+
{
|
|
378
|
+
return static_cast<bool>(ptr_);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
friend bool operator==(const Indirect& lhs, const Indirect& rhs)
|
|
382
|
+
{
|
|
383
|
+
if (!lhs.ptr_ || !rhs.ptr_)
|
|
384
|
+
return !lhs.ptr_ && !rhs.ptr_;
|
|
385
|
+
return *lhs.ptr_ == *rhs.ptr_;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private:
|
|
389
|
+
std::unique_ptr<T> ptr_;
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// DiscriminatedUnion[A, B]: a std::variant, which is serialised with a tag naming its member's type. A type of
|
|
393
|
+
// its own, as that is all which tells it from a plain union of the same members.
|
|
394
|
+
template <class... Ts>
|
|
395
|
+
class DiscriminatedUnion : public std::variant<Ts...>
|
|
396
|
+
{
|
|
397
|
+
public:
|
|
398
|
+
using std::variant<Ts...>::variant;
|
|
399
|
+
using std::variant<Ts...>::operator=;
|
|
400
|
+
|
|
401
|
+
DiscriminatedUnion() = default;
|
|
402
|
+
|
|
403
|
+
explicit DiscriminatedUnion(std::variant<Ts...> value) : std::variant<Ts...>(std::move(value))
|
|
404
|
+
{
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const std::variant<Ts...>& as_variant() const
|
|
408
|
+
{
|
|
409
|
+
return *this;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
friend bool operator==(const DiscriminatedUnion& lhs, const DiscriminatedUnion& rhs)
|
|
413
|
+
{
|
|
414
|
+
return lhs.as_variant() == rhs.as_variant();
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
} // namespace dataclass_native
|