我有一个 C 实现,我将变量的参数列表序列化为字节数组。因此,我可以将一定数量的变量保存到文件中。
C 中的代码如下所示:
static uint8_t byte_array[1024];
/* In this example, we assume that fmt contains only 'c', 'i', or 'f'
We also assume that an argument is available for each char in fmt.
*/
uint8_t * serialize_args(const char *fmt, ...) {
char *ptr = fmt;
uint32_t idx = 0;
va_list args;
va_start(args, fmt);
while(*ptr != NULL) {
char p = *ptr++;
char c;
int i;
float f;
switch(p) {
case 'c': /* serialize char */
c = va_arg(args, int32_t);
byte_array[idx++] = (uint8_t) c;
break;
case 'i': /* serialize int */
i = va_arg(args, int32_t);
memcpy(&byte_array[idx], &i, sizeof i);
idx += sizeof i;
break;
case 'f': /* serialize float */
f = (float)va_arg(args, double);
memcpy(&byte_array[idx], &f, sizeof f);
idx += sizeof f;
break;
}
}
va_end(args, fmt);
byte_array[idx++] = 0;
return byte_array;
}
现在假设我想用 C++ 翻译它。什么容器最适合?一个向量?然后我看起来像这样:
vector<uint8_t> serialize_args(const char *fmt, ...) {
char *ptr = fmt;
vector<uint8_t> byte_array;
va_list args;
va_start(args, fmt);
while(*ptr != NULL) {
char p = *ptr++;
char c;
int i;
float f;
switch(p) {
case 'c': // serialize char
c = (char)va_arg(args, int32_t);
byte_array.push_back(c); // do I need a cast ?
break;
case 'i': // serialize int
i = va_arg(args, int32_t);
// here how do I memcpy to a vector ?
break;
case 'f': // serialize float
f = (float)va_arg(args, double);
// here how do I memcpy to a vector ?
break;
}
}
va_end(args, fmt);
return byte_array;
}
有什么建议吗?