Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

No more blah, blah, blah! -- Kirk, "Miri", stardate 2713.6


devel / comp.lang.c / some of my old "interface" code in C...

SubjectAuthor
o some of my old "interface" code in C...Chris M. Thomasson

1
some of my old "interface" code in C...

<uoffft$3hceb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31291&group=comp.lang.c#31291

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: some of my old "interface" code in C...
Date: Fri, 19 Jan 2024 19:42:53 -0800
Organization: A noiseless patient Spider
Lines: 180
Message-ID: <uoffft$3hceb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 Jan 2024 03:42:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9f59f002e03eff1e5bb7b9112097a050";
logging-data="3715531"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19z8dV9dcrxgFDUttXtPKqCTYp07Qt9FsE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4aIVhuwPmrZW12ObhkUg0SubYXA=
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Jan 2024 03:42 UTC

Some older interface code of mine in C, should compile right up:

https://pastebin.com/raw/f52a443b1
(raw text, no ads)

______________________________

/* Interfaces
____________________________________________________________________*/
#include <stddef.h>

struct object_prv_vtable {
int (*fp_destroy) (void* const);
};

struct device_prv_vtable {
int (*fp_read) (void* const, void*, size_t);
int (*fp_write) (void* const, void const*, size_t);
};

struct device_vtable {
struct object_prv_vtable const object;
struct device_prv_vtable const device;
};

struct device {
struct device_vtable const* vtable;
};

#define object_destroy(mp_self) ( \
(mp_self)->vtable->object.fp_destroy((mp_self)) \
)

#define device_read(mp_self, mp_buf, mp_size) ( \
(mp_self)->vtable->device.fp_read((mp_self), (mp_buf), (mp_size)) \
)

#define device_write(mp_self, mp_buf, mp_size) ( \
(mp_self)->vtable->device.fp_write((mp_self), (mp_buf), (mp_size)) \
)

/* Sample Header (usb_drive.h)
____________________________________________________________________*/
#if ! defined(USB_HEADER_H)
#define USB_HEADER_H

extern int usb_drive_create(struct device** const);

#endif

/* Sample Impl (usb_drive.c)
____________________________________________________________________*/
/* #include "usb_drive.c" */
#include <stdio.h>
#include <stdlib.h>

struct usb_drive {
struct device device;
/* whatever */
};

static int usb_drive_object_destroy(void* const);
static int usb_drive_device_read(void* const, void*, size_t);
static int usb_drive_device_write(void* const, void const*, size_t);

static struct device_vtable const g_table = {
{ /* object */
usb_drive_object_destroy
},

{ /* device */
usb_drive_device_read,
usb_drive_device_write
}
};

int usb_drive_create(
struct device** const pself
) {
struct usb_drive* const self = malloc(sizeof(*self));
if (self) {
self->device.vtable = &g_table;
*pself = &self->device;
return 0;
}
return -1;
}

int usb_drive_object_destroy(
void* const self_
) {
struct usb_drive* const self = self_;
printf("usb_drive_object_destroy(%p)\n", (void*)self);
free(self_);
return 0;
}

int usb_drive_device_read(
void* const self_,
void* buf,
size_t size
) {
struct usb_drive* const self = self_;
printf("usb_drive_device_read(%p, %p, %lu)\n",
(void*)self, buf, (unsigned long)size);
return 0;
}

int usb_drive_device_write(
void* const self_,
void const* buf,
size_t size
) {
struct usb_drive* const self = self_;
printf("usb_drive_device_write(%p, %p, %lu)\n",
(void*)self, buf, (unsigned long)size);
return 0;
}

/* Sample Application
____________________________________________________________________*/
void read_write(
struct device* const self
) {
char buf[100];

device_read(self, buf, 50);

device_write(self, buf, 5);
}

int main(void) {
struct device* a_device;

if (! usb_drive_create(&a_device)) {
read_write(a_device);

object_destroy(a_device);
}

return 0;
}

______________________________

;^)

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor