GTK interface structure: Why is it built as casting interface? - c

GTK3 provides functions, that make casting necessary. As in
gtk_grid_attach(GTK_GRID(grid), button, 0,0,0,0)
However, this method will always take a grid. So why wouldn't the cast be inside the function? The call would become:
gtk_grid_attach(grid, button, 0,0,0,0)
And would therefore be much shorter and easier readable (because no redundancy).
edit:
Due to the discussion in the comments, I'll try to make my question more clear using the example from David Ranieri below
Given the code:
typedef struct {char *sound;} animal_t;
typedef struct {animal_t animal; char *name;} dog_t;
typedef struct {animal_t animal;} cat_t;
#define DOG(animal) (dog_t *)animal
#define CAT(animal) (cat_t *)animal
There are two ways to implement the function dog_print_name without loosing the typecheck-mechanism. In the example below I will hand over a cat, where a dog is expected.
(1)
void dog_print_name(dog_t *dog)
{
puts(dog->name);
}
int main(){
// ...
dog_print_name(DOG(cat)); // perfom type check here
// and fail on 'cat'
}
(2)
void dog_print_name(void *dog)
{
dog_t *dog_ = DOG(dog); // check performed
// will fail, if 'dog' is a cat.
puts(dog->name);
}
int main(){
// ...
dog_print_name(cat); // no check performed here
}
These two pieces of code being given, what is the reason one would choose implementation (1) over implementation (2)?
The goal is to get a warning / error on a call where dog is expected, but cat is given.

It's needed because C doesn't handle polymorphism. It can't check if that argument's type is valid based on the class hierarchy. So the macro triggers a check for that, and also make sure that even if you stored your pointer in the right pointer type, the content it points to (a GObject-derived object) uses the right type.
Here, grid has the right type, but not the right content. Making an explicit check makes debugging much easier.
GtkGrid *grid = gtk_image_new();
GtkButton *button = gtk_button_new();
gtk_grid_attach(grid, button, 0,0,0,0); // Won't catch early the fact that grid is a GtkImage, not a GtkGrid.
Also, most of the times, you just declare everything as a pointer to a GtkWidget. This is because depending on which methods you call, the class they come from may be different, so you'll need to cast anyway.
GtkWidget *grid = gtk_grid_new();
GtkWidget *button = gtk_button_new();
gtk_grid_attach(GTK_GRID(grid), button, 0,0,0,0); // WILL check at runtime that grid is a GtkGrid.
gtk_grid_remove_row(GTK_GRID(grid), 0);
gtk_container_add(GTK_CONTAINER(grid), button);

Just to expand a bit the nice answer of #liberfforce:
The only type allowed to receive a pointer to a different type without a cast is void *, so you need the cast because some functions (like grid_attach) are expecting to receive the derived object (GtkGrid), not the base (GtkWidget)
A little example that illustrates how inheritance works in gtk:
typedef struct {char *sound;} animal_t;
typedef struct {animal_t animal; char *name;} dog_t;
typedef struct {animal_t animal; int age;} cat_t;
#define DOG(animal) (dog_t *)animal
#define CAT(animal) (cat_t *)animal
static animal_t *new_dog(char *name)
{
dog_t *dog = malloc(sizeof *dog);
dog->animal.sound = "worf";
dog->name = name;
return &dog->animal;
}
static animal_t *new_cat(int age)
{
cat_t *cat = malloc(sizeof *cat);
cat->animal.sound = "meow";
cat->age = age;
return &cat->animal;
}
void animal_print_sound(animal_t *animal)
{
puts(animal->sound);
}
void dog_print_name(dog_t *dog)
{
puts(dog->name);
}
void cat_print_age(cat_t *cat)
{
printf("%d\n", cat->age);
}
int main(void)
{
animal_t *dog = new_dog("Bobbie");
animal_t *cat = new_cat(5);
animal_print_sound(dog);
animal_print_sound(cat);
dog_print_name(DOG(dog));
cat_print_age(CAT(cat));
return 0;
}
So why wouldn't you implement:
void dog_print_name(animal_t *dog) { puts(DOG(dog)->name); }
void cat_print_age(animal_t *cat) { printf("%d\n", CAT(cat)->age); }
?
From a "users"-perspective dog_print_name(dog); looks more
understandable to me.
Because in this way the compiler can not protect you checking the type, think what will happen if you pass dog_print_name(cat); using your approach.

Related

Inline functions, macros and other solutions to allocate on caller's stack

I have a lot of code that looks like this:
int bufferSize = fooBufferSize(); // hate having to do this; this logic should be in `foo`
char buffer[bufferSize];
foo(buffer);
bar(buffer);
It happens all the time for me. In the wild, I see something similar a lot:
int bufferSize = snprintf(NULL, 0, format, ...); // exact same issue as above
char buffer[bufferSize+1];
sprintf(buffer, format, ...);
Besides the fact that the above are tedious for the user to write, they also probably redo a lot of computations, which is not only inefficient, but it isn't DRY. I know that I could just malloc the buffer within foo, but there's a lot of issues with that: memory fragmentation, remembering to call free, overhead of malloc/free.
char *foo() {
char *buffer = malloc(...);
// process the data in the buffer
return buffer;
}
main() {
char *buffer = foo();
bar(buffer);
}
There are a lot of cases where I probably would use malloc (or a buffer pool) for things that are constantly removed and deleted (e.g. projectile objects in a game). However, the case that comes up a lot for me is that I want to allocate an object on the stack and then dispose of it when my function returns. The issue is that the object is generally allocated in a stack frame further down from the stack frame I want the object to live in. I'd prefer if I could just build on-top of the stack that foo uses. Like, what if I did this instead:
void foo(char **bufferPtr) {
char buffer[...];
// process buffer
*bufferPtr = buffer;
jmp __builtin_return_address(0); // pseudocode to jump to return address
}
main() {
char *buffer = NULL;
foo(&buffer);
bar(buffer);
}
I don't even know the exact syntax to make this approach work, but it is both GCC-specific and extremely hacky. In addition, if the jump isn't understood by the compiler, local variable states might not be restored properly. I guess, what I really want is for foo to behave like a macro, e.g. like this:
#define foo(buffer) \
char buffer[4]; \
strcpy(buffer, "hey");
int main() {
foo(buffer)
bar(buffer);
}
However, I really don't like using macros (terrible error messages, bad IDE support, slippery slope, etc.)
The macro above looks nice, but in my current usecase, I'm building a computation graph (similar to TensorFlow), and some of the node constructors would look really awkward using macros.
typedef struct {
float *data; // buffer to store output data of computation
int order; // number of dimensions
int *dimensions; // e.g. [3,4] for a 3x4 matrix
} Node;
typedef struct {
Node super; // it's still a node, so just pass a ref to this whenever you need a Node*
Node *A;
Node *B;
} MatMulNode;
void printMatrix(const char *name, Node *node) {
assert(node->order == 2);
printf("%s: [%d x %d]\n", name, node->dimensions[0], node->dimensions[1]);
}
// look at all these backslashes
// also, `return -1` might not make sense in the context this macro is used.
#define matmul(node, left, right) \
if (left.order != 2 || right.order != 2) {\
return -1;\
}\
if (left.dimensions[1] != right.dimensions[0]) {\
return -1;\
}\
int dimensions[2];\
dimensions[0] = left.dimensions[0];\
dimensions[1] = right.dimensions[1];\
float data[dimensions[0] * dimensions[1]];\
MatMulNode node = {\
.super = {\
.data = data,\
.order = 2,\
.dimensions = dimensions,\
},\
.A = &left,\
.B = &right,\
};
int main() {
Node A = {
NULL,
2,
(int[]) {1, 2}
};
Node B = {
NULL,
2,
(int[]) {2, 3}
};
matmul(C, A, B);
printMatrix("A", &A);
printMatrix("B", &B);
printMatrix("C", &C.super);
}
I'm honestly surprised by how well this works, but I also hate the fact that I have to use macros for it and refuse to believe that this is the best API I can make which avoids malloc.
I tried using inline functions, but inline is just a suggestion unless I'm using the always_inline attribute (but that's GCC only), and AFAICT, it doesn't seem to work with alloca. I'm not even sure if the below code has defined behavior, or if I'm just getting lucky:
static inline __attribute__((always_inline)) Node *matmul(Node *left, Node *right) {
if (left->order != 2 || right->order != 2) {
return NULL;
}
if (left->dimensions[1] != right->dimensions[0]) {
return NULL;
}
int dimensions[2];
dimensions[0] = left->dimensions[0];
dimensions[1] = right->dimensions[1];
float data[dimensions[0] * dimensions[1]];
MatMulNode node = {
.super = {
.data = data,
.order = 2,
.dimensions = dimensions,
},
.A = left,
.B = right,
};
return &node.super;
}
The final approach I know of is to use continuation-passing style, e.g.
void matmul(Node *A, Node *B, void callback(void *, Node *C), void *context) {
MatMulNode C = ...;
callback(context, &C.super);
}
The obvious tail-call optimization of this approach is nice, and the fact that the stack is obviously preserved would make it much less hacky than things like jmp, but the API that it presents to the user is really ugly. For example, what if I want to do matmul(A, matmul(B, C))? The code I'd have to write is extremely counter-intuitive, especially because I have to pass in a context variable to the callbacks, when they should Ideally just have access to the entire stack and choose whatever variables they need from there.
void callbackABC(void *context, Node *ABC) {
assert(ABC->order == 2);
printf("A(BC) is [%d x %d]\n", ABC->dimensions[0], ABC->dimensions[1]);
}
void callbackBC(void *context, Node *BC) {
Node *A = context;
matmul(A, BC, callbackABC, NULL);
}
int main() {
Node A = {
NULL,
2,
(int[]) {1, 2}
};
Node B = {
NULL,
2,
(int[]) {2, 3}
};
Node C = {
NULL,
2,
(int[]) {3, 4}
};
matmul(&B, &C, callbackBC, &A);
}
Overall, I think that inline functions are the closest thing to what I want, but rather than ask "How do I force a function to always inline?" I figured I'd ask with the full context of what I want to achieve and why none of my solutions work.
I'm not recommending it, but also you may want to take a look at the __attribute__ cleanup(freefunc) feature in gcc. Basically handles cleanup when the associated variable leaves scope.

passing argument from incompatible pointer type

static struct dll_wifi_state **dll_states;
enum dll_type {
DLL_UNSUPPORTED,
DLL_ETHERNET,
DLL_WIFI
};
struct dll_state {
enum dll_type type;
union {
struct dll_eth_state *ethernet;
struct dll_wifi_state *wifi;
} data;
};
static struct dll_state *dll_states = NULL;
struct dll_wifi_state {
int link;
// A pointer to the function that is called to pass data up to the next layer.
up_from_dll_fn_ty nl_callback;
bool is_ds;
};
This is the method whose pointer is being passed in the dll_wifi_state struct.
static void up_from_dll(int link, const char *data, size_t length)
{
//some code here
}
In other file, I am calling this method
void reboot_accesspoint()
{
// We require each node to have a different stream of random numbers.
CNET_srand(nodeinfo.time_of_day.sec + nodeinfo.nodenumber);
// Provide the required event handlers.
CHECK(CNET_set_handler(EV_PHYSICALREADY, physical_ready, 0));
// Prepare to talk via our wireless connection.
CHECK(CNET_set_wlan_model(my_WLAN_model));
// Setup our data link layer instances.
dll_states = calloc(nodeinfo.nlinks + 1, sizeof(struct dll_state));
for (int link = 0; link <= nodeinfo.nlinks; ++link) {
switch (linkinfo[link].linktype) {
case LT_LOOPBACK:
dll_states[link].type = DLL_UNSUPPORTED;
break;
case LT_WAN:
dll_states[link].type = DLL_UNSUPPORTED;
break;
case LT_LAN:
dll_states[link].type = DLL_ETHERNET;
dll_states[link].data.ethernet = dll_eth_new_state(link, up_from_dll);
break;
case LT_WLAN:
dll_states[link].type = DLL_WIFI;
dll_states[link].data.wifi = dll_wifi_new_state(link,
up_from_dll,
true /* is_ds */);
break;
}
}
// printf("reboot_accesspoint() complete.\n");
}
It works fine like this, but I want to add another argument i.e. up_from_dll((int link, const char *data, size_t length, int seq). And as soon as I add this argument, following error starts coming up
ap.c:153: warning: passing argument 2 of ‘dll_wifi_new_state’ from incompatible pointer type
Is there a way of adding another argument to that method without getting error ??? I am really bad with pointers :(
Any help would be much appreciated.
Line 153 :
dll_states[link].data.wifi = dll_wifi_new_state(link,
up_from_dll,
true /* is_ds */);
And method
struct dll_wifi_state *dll_wifi_new_state(int link,
up_from_dll_fn_ty callback,
bool is_ds)
{
// Ensure that the given link exists and is a WLAN link.
if (link > nodeinfo.nlinks || linkinfo[link].linktype != LT_WLAN)
return NULL;
// Allocate memory for the state.
struct dll_wifi_state *state = calloc(1, sizeof(struct dll_wifi_state));
// Check whether or not the allocation was successful.
if (state == NULL)
return NULL;
// Initialize the members of the structure.
state->link = link;
state->nl_callback = callback;
state->is_ds = is_ds;
return state;
}
I haven't changed anything else apart from adding the new parameter to up_from_dll.
The second parameter to dll_wifi_new_state is up_from_dll_fn_ty callback.
It's not in your code listing right now, but up_from_dll_fn_ty is a typedef saying that the up_from_dll_fn_ty is a function pointer with specific parameters (which don't include int seq)
When you updated up_from_dll with different parameters, it no longer matches the type specified by up_from_dll_fn_ty and expected as the second parameter for dll_wifi_new_state. You'll need to add the parameter to up_from_dll_fn_ty and you should be good.
If you post the definition of up_from_dll_fn_ty, it would make the question have all the information and allow me to help you more if you still need it.
You're looking for something like:
typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length);
and change it to
typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length, int seq);
Here's a link to a question that has good information about creating typedefs for function pointers:
Understanding typedefs for function pointers in C

Ignore "initialization from incompatible pointer type" warnings?

Is there a compiler directive in order to ignore the "initialization from incompatible pointer type" warnings in Hardware_MouseDrivers_GPM_Methods and Hardware_MouseDrivers_DevInput_Methods? Turning off warnings globally is not an option though.
#include <stdio.h>
/* Mouse driver interface */
typedef struct _Hardware_MouseDriver {
int (*open)(void*, char *);
int (*close)(void*);
int (*poll)(void*);
} Hardware_MouseDriver;
/* GPM */
typedef struct _Hardware_MouseDrivers_GPM {
char *path;
} Hardware_MouseDrivers_GPM;
static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path);
static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this);
static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this);
static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path) {
printf("GPM: Opening %s...\n", path);
this->path = path;
}
static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this) {
printf("GPM: Closing %s...\n", this->path);
}
static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this) {
printf("GPM: Polling %s...\n", this->path);
}
Hardware_MouseDriver Hardware_MouseDrivers_GPM_Methods = {
.open = Hardware_MouseDrivers_GPM_Open,
.close = Hardware_MouseDrivers_GPM_Close,
.poll = Hardware_MouseDrivers_GPM_Poll
};
/* DevInput */
typedef struct _Hardware_MouseDrivers_DevInput {
char *path;
} Hardware_MouseDrivers_DevInput;
static int Hardware_MouseDrivers_DevInput_Open(Hardware_MouseDrivers_DevInput *this, char *path);
static int Hardware_MouseDrivers_DevInput_Close(Hardware_MouseDrivers_DevInput *this);
static int Hardware_MouseDrivers_DevInput_Poll(Hardware_MouseDrivers_DevInput *this);
static int Hardware_MouseDrivers_DevInput_Open(Hardware_MouseDrivers_DevInput *this, char *path) {
printf("DevInput: Opening %s...\n", path);
this->path = path;
}
static int Hardware_MouseDrivers_DevInput_Close(Hardware_MouseDrivers_DevInput *this) {
printf("DevInput: Closing %s...\n", this->path);
}
static int Hardware_MouseDrivers_DevInput_Poll(Hardware_MouseDrivers_DevInput *this) {
printf("DevInput: Polling %s...\n", this->path);
}
Hardware_MouseDriver Hardware_MouseDrivers_DevInput_Methods = {
.open = Hardware_MouseDrivers_DevInput_Open,
.close = Hardware_MouseDrivers_DevInput_Close,
.poll = Hardware_MouseDrivers_DevInput_Poll
};
/* Test drivers */
void TestDriver(Hardware_MouseDriver driver, void *data) {
/* Access the driver using a generic interface
* (Hardware_MouseDriver) */
driver.poll(data);
}
void main() {
Hardware_MouseDrivers_GPM gpm;
Hardware_MouseDrivers_DevInput devinput;
Hardware_MouseDrivers_GPM_Open(&gpm, "/dev/gpmctl");
Hardware_MouseDrivers_DevInput_Open(&devinput, "/dev/input/mice");
TestDriver(Hardware_MouseDrivers_GPM_Methods, &gpm);
TestDriver(Hardware_MouseDrivers_DevInput_Methods, &devinput);
Hardware_MouseDrivers_GPM_Close(&gpm);
Hardware_MouseDrivers_DevInput_Close(&devinput);
}
Cast the assignments to the proper types (function pointers with void * rather than your instance pointer):
.open= (int (*)(void*, char *))Hardware_MouseDrivers_GPM_Open;
Or make a type and use it in the definition and initialization of the struct:
typedef int (*openfcnt_t)(void*, char *);
typedef struct _Hardware_MouseDriver {
openfnct_t open;
} Hardware_MouseDriver;
and then
.open= (openfnct_t)Hardware_MouseDrivers_GPM_Open;
EDIT:
Upon further thought the easiest and least fiddly way for a C program will be:
.open= (void *)Hardware_MouseDrivers_GPM_Open;
I guess the obvious answer to this is the question "why not fix the code to use the right pointer type"?
EDIT:
OK, I can understand that you don't want to complicate the code unnecessarily, but I don't think it's that much of a complication, or even an unneccessary one.
Let's look at the field open in the struct Hardware_MouseDriver, which is supposed to be a pointer to a function that takes a pointer to void as its first argument.
To initialize this field, you use a pointer to the function Hardware_MouseDrivers_GPM_Open, and at another place a pointer to the function Hardware_MouseDrivers_DevInput_Open. None of these take a pointer to void as their first argument, and this is of course what the compiler warns about.
Now, if a void pointer is the same size as these pointers, and there are no other surprising differences between how they are stored and handled, calls to these functions through the open pointer will work as expected. It is likely that it will, and I guess that with this type of low-level code it is unlikely that someone will port it to TOPS-20 or something. But there is no guarantee that it
will work, and it looks (to me) strange. (And to the compiler, obviously!)
So my suggestion would be to change code like this:
static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path) {
printf("GPM: Opening %s...\n", path);
this->path = path;
}
to the just slightly more complicated:
static int Hardware_MouseDrivers_GPM_Open(void *arg1, char *path) {
Hardware_MouseDrivers_GPM *this = arg1;
printf("GPM: Opening %s...\n", path);
this->path = path;
}
I think this change would be easier and less complicated than (1) turning off the warnings, (2) documenting it so readers can understand why that warning isn't supposed to be important here, (3) documenting it some more so your readers actually believe that you know what you are doing, and (4) handling the problems that will occur if someone actually does port your code to TOPS-20.
I had this problem and after careful examination, I decided that I should not have gotten this message. Similar lines in the structure did not generate this error.
Using (void *) function_name fixed it.
This saved me from having to examine the gcc tree.

How do I write a dispatcher, if my compiler's support for pointers-to-functions is broken?

I am working on an embedded application where the device is controlled through a command interface. I mocked the command dispatcher in VC and had it working to my satisfaction; but when I then moved the code over to the embedded environment, I found out that the compiler has a broken implementation of pointer-to-func's.
Here's how I originally implemented the code (in VC):
/* Relevant parts of header file */
typedef struct command {
const char *code;
void *set_dispatcher;
void *get_dispatcher;
const char *_description;
} command_t;
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label, &set_##dispatcher, &get_##dispatcher, (const char*)description}
/* Dispatcher data structure in the C file */
const command_t commands[] = {
COMMAND_ENTRY("DH", Dhcp, "DHCP (0=off, 1=on)"),
COMMAND_ENTRY("IP", Ip, "IP Address (192.168.1.205)"),
COMMAND_ENTRY("SM", Subnet, "Subunet Mask (255.255.255.0)"),
COMMAND_ENTRY("DR", DefaultRoute, "Default router (192.168.1.1)"),
COMMAND_ENTRY("UN", Username, "Web username"),
COMMAND_ENTRY("PW", Password, "Web password"),
...
}
/* After matching the received command string to the command "label", the command is dispatched */
if (pc->isGetter)
return ((get_fn_t)(commands[i].get_dispatcher))(pc);
else
return ((set_fn_t)(commands[i].set_dispatcher))(pc);
}
Without the use of function pointers, it seems like my only hope is to use switch()/case statements to call functions. But I'd like to avoid having to manually maintain a large switch() statement.
What I was thinking of doing is moving all the COMMAND_ENTRY lines into a separate include file. Then wraps that include file with varying #define and #undefines. Something like:
/* Create enum's labels */
#define COMMAND_ENTRY(label,dispatcher,description) SET_##dispatcher, GET_##dispatcher
typedef enum command_labels = {
#include "entries.cinc"
DUMMY_ENUM_ENTRY} command_labels_t;
#undefine COMMAND_ENTRY
/* Create command mapping table */
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label, SET_##dispatcher, GET_##dispatcher, (const char*)description}
const command_t commands[] = {
#include "entries.cinc"
NULL /* dummy */ };
#undefine COMMAND_ENTRY
/*...*/
int command_dispatcher(command_labels_t dispatcher_id) {
/* Create dispatcher switch statement */
#define COMMAND_ENTRY(label,dispatcher,description) case SET_##dispatcher: return set_##dispatcher(pc); case GET_##dispatcher: return get_##dispatcher(pc);
switch(dispatcher_id) {
#include "entries.cinc"
default:
return NOT_FOUND;
}
#undefine COMMAND_ENTRY
}
Does anyone see a better way to handle this situation? Sadly, 'get another compiler' is not a viable option. :(
--- Edit to add:
Just to clarify, the particular embedded environment is broken in that the compiler is supposed to create a "function-pointer table" which is then used by the compiler to resolve calls to functions through a pointer. Unfortunately, the compiler is broken and doesn't generate a correct function-table.
So I don't have an easy way to extract the func address to invoke it.
--- Edit #2:
Ah, yes, the use of void *(set|get)_dispatcher was my attempt to see if the problem was with the typedefine of the func pointers. Originally, I had
typedef int (*set_fn_t)(cmdContext_t *pCmdCtx);
typedef int (*get_fn_t)(cmdContext_t *pCmdCtx);
typedef struct command {
const char *code;
set_fn_t set_dispatcher;
get_fn_t get_dispatcher;
const char *_description;
} command_t;
You should try changing your struct command so the function pointers have the actual type:
typedef struct command {
const char *code;
set_fn_t set_dispatcher;
get_fn_t get_dispatcher;
const char *_description;
} command_t;
Unfortunately, function pointers are not guaranteed to be able to convert to/from void pointers (that applies only to pointers to objects).
What's the embedded environment?
Given the information posted in the updates to the question, I see that it's really a bugged compiler.
I think that your proposed solution seems pretty reasonable - it's probably similar to what I would have come up with.
A function pointer isn't actually required to fit in a void*. You could check to make sure that the value you're calling is actually the address of the function. If not, use a function pointer type in the struct: either get_fn_t, or IIRC void(*)(void) is guaranteed to be compatible with any function pointer type.
Edit: OK, assuming that calling by value can't be made to work, I can't think of a neater way to do what you need than auto-generating the switch statement. You could maybe use an off-the-shelf ASP-style preprocessor mode for ruby/python/perl/php/whatever prior to the C preprocessor. Something like this:
switch(dispatcher_id) {
<% for c in commands %>
case SET_<% c.dispatcher %>: return set_<% c.dispatcher %>(pc);
case GET_<% c.dispatcher %>: return get_<% c.dispatcher %>(pc);
<% end %>
default:
return NOT_FOUND;
}
might be a bit more readable than the macro/include trick, but introducing a new tool and setting up the makefiles is probably not worth it for such a small amount of code. And the line numbers in the debug info won't relate to the file you think of as the source file unless you do extra work in your preprocessor to specify them.
Can you get the vendor to fix the compiler?
To what extent is the pointer-to-function broken?
If the compiler allows you to get the address of a function (I'm from C++, but &getenv is what I mean), you could wrap the calling convention stuff into assembler.
As said, I'm a C++ssie, but something in the way of
; function call
push [arg1]
push [arg2]
call [command+8] ; at the 4th location, the setter is stored
ret
If even that is broken, you could define an array of extern void* pointers which you define, again, in assembly.
try this syntax:
return (*((get_fn_t)commands[i].get_dispatcher))(pc);
It's been awhile since I've done C & function pointers, but I believe the original C syntax required the * when dereferencing function pointers but most compilers would let you get away without it.
Do you have access to the link map?
If so, maybe you can hack your way around the wonky function-pointer table:
unsigned long addr_get_dhcp = 0x1111111;
unsigned long addr_set_dhcp = 0x2222222; //make these unique numbers.
/* Relevant parts of header file */
typedef struct command {
const char *code;
unsigned long set_dispatcher;
unsigned long get_dispatcher;
const char *_description;
} command_t;
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label,
addr_set_##dispatcher, addr_get_##dispatcher, (const char*)description}
Now compile, grab the relevant addresses from the link map, replace the constants, and recompile. Nothing should move, so the map ought to stay the same. (Making the original constants unique should prevent the compiler from collapsing identical values into one storage location. You may need a long long, depending on the architecture)
If the concept works, you could probably add a post-link step running a script to do the replacement automagically. Of course, this is just a theory, it may fail miserably.
Maybe, you need to look into the structure again:
typedef struct command {
const char *code;
void *set_dispatcher; //IMO, it does not look like a function pointer...
void *get_dispatcher; //more like a pointer to void
const char *_description;
} command_t;
Let say your dispatchers have the following similar function definition:
//a function pointer type definition
typedef int (*genericDispatcher)(int data);
Assume that the dispatchers are like below:
int set_DhcpDispatcher(int data) { return data; }
int get_DhcpDispatcher(int data) { return 2*data; }
So, the revised structure will be:
typedef struct command {
const char *code;
genericDispatcher set_dispatcher;
genericDispatcher get_dispatcher;
const char *_description;
} command_t;
Your macro will be:
#define COMMAND_ENTRY(label,dispatcher,description) \
{ (const char*)label, \
set_##dispatcher##Dispatcher, \
get_##dispatcher##Dispatcher, \
(const char*)description }
Then, you can set your array as usual:
int main(int argc, char **argv)
{
int value1 = 0, value2 = 0;
const command_t commands[] = {
COMMAND_ENTRY("DH", Dhcp, "DHCP (0=off, 1=on)")
};
value1 = commands[0].set_dispatcher(1);
value2 = commands[0].get_dispatcher(2);
printf("value1 = %d, value2 = %d", value1, value2);
return 0;
}
Correct me, if I am wrong somewhere... ;)

Object-orientation in C

What would be a set of nifty preprocessor hacks (ANSI C89/ISO C90 compatible) which enable some kind of ugly (but usable) object-orientation in C?
I am familiar with a few different object-oriented languages, so please don't respond with answers like "Learn C++!". I have read "Object-Oriented Programming With ANSI C" (beware: PDF format) and several other interesting solutions, but I'm mostly interested in yours :-)!
See also Can you write object oriented code in C?
I would advise against preprocessor (ab)use to try and make C syntax more like that of another more object-oriented language. At the most basic level, you just use plain structs as objects and pass them around by pointers:
struct monkey
{
float age;
bool is_male;
int happiness;
};
void monkey_dance(struct monkey *monkey)
{
/* do a little dance */
}
To get things like inheritance and polymorphism, you have to work a little harder. You can do manual inheritance by having the first member of a structure be an instance of the superclass, and then you can cast around pointers to base and derived classes freely:
struct base
{
/* base class members */
};
struct derived
{
struct base super;
/* derived class members */
};
struct derived d;
struct base *base_ptr = (struct base *)&d; // upcast
struct derived *derived_ptr = (struct derived *)base_ptr; // downcast
To get polymorphism (i.e. virtual functions), you use function pointers, and optionally function pointer tables, also known as virtual tables or vtables:
struct base;
struct base_vtable
{
void (*dance)(struct base *);
void (*jump)(struct base *, int how_high);
};
struct base
{
struct base_vtable *vtable;
/* base members */
};
void base_dance(struct base *b)
{
b->vtable->dance(b);
}
void base_jump(struct base *b, int how_high)
{
b->vtable->jump(b, how_high);
}
struct derived1
{
struct base super;
/* derived1 members */
};
void derived1_dance(struct derived1 *d)
{
/* implementation of derived1's dance function */
}
void derived1_jump(struct derived1 *d, int how_high)
{
/* implementation of derived 1's jump function */
}
/* global vtable for derived1 */
struct base_vtable derived1_vtable =
{
&derived1_dance, /* you might get a warning here about incompatible pointer types */
&derived1_jump /* you can ignore it, or perform a cast to get rid of it */
};
void derived1_init(struct derived1 *d)
{
d->super.vtable = &derived1_vtable;
/* init base members d->super.foo */
/* init derived1 members d->foo */
}
struct derived2
{
struct base super;
/* derived2 members */
};
void derived2_dance(struct derived2 *d)
{
/* implementation of derived2's dance function */
}
void derived2_jump(struct derived2 *d, int how_high)
{
/* implementation of derived2's jump function */
}
struct base_vtable derived2_vtable =
{
&derived2_dance,
&derived2_jump
};
void derived2_init(struct derived2 *d)
{
d->super.vtable = &derived2_vtable;
/* init base members d->super.foo */
/* init derived1 members d->foo */
}
int main(void)
{
/* OK! We're done with our declarations, now we can finally do some
polymorphism in C */
struct derived1 d1;
derived1_init(&d1);
struct derived2 d2;
derived2_init(&d2);
struct base *b1_ptr = (struct base *)&d1;
struct base *b2_ptr = (struct base *)&d2;
base_dance(b1_ptr); /* calls derived1_dance */
base_dance(b2_ptr); /* calls derived2_dance */
base_jump(b1_ptr, 42); /* calls derived1_jump */
base_jump(b2_ptr, 42); /* calls derived2_jump */
return 0;
}
And that's how you do polymorphism in C. It ain't pretty, but it does the job. There are some sticky issues involving pointer casts between base and derived classes, which are safe as long as the base class is the first member of the derived class. Multiple inheritance is much harder - in that case, in order to case between base classes other than the first, you need to manually adjust your pointers based on the proper offsets, which is really tricky and error-prone.
Another (tricky) thing you can do is change the dynamic type of an object at runtime! You just reassign it a new vtable pointer. You can even selectively change some of the virtual functions while keeping others, creating new hybrid types. Just be careful to create a new vtable instead of modifying the global vtable, otherwise you'll accidentally affect all objects of a given type.
I once worked with a C library that was implemented in a way that struck me as quite elegant. They had written, in C, a way to define objects, then inherit from them so that they were as extensible as a C++ object. The basic idea was this:
Each object had its own file
Public functions and variables are defined in the .h file for an object
Private variables and functions were only located in the .c file
To "inherit" a new struct is created with the first member of the struct being the object to inherit from
Inheriting is difficult to describe, but basically it was this:
struct vehicle {
int power;
int weight;
}
Then in another file:
struct van {
struct vehicle base;
int cubic_size;
}
Then you could have a van created in memory, and being used by code that only knew about vehicles:
struct van my_van;
struct vehicle *something = &my_van;
vehicle_function( something );
It worked beautifully, and the .h files defined exactly what you should be able to do with each object.
C Object System (COS) sounds promising (it's still in alpha version). It tries to keep minimal the available concepts for the sake of simplicity and flexibility: uniform object oriented programming including open classes, metaclasses, property metaclasses, generics, multimethods, delegation, ownership, exceptions, contracts and closures. There is a draft paper (PDF) that describes it.
Exception in C is a C89 implementation of the TRY-CATCH-FINALLY found in other OO languages. It comes with a testsuite and some examples.
Both by Laurent Deniau, which is working a lot on OOP in C.
The GNOME desktop for Linux is written in object-oriented C, and it has an object model called "GObject" which supports properties, inheritance, polymorphism, as well as some other goodies like references, event handling (called "signals"), runtime typing, private data, etc.
It includes preprocessor hacks to do things like typecasting around in the class hierarchy, etc. Here's an example class I wrote for GNOME (things like gchar are typedefs):
Class Source
Class Header
Inside the GObject structure there's a GType integer which is used as a magic number for GLib's dynamic typing system (you can cast the entire struct to a "GType" to find it's type).
Slightly off-topic, but the original C++ compiler, Cfront, compiled C++ to C and then to assembler.
Preserved here.
If you think of methods called on objects as static methods that pass an implicit 'this' into the function it can make thinking OO in C easier.
For example:
String s = "hi";
System.out.println(s.length());
becomes:
string s = "hi";
printf(length(s)); // pass in s, as an implicit this
Or something like that.
I used to do this kind of thing in C, before I knew what OOP was.
Following is an example, which implements a data-buffer which grows on demand, given a minimum size, increment and maximum size. This particular implementation was "element" based, which is to say it was designed to allow a list-like collection of any C type, not just a variable length byte-buffer.
The idea is that the object is instantiated using the xxx_crt() and deleted using xxx_dlt(). Each of the "member" methods takes a specifically typed pointer to operate on.
I implemented a linked list, cyclic buffer, and a number of other things in this manner.
I must confess, I have never given any thought on how to implement inheritance with this approach. I imagine that some blend of that offered by Kieveli might be a good path.
dtb.c:
#include <limits.h>
#include <string.h>
#include <stdlib.h>
static void dtb_xlt(void *dst, const void *src, vint len, const byte *tbl);
DTABUF *dtb_crt(vint minsiz,vint incsiz,vint maxsiz) {
DTABUF *dbp;
if(!minsiz) { return NULL; }
if(!incsiz) { incsiz=minsiz; }
if(!maxsiz || maxsiz<minsiz) { maxsiz=minsiz; }
if(minsiz+incsiz>maxsiz) { incsiz=maxsiz-minsiz; }
if((dbp=(DTABUF*)malloc(sizeof(*dbp))) == NULL) { return NULL; }
memset(dbp,0,sizeof(*dbp));
dbp->min=minsiz;
dbp->inc=incsiz;
dbp->max=maxsiz;
dbp->siz=minsiz;
dbp->cur=0;
if((dbp->dta=(byte*)malloc((vuns)minsiz)) == NULL) { free(dbp); return NULL; }
return dbp;
}
DTABUF *dtb_dlt(DTABUF *dbp) {
if(dbp) {
free(dbp->dta);
free(dbp);
}
return NULL;
}
vint dtb_adddta(DTABUF *dbp,const byte *xlt256,const void *dtaptr,vint dtalen) {
if(!dbp) { errno=EINVAL; return -1; }
if(dtalen==-1) { dtalen=(vint)strlen((byte*)dtaptr); }
if((dbp->cur + dtalen) > dbp->siz) {
void *newdta;
vint newsiz;
if((dbp->siz+dbp->inc)>=(dbp->cur+dtalen)) { newsiz=dbp->siz+dbp->inc; }
else { newsiz=dbp->cur+dtalen; }
if(newsiz>dbp->max) { errno=ETRUNC; return -1; }
if((newdta=realloc(dbp->dta,(vuns)newsiz))==NULL) { return -1; }
dbp->dta=newdta; dbp->siz=newsiz;
}
if(dtalen) {
if(xlt256) { dtb_xlt(((byte*)dbp->dta+dbp->cur),dtaptr,dtalen,xlt256); }
else { memcpy(((byte*)dbp->dta+dbp->cur),dtaptr,(vuns)dtalen); }
dbp->cur+=dtalen;
}
return 0;
}
static void dtb_xlt(void *dst,const void *src,vint len,const byte *tbl) {
byte *sp,*dp;
for(sp=(byte*)src,dp=(byte*)dst; len; len--,sp++,dp++) { *dp=tbl[*sp]; }
}
vint dtb_addtxt(DTABUF *dbp,const byte *xlt256,const byte *format,...) {
byte textÝ501¨;
va_list ap;
vint len;
va_start(ap,format); len=sprintf_len(format,ap)-1; va_end(ap);
if(len<0 || len>=sizeof(text)) { sprintf_safe(text,sizeof(text),"STRTOOLNG: %s",format); len=(int)strlen(text); }
else { va_start(ap,format); vsprintf(text,format,ap); va_end(ap); }
return dtb_adddta(dbp,xlt256,text,len);
}
vint dtb_rmvdta(DTABUF *dbp,vint len) {
if(!dbp) { errno=EINVAL; return -1; }
if(len > dbp->cur) { len=dbp->cur; }
dbp->cur-=len;
return 0;
}
vint dtb_reset(DTABUF *dbp) {
if(!dbp) { errno=EINVAL; return -1; }
dbp->cur=0;
if(dbp->siz > dbp->min) {
byte *newdta;
if((newdta=(byte*)realloc(dbp->dta,(vuns)dbp->min))==NULL) {
free(dbp->dta); dbp->dta=null; dbp->siz=0;
return -1;
}
dbp->dta=newdta; dbp->siz=dbp->min;
}
return 0;
}
void *dtb_elmptr(DTABUF *dbp,vint elmidx,vint elmlen) {
if(!elmlen || (elmidx*elmlen)>=dbp->cur) { return NULL; }
return ((byte*)dbp->dta+(elmidx*elmlen));
}
dtb.h
typedef _Packed struct {
vint min; /* initial size */
vint inc; /* increment size */
vint max; /* maximum size */
vint siz; /* current size */
vint cur; /* current data length */
void *dta; /* data pointer */
} DTABUF;
#define dtb_dtaptr(mDBP) (mDBP->dta)
#define dtb_dtalen(mDBP) (mDBP->cur)
DTABUF *dtb_crt(vint minsiz,vint incsiz,vint maxsiz);
DTABUF *dtb_dlt(DTABUF *dbp);
vint dtb_adddta(DTABUF *dbp,const byte *xlt256,const void *dtaptr,vint dtalen);
vint dtb_addtxt(DTABUF *dbp,const byte *xlt256,const byte *format,...);
vint dtb_rmvdta(DTABUF *dbp,vint len);
vint dtb_reset(DTABUF *dbp);
void *dtb_elmptr(DTABUF *dbp,vint elmidx,vint elmlen);
PS: vint was simply a typedef of int - I used it to remind me that it's length was variable from platform to platform (for porting).
I think what Adam Rosenfield posted is the correct way of doing OOP in C. I'd like to add that what he shows is the implementation of the object. In other words the actual implementation would be put in the .c file, while the interface would be put in the header .h file. For example, using the monkey example above:
The interface would look like:
//monkey.h
struct _monkey;
typedef struct _monkey monkey;
//memory management
monkey * monkey_new();
int monkey_delete(monkey *thisobj);
//methods
void monkey_dance(monkey *thisobj);
You can see in the interface .h file you are only defining prototypes. You can then compile the implementation part " .c file" into a static or dynamic library. This creates encapsulation and also you can change the implementation at will. The user of your object needs to know almost nothing about the implementation of it. This also places focus on the overall design of the object.
It's my personal belief that oop is a way of conceptualizing your code structure and reusability and has really nothing to do with those other things that are added to c++ like overloading or templates. Yes those are very nice useful features but they are not representative of what object oriented programming really is.
ffmpeg (a toolkit for video processing) is written in straight C (and assembly language), but using an object-oriented style. It's full of structs with function pointers. There are a set of factory functions that initialize the structs with the appropriate "method" pointers.
If you really thinks catefully, even standard C library use OOP - consider FILE * as an example: fopen() initializes an FILE * object, and you use it use member methods fscanf(), fprintf(), fread(), fwrite() and others, and eventually finalize it with fclose().
You can also go with the pseudo-Objective-C way which is not difficult as well:
typedef void *Class;
typedef struct __class_Foo
{
Class isa;
int ivar;
} Foo;
typedef struct __meta_Foo
{
Foo *(*alloc)(void);
Foo *(*init)(Foo *self);
int (*ivar)(Foo *self);
void (*setIvar)(Foo *self);
} meta_Foo;
meta_Foo *class_Foo;
void __meta_Foo_init(void) __attribute__((constructor));
void __meta_Foo_init(void)
{
class_Foo = malloc(sizeof(meta_Foo));
if (class_Foo)
{
class_Foo = {__imp_Foo_alloc, __imp_Foo_init, __imp_Foo_ivar, __imp_Foo_setIvar};
}
}
Foo *__imp_Foo_alloc(void)
{
Foo *foo = malloc(sizeof(Foo));
if (foo)
{
memset(foo, 0, sizeof(Foo));
foo->isa = class_Foo;
}
return foo;
}
Foo *__imp_Foo_init(Foo *self)
{
if (self)
{
self->ivar = 42;
}
return self;
}
// ...
To use:
int main(void)
{
Foo *foo = (class_Foo->init)((class_Foo->alloc)());
printf("%d\n", (foo->isa->ivar)(foo)); // 42
foo->isa->setIvar(foo, 60);
printf("%d\n", (foo->isa->ivar)(foo)); // 60
free(foo);
}
This is what may be resulted from some Objective-C code like this, if a pretty-old Objective-C-to-C translator is used:
#interface Foo : NSObject
{
int ivar;
}
- (int)ivar;
- (void)setIvar:(int)ivar;
#end
#implementation Foo
- (id)init
{
if (self = [super init])
{
ivar = 42;
}
return self;
}
#end
int main(void)
{
Foo *foo = [[Foo alloc] init];
printf("%d\n", [foo ivar]);
[foo setIvar:60];
printf("%d\n", [foo ivar]);
[foo release];
}
My recommendation: keep it simple. One of the biggest issues I have is maintaining older software (sometimes over 10 years old). If the code is not simple, it can be difficult. Yes, one can write very useful OOP with polymorphism in C, but it can be difficult to read.
I prefer simple objects that encapsulate some well-defined functionality. A great example of this is GLIB2, for example a hash table:
GHastTable* my_hash = g_hash_table_new(g_str_hash, g_str_equal);
int size = g_hash_table_size(my_hash);
...
g_hash_table_remove(my_hash, some_key);
The keys are:
Simple architecture and design pattern
Achieves basic OOP encapsulation.
Easy to implement, read, understand, and maintain
I'm a bit late to the party here but I like to avoid both macro extremes - too many or too much obfuscates code, but a couple obvious macros can make the OOP code easier to develop and read:
/*
* OOP in C
*
* gcc -o oop oop.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct obj2d {
float x; // object center x
float y; // object center y
float (* area)(void *);
};
#define X(obj) (obj)->b1.x
#define Y(obj) (obj)->b1.y
#define AREA(obj) (obj)->b1.area(obj)
void *
_new_obj2d(int size, void * areafn)
{
struct obj2d * x = calloc(1, size);
x->area = areafn;
// obj2d constructor code ...
return x;
}
// --------------------------------------------------------
struct rectangle {
struct obj2d b1; // base class
float width;
float height;
float rotation;
};
#define WIDTH(obj) (obj)->width
#define HEIGHT(obj) (obj)->height
float rectangle_area(struct rectangle * self)
{
return self->width * self->height;
}
#define NEW_rectangle() _new_obj2d(sizeof(struct rectangle), rectangle_area)
// --------------------------------------------------------
struct triangle {
struct obj2d b1;
// deliberately unfinished to test error messages
};
#define NEW_triangle() _new_obj2d(sizeof(struct triangle), triangle_area)
// --------------------------------------------------------
struct circle {
struct obj2d b1;
float radius;
};
#define RADIUS(obj) (obj)->radius
float circle_area(struct circle * self)
{
return M_PI * self->radius * self->radius;
}
#define NEW_circle() _new_obj2d(sizeof(struct circle), circle_area)
// --------------------------------------------------------
#define NEW(objname) (struct objname *) NEW_##objname()
int
main(int ac, char * av[])
{
struct rectangle * obj1 = NEW(rectangle);
struct circle * obj2 = NEW(circle);
X(obj1) = 1;
Y(obj1) = 1;
// your decision as to which of these is clearer, but note above that
// macros also hide the fact that a member is in the base class
WIDTH(obj1) = 2;
obj1->height = 3;
printf("obj1 position (%f,%f) area %f\n", X(obj1), Y(obj1), AREA(obj1));
X(obj2) = 10;
Y(obj2) = 10;
RADIUS(obj2) = 1.5;
printf("obj2 position (%f,%f) area %f\n", X(obj2), Y(obj2), AREA(obj2));
// WIDTH(obj2) = 2; // error: struct circle has no member named width
// struct triangle * obj3 = NEW(triangle); // error: triangle_area undefined
}
I think this has a good balance, and the errors it generates (at least with default gcc 6.3 options) for some of the more likely mistakes are helpful instead of confusing. The whole point is to improve programmer productivity no?
#include "triangle.h"
#include "rectangle.h"
#include "polygon.h"
#include <stdio.h>
int main()
{
Triangle tr1= CTriangle->new();
Rectangle rc1= CRectangle->new();
tr1->width= rc1->width= 3.2;
tr1->height= rc1->height= 4.1;
CPolygon->printArea((Polygon)tr1);
printf("\n");
CPolygon->printArea((Polygon)rc1);
}
Output:
6.56
13.12
Here is a show of what is OO programming with C.
This is real, pure C, no preprocessor macros. We have inheritance,
polymorphism and data encapsulation (including data private to classes or objects).
There is no chance for protected qualifier equivalent, that is,
private data is private down the innheritance chain too.
But this is not an inconvenience because I don't think it is necessary.
CPolygon is not instantiated because we only use it to manipulate objects
of down the innheritance chain that have common aspects but different
implementation of them (Polymorphism).
If I were going to write OOP in C I would probably go with a pseudo-Pimpl design. Instead of passing pointers to structs, you end up passing pointers to pointers to structs. This makes the content opaque and facilitates polymorphism and inheritance.
The real problem with OOP in C is what happens when variables exit scope. There are no compiler-generated destructors and that can cause issues. Macros can possibly help, but it is always going to be ugly to look at.
I'm also working on this based on a macro solution. So it is for the bravest only, I guess ;-) But it is quite nice already, and I'm already working on a few projects on top of it.
It works so that you first define a separate header file for each class. Like this:
#define CLASS Point
#define BUILD_JSON
#define Point__define \
METHOD(Point,public,int,move_up,(int steps)) \
METHOD(Point,public,void,draw) \
\
VAR(read,int,x,JSON(json_int)) \
VAR(read,int,y,JSON(json_int)) \
To implement the class, you create a header file for it and a C file where you implement the methods:
METHOD(Point,public,void,draw)
{
printf("point at %d,%d\n", self->x, self->y);
}
In the header you created for the class, you include other headers you need and define types etc. related to the class. In both the class header and in the C file you include the class specification file (see the first code example) and an X-macro. These X-macros (1,2,3 etc.) will expand the code to the actual class structs and other declarations.
To inherit a class, #define SUPER supername and add supername__define \ as the first line in the class definition. Both must be there. There is also JSON support, signals, abstract classes, etc.
To create an object, just use W_NEW(classname, .x=1, .y=2,...). The initialization is based on struct initialization introduced in C11. It works nicely and everything not listed is set to zero.
To call a method, use W_CALL(o,method)(1,2,3). It looks like a higher order function call but it is just a macro. It expands to ((o)->klass->method(o,1,2,3)) which is a really nice hack.
See Documentation and the code itself.
Since the framework needs some boilerplate code, I wrote a Perl script (wobject) that does the job. If you use that, you can just write
class Point
public int move_up(int steps)
public void draw()
read int x
read int y
and it will create the class specification file, class header, and a C file, which includes Point_impl.c where you implement the class. It saves quite a lot of work, if you have many simple classes but still everything is in C. wobject is a very simple regular expression based scanner which is easy to adapt to specific needs, or to be rewritten from scratch.
Another way to program in an object oriented style with C is to use a code generator which transforms a domain specific language to C. As it's done with TypeScript and JavaScript to bring OOP to js.
I'd suggest you to try out COOP
It features Classes, Inheritance, Exceptions, Memory management, its own Unit Testing Framework for C, and more.
All of this while maintaining type safety and (many parts of the) intellisence!
And, yes, it uses Macro magics to do it.
#Adam Rosenfield has a very good explanation of how to achieve OOP with C
Besides, I would recommend you to read
1) pjsip
A very good C library for VoIP. You can learn how it achieves OOP though structs and function pointer tables
2) iOS Runtime
Learn how iOS Runtime powers Objective C. It achieves OOP through isa pointer, meta class
For me object orientation in C should have these features:
Encapsulation and data hiding (can be achieved using structs/opaque pointers)
Inheritance and support for polymorphism (single inheritance can be achieved using structs - make sure the abstract base is not instantiable)
Constructor and destructor functionality (not easy to achieve)
Type checking (at least for user-defined types as C doesn't enforce any)
Reference counting (or something to implement RAII)
Limited support for exception handling (setjmp and longjmp)
On top of the above it should rely on ANSI/ISO specifications and should not rely on compiler-specific functionality.
Look at http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. If nothing else reading through the documentation is an enlightening experience.
If you need to write a little code
try this: https://github.com/fulminati/class-framework
#include "class-framework.h"
CLASS (People) {
int age;
};
int main()
{
People *p = NEW (People);
p->age = 10;
printf("%d\n", p->age);
}
The open-source Dynace project does exactly that. It's at https://github.com/blakemcbride/Dynace
I have managed to implement inheritance and polymorphism in C.
I can do single inheritance with virtual tables and I can implement multiple interfaces with a technique where the struct that implements an interface simply creates the interface struct by giving it its own methods and a pointer to itself. The interface struct then calls these methods and, among other parameters, it passes them the pointer to the struct which created the implementation of the interface.
When it comes to inheriting non abstract classes, I have achieved that with virtual tables, I have already explained inheritance with virtual tables in this answer. The code from that answer doesn't allow implementation of multiple interfaces. In this answer however, I changed my code so that it allows implementation of multiple interfaces. Here is the entire code that I posted on github. I will post the code here as well but maybe it is more readable on github, as I put the code in multiple files.
Here is the code, I have structs Zivotinja, Pas, Automobil and the struct MozeProizvestiZvuk. This last struct is an interface. Pas and Automobil implement it. Struct Pas also inherits from Zivotinja.
Here is the code for the main function
Pas *pas = Pas_new_sve(4, 20, "some dog name");
MozeProizvestiZvuk *mozeProizvestiZvuk = pas->getMozeProizvestiZvuk(pas);
mozeProizvestiZvuk->proizvediZvuk(mozeProizvestiZvuk->strukturaKojuMetodeInterfejsaKoriste);
mozeProizvestiZvuk->proizvediZvuk(mozeProizvestiZvuk->strukturaKojuMetodeInterfejsaKoriste);
printf("number of times it made noise = %d\n", mozeProizvestiZvuk->getKolikoPutaJeProizveoZvuk(mozeProizvestiZvuk->strukturaKojuMetodeInterfejsaKoriste));
Automobil *automobil = Automobil_new("Sandero", 2009);
MozeProizvestiZvuk *zvukAutomobil = automobil->getMozeProizvestiZvuk(automobil);
for(int i=0; i<3; i++){
zvukAutomobil->proizvediZvuk(zvukAutomobil->strukturaKojuMetodeInterfejsaKoriste);
}
printf("number of times it made noise = %d\n", zvukAutomobil->getKolikoPutaJeProizveoZvuk(zvukAutomobil->strukturaKojuMetodeInterfejsaKoriste));
Zivotinja *zivotinja = Zivotinja_new(10);
zivotinja->vTable->ispisiPodatkeOZivotinji(zivotinja);
zivotinja->vTable->obrisi(&zivotinja);
Zivotinja *pasKaoZivotinja = Pas_new_sve(5, 50, "Milojko");
pasKaoZivotinja->vTable->ispisiPodatkeOZivotinji(pasKaoZivotinja);
int godine = pasKaoZivotinja->vTable->dajGodine(pasKaoZivotinja);
printf("age of the dog which was upcasted to an animal = %d \n", godine);
pasKaoZivotinja->vTable->obrisi(&pasKaoZivotinja);
Here is the MozeProizvestiZvuk.h file
#ifndef MOZE_PROIZVESTI_ZVUK_H
#define MOZE_PROIZVESTI_ZVUK_H
typedef struct MozeProizvestiZvukStruct{
void (*proizvediZvuk)(void *strukturaKojuMetodeInterfejsaKoriste);
unsigned int (*getKolikoPutaJeProizveoZvuk)(void *strukturaKojaImplementiraInterfejs);
void *strukturaKojuMetodeInterfejsaKoriste;
}MozeProizvestiZvuk;
#endif
Here is the Automobil struct which implements this interface.
#include"MozeProizvestiZvuk.h"
#include<stdlib.h>
typedef struct AutomobilStruct{
const char *naziv;
int godinaProizvodnje;
unsigned int kolikoPutaJeProizveoZvuk;
MozeProizvestiZvuk* (*getMozeProizvestiZvuk)(struct AutomobilStruct *_this);
}Automobil;
MozeProizvestiZvuk* Automobil_getMozeProizvestiZvuk(Automobil *automobil);
Automobil* Automobil_new(const char* naziv, int godiste){
Automobil *automobil = (Automobil*) malloc(sizeof(Automobil));
automobil->naziv = naziv;
automobil->godinaProizvodnje = godiste;
automobil->kolikoPutaJeProizveoZvuk = 0;
automobil->getMozeProizvestiZvuk = Automobil_getMozeProizvestiZvuk;
return automobil;
}
void Automobil_delete(Automobil **adresaAutomobilPointera){
free(*adresaAutomobilPointera);
*adresaAutomobilPointera = NULL;
}
unsigned int Automobil_getKolikoJeZvukovaProizveo(Automobil *automobil){
return automobil->kolikoPutaJeProizveoZvuk;
}
void Automobil_proizvediZvuk(Automobil *automobil){
printf("Automobil koji se zove %s, godiste %d proizvodi zvuk. \n", automobil->naziv, automobil->godinaProizvodnje);
automobil->kolikoPutaJeProizveoZvuk++;
}
MozeProizvestiZvuk* Automobil_getMozeProizvestiZvuk(Automobil *automobil){
MozeProizvestiZvuk *mozeProizvestiZvuk = (MozeProizvestiZvuk*) malloc(sizeof(MozeProizvestiZvuk));
mozeProizvestiZvuk->strukturaKojuMetodeInterfejsaKoriste = automobil;
mozeProizvestiZvuk->proizvediZvuk = Automobil_proizvediZvuk;
mozeProizvestiZvuk->getKolikoPutaJeProizveoZvuk = Automobil_getKolikoJeZvukovaProizveo;
return mozeProizvestiZvuk;
}
Here is the Zivotinja struct, this struct doesn't inherit from anything, neither does it implement any interfaces, but the struct Pas will inherit from Zivotinja.
#include<stdio.h>
#include<stdlib.h>
typedef struct ZivotinjaVTableStruct{
void (*ispisiPodatkeOZivotinji)(void *zivotinja);
int (*dajGodine) (void *zivotinja);
} ZivotinjaVTable;
typedef struct ZivotinjaStruct{
ZivotinjaVTable *vTable;
int godine;
} Zivotinja;
void ispisiPodatkeOOvojZivotinji(Zivotinja* zivotinja){
printf("Ova zivotinja ima %d godina. \n", zivotinja->godine);
}
int dajGodineOveZivotinje(Zivotinja *z){
return z->godine;
}
void Zivotinja_obrisi(Zivotinja **adresaPointeraKaZivotinji){
Zivotinja *zivotinjaZaBrisanje = *adresaPointeraKaZivotinji;
free(zivotinjaZaBrisanje);
*adresaPointeraKaZivotinji = NULL;
}
struct ZivotinjaVTableStruct zivotinjaVTableGlobal = {Zivotinja_obrisi, ispisiPodatkeOOvojZivotinji, dajGodineOveZivotinje};
Zivotinja* Zivotinja_new(int godine){
ZivotinjaVTable *vTable = &zivotinjaVTableGlobal;
Zivotinja *z = (Zivotinja*) malloc(sizeof(Zivotinja));
z->vTable = vTable;
z->godine = godine;
}
And finally, here is the struct Pas which inherits from Zivotinja and implements MozeProizvestiZvuk interface.
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include"Zivotinja.h"
#include"MozeProizvestiZvuk.h"
typedef struct PasVTableStruct{
bool (*obrisi)(void **Pas);
void (*ispisiPodatkeOZivotinji)(void *Pas);
int (*dajGodine) (void *Pas);
bool (*daLiJeVlasnikStariji) (void *Pas);
} PasVTable;
typedef struct PasStruct{
PasVTable *vTable;
int godine;
const char* vlasnik;
int godineVlasnika;
unsigned int kolikoPutaJeProizveoZvuk;
MozeProizvestiZvuk* (*getMozeProizvestiZvuk)(struct PasStruct *_this);
} Pas;
MozeProizvestiZvuk* Pas_getMozeProizvestiZvuk(Pas *_this);
void ispisiPodatkeOPsu(void *pasVoid){
Pas *pas = (Pas*)pasVoid;
printf("Pas ima %d godina, vlasnik se zove %s, vlasnik ima %d godina. \n", pas->godine, pas->vlasnik, pas->godineVlasnika);
}
int dajGodinePsa(void *pasVoid){
Pas *pas = (Pas*) pasVoid;
return pas->godine;
}
bool daLiJeVlasnikStariji(Pas *pas){
return pas->godineVlasnika >= pas->godine;
}
void Pas_obrisi(Pas **adresaPointeraPsa){
Pas *pasZaBrisanje = *adresaPointeraPsa;
free(pasZaBrisanje);
*adresaPointeraPsa = NULL;
}
struct PasVTableStruct pasVTableGlobal = {
Pas_obrisi,
ispisiPodatkeOPsu,
dajGodinePsa,
daLiJeVlasnikStariji
};
Pas* Pas_new(int godine){
Pas *z = (Pas*) malloc(sizeof(Pas));
z->godine = godine;
z->kolikoPutaJeProizveoZvuk = 0;
z->vTable = (&pasVTableGlobal);
z->getMozeProizvestiZvuk = Pas_getMozeProizvestiZvuk;
return z;
}
Pas *Pas_new_sve(int godine, int godineVlasnika, char* imeVlasnika){
Pas *pas = (Pas*) malloc(sizeof(Pas));
pas->kolikoPutaJeProizveoZvuk = 0;
pas->godine = godine;
pas->godineVlasnika = godineVlasnika;
pas->vlasnik = imeVlasnika;
pas->vTable = &pasVTableGlobal;
pas->getMozeProizvestiZvuk = Pas_getMozeProizvestiZvuk;
return pas;
}
unsigned int Pas_getBrojZvukova(Pas *_this){
return _this->kolikoPutaJeProizveoZvuk;
}
void Pas_proizvediZvuk(Pas *_this){
printf("Pas godina %d, vlasnika %s je proizveo zvuk.\n", _this->godine, _this->vlasnik);
_this->kolikoPutaJeProizveoZvuk++;
}
MozeProizvestiZvuk* Pas_getMozeProizvestiZvuk(Pas *_this){
MozeProizvestiZvuk *mozeProizvestiZvuk = (MozeProizvestiZvuk*) malloc(sizeof(MozeProizvestiZvuk));
mozeProizvestiZvuk->getKolikoPutaJeProizveoZvuk = Pas_getBrojZvukova;
mozeProizvestiZvuk->proizvediZvuk = Pas_proizvediZvuk;
mozeProizvestiZvuk->strukturaKojuMetodeInterfejsaKoriste = _this;
return mozeProizvestiZvuk;
}

Resources