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-) .

About these ads

One Comment (+add yours?)

  1. Trackback: What are the features required for Object Orientation? | Q&A System

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Community

Follow

Get every new post delivered to your Inbox.

Join 211 other followers