EDIT:// Got it working thanks to the answer below, adding the code that is currently working and test case just in case some one might find it useful.
// 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();
}
Below is the code I used to test, it correctly outputs all the elements of the vector.
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;
}
Thanks to Barmak Shemirani for the help.