Skip to content

Instantly share code, notes, and snippets.

@tin-z
Created May 8, 2020 17:49
Show Gist options
  • Save tin-z/9de5fc6f0f21cbe4bf4cabc91d59adf8 to your computer and use it in GitHub Desktop.
Save tin-z/9de5fc6f0f21cbe4bf4cabc91d59adf8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// skel Virtual function table (VFT)
struct file_operations {
int (*read) (struct Classe1 *, char **);
int (*write) (struct Classe1 *, char **, char **);
};
// skel classe
struct Classe1 {
const struct file_operations *f_op;
unsigned int flags;
void *public;
void *private;
};
// skel Costruttore classe
#define __cls_init(oops) { \
.flags = 0xfbadfbad, \
.private = {NULL}, \
.public = {NULL}, \
.f_op = oops \
}
// new classe
typedef struct Classe1 Classe1_t; // remove struct prefix
#define Classe1_new(name, oops) \
Classe1_t name = __cls_init(oops)
// Default function table class
int read_int(struct Classe1 * cls, char **var){
//cls.public[var]
return 0;
}
int write_int(struct Classe1 * cls, char **var, char **val){
//cls.public[var]=val
return 0;
}
static const struct file_operations default_op = {
.read = &read_int,
.write = &write_int
};
/* e.g.
gcc -fpic -O0 -std=c99 -Wall main.c -o main
*/
int main() {
char ** example;
Classe1_new( iamclass, &default_op);
printf("read() -> %d\n", iamclass.f_op->read(&iamclass, example));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment