gpt4 book ai didi

c++ - 序列化 `std::type_index`

转载 作者:行者123 更新时间:2023-11-30 05:29:57 30 4
gpt4 key购买 nike

我一直在使用 std::type_index存储 std::unordered_map<std::type_index, MyProperty>MyClass .现在我想序列化(使用 boost::serialization)MyClass .编译器说 struct std::type_index has no member named serialize这表明 boost::serialization 不支持 std::type_index .那么问题来了,遇到这种情况怎么办?有没有人有 serialize std::type_index 的功能?或者是否有不同的对象可用于此 map 的 key 我需要的已经是可序列化的,可以做同样类型的事情。即,当我使用函数模板时:

template <typename T>
void MyClass::func(T)
{
myMap.find(std::type_index(typeid(T)));
}

下面是一个缺乏支持的demo:

#include <boost/archive/text_oarchive.hpp>

#include <fstream>
#include <typeindex>

int main()
{
std::type_index myTypeIndex = typeid(double);

std::ofstream outputStream("test.txt");
boost::archive::text_oarchive outputArchive(outputStream);

outputArchive << myTypeIndex;
outputStream.close();

return 0;
}

最佳答案

第 1 步:定义您自己的 boost::serialization::load/save<> std::type_index 的重载

第 2 步:提供一种将字符串映射到 type_index 的方法,反之亦然。

第 3 步:根据名称将 type_index 存储在存档中。

当然,您需要记住将要用作 map 键的每种类型的名称注册。在下面的示例中,您将通过调用 register_name("Foo", typeid(Foo)); 来完成此操作等

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_free.hpp>

#include <fstream>
#include <sstream>
#include <typeindex>
#include <tuple>
#include <vector>
#include <string>

struct nothing {};

using named_typeindex = std::tuple<std::string, std::type_index>;
std::vector<named_typeindex> name_register =
{
};

std::type_index type_for_name(const std::string& name)
{
auto i = std::find_if(std::begin(name_register), std::end(name_register),
[&name](const auto& entry) { return std::get<std::string>(entry) == name; } );
if (i == std::end(name_register))
return typeid(nothing);
return std::get<std::type_index>(*i);
}

std::string const& name_for_type(std::type_index type)
{
auto i = std::find_if(std::begin(name_register), std::end(name_register),
[type](const auto& entry) { return std::get<std::type_index>(entry) == type; } );

using namespace std::string_literals;
if (i == std::end(name_register))
throw std::logic_error("unregistered type "s + type.name());

return std::get<std::string>(*i);
}

bool register_name(std::string name, std::type_index ti)
{
if (type_for_name(name) == typeid(nothing))
{
name_register.push_back(std::make_tuple(std::move(name), ti));
return true;
}
return false;
}

namespace boost {
namespace serialization {

template<class Archive>
void save(Archive & ar, const std::type_index & t, unsigned int version)
{
ar << name_for_type(t);
}

template<class Archive>
void load(Archive & ar, std::type_index & t, unsigned int version)
{
std::string s;
ar >> s;
t = type_for_name(s);
}

} // namespace serialization
} // namespace boost

BOOST_SERIALIZATION_SPLIT_FREE(std::type_index);

int main()
{
std::type_index myTypeIndex = typeid(double);

std::ostringstream outputStream {};
boost::archive::text_oarchive outputArchive(outputStream);

outputArchive << myTypeIndex;

std::cout << outputStream.str() << std::endl;

return 0;
}

关于c++ - 序列化 `std::type_index`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36219532/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com