std::any是C++17引入的通用类型容器,可以存储任意可拷贝的类型数据,但标准库没有提供它的序列化支持,因为std::any本身不记录类型的具体结构信息,直接序列化会出现数据类型丢失的问题。要实现将std::any存储的数据序列化到文件,需要自己设计类型映射和数据编码逻辑。

核心实现思路
序列化的核心是解决两个问题:记录当前std::any存储的数据类型,以及将对应类型的数据转换为可写入文件的字节流。我们可以设计一个类型注册机制,给每个需要支持序列化的类型分配唯一的类型标识,序列化时先写入类型标识,再写入数据内容,反序列化时根据类型标识还原数据。
类型注册与标识映射
首先定义一个类型标识的枚举或者字符串映射,这里用模板特化给每个类型生成唯一ID:
#include <any>
#include <unordered_map>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
// 基础类型ID生成模板
template<typename T>
struct TypeId {
static int id;
};
// 初始化静态成员
template<typename T>
int TypeId<T>::id = -1;
// 类型注册器,给每个类型分配唯一ID
class TypeRegistry {
private:
static int nextId;
static std::unordered_map<std::string, int> typeNameToId;
static std::unordered_map<int, std::string> idToTypeName;
public:
template<typename T>
static int registerType(const std::string& typeName) {
if (typeNameToId.find(typeName) != typeNameToId.end()) {
return typeNameToId[typeName];
}
int id = nextId++;
typeNameToId[typeName] = id;
idToTypeName[id] = typeName;
TypeId<T>::id = id;
return id;
}
static int getTypeId(const std::string& typeName) {
auto it = typeNameToId.find(typeName);
if (it != typeNameToId.end()) {
return it->second;
}
return -1;
}
static std::string getTypeName(int id) {
auto it = idToTypeName.find(id);
if (it != idToTypeName.end()) {
return it->second;
}
return "";
}
};
// 静态成员初始化
int TypeRegistry::nextId = 0;
std::unordered_map<std::string, int> TypeRegistry::typeNameToId;
std::unordered_map<int, std::string> TypeRegistry::idToTypeName;
// 预注册常用类型
#define REGISTER_TYPE(T) template<> int TypeId<T>::id = TypeRegistry::registerType<T>(#T)
REGISTER_TYPE(int);
REGISTER_TYPE(double);
REGISTER_TYPE(std::string);
REGISTER_TYPE(std::vector<int>);
序列化接口设计
我们需要为每种支持的类型实现序列化和反序列化函数,然后通过std::any的类型匹配调用对应函数。这里用重载的方式实现不同类的序列化逻辑:
// 序列化int类型
void serialize(std::ofstream& ofs, int val) {
ofs.write(reinterpret_cast<const char*>(&val), sizeof(val));
}
// 反序列化int类型
void deserialize(std::ifstream& ifs, int& val) {
ifs.read(reinterpret_cast<char*>(&val), sizeof(val));
}
// 序列化double类型
void serialize(std::ofstream& ofs, double val) {
ofs.write(reinterpret_cast<const char*>(&val), sizeof(val));
}
// 反序列化double类型
void deserialize(std::ifstream& ifs, double& val) {
ifs.read(reinterpret_cast<char*>(&val), sizeof(val));
}
// 序列化string类型
void serialize(std::ofstream& ofs, const std::string& val) {
size_t len = val.size();
ofs.write(reinterpret_cast<const char*>(&len), sizeof(len));
ofs.write(val.data(), len);
}
// 反序列化string类型
void deserialize(std::ifstream& ifs, std::string& val) {
size_t len;
ifs.read(reinterpret_cast<char*>(&len), sizeof(len));
val.resize(len);
ifs.read(&val[0], len);
}
// 序列化vector<int>类型
void serialize(std::ofstream& ofs, const std::vector<int>& val) {
size_t len = val.size();
ofs.write(reinterpret_cast<const char*>(&len), sizeof(len));
for (int item : val) {
ofs.write(reinterpret_cast<const char*>(&item), sizeof(item));
}
}
// 反序列化vector<int>类型
void deserialize(std::ifstream& ifs, std::vector<int>& val) {
size_t len;
ifs.read(reinterpret_cast<char*>(&len), sizeof(len));
val.resize(len);
for (int i = 0; i < len; ++i) {
ifs.read(reinterpret_cast<char*>(&val[i]), sizeof(val[i]));
}
}
std::any的序列化与反序列化实现
接下来封装std::any的序列化函数,先判断std::any存储的类型,写入类型ID,再调用对应类型的序列化函数:
// 将std::any序列化到文件
bool serializeAny(std::ofstream& ofs, const std::any& data) {
if (!data.has_value()) {
// 空数据写入-1类型标识
int emptyId = -1;
ofs.write(reinterpret_cast<const char*>(&emptyId), sizeof(emptyId));
return true;
}
// 判断类型并写入类型ID
if (data.type() == typeid(int)) {
int typeId = TypeId<int>::id;
ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
serialize(ofs, std::any_cast<int>(data));
} else if (data.type() == typeid(double)) {
int typeId = TypeId<double>::id;
ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
serialize(ofs, std::any_cast<double>(data));
} else if (data.type() == typeid(std::string)) {
int typeId = TypeId<std::string>::id;
ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
serialize(ofs, std::any_cast<std::string>(data));
} else if (data.type() == typeid(std::vector<int>)) {
int typeId = TypeId<std::vector<int>>::id;
ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
serialize(ofs, std::any_cast<std::vector<int>>(data));
} else {
std::cerr << "不支持的类型,无法序列化" << std::endl;
return false;
}
return true;
}
// 从文件反序列化为std::any
std::any deserializeAny(std::ifstream& ifs) {
int typeId;
ifs.read(reinterpret_cast<char*>(&typeId), sizeof(typeId));
if (typeId == -1) {
return std::any();
}
std::string typeName = TypeRegistry::getTypeName(typeId);
if (typeName == "int") {
int val;
deserialize(ifs, val);
return val;
} else if (typeName == "double") {
double val;
deserialize(ifs, val);
return val;
} else if (typeName == "std::string") {
std::string val;
deserialize(ifs, val);
return val;
} else if (typeName == "std::vector<int>") {
std::vector<int> val;
deserialize(ifs, val);
return val;
} else {
std::cerr << "未知类型,无法反序列化" << std::endl;
return std::any();
}
}
完整使用示例
下面是完整的测试代码,演示如何存储不同类型的std::any到文件并读取:
int main() {
// 准备要序列化的数据
std::vector<std::any> datas;
datas.push_back(100);
datas.push_back(3.1415);
datas.push_back(std::string("hello std::any"));
datas.push_back(std::vector<int>{1,2,3,4,5});
datas.push_back(std::any()); // 空数据
// 序列化到文件
std::ofstream ofs("data.bin", std::ios::binary);
if (!ofs.is_open()) {
std::cerr << "打开文件失败" << std::endl;
return 1;
}
for (const auto& data : datas) {
if (!serializeAny(ofs, data)) {
std::cerr << "序列化失败" << std::endl;
return 1;
}
}
ofs.close();
// 从文件反序列化
std::ifstream ifs("data.bin", std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "打开文件失败" << std::endl;
return 1;
}
while (ifs.peek() != EOF) {
std::any data = deserializeAny(ifs);
if (data.has_value()) {
if (data.type() == typeid(int)) {
std::cout << "int: " << std::any_cast<int>(data) << std::endl;
} else if (data.type() == typeid(double)) {
std::cout << "double: " << std::any_cast<double>(data) << std::endl;
} else if (data.type() == typeid(std::string)) {
std::cout << "string: " << std::any_cast<std::string>(data) << std::endl;
} else if (data.type() == typeid(std::vector<int>)) {
std::vector<int> vec = std::any_cast<std::vector<int>>(data);
std::cout << "vector<int>: ";
for (int item : vec) {
std::cout << item << " ";
}
std::cout << std::endl;
}
} else {
std::cout << "empty data" << std::endl;
}
}
ifs.close();
return 0;
}
注意事项
- 新增支持的类型时,需要先通过REGISTER_TYPE宏注册类型,再实现对应的序列化和反序列化函数,最后在serializeAny和deserializeAny中添加类型判断分支。
- 序列化自定义类型时,需要确保类型的所有成员变量都是可序列化的,并且序列化和反序列化的顺序完全一致。
- 二进制序列化需要注意不同平台的字节序问题,如果需要跨平台使用,需要添加字节序转换逻辑。
- std::any存储的类型必须是可拷贝的,否则无法正确序列化数据。