Callbacks, Objects and Private Methods in C?

I’ve been tinkering with C  for a few hours after getting a bit tired of all the OOP and C++.  I just tried to do some high-level stuff, and here’s one or two ideas that worked. I don’t recommend any C++ developers to start using these instead of the good old OO System. That would be re-inventing the wheel. I’m just sharing these because they seem interesting.

pro_lang.h

/* header guards etc. */

typedef struct _Pro_Object Pro_Object;

struct _Pro_Object
{
    /* members */
    int money;

    /* callbacks */
    void (*on_print)(Pro_Object*);
    void (*on_start)(Pro_Object*);
};

Pro_Object* pro_object_new( void );

/* inline functions */
#define pro_object_set_money( obj, v ) obj->money = v
#define pro_object_get_money( obj ) obj->money

/* methods */
void pro_object_print_money( Pro_Object* );
void pro_object_start_up( Pro_Object* );

pro_lang.c

/* Simply declare private methods as 'static'
   in your source file! */

static void pro_object_init( Pro_Object* obj );

Pro_Object* pro_object_new( void )
{
    Pro_Object* obj = (Pro_Object*) malloc( sizeof( Pro_Object ) );
    pro_object_init( obj );
}

void pro_object_init( Pro_Object* obj )
{
    obj->money = 0;

    obj->on_print = NULL;
    obj->on_start = NULL;
}

void pro_object_start_up( Pro_Object* obj )
{
    if ( obj->on_start ) ( *obj->on_start )( obj );
}

void pro_object_print_money( Pro_Object* obj )
{
    printf( "\nMoney : %i \n", obj->money );
    if( obj->on_print ) ( *obj->on_print )( obj );
}

main.c

void print_callback( Pro_Object* obj )
{
    printf( "THIS IS A PRINT CALLBACK! \n" );
}

void start_callback( Pro_Object* obj )
{
    printf( "\nTHIS IS A START CALLBACK! \n");
}

int main()
{
    /* constructor */
    Pro_Object* obj = pro_object_new();

    /* testing the methods and inline functions */
    int tmp = pro_object_get_money( obj );
    printf( "Default Money Value: %i \n", tmp );

    pro_object_set_money( obj , 23 );

    tmp = pro_object_get_money( obj );
    printf( "New Money Value: %i \n", tmp );

    /* setting the callback functions */
    obj->on_print = print_callback;
    obj->on_start = start_callback;
    
    /* these will call the callback functions */
    pro_object_start_up( obj );
    pro_object_print_money( obj );

    /* destructing */
    free( obj );

    return 0;
}

Output

Default Money Value: 0
New Money Value: 23

THIS IS A START CALLBACK!

Money : 23
THIS IS A PRINT CALLBACK!

I hope to learn more about C’s latent potential and the various creative ways of unleashing it. I’ll post more discoveries…. as soon as I discover them 8-) .

An Ode To Examinations

O' Level exams are coming up, my spirits are going down Exam tensions are heating up, anxiety spreading all around! Not a whisper, not a sound I already know I'm doomed to drown! O' Level exams are coming up, my spirits are going down Exam tensions are heating up, anxiety spreading all around! I'm doomed as I said, that is all I'm doomed to fail, no doubt no poll! I'm doomed can't you hear? There's no doubt By this time next year, I'll be down and out! I'm doomed, you fool, why can't you comprehend? It's the final straw, the invasion, the end! I'm doomed to failure, let me emphasize it some more There's a cyclone, and my ship is off-shore! I'm doomed as you know, now I'm overdoing it But that's the plain truth, there's no denying it! O' Level exams are coming up, my spirits are going down Exam tensions are heating up, anxiety spreading all around! Oh! Can't you hear the dooms-day count? The Grim-Reaper is roaming about! Oh! You ask, what am I sulking about? I say it's the exams, I'll fail no doubt! O' Level exams are coming up, my spirits are going down Exam tensions are heating up, anxiety spreading all around! The supervisors tell me to cover my working But what is there to hide? I didn't write a damn thing! As you know, I'm doomed, didn't learn a thing Even now, instead of learning, I sing! With a heavy heart, I'm writing this ode Cuz' all the books seem to be in machine code!

by Muhammad Ahmad Tirmazi

Community

Follow

Get every new post delivered to your Inbox.

Join 211 other followers