假设我有这样的功能
int writetofile(wstring name, any sdata){
...
return error;
}
此函数不知道将存储哪些数据,但需要知道存储在sdata
中的数据的大小。虽然很容易确定存储在sdata
中的数据类型,但我认为有一些简单的方法来了解sdata
中的数据大小。
我有一个数据结构,其成员类型为wstring
。现在我们不能将该数据结构直接写入文件,因为它包含wstring。就我在互联网上研究而言,编写wstring
或string
的最佳方法是首先写出字符串的大小,然后是字符串。然后当我读取字符串时首先读取大小然后读取那么多的大小。
为此,我已经发挥了作用。
int filemanager::write(any data, fileid uid, DWORD *byteswritten) const
{
// files is a map<fileid, fileinfo> where fileinfo is a struct which has
// members including file's name and handle
// fileid is a typedef of int
if (!files.count(uid)) return -1;
if (!data.has_value()) return -2;
if (data.type() == typeid(wstring)) {
DWORD sz1, sz2;
wstring str = any_cast<wstring>(data);
size_t sz3 = str.length()*sizeof(wchar_t);
if (sz3 == 0) return -2;
if (FALSE == WriteFile(files[uid].handle, &sz3, sizeof(size_t), &sz1, NULL)){
return GetLastError();
}
if (FALSE == WriteFile(files[uid].handle, str.c_str(), sz3, &sz2, NULL) && sz2 != sz3) {
return GetLastError();
}
if (byteswritten != nullptr) *byteswritten = sz1 + sz2;
}
else {
// now if the type is not a wstring then just write it to a file
// here i would need the size of the data stored in the data
}
return 0;
}
std::any
不是你想要做的正确的工具。它是一种主要用于在A点和B点之间进行通信的工具,其中两点都知道类型是什么,但是通信需要通过一些不需要知道类型的中间代码C来实现。
如果B需要尝试一堆不同的强制转换以查看提供的值,那么any
不适合该工作。由于类型安全原因,演员出现了:如果提供了错误的any
,则有明确的故障路径。也就是说,确保A和B正确通信。它不存在,所以你可以尝试一堆不同的东西。
你不能问any
存储对象的大小是多少,因为你应该已经知道了答案。即使你不知道答案,你也永远无法有效地使用那个答案。
以你的用例为例。 any
不是TriviallyCopyable,因此将其字节直接复制到文件然后将其复制回来是不合法的C ++。即使概念上可以这样做,any
也可能只存储指向它包含的对象的指针。所以你只需要写一个指向该文件的指针。
C ++中的序列化并不像你想要的那样简单。