持久保存C ++类型信息以备程序调用时使用 [英] Persist C++ type info to file for use across program invocations

查看:69
本文介绍了持久保存C ++类型信息以备程序调用时使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:突出显示实际问题,并根据需要提供更多上下文。

highlighting the actual question with more context available if desired.

我想实现以下方法:

template <typename T>
<unspecified> type_identification();

对于通用类型T,它必须返回一个(相对)唯一的标识,该标识在多次调用中是稳定的

For a generic type T, it must return a (relatively) unique identification that is stable over multiple invocations of the same program and may be used for inter-process communication (so no pointer-based solutions).

可以使用编译器特定的宏/扩展名/内部函数,最好使用编译器专用的宏/扩展名/内部函数。

Compiler-specific macros/extensions/intrinsics may be used, preferably available for both MSVC and clang.

我考虑过 std :: type_info :: hash_code std :: type_info :: name ,但两者都不能保证通过同一程序的多次调用获得相同的输出。

I have considered std::type_info::hash_code, or std::type_info::name but both of those cannot guarantee the same output over multiple invocations of the same program.

通过立即解释问题来避免 XY问题我正在尝试解决。

Trying to avoid the XY-problem by immediately explaining the problem I am trying to solve.

我编写了将代码一般存储在文件中以供以后使用的代码。文件中的每个所谓条目都由应用程序代码分配了一个标签,在以后的程序调用中,该代码必须用于访问同一条目。该API基本上可以归结为:

I have written code to generically store data on file for later use. Each so-called entry in the file is assigned a label by the application code that it must use to access the same entry in a later program invocation. The API basically boils down to:

template <typename T>
void make(const std::string &name, T value);

template <typename T>
T get(const std::string &name);

请注意,这只是示例代码。

Note that this is merely example code.

当应用程序代码通过 get< T> 访问值时,它将显式指定条目的类型,以便实现可以使用 reinterpret_cast 允许以实际类型而不是 void * 的形式访问条目。

When application code accesses a value through get<T>, it explicitly specifies the type of the entry so that the implementation may use reinterpret_cast to give access to the entry as the actual type instead of as a void *.

出于这个问题,让我们假设已考虑到与 reinterpret_cast 和持久性数据归档有关的所有危险和陷阱。

Let's assume for the sake of this question that all dangers and pitfalls concerning reinterpret_cast and persisting data to file have been taken into account.

为避免由于应用程序代码弄乱了模板参数而导致的严重崩溃,我想向文件中的每个条目添加一些类型标识。基本上,当应用程序代码执行以下操作时:

To avoid nasty crashes because application code has messed up the template argument, I would like to add some type identification to each entry in the file. Basically, when the application code does something like:

make("integer", 5);
auto a = get<std::string>("integer");

我想抛出一个异常,指示实际类型和请求的类型不匹配。

I would like to throw an exception indicating the mismatch in actual type and requested type.

推荐答案

您可以添加代码以定义要保留的类型的永久名称。

You can add code to define the persistent names of the types you wish to persist.

template <typename T> struct persistent_type;

template <> struct persistent_type<int>
{
   static std::string_view name() { return "int"; }
}

template <> struct persistent_type<double>
{
   static std::string_view name() { return "double"; }
}

等。

并在 make

template <typename T>
void make(std::string_view name, T value)
{
   // Save the type name of the data, persistent_type<T>::name()

   // Save the name of the data, name

   // Save the data, value
}

获取值时,使用

template <typename T>
T get(std::string_view name)
{
   // Read the type name and make sure it is equal to persistent_type<T>::name().

   // Rest of your logic to read the object
}

这篇关于持久保存C ++类型信息以备程序调用时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆