替代C中的“子类化”结构

时间:2013-05-31 23:49:29

标签: c sockets struct

我听说当设计BSD套接字库时,C编程缺少一定的模式或功能,导致我们今天的设计。

例如,struct socketaddr_in在传递给struct socketaddr等系统调用时会投射到bind

来自GNU libc头文件:

/* POSIX.1g specifies this type name for the `sa_family' member.  */
    typedef unsigned short int sa_family_t;

/* This macro is used to declare the initial common members
   of the data types used for socket addresses, `struct sockaddr',
   `struct sockaddr_in', `struct sockaddr_un', etc.  */

#define __SOCKADDR_COMMON(sa_prefix) \
  sa_family_t sa_prefix##family

#define __SOCKADDR_COMMON_SIZE  (sizeof (unsigned short int))

/* Structure describing a generic socket address.  */
struct sockaddr
  {
    __SOCKADDR_COMMON (sa_);    /* Common data: address family and length.  */
    char sa_data[14];       /* Address data.  */
  };

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;         /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };

示例程序:

int main(int argc, char *argv[]) {
    ...

    // Initialize the server address
    struct sockaddr_in serv_addr;
    bzero((char *)&serv_addr, sizeof(serv_addr));

    serv_addr = (struct sockaddr_in) {
        .sin_port = htons(port),
        .sin_family = AF_INET ,
        .sin_addr.s_addr = INADDR_ANY
    };

    // Bind the server address to the TCP/IP socket
    int status = bind(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

    ...
}

我想不出这个通用接口可以如何实现。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您可以使用委托模式来支持C中的继承形式。它看起来很漂亮,它是通用的,但它非常重:有相当多的潜在故障点,需要一些堆分配等等。我猜他们做了不想为OS API提供这种权重吗?

以下是此

的示例
struct ShapeDelegate {
  double (*getSurfaceArea)(void* data);
  int (*isInside)(void* data, double x, double y);
  void (*destroy)(void* data);
}

struct Shape {
  void* data;
  struct ShapeDelegate delegate;
}

double Shape_getSurfaceArea(Shape* self) {
  return self->delegate.getSurfaceArea(self->data);
}

int Shape_isInside(Shape* self, double x, double y) {
  return self->delegate.isInside(self->data, x, y);
}

void Shape_destroy(Shape* self) {
  if (self->delegate.destroy != NULL)
    self->delegate.destroy(self->data);
}

现在,比方说,你想要一个Circle作为Shape的实现

struct CircleData {
  double x, y, r;
}

double Circle_getSurfaceArea(void* data) {
  CircleData* self = (CircleData*)data;
  return 2 * M_PI * self->r * self->r;
}

int Circle_isInside(void* data, double x, double y) {
  double dist;
  CircleData* self = (CircleData*)data;
  dist = sqrt(sqr(x - self->x) + sqr(y - self->y)); 
  return dist < self->r;
}

void Circle_destroy(void* data) {
  free(data);
}

struct ShapeDelegate Circle_ShapeDelegate {
   Circle_getSurfaceArea,
   Circle_isInside,
   Circle_destroy
};

你需要某种构造函数

void
Shape_initAsCircle(Shape* self, double x, double y, double r) {
  CircleData* data;

  data = (CircleData*)malloc(sizeof(CircleData));
  data->x = x, data->y = y, data->r = r;

  self->data = data;
  self.delegate = Circle_ShapeDelegate;
}

答案 1 :(得分:2)

在设计BSD套接字库时,我不会说C缺少子类模式。相反,BSD套接字API设计者专门利用了这种模式。将结构体转换为基础结构(具有较少字段的更通用结构)并返回到专用结构体是非常常见且完全支持的。 C甚至有一些保证可以确保它可以移植:如果结构定义的第一个元素是另一个结构,C保证封闭结构的第一个成员的类型,内存地址和布局与类型,内存相同封闭结构的地址和布局强制转换为其第一个成员的类型。