只公开需要的信息,不包括不必要的头文件 [英] Expose only required information without including unnecessary header files

查看:24
本文介绍了只公开需要的信息,不包括不必要的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个进程公开一个消息的结构,以便其他接收它的进程可以使用它.然而,这个结构包含定义在其他头文件中的参数类型,这些头文件本身包含一堆其他头文件.

I want a process to expose a structure of a message, so it can be used by other processes receiving it. However, this structure includes types of arguments which are defined in other header files, which themselves include a bunch of other header files.

typedef struct sfp_info_s {
        int port;
        char platform_id[50];
        sff_eeprom_t sff_type; 
        char status_str[50];
        sff_dom_t sff_dom; 
}sfp_info_t;

sff_eeprom_t 在名为 sff_db.h 的头文件中定义这个文件本身包含其他文件:

sff_eeprom_t is defined in a header file named : sff_db.h and this file itself includes other files :

#ifndef __SFF_SFF_H__
#define __SFF_SFF_H__

#include <sff/sff_config.h>
#include <AIM/aim_pvs.h>

#include <dependmodules.x>

创建这个 API 的最佳方法是什么,而不会在所有只需要知道消息结构的进程中包含所有这些文件?

What is the best way to create this API without the mess of including all these files in all the processes which need to know only the structure of the message?

推荐答案

C 标准允许将指向结构的指针转换为指向其第一个成员并返回的指针.因此,您可以将要公开的成员打包成一个结构并将其发布在公共标头中:

The C standard allows a pointer to a struct to be converted to a pointer to its first member and back. So you can package the members you want to expose into a struct and publish that in a public header:

typedef struct
{
    int port;
    char platform_id[50];
} sfp_public_t;

在私人标题中,您将:

typedef struct
{
    sfp_public_t public;
    sff_eeprom_t sff_type; 
    char status_str[50];
    sff_dom_t sff_dom; 
} sfp_info_t;

给定一个指向 sfp_info_t 的指针 p,你可以将它转换成一个指向 sfp_public_t 的指针,并将它传递给其他代码.当您从其他代码收到这样的指针时,您可以将其转换为指向 sfp_info_t 的指针.

Given a pointer p to an sfp_info_t, you may convert it to a pointer to an sfp_public_t and pass it to other code. When you receive such a pointer from other code, you may convert it to a pointer to an sfp_info_t.

其他代码当然不会知道对象的真实大小,因此无法分配新实例.您的 API 需要提供支持例程来分配此类对象.

The other code will of course not know the true size of the object, so it cannot allocate new instances. Your API will need to provide supporting routines to allocate such objects.

缺点是这要求您在代码中使用 p->public.name 而不是 p->name 访问打包成员,尽管代码接收转换后的指针可以简单地使用p->name.我认为您可以通过在 sfp_info_t 中使用匿名结构成员来避免这种情况.但是匿名结构体不能用标签或类型名声明,所以需要重复声明:

A drawback is this requires you access the packaged members using p->public.name instead of p->name inside your code, although the code that receives the converted pointer can simply use p->name. I think you may be able to avoid that by using an anonymous struct member inside sfp_info_t. But an anonymous struct cannot be declared with a tag or a typename, so you need to repeat the declaration:

typedef struct
{
    struct
    {
        int port;
        char platform_id[50];
    };
    sff_eeprom_t sff_type; 
    char status_str[50];
    sff_dom_t sff_dom; 
} sfp_info_t;

这篇关于只公开需要的信息,不包括不必要的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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