Function Pointer

S

Sikandar

typedef struct
{

int (*close)( void );
void (*enable)( bool );
void (*tx)( void );
int (*sergetchar)( bool );

}SERINTERFACE, *PSERINTERFACE;



typedef enum
{
SER_NONE,
SER_TELEGRAPH, /* telegraph plug in board */
SER_MEMORY, /* Trace messages dumped to a RAM buffer */
SER_DBGCOMM, /* the muli-ice debug comms channel */
SER_HOSTMODEM /* the host modem llserial interface */
} LLSERID;


typedef struct
{
void (*flowHandler)( bool );
void (*flowReadyHandler)( bool );
void (*cdHandler)( bool );
void (*ringHandler)( void );
void (*rxHandler)( unsigned int, char* );

} LLSERCALLBACKS, *PLLSERCALLBACKS;


static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}



PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.


Regards in anticipation of your replies,

Sikandar
 
T

Tak-Shing Chan

[snip]

static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}



PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}

[snip]

What exactly is happening in Line 1 and line 2.

Line 1: makes sure that llsropen[id] is not NULL.

Line 2: call the function pointed to by llsropen[id] with
argument pcb. This is equivalent to:

switch (id) {
case 1:
ll_Int0(pcb);
break;
case 2:
ll_Int1(pcb);
break;
case 3:
ll_Int2(pcb);
}

Tak-Shing
 
C

Chris Dollin

Sikandar wrote:

(fx:snip)
static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}

PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.

In line 1, a function pointer is being tested to see if it's
not null.

[The function pointer is the id'th element of the `llseropen`
array.]

In line 2, a that now-known-to-be-non-null function pointer is
being called.

The coder appears to be over-fond of CAPITALS, impenetrable names,
and excessive use of punctuation, so they're probably stuck with
some legacy coding standard.
 
A

Ancient_Hacker

Sikandar wrote:

This struct holds four pointers to functions, some taking no
parameter, some taking a bool.

typedef struct
{

int (*close)( void );
void (*enable)( bool );
void (*tx)( void );
int (*sergetchar)( bool );

}SERINTERFACE, *PSERINTERFACE;

This typedef defines some serial id identifiers.
typedef enum
{
SER_NONE,
SER_TELEGRAPH, /* telegraph plug in board */
SER_MEMORY, /* Trace messages dumped to a RAM buffer */
SER_DBGCOMM, /* the muli-ice debug comms channel */
SER_HOSTMODEM /* the host modem llserial interface */
} LLSERID;




Another struct of function pointers.
typedef struct
{
void (*flowHandler)( bool );
void (*flowReadyHandler)( bool );
void (*cdHandler)( bool );
void (*ringHandler)( void );
void (*rxHandler)( unsigned int, char* );

} LLSERCALLBACKS, *PLLSERCALLBACKS;


This actually declares and sets up an array of functions, indexed by
the above enums..

static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}


This apparently is a function you call with a serial id enum and a pcb
index,

first it checks to ensure that id is >= 0 and <= the highest index in
llseropen.
otherwise it returns NULL,

then if the index is okay, (IN LINE1): it checks the indexth entry in
the llseropen table, if its null it immediately returns NULL,

if the pointer isnt null, (IN LINE 2): it calls a function in the
table,, passing it the pcb value, then returrns that function's return
value.

This is somewhat tricky code, as the function pointers have to be set
just right.


PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.


Regards in anticipation of your replies,

Sikandar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,800
Messages
2,569,656
Members
45,399
Latest member
JettTancre
Top