具有多个定义不透明的结构 [英] Opaque structures with multiple definitions

查看:167
本文介绍了具有多个定义不透明的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑实现在C语言中一个简单的界面模式。

I am considering realizing a simple interface pattern in the C language.

一个关键特征是,它会提供多个定义为通过接口的公共头供给的不透明内在张力结构,​​也就是,不同的实现将为该结构(因此在不同的转换单元提供不同的垫层数据,相同的结构将具有不同的实现)。

A key characteristic is that it would provide multiple definitions for an opaque sturcture supplied by the interface's public header, that is, different implementations would provide different underlaying data for that structure (so across different translation units, the same structure would have different implementation).

我找不到任何引用是否这将是一个好或坏的设计模式。至少它似乎并不违反严格别名规则,只有依靠一致的顺序和填充为共同的第一个元素(S),它是由C标准保证的(据我看到的)。当然,我知道用C opject导向的模式,但我看不出使用这个特殊的。

I couldn't find any reference whether this would be a good or bad design pattern. At least it doesn't seem to violate the strict aliasing rule, only relying on the consistent order and padding for the common first element(s), which is guaranteed by the C standard (as far as I see). Of course I am aware of opject oriented patterns in C, but I couldn't see this particular one used.

这是可以接受的?在这样的模式甚至用? (我无法找到任何东西)

Is this acceptable? Was such a pattern even used? (I couldn't find anything)

有关宽松政策的理解,下面是三个源文件的工作例如:

For easing understanding, following is a working example of three source files:

reader.h(公共接口定义)

reader.h (The public interface definition)

#ifndef READER_H
#define READER_H

typedef struct reader_s reader_t;

char reader_read(reader_t* r);

#endif

reader.c(为接口胶合逻辑)

reader.c (Glue logic for the interface)

#include "reader.h"

typedef char (reader_f)(reader_t*);

struct reader_s{
 reader_f* rfunc;
};

char reader_read(reader_t* r)
{
 return r->rfunc(r);
}

reader1.h(该接口的实现,报头)

reader1.h (An implementation of the interface, header)

#ifndef READER1_H
#define READER1_H

#include "reader.h"

reader_t* reader1_get(void);

#endif

reader1.c(接口,code的实现)

reader1.c (An implementation of the interface, code)

#include "reader1.h"

typedef char (reader_f)(reader_t*);

struct reader_s{
 reader_f* rfunc;
 int       pos;
};

static char reader1_read(reader_t* r);

reader_t reader1_obj = {&reader1_read, 0};

reader_t* reader1_get(void)
{
 return &reader1_obj;
}

static char reader1_read(reader_t* r)
{
 char rval = 'A' + (r->pos);
 (r->pos) = (r->pos) + 1;
 if ((r->pos) == 24){ (r->pos) = 0; }
 return rval;
}

main.c中(用法示例)

main.c (Example usage)

#include "reader1.h"
#include <stdio.h>

int main(void)
{
 reader_t* rd = reader1_get();
 int       i;

 printf("\n");
 for (i = 0; i < 60; i++){
  printf("%c", reader_read(rd));
 }
 printf("\n");

 return 0;
}

可能的接口应该提供实施者另一头文件,提供含函数指针结构头的定义。也许它甚至颇有点一类结构中含有的,以尽量减少许多方法的类对象大小。

Possibly the interface should provide another header file for implementers, providing the definition of the structure head containing the function pointers. Maybe it could even rather point to a class structure containing those to minimize object sizes for classes with many methods.

推荐答案

这似乎真的危险了我,至少暧昧的人读你的code。为什么不直接声明结构包含在这两个你的.c的一个单独的文件:

It seems really dangerous to me, at least ambiguous for someone reading your code. Why not just declare the structure in a separate file included in both of your .c:

reader_s.h(私有的结构声明)

reader_s.h (The "private" structure declaration)

#ifndef READER_S_H
#define READER_S_H

struct reader_s{
 reader_f* rfunc;

 // will only be used in reader1.c
 int       pos;

 // will only be used in reader2.c
 char      foo;
};

#endif

reader.c(为接口胶合逻辑)

reader.c (Glue logic for the interface)

#include "reader.h"

#include "reader_s.h" // include structure declaration

...

// pos and foo won't be used here

reader1.c(接口,code的实现)

reader1.c (An implementation of the interface, code)

#include "reader1.h"

#include "reader_s.h" // both files rely on the same declaration

...

static char reader1_read(reader_t* r)
{
 char rval = 'A' + (r->pos);
 (r->pos) = (r->pos) + 1;
 if ((r->pos) == 24){ (r->pos) = 0; }
 return rval;
}

reader2.c(A第2个接口的实现,code的)

reader2.c (A 2nd implementation of the interface, code)

#include "reader2.h"

#include "reader_s.h"

...

static char reader2_read(reader_t* r)
{
 return r->foo;
}

这篇关于具有多个定义不透明的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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