> "Tim Rentsch" wrote in message
> news:kfnd22f...@x-alumni2.alumni.caltech.edu...
> > FWIW, I actually made two tries that are unaccepatble to David (OP).
> >
> > I will post one here, and another in a followup post...
> Okay, thank you.
> > Well, barring any formatting issues, here is try 1:
[...]
> A fairly common approach to implementing OOP-in-C is how I might
> describe this.
It think it is a reasonable question as well Time.
> So it's reasonable to ask (as I guess you did) "how about this
> scheme instead?".
I thought that David perhaps did not approve because it lacked a way to call
a
function from a "unified place" using an index, almost like a syscall
interface
and op codes. Anyway, codepad seems to be back up and running, so here
is the my second try that also was deemed unacceptable for the OP's needs:
http://codepad.org/SJvF3Y3v
_________________________________________________________________
#include <stdio.h>
#include <stdlib.h>
/* "SysCall" Interface
___________________________________________________________*/
typedef int (*fp_syscall_type) (void*, void*);
#define syscall_explicit(mp_obj, mp_fidx, mp_ctx) ( \
(mp_obj)->vtable->fp[(mp_fidx)]((mp_obj), (mp_ctx)) \
)
/* Sample Device Object Interface
___________________________________________________________*/
enum device_vtable_fidx
{
DEVICE_FIDX_DESTROY = 0,
DEVICE_FIDX_READ = 1,
DEVICE_FIDX_WRITE = 2
};
struct device_vtable
{
fp_syscall_type fp[3];
};
struct device_ctx
{
void* buf;
size_t bufsz;
};
struct device
{
struct device_vtable const* vtable;
};
#define device_destroy(mp_obj) \
syscall_explicit(mp_obj, DEVICE_FIDX_DESTROY, NULL)
#define device_read(mp_obj, mp_ctx) \
syscall_explicit(mp_obj, DEVICE_FIDX_READ, mp_ctx)
#define device_write(mp_obj, mp_ctx) \
syscall_explicit(mp_obj, DEVICE_FIDX_WRITE, mp_ctx)
/* Sample USB Device Interface
___________________________________________________________*/
extern int usb_drive_create(struct device**);
/* Sample USB Drive Impl
___________________________________________________________*/
struct usb_drive_impl
{
struct device iface;
/* usb drive specific stuff */
};
static int usb_drive_syscall_destroy(void*, void*);
static int usb_drive_syscall_read(void*, void*);
static int usb_drive_syscall_write(void*, void*);
static struct device_vtable const g_device_vtable = {{
usb_drive_syscall_destroy,
usb_drive_syscall_read,
usb_drive_syscall_write
}};
int
usb_drive_create(
struct device** const self_iface
){
struct usb_drive_impl* self = malloc(sizeof(*self));
if (self)
{
self->iface.vtable = &g_device_vtable;
*self_iface = &self->iface;
printf("usb_drive_create(%p)\r\n", (void*)self);
return 0;
}
return 1;
}
int
usb_drive_syscall_destroy(
void* self_,
void* ctx_
){
struct usb_drive_impl* const self = self_;
free(self);
printf("usb_drive_syscall_destroy(%p, %p)\r\n", self_, ctx_);
return 0;
}
int
usb_drive_syscall_read(
void* self_,
void* ctx_
){
struct usb_drive_impl* const self = self_;
struct device_ctx* const ctx = ctx_;
printf("usb_drive_syscall_read(%p, %p)\r\n", self_, ctx_);
printf("read %lu bytes into %p\r\n",
(unsigned long)ctx->bufsz, ctx->buf);
return 0;
}
int
usb_drive_syscall_write(
void* self_,
void* ctx_
){
struct usb_drive_impl* const self = self_;
struct device_ctx* const ctx = ctx_;
printf("usb_drive_syscall_write(%p, %p)\r\n", self_, ctx_);
printf("write %lu bytes from %p\r\n",
(unsigned long)ctx->bufsz, ctx->buf);
return 0;
}
/* Sample Application
___________________________________________________________*/
void
read_write(
struct device* self
){
char buf[32] = { '\0' };
struct device_ctx dctx = { buf, 16 };
/* Use the device object interface */
device_read(self, &dctx);
dctx.bufsz = 8;
device_write(self, &dctx);
/* Use the device with explicit syscall's */
dctx.bufsz = 24;
syscall_explicit(self, DEVICE_FIDX_READ, &dctx);
dctx.bufsz = 13;
syscall_explicit(self, DEVICE_FIDX_WRITE, &dctx);
}
int
main()
{
struct device* self = NULL;
if (! usb_drive_create(&self))
{
read_write(self);
device_destroy(self);
}
return 0;
}
_________________________________________________________________
Yes it is highly contrived, but you can call functions using an index at
runtime...
lol ;^D