路泰科技体检小程序UI设计新版本
1
wwl
2025-07-30 61b58bd03d04d2eb50ac2d93a188c819fe67e01e
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
156
157
158
159
160
161
162
163
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <stdint.h>
#include "./BSER.hh"
 
BSERType decodeType(std::istream &iss) {
  int8_t type;
  iss.read(reinterpret_cast<char*>(&type), sizeof(type));
  return (BSERType) type;
}
 
void expectType(std::istream &iss, BSERType expected) {
  BSERType got = decodeType(iss);
  if (got != expected) {
    throw std::runtime_error("Unexpected BSER type");
  }
}
 
void encodeType(std::ostream &oss, BSERType type) {
  int8_t t = (int8_t)type;
  oss.write(reinterpret_cast<char*>(&t), sizeof(t));
}
 
template<typename T>
class Value : public BSERValue {
public:
  T value;
  Value(T val) {
    value = val;
  }
 
  Value() {}
};
 
class BSERInteger : public Value<int64_t> {
public:
  BSERInteger(int64_t value) : Value(value) {}
  BSERInteger(std::istream &iss) {
    int8_t int8;
    int16_t int16;
    int32_t int32;
    int64_t int64;
 
    BSERType type = decodeType(iss);
 
    switch (type) {
      case BSER_INT8:
        iss.read(reinterpret_cast<char*>(&int8), sizeof(int8));
        value = int8;
        break;
      case BSER_INT16:
        iss.read(reinterpret_cast<char*>(&int16), sizeof(int16));
        value = int16;
        break;
      case BSER_INT32:
        iss.read(reinterpret_cast<char*>(&int32), sizeof(int32));
        value = int32;
        break;
      case BSER_INT64:
        iss.read(reinterpret_cast<char*>(&int64), sizeof(int64));
        value = int64;
        break;
      default:
        throw std::runtime_error("Invalid BSER int type");
    }
  }
 
  int64_t intValue() override {
    return value;
  }
 
  void encode(std::ostream &oss) override {
    if (value <= INT8_MAX) {
      encodeType(oss, BSER_INT8);
      int8_t v = (int8_t)value;
      oss.write(reinterpret_cast<char*>(&v), sizeof(v));
    } else if (value <= INT16_MAX) {
      encodeType(oss, BSER_INT16);
      int16_t v = (int16_t)value;
      oss.write(reinterpret_cast<char*>(&v), sizeof(v));
    } else if (value <= INT32_MAX) {
      encodeType(oss, BSER_INT32);
      int32_t v = (int32_t)value;
      oss.write(reinterpret_cast<char*>(&v), sizeof(v));
    } else {
      encodeType(oss, BSER_INT64);
      oss.write(reinterpret_cast<char*>(&value), sizeof(value));
    }
  }
};
 
class BSERArray : public Value<BSER::Array> {
public:
  BSERArray() : Value() {}
  BSERArray(BSER::Array value) : Value(value) {}
  BSERArray(std::istream &iss) {
    expectType(iss, BSER_ARRAY);
    int64_t len = BSERInteger(iss).intValue();
    for (int64_t i = 0; i < len; i++) {
      value.push_back(BSER(iss));
    }
  }
  
  BSER::Array arrayValue() override {
    return value;
  }
 
  void encode(std::ostream &oss) override {
    encodeType(oss, BSER_ARRAY);
    BSERInteger(value.size()).encode(oss);
    for (auto it = value.begin(); it != value.end(); it++) {
      it->encode(oss);
    }
  }
};
 
class BSERString : public Value<std::string> {
public:
  BSERString(std::string value) : Value(value) {}
  BSERString(std::istream &iss) {
    expectType(iss, BSER_STRING);
    int64_t len = BSERInteger(iss).intValue();
    value.resize(len);
    iss.read(&value[0], len);
  }
 
  std::string stringValue() override {
    return value;
  }
 
  void encode(std::ostream &oss) override {
    encodeType(oss, BSER_STRING);
    BSERInteger(value.size()).encode(oss);
    oss << value;
  }
};
 
class BSERObject : public Value<BSER::Object> {
public:
  BSERObject() : Value() {}
  BSERObject(BSER::Object value) : Value(value) {}
  BSERObject(std::istream &iss) {
    expectType(iss, BSER_OBJECT);
    int64_t len = BSERInteger(iss).intValue();
    for (int64_t i = 0; i < len; i++) {
      auto key = BSERString(iss).stringValue();
      auto val = BSER(iss);
      value.emplace(key, val);
    }
  }
 
  BSER::Object objectValue() override {
    return value;
  }
 
  void encode(std::ostream &oss) override {
    encodeType(oss, BSER_OBJECT);
    BSERInteger(value.size()).encode(oss);
    for (auto it = value.begin(); it != value.end(); it++) {
      BSERString(it->first).encode(oss);
      it->second.encode(oss);
    }
  }
};
 
class BSERDouble : public Value<double> {
public:
  BSERDouble(double value) : Value(value) {}
  BSERDouble(std::istream &iss) {
    expectType(iss, BSER_REAL);
    iss.read(reinterpret_cast<char*>(&value), sizeof(value));
  }
 
  double doubleValue() override {
    return value;
  }
 
  void encode(std::ostream &oss) override {
    encodeType(oss, BSER_REAL);
    oss.write(reinterpret_cast<char*>(&value), sizeof(value));
  }
};
 
class BSERBoolean : public Value<bool> {
public:
  BSERBoolean(bool value) : Value(value) {}
  bool boolValue() override { return value; }
  void encode(std::ostream &oss) override {
    int8_t t = value == true ? BSER_BOOL_TRUE : BSER_BOOL_FALSE;
    oss.write(reinterpret_cast<char*>(&t), sizeof(t));
  }
};
 
class BSERNull : public Value<bool> {
public:
  BSERNull() : Value(false) {}
  void encode(std::ostream &oss) override {
    encodeType(oss, BSER_NULL);
  }
};
 
std::shared_ptr<BSERArray> decodeTemplate(std::istream &iss) {
  expectType(iss, BSER_TEMPLATE);
  auto keys = BSERArray(iss).arrayValue();
  auto len = BSERInteger(iss).intValue();
  std::shared_ptr<BSERArray> arr = std::make_shared<BSERArray>();
  for (int64_t i = 0; i < len; i++) {
    BSER::Object obj;
    for (auto it = keys.begin(); it != keys.end(); it++) {
      if (iss.peek() == 0x0c) {
        iss.ignore(1);
        continue;
      }
 
      auto val = BSER(iss);
      obj.emplace(it->stringValue(), val);
    }
    arr->value.push_back(obj);
  }
  return arr;
}
 
BSER::BSER(std::istream &iss) {
  BSERType type = decodeType(iss);
  iss.unget();
 
  switch (type) {
    case BSER_ARRAY:
      m_ptr = std::make_shared<BSERArray>(iss);
      break;
    case BSER_OBJECT:
      m_ptr = std::make_shared<BSERObject>(iss);
      break;
    case BSER_STRING:
      m_ptr = std::make_shared<BSERString>(iss);
      break;
    case BSER_INT8:
    case BSER_INT16:
    case BSER_INT32:
    case BSER_INT64:
      m_ptr = std::make_shared<BSERInteger>(iss);
      break;
    case BSER_REAL:
      m_ptr = std::make_shared<BSERDouble>(iss);
      break;
    case BSER_BOOL_TRUE:
      iss.ignore(1);
      m_ptr = std::make_shared<BSERBoolean>(true);
      break;
    case BSER_BOOL_FALSE:
      iss.ignore(1);
      m_ptr = std::make_shared<BSERBoolean>(false);
      break;
    case BSER_NULL:
      iss.ignore(1);
      m_ptr = std::make_shared<BSERNull>();
      break;
    case BSER_TEMPLATE:
      m_ptr = decodeTemplate(iss);
      break;
    default:
      throw std::runtime_error("unknown BSER type");
  }
}
 
BSER::BSER() : m_ptr(std::make_shared<BSERNull>()) {}
BSER::BSER(BSER::Array value) : m_ptr(std::make_shared<BSERArray>(value)) {}
BSER::BSER(BSER::Object value) : m_ptr(std::make_shared<BSERObject>(value)) {}
BSER::BSER(const char *value) : m_ptr(std::make_shared<BSERString>(value)) {}
BSER::BSER(std::string value) : m_ptr(std::make_shared<BSERString>(value)) {}
BSER::BSER(int64_t value) : m_ptr(std::make_shared<BSERInteger>(value)) {}
BSER::BSER(double value) : m_ptr(std::make_shared<BSERDouble>(value)) {}
BSER::BSER(bool value) : m_ptr(std::make_shared<BSERBoolean>(value)) {}
 
BSER::Array BSER::arrayValue() { return m_ptr->arrayValue(); }
BSER::Object BSER::objectValue() { return m_ptr->objectValue(); }
std::string BSER::stringValue() { return m_ptr->stringValue(); }
int64_t BSER::intValue() { return m_ptr->intValue(); }
double BSER::doubleValue() { return m_ptr->doubleValue(); }
bool BSER::boolValue() { return m_ptr->boolValue(); }
void BSER::encode(std::ostream &oss) {
  m_ptr->encode(oss);
}
 
int64_t BSER::decodeLength(std::istream &iss) {
  char pdu[2];
  if (!iss.read(pdu, 2) || pdu[0] != 0 || pdu[1] != 1) {
    throw std::runtime_error("Invalid BSER");
  }
 
  return BSERInteger(iss).intValue();
}
 
std::string BSER::encode() {
  std::ostringstream oss(std::ios_base::binary);
  encode(oss);
 
  std::ostringstream res(std::ios_base::binary);
  res.write("\x00\x01", 2);
  
  BSERInteger(oss.str().size()).encode(res);
  res << oss.str();
  return res.str();
}