|
|
|
|
|
|
| Author |
Message |
Sikandar *nix forums beginner
Joined: 21 Jul 2006
Posts: 3
|
Posted: Fri Jul 21, 2006 1:23 pm Post subject:
Function Pointer
|
|
|
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 |
|
| Back to top |
|
 |
Tak-Shing Chan *nix forums beginner
Joined: 10 Jul 2006
Posts: 19
|
Posted: Fri Jul 21, 2006 1:37 pm Post subject:
Re: Function Pointer
|
|
|
On Fri, 21 Jul 2006, Sikandar wrote:
| Quote: | [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 |
|
| Back to top |
|
 |
Chris Dollin *nix forums Guru
Joined: 24 Feb 2005
Posts: 373
|
Posted: Fri Jul 21, 2006 1:41 pm Post subject:
Re: Function Pointer
|
|
|
Sikandar wrote:
(fx:snip)
| Quote: | 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.
--
Chris "opinion offered in passing" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/ |
|
| Back to top |
|
 |
Ancient_Hacker *nix forums beginner
Joined: 31 May 2006
Posts: 42
|
Posted: Fri Jul 21, 2006 1:41 pm Post subject:
Re: Function Pointer
|
|
|
Sikandar wrote:
This struct holds four pointers to functions, some taking no
parameter, some taking a bool.
| Quote: | typedef struct
{
int (*close)( void );
void (*enable)( bool );
void (*tx)( void );
int (*sergetchar)( bool );
}SERINTERFACE, *PSERINTERFACE;
|
This typedef defines some serial id identifiers.
| Quote: | 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.
| Quote: | 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..
| Quote: | 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.
| Quote: | 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 |
|
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sat Nov 22, 2008 9:02 pm | All times are GMT
|
|
Debt Consolidation | Free Wordpress Blogs Hosting | Personal Loans | Camilo jose cela | Mobile Phones
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|