From 3e0ceaf4e569ea1f57a14de2f6135d1f1a50d080 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期三, 27 八月 2025 13:47:31 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang

---
 SourceCode/Bond/Servo/SerializeUtil.h |   72 ++++++++++++++++++++++++++++++++++++
 1 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/SerializeUtil.h b/SourceCode/Bond/Servo/SerializeUtil.h
new file mode 100644
index 0000000..fe5fdf7
--- /dev/null
+++ b/SourceCode/Bond/Servo/SerializeUtil.h
@@ -0,0 +1,72 @@
+#pragma once
+#include <fstream>
+
+
+// --------- 私有/内部:读写工具 ---------
+namespace SERVO {
+    template<typename T>
+    inline void write_pod(std::ostream& os, const T& v) {
+        os.write(reinterpret_cast<const char*>(&v), sizeof(T));
+    }
+    template<typename T>
+    inline bool read_pod(std::istream& is, T& v) {
+        return bool(is.read(reinterpret_cast<char*>(&v), sizeof(T)));
+    }
+
+    inline void write_string(std::ostream& os, const std::string& s) {
+        uint32_t n = static_cast<uint32_t>(s.size());
+        write_pod(os, n);
+        if (n) os.write(s.data(), n);
+    }
+    inline bool read_string(std::istream& is, std::string& s) {
+        uint32_t n = 0; if (!read_pod(is, n)) return false;
+        s.resize(n);
+        if (n) return bool(is.read(&s[0], n));
+        return true;
+    }
+
+    template<typename T>
+    inline void write_vec(std::ostream& os, const std::vector<T>& v) {
+        uint32_t n = static_cast<uint32_t>(v.size());
+        write_pod(os, n);
+        if (n) os.write(reinterpret_cast<const char*>(v.data()), sizeof(T) * n);
+    }
+    template<typename T>
+    inline bool read_vec(std::istream& is, std::vector<T>& v) {
+        uint32_t n = 0; if (!read_pod(is, n)) return false;
+        v.resize(n);
+        if (n) return bool(is.read(reinterpret_cast<char*>(v.data()), sizeof(T) * n));
+        return true;
+    }
+
+    // vector<string> 特化写读
+    inline void write_vec_str(std::ostream& os, const std::vector<std::string>& v) {
+        uint32_t n = static_cast<uint32_t>(v.size());
+        write_pod(os, n);
+        for (const auto& s : v) write_string(os, s);
+    }
+    inline bool read_vec_str(std::istream& is, std::vector<std::string>& v) {
+        uint32_t n = 0; if (!read_pod(is, n)) return false;
+        v.clear(); v.reserve(n);
+        for (uint32_t i = 0; i < n; ++i) { std::string s; if (!read_string(is, s)) return false; v.emplace_back(std::move(s)); }
+        return true;
+    }
+
+    // optional<time_point> → bool + int64 (ms since epoch)
+    inline void write_opt_time(std::ostream& os, const std::optional<std::chrono::system_clock::time_point>& tp) {
+        uint8_t has = tp.has_value() ? 1 : 0;
+        write_pod(os, has);
+        if (has) {
+            auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp->time_since_epoch()).count();
+            int64_t v = static_cast<int64_t>(ms);
+            write_pod(os, v);
+        }
+    }
+    inline bool read_opt_time(std::istream& is, std::optional<std::chrono::system_clock::time_point>& tp) {
+        uint8_t has = 0; if (!read_pod(is, has)) return false;
+        if (!has) { tp.reset(); return true; }
+        int64_t v = 0; if (!read_pod(is, v)) return false;
+        tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(v));
+        return true;
+    }
+}

--
Gitblit v1.9.3