From 0569c29b19e4d23f055845a167c706f11590fa2a Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 19 八月 2025 15:55:00 +0800
Subject: [PATCH] 1.CControlJob和CProcessJob的序列化和反序列化;
---
SourceCode/Bond/Servo/CControlJob.cpp | 80 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 80 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/Servo/CControlJob.cpp b/SourceCode/Bond/Servo/CControlJob.cpp
index 3f0237b..e88f816 100644
--- a/SourceCode/Bond/Servo/CControlJob.cpp
+++ b/SourceCode/Bond/Servo/CControlJob.cpp
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "CControlJob.h"
#include <cctype>
+#include "SerializeUtil.h"
static inline std::string trimCopy(std::string s) {
auto notspace = [](int ch) { return !std::isspace(ch); };
@@ -10,6 +11,11 @@
}
namespace SERVO {
+ CControlJob::CControlJob()
+ {
+
+ }
+
CControlJob::CControlJob(std::string cjId)
: m_cjId(trimCopy(std::move(cjId)))
{
@@ -221,4 +227,78 @@
return c >= 0x20 && c <= 0x7E;
});
}
+
+ void CControlJob::serialize(std::ostream& os) const {
+ write_pod(os, CJ_MAGIC);
+ write_pod(os, CJ_VERSION);
+
+ // 标识/优先级/状态/失败原因
+ write_string(os, id()); // 或 m_cjId
+ write_pod<uint8_t>(os, priority()); // 或 m_priority
+ write_pod<uint8_t>(os, static_cast<uint8_t>(state())); // 或 m_state
+ write_string(os, failReason()); // 或 m_failReason
+
+ // 时间戳
+ write_opt_time(os, tQueued());
+ write_opt_time(os, tStart());
+ write_opt_time(os, tEnd());
+
+ // 关联 PJ 列表
+ write_vec_str(os, pjIds()); // 或 m_pjIds
+ }
+
+ bool CControlJob::deserialize(std::istream& is, CControlJob& out, std::string* err) {
+ auto fail = [&](const char* msg) { if (err) *err = msg; return false; };
+
+ uint32_t magic = 0; if (!read_pod(is, magic)) return fail("read CJ magic");
+ if (magic != CJ_MAGIC) return fail("bad CJ magic");
+
+ uint16_t ver = 0; if (!read_pod(is, ver)) return fail("read CJ version");
+ if (ver != CJ_VERSION) return fail("unsupported CJ version");
+
+ std::string cjId;
+ if (!read_string(is, cjId)) return fail("read CJID");
+
+ uint8_t prio = 0;
+ if (!read_pod(is, prio)) return fail("read Priority");
+
+ uint8_t st = 0;
+ if (!read_pod(is, st)) return fail("read State");
+
+ std::string failText;
+ if (!read_string(is, failText)) return fail("read failReason");
+
+ std::optional<std::chrono::system_clock::time_point> tQ, tS, tE;
+ if (!read_opt_time(is, tQ)) return fail("read tQueued");
+ if (!read_opt_time(is, tS)) return fail("read tStart");
+ if (!read_opt_time(is, tE)) return fail("read tEnd");
+
+ std::vector<std::string> pjIds;
+ if (!read_vec_str(is, pjIds)) return fail("read PJIDs");
+
+ // —— 写回对象(直接改成员,或通过 setter)——
+ // 若你有 setter:out.setId(...)/setPriority(...)/setState(...)/setFailReason(...)
+ out = CControlJob(cjId);
+ out.setPriority(prio);
+
+ // 直接恢复内部状态(若你要求走状态机,可在这里按合法过渡调用 queue()/start()/...)
+ // 简化:直接赋值(你在 CControlJob.cpp 内部,可访问私有成员)
+ struct Access : CControlJob {
+ using CControlJob::m_state;
+ using CControlJob::m_failReason;
+ using CControlJob::m_tQueued;
+ using CControlJob::m_tStart;
+ using CControlJob::m_tEnd;
+ using CControlJob::m_pjIds;
+ };
+ auto& a = reinterpret_cast<Access&>(out);
+ a.m_state = static_cast<CJState>(st);
+ a.m_failReason = std::move(failText);
+ a.m_tQueued = std::move(tQ);
+ a.m_tStart = std::move(tS);
+ a.m_tEnd = std::move(tE);
+ a.m_pjIds = std::move(pjIds);
+
+ return true;
+ }
}
--
Gitblit v1.9.3