LAPTOP-SNT8I5JK\Boounion
2025-08-27 e693f57ec7079cc8bfee498720b7cf87a67165ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
#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;
    }
}