| 1 |
#ifndef SERIALIZE_HH |
| 2 |
#define SERIALIZE_HH |
| 3 |
|
| 4 |
#include "SerializeBuffer.hh" |
| 5 |
#include "XMLElement.hh" |
| 6 |
#include "XMLOutputStream.hh" |
| 7 |
#include "serialize_core.hh" |
| 8 |
|
| 9 |
#include "MemBuffer.hh" |
| 10 |
#include "StringOp.hh" |
| 11 |
#include "hash_map.hh" |
| 12 |
#include "inline.hh" |
| 13 |
#include "strCat.hh" |
| 14 |
#include "unreachable.hh" |
| 15 |
#include "zstring_view.hh" |
| 16 |
|
| 17 |
#include <zlib.h> |
| 18 |
|
| 19 |
#include <array> |
| 20 |
#include <cassert> |
| 21 |
#include <memory> |
| 22 |
#include <optional> |
| 23 |
#include <span> |
| 24 |
#include <sstream> |
| 25 |
#include <string> |
| 26 |
#include <type_traits> |
| 27 |
#include <typeindex> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
namespace openmsx { |
| 31 |
|
| 32 |
class LastDeltaBlocks; |
| 33 |
class DeltaBlock; |
| 34 |
|
| 35 |
|
| 36 |
struct HashPair { |
| 37 |
template<typename T1, typename T2> |
| 38 |
size_t operator()(const std::pair<T1, T2>& p) const { |
| 39 |
return 31 * std::hash<T1>()(p.first) + |
| 40 |
std::hash<T2>()(p.second); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
template<typename T> struct SerializeClassVersion; |
| 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 |
template<typename Derived> class ArchiveBase |
| 85 |
{ |
| 86 |
public: |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
template<typename Base, typename T> |
| 96 |
void serializeBase(T& t) |
| 97 |
{ |
| 98 |
const char* tag = BaseClassName<Base>::getName(); |
| 99 |
self().serialize(tag, static_cast<Base&>(t)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
template<typename Base, typename T> |
| 119 |
void serializeInlinedBase(T& t, unsigned version) |
| 120 |
{ |
| 121 |
::openmsx::serialize(self(), static_cast<Base&>(t), version); |
| 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 |
template<typename T> |
| 156 |
void serialize_blob(const char* tag, std::span<T> data, bool diff = true) |
| 157 |
{ |
| 158 |
self().serialize_blob(tag, std::span<uint8_t>{static_cast<uint8_t*>(data.data()), data.size_bytes()}, diff); |
| 159 |
} |
| 160 |
template<typename T> |
| 161 |
void serialize_blob(const char* tag, std::span<const T> data, bool diff = true) |
| 162 |
{ |
| 163 |
self().serialize_blob(tag, std::span<uint8_t>{static_cast<const uint8_t*>(data.data()), data.size_bytes()}, diff); |
| 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 |
static constexpr bool NEED_VERSION = true; |
| 236 |
|
| 237 |
|
| 238 |
[[nodiscard]] bool isReverseSnapshot() const { return false; } |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
static constexpr bool TRANSLATE_ENUM_TO_STRING = false; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
template<typename T> void attribute(const char* name, T& t) |
| 252 |
{ |
| 253 |
self().serialize(name, t); |
| 254 |
} |
| 255 |
void attribute(const char* name, const char* value); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static constexpr bool CAN_HAVE_OPTIONAL_ATTRIBUTES = false; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
[[nodiscard]] bool hasAttribute(const char* ) const |
| 269 |
{ |
| 270 |
UNREACHABLE; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
template<typename T> |
| 277 |
[[nodiscard]] std::optional<T> findAttributeAs(const char* ) |
| 278 |
{ |
| 279 |
UNREACHABLE; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static constexpr bool CAN_COUNT_CHILDREN = false; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
[[nodiscard]] int countChildren() const |
| 297 |
{ |
| 298 |
UNREACHABLE; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
void beginTag(const char* ) const |
| 310 |
{ |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
void endTag(const char* ) const |
| 320 |
{ |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
protected: |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
Derived& self() |
| 363 |
{ |
| 364 |
return static_cast<Derived&>(*this); |
| 365 |
} |
| 366 |
}; |
| 367 |
|
| 368 |
|
| 369 |
class OutputArchiveBase2 |
| 370 |
{ |
| 371 |
public: |
| 372 |
static constexpr bool IS_LOADER = false; |
| 373 |
[[nodiscard]] bool versionAtLeast(unsigned , unsigned ) const |
| 374 |
{ |
| 375 |
return true; |
| 376 |
} |
| 377 |
[[nodiscard]] bool versionBelow(unsigned , unsigned ) const |
| 378 |
{ |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
void skipSection(bool ) const |
| 383 |
{ |
| 384 |
UNREACHABLE; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
#ifdef linux |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
[[nodiscard]] static NEVER_INLINE bool addressOnStack(const void* p) |
| 395 |
{ |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
int dummy; |
| 402 |
return &dummy < p; |
| 403 |
} |
| 404 |
#endif |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
template<typename T> [[nodiscard]] unsigned generateId(const T* p) |
| 409 |
{ |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if constexpr (std::is_polymorphic_v<T>) { |
| 421 |
return generateID1(p); |
| 422 |
} else { |
| 423 |
return generateID2(p, typeid(T)); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
template<typename T> [[nodiscard]] unsigned getId(const T* p) |
| 428 |
{ |
| 429 |
if constexpr (std::is_polymorphic_v<T>) { |
| 430 |
return getID1(p); |
| 431 |
} else { |
| 432 |
return getID2(p, typeid(T)); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
protected: |
| 437 |
OutputArchiveBase2() = default; |
| 438 |
|
| 439 |
private: |
| 440 |
[[nodiscard]] unsigned generateID1(const void* p); |
| 441 |
[[nodiscard]] unsigned generateID2(const void* p, const std::type_info& typeInfo); |
| 442 |
[[nodiscard]] unsigned getID1(const void* p); |
| 443 |
[[nodiscard]] unsigned getID2(const void* p, const std::type_info& typeInfo); |
| 444 |
|
| 445 |
private: |
| 446 |
hash_map<std::pair<const void*, std::type_index>, unsigned, HashPair> idMap; |
| 447 |
hash_map<const void*, unsigned> polyIdMap; |
| 448 |
unsigned lastId = 0; |
| 449 |
}; |
| 450 |
|
| 451 |
template<typename Derived> |
| 452 |
class OutputArchiveBase : public ArchiveBase<Derived>, public OutputArchiveBase2 |
| 453 |
{ |
| 454 |
public: |
| 455 |
template<typename Base, typename T> |
| 456 |
void serializeInlinedBase(T& t, unsigned version) |
| 457 |
{ |
| 458 |
|
| 459 |
static_assert(SerializeClassVersion<Base>::value == |
| 460 |
SerializeClassVersion<T >::value, |
| 461 |
"base and derived must have same version when " |
| 462 |
"using serializeInlinedBase()"); |
| 463 |
ArchiveBase<Derived>::template serializeInlinedBase<Base>(t, version); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
template<typename T, typename... Args> |
| 471 |
void serializeWithID(const char* tag, const T& t, Args... ) |
| 472 |
{ |
| 473 |
this->self().beginTag(tag); |
| 474 |
Saver<T> saver; |
| 475 |
saver(this->self(), t, true); |
| 476 |
this->self().endTag(tag); |
| 477 |
} |
| 478 |
|
| 479 |
template<typename T> void serialize(const char* tag, const T& t) |
| 480 |
{ |
| 481 |
this->self().beginTag(tag); |
| 482 |
Saver<T> saver; |
| 483 |
saver(this->self(), t, false); |
| 484 |
this->self().endTag(tag); |
| 485 |
} |
| 486 |
template<typename T> void serializePointerID(const char* tag, const T& t) |
| 487 |
{ |
| 488 |
this->self().beginTag(tag); |
| 489 |
IDSaver<T> saver; |
| 490 |
saver(this->self(), t); |
| 491 |
this->self().endTag(tag); |
| 492 |
} |
| 493 |
template<typename T> void serializePolymorphic(const char* tag, const T& t) |
| 494 |
{ |
| 495 |
static_assert(std::is_polymorphic_v<T>, |
| 496 |
"must be a polymorphic type"); |
| 497 |
PolymorphicSaverRegistry<Derived>::save(tag, this->self(), t); |
| 498 |
} |
| 499 |
template<typename T> void serializeOnlyOnce(const char* tag, const T& t) |
| 500 |
{ |
| 501 |
if (!getId(&t)) { |
| 502 |
serializeWithID(tag, t); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
void serializeChar(const char* tag, char c) |
| 508 |
{ |
| 509 |
this->self().beginTag(tag); |
| 510 |
this->self().saveChar(c); |
| 511 |
this->self().endTag(tag); |
| 512 |
} |
| 513 |
|
| 514 |
protected: |
| 515 |
OutputArchiveBase() = default; |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
class InputArchiveBase2 |
| 521 |
{ |
| 522 |
public: |
| 523 |
static constexpr bool IS_LOADER = true; |
| 524 |
|
| 525 |
void beginSection() const |
| 526 |
{ |
| 527 |
UNREACHABLE; |
| 528 |
} |
| 529 |
void endSection() const |
| 530 |
{ |
| 531 |
UNREACHABLE; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
[[nodiscard]] void* getPointer(unsigned id); |
| 536 |
void addPointer(unsigned id, const void* p); |
| 537 |
[[nodiscard]] unsigned getId(const void* p) const; |
| 538 |
|
| 539 |
template<typename T> void resetSharedPtr(std::shared_ptr<T>& s, T* r) |
| 540 |
{ |
| 541 |
if (!r) { |
| 542 |
s.reset(); |
| 543 |
return; |
| 544 |
} |
| 545 |
auto it = sharedPtrMap.find(r); |
| 546 |
if (it == end(sharedPtrMap)) { |
| 547 |
s.reset(r); |
| 548 |
sharedPtrMap.emplace_noDuplicateCheck(r, s); |
| 549 |
} else { |
| 550 |
s = std::static_pointer_cast<T>(it->second); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
protected: |
| 555 |
InputArchiveBase2() = default; |
| 556 |
|
| 557 |
private: |
| 558 |
hash_map<unsigned, void*> idMap; |
| 559 |
hash_map<void*, std::shared_ptr<void>> sharedPtrMap; |
| 560 |
}; |
| 561 |
|
| 562 |
template<typename Derived> |
| 563 |
class InputArchiveBase : public ArchiveBase<Derived>, public InputArchiveBase2 |
| 564 |
{ |
| 565 |
public: |
| 566 |
template<typename T, typename... Args> |
| 567 |
void serializeWithID(const char* tag, T& t, Args... args) |
| 568 |
{ |
| 569 |
doSerialize(tag, t, std::tuple<Args...>(args...)); |
| 570 |
} |
| 571 |
|
| 572 |
template<typename T> |
| 573 |
void serialize(const char* tag, T& t) |
| 574 |
{ |
| 575 |
this->self().beginTag(tag); |
| 576 |
using TNC = std::remove_const_t<T>; |
| 577 |
auto& tnc = const_cast<TNC&>(t); |
| 578 |
Loader<TNC> loader; |
| 579 |
loader(this->self(), tnc, std::tuple<>(), -1); |
| 580 |
this->self().endTag(tag); |
| 581 |
} |
| 582 |
template<typename T> void serializePointerID(const char* tag, const T& t) |
| 583 |
{ |
| 584 |
this->self().beginTag(tag); |
| 585 |
using TNC = std::remove_const_t<T>; |
| 586 |
auto& tnc = const_cast<TNC&>(t); |
| 587 |
IDLoader<TNC> loader; |
| 588 |
loader(this->self(), tnc); |
| 589 |
this->self().endTag(tag); |
| 590 |
} |
| 591 |
template<typename T> void serializePolymorphic(const char* tag, T& t) |
| 592 |
{ |
| 593 |
static_assert(std::is_polymorphic_v<T>, |
| 594 |
"must be a polymorphic type"); |
| 595 |
PolymorphicInitializerRegistry<Derived>::init(tag, this->self(), &t); |
| 596 |
} |
| 597 |
template<typename T> void serializeOnlyOnce(const char* tag, const T& t) |
| 598 |
{ |
| 599 |
if (!getId(&t)) { |
| 600 |
serializeWithID(tag, t); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
void serializeChar(const char* tag, char& c) |
| 606 |
{ |
| 607 |
this->self().beginTag(tag); |
| 608 |
this->self().loadChar(c); |
| 609 |
this->self().endTag(tag); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
template<typename T, typename TUPLE> |
| 615 |
void doSerialize(const char* tag, T& t, TUPLE args, int id = 0) |
| 616 |
{ |
| 617 |
this->self().beginTag(tag); |
| 618 |
using TNC = std::remove_const_t<T>; |
| 619 |
auto& tnc = const_cast<TNC&>(t); |
| 620 |
Loader<TNC> loader; |
| 621 |
loader(this->self(), tnc, args, id); |
| 622 |
this->self().endTag(tag); |
| 623 |
} |
| 624 |
|
| 625 |
protected: |
| 626 |
InputArchiveBase() = default; |
| 627 |
}; |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
template<typename T> struct SerializeAsMemcpy : std::false_type {}; |
| 633 |
template<> struct SerializeAsMemcpy< bool > : std::true_type {}; |
| 634 |
template<> struct SerializeAsMemcpy< char > : std::true_type {}; |
| 635 |
template<> struct SerializeAsMemcpy< signed char > : std::true_type {}; |
| 636 |
template<> struct SerializeAsMemcpy<unsigned char > : std::true_type {}; |
| 637 |
template<> struct SerializeAsMemcpy< short > : std::true_type {}; |
| 638 |
template<> struct SerializeAsMemcpy<unsigned short > : std::true_type {}; |
| 639 |
template<> struct SerializeAsMemcpy< int > : std::true_type {}; |
| 640 |
template<> struct SerializeAsMemcpy<unsigned int > : std::true_type {}; |
| 641 |
template<> struct SerializeAsMemcpy< long > : std::true_type {}; |
| 642 |
template<> struct SerializeAsMemcpy<unsigned long > : std::true_type {}; |
| 643 |
template<> struct SerializeAsMemcpy< long long> : std::true_type {}; |
| 644 |
template<> struct SerializeAsMemcpy<unsigned long long> : std::true_type {}; |
| 645 |
template<> struct SerializeAsMemcpy< float > : std::true_type {}; |
| 646 |
template<> struct SerializeAsMemcpy< double > : std::true_type {}; |
| 647 |
template<> struct SerializeAsMemcpy< long double > : std::true_type {}; |
| 648 |
template<typename T, size_t N> struct SerializeAsMemcpy<std::array<T, N>> : SerializeAsMemcpy<T> {}; |
| 649 |
|
| 650 |
class MemOutputArchive final : public OutputArchiveBase<MemOutputArchive> |
| 651 |
{ |
| 652 |
public: |
| 653 |
MemOutputArchive(LastDeltaBlocks& lastDeltaBlocks_, |
| 654 |
std::vector<std::shared_ptr<DeltaBlock>>& deltaBlocks_, |
| 655 |
bool reverseSnapshot_) |
| 656 |
: lastDeltaBlocks(lastDeltaBlocks_) |
| 657 |
, deltaBlocks(deltaBlocks_) |
| 658 |
, reverseSnapshot(reverseSnapshot_) |
| 659 |
{ |
| 660 |
} |
| 661 |
|
| 662 |
~MemOutputArchive() |
| 663 |
{ |
| 664 |
assert(openSections.empty()); |
| 665 |
} |
| 666 |
|
| 667 |
static constexpr bool NEED_VERSION = false; |
| 668 |
[[nodiscard]] bool isReverseSnapshot() const { return reverseSnapshot; } |
| 669 |
|
| 670 |
template<typename T> void save(const T& t) |
| 671 |
{ |
| 672 |
buffer.insert(&t, sizeof(t)); |
| 673 |
} |
| 674 |
void saveChar(char c) |
| 675 |
{ |
| 676 |
save(c); |
| 677 |
} |
| 678 |
void save(const std::string& s) { save(std::string_view(s)); } |
| 679 |
void save(zstring_view s) { save(std::string_view(s)); } |
| 680 |
void save(std::string_view s); |
| 681 |
void serialize_blob(const char* tag, std::span<const uint8_t> data, |
| 682 |
bool diff = true); |
| 683 |
|
| 684 |
using OutputArchiveBase<MemOutputArchive>::serialize; |
| 685 |
template<typename T, typename ...Args> |
| 686 |
ALWAYS_INLINE void serialize(const char* tag, const T& t, Args&& ...args) |
| 687 |
{ |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
serialize_group(std::tuple<>(), tag, t, std::forward<Args>(args)...); |
| 695 |
} |
| 696 |
template<typename T, size_t N> |
| 697 |
ALWAYS_INLINE void serialize(const char* , const std::array<T, N>& t) |
| 698 |
requires(SerializeAsMemcpy<T>::value) |
| 699 |
{ |
| 700 |
buffer.insert(t.data(), N * sizeof(T)); |
| 701 |
} |
| 702 |
|
| 703 |
void beginSection() |
| 704 |
{ |
| 705 |
size_t skip = 0; |
| 706 |
save(skip); |
| 707 |
size_t beginPos = buffer.getPosition(); |
| 708 |
openSections.push_back(beginPos); |
| 709 |
} |
| 710 |
void endSection() |
| 711 |
{ |
| 712 |
assert(!openSections.empty()); |
| 713 |
size_t endPos = buffer.getPosition(); |
| 714 |
size_t beginPos = openSections.back(); |
| 715 |
openSections.pop_back(); |
| 716 |
size_t skip = endPos - beginPos; |
| 717 |
buffer.insertAt(beginPos - sizeof(skip), |
| 718 |
&skip, sizeof(skip)); |
| 719 |
} |
| 720 |
|
| 721 |
[[nodiscard]] MemBuffer<uint8_t> releaseBuffer() && |
| 722 |
{ |
| 723 |
return std::move(buffer).release(); |
| 724 |
} |
| 725 |
|
| 726 |
private: |
| 727 |
ALWAYS_INLINE void serialize_group(const std::tuple<>& ) const |
| 728 |
{ |
| 729 |
|
| 730 |
} |
| 731 |
template<typename ...Args> |
| 732 |
ALWAYS_INLINE void serialize_group(const std::tuple<Args...>& tuple) |
| 733 |
{ |
| 734 |
|
| 735 |
buffer.insert_tuple_ptr(tuple); |
| 736 |
} |
| 737 |
template<typename TUPLE, typename T, typename ...Args> |
| 738 |
ALWAYS_INLINE void serialize_group(const TUPLE& tuple, const char* tag, const T& t, Args&& ...args) |
| 739 |
{ |
| 740 |
|
| 741 |
if constexpr (SerializeAsMemcpy<T>::value) { |
| 742 |
|
| 743 |
(void)tag; |
| 744 |
serialize_group(std::tuple_cat(tuple, std::tuple(&t)), std::forward<Args>(args)...); |
| 745 |
} else { |
| 746 |
serialize(tag, t); |
| 747 |
serialize_group(tuple, std::forward<Args>(args)...); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
private: |
| 752 |
OutputBuffer buffer; |
| 753 |
std::vector<size_t> openSections; |
| 754 |
LastDeltaBlocks& lastDeltaBlocks; |
| 755 |
std::vector<std::shared_ptr<DeltaBlock>>& deltaBlocks; |
| 756 |
const bool reverseSnapshot; |
| 757 |
}; |
| 758 |
|
| 759 |
class MemInputArchive final : public InputArchiveBase<MemInputArchive> |
| 760 |
{ |
| 761 |
public: |
| 762 |
MemInputArchive(std::span<const uint8_t> buf_, |
| 763 |
std::span<const std::shared_ptr<DeltaBlock>> deltaBlocks_) |
| 764 |
: buffer(buf_) |
| 765 |
, deltaBlocks(deltaBlocks_) |
| 766 |
{ |
| 767 |
} |
| 768 |
|
| 769 |
static constexpr bool NEED_VERSION = false; |
| 770 |
[[nodiscard]] bool versionAtLeast(unsigned , unsigned ) const |
| 771 |
{ |
| 772 |
return true; |
| 773 |
} |
| 774 |
[[nodiscard]] bool versionBelow(unsigned , unsigned ) const |
| 775 |
{ |
| 776 |
return false; |
| 777 |
} |
| 778 |
|
| 779 |
template<typename T> void load(T& t) |
| 780 |
{ |
| 781 |
buffer.read(&t, sizeof(t)); |
| 782 |
} |
| 783 |
void loadChar(char& c) |
| 784 |
{ |
| 785 |
load(c); |
| 786 |
} |
| 787 |
void load(std::string& s); |
| 788 |
[[nodiscard]] std::string_view loadStr(); |
| 789 |
void serialize_blob(const char* tag, std::span<uint8_t> data, |
| 790 |
bool diff = true); |
| 791 |
|
| 792 |
using InputArchiveBase<MemInputArchive>::serialize; |
| 793 |
template<typename T, typename ...Args> |
| 794 |
ALWAYS_INLINE void serialize(const char* tag, T& t, Args&& ...args) |
| 795 |
{ |
| 796 |
|
| 797 |
serialize_group(std::tuple<>(), tag, t, std::forward<Args>(args)...); |
| 798 |
} |
| 799 |
|
| 800 |
template<typename T, size_t N> |
| 801 |
ALWAYS_INLINE void serialize(const char* , std::array<T, N>& t) |
| 802 |
requires(SerializeAsMemcpy<T>::value) |
| 803 |
{ |
| 804 |
buffer.read(t.data(), N * sizeof(T)); |
| 805 |
} |
| 806 |
|
| 807 |
void skipSection(bool skip) |
| 808 |
{ |
| 809 |
size_t num; |
| 810 |
load(num); |
| 811 |
if (skip) { |
| 812 |
buffer.skip(num); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
private: |
| 817 |
|
| 818 |
template<typename TUPLE> |
| 819 |
ALWAYS_INLINE void serialize_group(const TUPLE& tuple) |
| 820 |
{ |
| 821 |
auto read = [&](auto* p) { buffer.read(p, sizeof(*p)); }; |
| 822 |
std::apply([&](auto&&... args) { (read(args), ...); }, tuple); |
| 823 |
} |
| 824 |
template<typename TUPLE, typename T, typename ...Args> |
| 825 |
ALWAYS_INLINE void serialize_group(const TUPLE& tuple, const char* tag, T& t, Args&& ...args) |
| 826 |
{ |
| 827 |
if constexpr (SerializeAsMemcpy<T>::value) { |
| 828 |
(void)tag; |
| 829 |
serialize_group(std::tuple_cat(tuple, std::tuple(&t)), std::forward<Args>(args)...); |
| 830 |
} else { |
| 831 |
serialize(tag, t); |
| 832 |
serialize_group(tuple, std::forward<Args>(args)...); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
private: |
| 837 |
InputBuffer buffer; |
| 838 |
std::span<const std::shared_ptr<DeltaBlock>> deltaBlocks; |
| 839 |
}; |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
class XmlOutputArchive final : public OutputArchiveBase<XmlOutputArchive> |
| 844 |
{ |
| 845 |
public: |
| 846 |
explicit XmlOutputArchive(zstring_view filename); |
| 847 |
void close(); |
| 848 |
~XmlOutputArchive(); |
| 849 |
|
| 850 |
template<typename T> void saveImpl(const T& t) |
| 851 |
{ |
| 852 |
|
| 853 |
|
| 854 |
save(std::string_view(tmpStrCat(t))); |
| 855 |
} |
| 856 |
template<typename T> void save(const T& t) |
| 857 |
{ |
| 858 |
saveImpl(t); |
| 859 |
} |
| 860 |
void saveChar(char c); |
| 861 |
void save(std::string_view str); |
| 862 |
void save(bool b); |
| 863 |
void save(unsigned char b); |
| 864 |
void save(signed char c); |
| 865 |
void save(char c); |
| 866 |
void save(int i); |
| 867 |
void save(unsigned u); |
| 868 |
void save(unsigned long long ull); |
| 869 |
|
| 870 |
void beginSection() const { } |
| 871 |
void endSection() const { } |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
using OutputArchiveBase<XmlOutputArchive>::serialize; |
| 876 |
template<typename T, typename ...Args> |
| 877 |
ALWAYS_INLINE void serialize(const char* tag, const T& t, Args&& ...args) |
| 878 |
{ |
| 879 |
|
| 880 |
this->self().serialize(tag, t); |
| 881 |
this->self().serialize(std::forward<Args>(args)...); |
| 882 |
} |
| 883 |
|
| 884 |
void serialize_blob(const char* tag, std::span<const uint8_t> data, |
| 885 |
bool diff = true); |
| 886 |
|
| 887 |
auto& getXMLOutputStream() { return writer; } |
| 888 |
|
| 889 |
|
| 890 |
static constexpr bool TRANSLATE_ENUM_TO_STRING = true; |
| 891 |
static constexpr bool CAN_HAVE_OPTIONAL_ATTRIBUTES = true; |
| 892 |
static constexpr bool CAN_COUNT_CHILDREN = true; |
| 893 |
|
| 894 |
void beginTag(const char* tag); |
| 895 |
void endTag(const char* tag); |
| 896 |
|
| 897 |
template<typename T> void attributeImpl(const char* name, const T& t) |
| 898 |
{ |
| 899 |
attribute(name, std::string_view(tmpStrCat(t))); |
| 900 |
} |
| 901 |
template<typename T> void attribute(const char* name, const T& t) |
| 902 |
{ |
| 903 |
attributeImpl(name, t); |
| 904 |
} |
| 905 |
void attribute(const char* name, std::string_view str); |
| 906 |
void attribute(const char* name, int i); |
| 907 |
void attribute(const char* name, unsigned u); |
| 908 |
|
| 909 |
|
| 910 |
void write(std::span<const char> buf); |
| 911 |
void write1(char c); |
| 912 |
void check(bool condition) const; |
| 913 |
[[noreturn]] void error(); |
| 914 |
|
| 915 |
private: |
| 916 |
zstring_view filename; |
| 917 |
gzFile file = nullptr; |
| 918 |
XMLOutputStream<XmlOutputArchive> writer; |
| 919 |
}; |
| 920 |
|
| 921 |
class XmlInputArchive final : public InputArchiveBase<XmlInputArchive> |
| 922 |
{ |
| 923 |
public: |
| 924 |
explicit XmlInputArchive(zstring_view filename); |
| 925 |
|
| 926 |
[[nodiscard]] bool versionAtLeast(unsigned actual, unsigned required) const |
| 927 |
{ |
| 928 |
return actual >= required; |
| 929 |
} |
| 930 |
[[nodiscard]] bool versionBelow(unsigned actual, unsigned required) const |
| 931 |
{ |
| 932 |
return actual < required; |
| 933 |
} |
| 934 |
|
| 935 |
void loadChar(char& c) const; |
| 936 |
void load(bool& b) const; |
| 937 |
void load(unsigned char& b) const; |
| 938 |
void load(signed char& c) const; |
| 939 |
void load(char& c) const; |
| 940 |
void load(int& i) const; |
| 941 |
void load(unsigned& u) const; |
| 942 |
void load(unsigned long long& ull) const; |
| 943 |
void load(std::string& t) const; |
| 944 |
template<typename T> void load(T& t) const |
| 945 |
{ |
| 946 |
std::string str; |
| 947 |
load(str); |
| 948 |
std::istringstream is(str); |
| 949 |
is >> t; |
| 950 |
} |
| 951 |
[[nodiscard]] std::string_view loadStr() const; |
| 952 |
|
| 953 |
void serialize_blob(const char* tag, std::span<uint8_t> data, |
| 954 |
bool diff = true); |
| 955 |
|
| 956 |
void skipSection(bool ) const { } |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
using InputArchiveBase<XmlInputArchive>::serialize; |
| 961 |
template<typename T, typename ...Args> |
| 962 |
ALWAYS_INLINE void serialize(const char* tag, T& t, Args&& ...args) |
| 963 |
{ |
| 964 |
|
| 965 |
this->self().serialize(tag, t); |
| 966 |
this->self().serialize(std::forward<Args>(args)...); |
| 967 |
} |
| 968 |
|
| 969 |
[[nodiscard]] XMLElement* currentElement() const { |
| 970 |
return elems.back().first; |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
static constexpr bool TRANSLATE_ENUM_TO_STRING = true; |
| 975 |
static constexpr bool CAN_HAVE_OPTIONAL_ATTRIBUTES = true; |
| 976 |
static constexpr bool CAN_COUNT_CHILDREN = true; |
| 977 |
|
| 978 |
void beginTag(const char* tag); |
| 979 |
void endTag(const char* tag); |
| 980 |
|
| 981 |
template<typename T> void attributeImpl(const char* name, T& t) const |
| 982 |
{ |
| 983 |
std::string str; |
| 984 |
attribute(name, str); |
| 985 |
std::istringstream is(str); |
| 986 |
is >> t; |
| 987 |
} |
| 988 |
template<typename T> void attribute(const char* name, T& t) const |
| 989 |
{ |
| 990 |
attributeImpl(name, t); |
| 991 |
} |
| 992 |
void attribute(const char* name, std::string& t) const; |
| 993 |
void attribute(const char* name, int& i) const; |
| 994 |
void attribute(const char* name, unsigned& u) const; |
| 995 |
|
| 996 |
[[nodiscard]] bool hasAttribute(const char* name) const; |
| 997 |
[[nodiscard]] int countChildren() const; |
| 998 |
|
| 999 |
template<typename T> |
| 1000 |
std::optional<T> findAttributeAs(const char* name) |
| 1001 |
{ |
| 1002 |
if (const auto* attr = currentElement()->findAttribute(name)) { |
| 1003 |
return StringOp::stringTo<T>(attr->getValue()); |
| 1004 |
} |
| 1005 |
return {}; |
| 1006 |
} |
| 1007 |
|
| 1008 |
private: |
| 1009 |
XMLDocument xmlDoc{16384}; |
| 1010 |
std::vector<std::pair<XMLElement*, XMLElement*>> elems; |
| 1011 |
}; |
| 1012 |
|
| 1013 |
#define INSTANTIATE_SERIALIZE_METHODS(CLASS) \ |
| 1014 |
template void CLASS::serialize(MemInputArchive&, unsigned); \ |
| 1015 |
template void CLASS::serialize(MemOutputArchive&, unsigned); \ |
| 1016 |
template void CLASS::serialize(XmlInputArchive&, unsigned); \ |
| 1017 |
template void CLASS::serialize(XmlOutputArchive&, unsigned); |
| 1018 |
|
| 1019 |
} |
| 1020 |
|
| 1021 |
#endif |