使用 C++远程模板功能

发布于 2024-12-11 00:35:27 字数 786 浏览 0 评论 0原文

我目前有一个对象,它实现了一个复杂的系统,用于存储和读取多种类型的配置对象,可以轻松地在本地使用:

template <typename T>
void rdData(std::string path, T& ref);

template <typename T>
void wrData(std::string path, T& ref);

int value;
rdData("tree1.scalar", value);
struct my_struct;
wrData("tree1", my_struct);

T 类型可以是原始数据类型,也可以是在单独文件中定义的 POD C Struct,例如 legal_structs .c.随着新结构的添加,定义结构的该文件会随着时间的推移而增长。如果使用了错误的类型,则会引发异常。

但是,现在我们希望多个其他进程同时使用同一系统。它可以通过套接字、RPC 或消息传递。

有没有办法像模板那样通过套接字确定对象的类型?例如,客户端将执行以下操作:

remote_rdData(path, data);

数据将作为原始字节传递到运行我们系统的服务器。 在服务器端,需要有一种方法来识别它是哪种数据类型,并使用适当的类型调用本地模板函数。 到目前为止,我看到的唯一方法是传输对象标识符(例如,其 type_info),并且在服务器中,有一个巨大的开关比较 type_info 并调用每个对象。但是,每次在系统中添加新结构时都需要附加它。

是否有更自动的方法来在服务器端重建对象?

I currently have an object that implements a complex system for storing and reading configuration objects of several types, that can be easily locally used:

template <typename T>
void rdData(std::string path, T& ref);

template <typename T>
void wrData(std::string path, T& ref);

int value;
rdData("tree1.scalar", value);
struct my_struct;
wrData("tree1", my_struct);

The T type can be a primitive data type, or a POD C Struct defined in a separate file, say legal_structs.c. This file defining structs grow over time, as new structs are added. If a wrong type is used, an exception is thrown.

However, now we want to use the same system concurrently by several other processes. It can be by sockets, RPC or messaging.

Is there a way to determine the type of an object over sockets, as the templates do? For example, the clients will do:

remote_rdData(path, data);

And data will be passed as raw bytes to the server running our system.
In the server side, there will need to be a way to identify which data type is it, and call the local template function with the appropriate type.
So far, the only way I see is to transmit an object identifier (its type_info, for example), and in the server, have a huge switch comparing type_infos and calling for each object. However, it will need to be appended each time a new struct is added in the system.

Is there a more automatic way to do this reconstruction of objects in the server side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-12-18 00:35:27

模板在运行时不执行任何操作。它们在编译时扩展。如果要从流中反序列化对象,则需要能够调用一些代码来基于从流中读取的类型代码构造正确的类型。模板与此无关。

最强大的解决方案通常是一些代码生成 - 只需编写一个简单的工具来为您生成丑陋的 switch 语句,并将其合并到您的构建过程中。

Templates do not do anything at runtime. They are expanded at compile time. If you are going to deserialize an object from a stream, you need to be able to call some code to construct the right type based on a type code read from the stream. Templates don't have anything to do with that.

The most powerful solution is often some code-generation - just write a simple tool that generates the ugly switch statement for you, and incorporate this into your build process.

尝蛊 2024-12-18 00:35:27

我认为序列化(例如在 boost 中)可以帮忙。除了对象识别之外,还有一些您可能会发现有用的功能(例如版本控制、效率、可移植性......)

I think serialization (for example in boost) could help. Apart object recognition there are also some features you could find useful (like versioning, efficiency, portability...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文