From 412b7710b7436cdd016fddf7941bd14901c4993a Mon Sep 17 00:00:00 2001 From: thecppzoo Date: Tue, 24 Jun 2025 08:24:07 -0700 Subject: [PATCH 1/3] Create serialization.md --- .../serialization.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 presentations/rust-traits-c++-on-sea-2025/serialization.md diff --git a/presentations/rust-traits-c++-on-sea-2025/serialization.md b/presentations/rust-traits-c++-on-sea-2025/serialization.md new file mode 100644 index 0000000..7edbe62 --- /dev/null +++ b/presentations/rust-traits-c++-on-sea-2025/serialization.md @@ -0,0 +1,27 @@ +## Example of subtyping as subclassing: Typical Serialization in C++ + +```c++ +#include + +// Infrastructure: +namespace user { + +struct ISerialize { + virtual std::ostream &serialize(std::ostream &) const; + virtual std::size_t length() const; +}; + +template +struct SerializeWrapper: ISerialize { + T value_; + virtual std::ostream &serialize(std::ostream &to) const override { + return to << type_index(typeid(T)) << ':' << value_; + } + virtual std::size_t length() const { + std::ostream temporary; + serialize(temporary); + return temporary.str().length(); + } +}; + +``` From a5c52748d8dfc38fb6929e554a912d907df607ba Mon Sep 17 00:00:00 2001 From: thecppzoo Date: Tue, 24 Jun 2025 09:01:58 -0700 Subject: [PATCH 2/3] Update serialization.md --- .../rust-traits-c++-on-sea-2025/serialization.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/presentations/rust-traits-c++-on-sea-2025/serialization.md b/presentations/rust-traits-c++-on-sea-2025/serialization.md index 7edbe62..f77b93f 100644 --- a/presentations/rust-traits-c++-on-sea-2025/serialization.md +++ b/presentations/rust-traits-c++-on-sea-2025/serialization.md @@ -1,11 +1,18 @@ ## Example of subtyping as subclassing: Typical Serialization in C++ ```c++ -#include +#include // Infrastructure: namespace user { +struct TypeRegistry { + template + std::uint64_t id() const; +}; + +extern TypeRegistry g_registry; + struct ISerialize { virtual std::ostream &serialize(std::ostream &) const; virtual std::size_t length() const; @@ -15,13 +22,14 @@ template struct SerializeWrapper: ISerialize { T value_; virtual std::ostream &serialize(std::ostream &to) const override { - return to << type_index(typeid(T)) << ':' << value_; + return + to << g_registry.id() << ':' << value_; } virtual std::size_t length() const { - std::ostream temporary; + std::ostringstream temporary; serialize(temporary); return temporary.str().length(); } }; - ``` +}``` From 1e57befceced7dc6ce745cbd5a76afdafa7d3e10 Mon Sep 17 00:00:00 2001 From: thecppzoo Date: Tue, 24 Jun 2025 09:03:06 -0700 Subject: [PATCH 3/3] Update serialization.md --- presentations/rust-traits-c++-on-sea-2025/serialization.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/presentations/rust-traits-c++-on-sea-2025/serialization.md b/presentations/rust-traits-c++-on-sea-2025/serialization.md index f77b93f..5aee853 100644 --- a/presentations/rust-traits-c++-on-sea-2025/serialization.md +++ b/presentations/rust-traits-c++-on-sea-2025/serialization.md @@ -31,5 +31,6 @@ struct SerializeWrapper: ISerialize { return temporary.str().length(); } }; + +} ``` -}```