EDIT:// 得到它的工作感谢下面的答案,添加当前工作的代码和测试用例以防万一有人发现它有用。
// Add as another type for Cereal or inside string.hpp in Cereal includes
template<class Archive> inline
void CEREAL_SAVE_FUNCTION_NAME(Archive & ar, CString str)
{
// Save number of chars + the data
size_type size = (str.GetLength() + 1) * sizeof(TCHAR);
ar(size);
ar(binary_data(str.GetBuffer(), static_cast<std::size_t>(size)));
str.ReleaseBuffer();
}
template<class Archive> inline
void CEREAL_LOAD_FUNCTION_NAME(Archive & ar, CString & str)
{
size_type size;
ar(size);
ar(binary_data(str.GetBuffer(static_cast<std::size_t>(size)), static_cast<std::size_t>(size)));
str.ReleaseBuffer();
}
下面是我用来测试的代码,它正确输出了向量的所有元素。
class Stuff
{
public:
Stuff() {}
std::vector<CString> vec;
private:
friend class cereal::access;
template <class Archive>
void serialize(Archive & ar)
{
ar(vec);
}
};
int main()
{
Stuff myStuff, otherStuff;
myStuff.vec.push_back(L"Testing different length CStrings");
myStuff.vec.push_back(L"Separator");
myStuff.vec.push_back(L"Is it working yet??");
myStuff.vec.push_back(L"1234567890");
myStuff.vec.push_back(L"TestingTestingTestingtestingTesting");
{
std::ofstream file("out.txt", std::ios::binary);
cereal::BinaryOutputArchive output(file);
output(myStuff);
}
{
std::ifstream file("out.txt", std::ios::binary);
cereal::BinaryInputArchive input(file);
input(otherStuff);
}
int nSize = otherStuff.vec.size();
for (int x = 0; x < nSize; x++)
{
std::wcout << (LPCWSTR)otherStuff.vec[x] << std::endl;
}
return 0;
}
感谢 Barmak Shemirani 的帮助。