C 和缓冲区中的多态性

发布于 2024-09-14 14:48:46 字数 1316 浏览 7 评论 0原文

我有这个联合:

typedef union Message
{
    message_base            base;
    message_with_parameters  parameters;
    reply_message          reply;
    buffer_t                *buffer; // can't figure out what to put here
} message;

message_with_parameters 有一个 message_base 作为第一个字段,而 reply_message 有一个 message_with_parameters 作为第一个字段字段又将 message_base 作为第一个字段。
所以基本上我可以访问它们中的任何一个,并且仍然可以获得我需要的所有数据,但是我从驱动程序中获取了一个缓冲区,现在我想将其序列化到消息中。
我已经知道指向缓冲区的指针是错误的,因为它不会与我的结构相关,但我不能有固定大小的缓冲区。
一路上我想这样做:

m->buffer = buff->payload;

无论我有哪种数据类型,它仍然会序列化。
怎么办呢?

编辑:
这是我的结构:

typedef struct MessageBase
{
    uint32_t    u32DeviceID;
    uint32_t    u32CoreID;
    uint16_t    u16Class;
    uint16_t    u16CRC;
    uint8_t     u8OpCode;

    void (*states [MAX_OPCODES]) (void *);
} message_base;

typedef struct MessageWithParameters
{
    message_base    base_class;
    uint8_t         u8Param1;
    uint8_t         u8Param2;
} message_with_parameters;

typedef message_with_parameters reply_message;

typedef union Message
{
    message_base            base;
    message_with_parameters parameters;
    reply_message           reply;
} message;

I have this union:

typedef union Message
{
    message_base            base;
    message_with_parameters  parameters;
    reply_message          reply;
    buffer_t                *buffer; // can't figure out what to put here
} message;

message_with_parameters has a message_base as the first field and reply_message has a message_with_parameters as as the first field which in turns has message_base as as the first field.
So basically I can access any of them and I'll still get all the data I need, however I am getting a buffer from my driver and now I want to serialize it into the message.
I already know that the pointer to the buffer is wrong as it won't correlate with my structs but I can't have a fixed size buffer.
Somewhere along the way I want to do this:

m->buffer = buff->payload;

And no matter what kind of data type I have, it will still serialize.
How can it be done?

EDIT:
Here are my structs:

typedef struct MessageBase
{
    uint32_t    u32DeviceID;
    uint32_t    u32CoreID;
    uint16_t    u16Class;
    uint16_t    u16CRC;
    uint8_t     u8OpCode;

    void (*states [MAX_OPCODES]) (void *);
} message_base;

typedef struct MessageWithParameters
{
    message_base    base_class;
    uint8_t         u8Param1;
    uint8_t         u8Param2;
} message_with_parameters;

typedef message_with_parameters reply_message;

typedef union Message
{
    message_base            base;
    message_with_parameters parameters;
    reply_message           reply;
} message;

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

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

发布评论

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

评论(1

夏天碎花小短裙 2024-09-21 14:48:46

这是因为缓冲区中的数据不是联合的一部分。

buffer_t* buffer 是一个指针,因此该指针是联合的一部分,而不是它指向的数据,

您可能想做类似的事情

 m =  (message*) buff->payload;

its because the data in the buffer isn't part of the union.

buffer_t* buffer is a pointer, so the pointer is part of the union, not the data which it points at

you probablly want to do something like

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