由于您必须处理非 POD 数据,因此您需要将结构数据展平为序列化格式,然后其他应用程序可以将其解析回其末端的结构。
您的ListOfItems
矢量可以很容易地展平,因为它的元素是 POD 数据。将vector
大小存储为整数,将其复制到缓冲区中,然后复制每个Item
(假设缓冲区足够大以容纳展平数据):
long double* Data = static_cast<long double*>(pData);
//Check if Data[1] contains a request command then copy.
unsigned char *Work = static_cast<unsigned char *>(pData);
int number = ListOfItems.size();
CopyMemory(Work, &number, sizeof(int));
Work += sizeof(int);
for (int i = 0; i < number; ++i)
{
CopyMemory(Work, &ListOfItems[i], sizeof(Item));
Work += sizeof(Item);
}
由于其中包含另一个向量,因此您的ListOfModels
向量将更难展平,但同样的概念通常适用:Model
vector
long double* Data = static_cast<long double*>(pData);
//Check if Data[1] contains a request command then copy.
unsigned char *Work = static_cast<unsigned char *>(pData);
int number = ListOfModels.size();
CopyMemory(Work, &number, sizeof(int));
Work += sizeof(int);
for (int i = 0; i < number; ++i)
{
Model &m = ListOfModels[i];
CopyMemory(Work, &(m.SX), sizeof(int) * 5);
Work += (sizeof(int) * 5);
int num = m.Vertices.size();
CopyMemory(Work, &num, sizeof(int));
Work += sizeof(int);
for (int j = 0; j < num; ++j)
{
Vector3D &v = m.Vertices[j];
// copy v as needed...
Work += ...;
}
// copy m.VBOPointer as needed ...
Work += ...;
}
我通常为这样的事情做的是使用模板来帮助简化逻辑并使其更易于阅读:
template<typename T>
void Copy(unsignd char* &Work, const T &value)
{
CopyMemory(Work, &value, sizeof(T));
}
template<template T>
void Copy(unsigned char* &Work, const std::vector<T> &vec)
{
int number = vec.size();
Copy(Work, number);
for (int i = 0; i < number; ++i)
Copy(Work, vec[i]);
}
.
long double* Data = static_cast<long double*>(pData);
//Check if Data[1] contains a request command then copy.
unsigned char *Work = static_cast<unsigned char *>(pData);
Copy(Work, ListOfItems);
.
template<>
void Copy<Vector3D>(unsigned char* &Work, const Vector3D &vec)
{
// copy vec as needed...
Work += ...;
}
template<>
void Copy<Model>(unsigned char* &Work, const Model &m)
{
Copy(Work, m.SX);
Copy(Work, m.SY);
Copy(Work, m.X);
Copy(Work, m.Y);
Copy(Work, m.Z);
Copy(Work, m.Vertices);
// copy m.VBOPointer as needed ...
Work += ...;
}
long double* Data = static_cast<long double*>(pData);
//Check if Data[1] contains a request command then copy.
unsigned char *Work = static_cast<unsigned char *>(pData);
Copy(Work, ListOfModels);