Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C modules

Name: Anonymous 2012-02-24 19:19

This just dawned on me today. I think in the ``what would you add to C'' thread, someone wanted nice modules, and they had some sort of export preprocessor thing to get stuff like MyModule.Function(). What about using a simple struct solution, i.e.

In the .h file:
#ifndef INCLUDE_MYMODULE_H
#define INCLUDE_MYMODULE_H

void LongassHiddenName_MyModule_Foo();
int LongassHiddenName_MyModule_Bar(int);

struct _LongassHiddenName_MyModulePackage {
    void (*Foo)();
    int (*Bar)(int);
};
struct _LongassHiddenName_MyModulePackage MyModule;

#endif


And in the .c file:
/* ... */
#include "mymodule.h"

MyModule.Foo = &LongassHiddenName_MyModule_Foo;
MyModule.Bar = &LongassHiddenName_MyModule_Bar;

/* Define the methods ... */


So that other things including mymodule.h now can do shit like MyModule.Foo(); or MyModule.Bar(42);. Sure it's not great, but has anyone done shit like this? Or some const variation?

Name: Anonymous 2012-02-24 20:10

Using static functions prevents them from being visible outside the file in which they are defined.
FooBar.h:
#define interface struct
#define import extern
#define private static
#define public
interface FooBar {
     void (*Foo)();
     int (*Bar)(int);
};
#define FooBar(foo, bar) { foo, bar }

MyModule.c:

#include "FooBar.h"
private void foo(void) {
     /* ... foo implementation ... */
}

private int bar(int i) {
     /* ... bar implementation ... */
}

public interface FooBar MyModule = FooBar(foo, bar);

main.c
#include "FooBar.h"
import interface FooBar MyModule;

int main(void) {
     MyModule.Foo();
     MyModule.Bar(5);
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List