Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Call Backs |
| Date | 2012-02-16 01:21 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jhi72t$3pc$1@dont-email.me> (permalink) |
| References | <jhh1aj$43c$1@speranza.aioe.org> |
On 2/15/2012 14:33, Edward Rutherford wrote:
> I've read plenty of discussion about callback functions as used in C (ie
> in qsort). I'm comfortable with this: really it is a
> function pointer. What I'm not sure about is how (or if) a
> callback differs to events like GUI's use. One of the things I
> found about events, at least in a VB6 project I did, is that an event
> can be broadcast to all objects and they can use it if desired (code
> only and GUI objects). Each object has to have a receive function for
> that event though.
>
> The only way I can see to do event broadcasting with a callback is to
> have a special callback that in turn calls a list of receiver functions
> that would be in each module. For example, a timer that broadcasts a
> time tick every second would call a function (for example)
> Timer_1min(void) that in turn would call functions in all of the modules
> that need to know the time. I've also thought about a message que, but
> that is begining to sound like too much work. Is there a better way?
You can 'typedef' a function type:
typedef int f_click(struct s_clickable *);
Then a function pointer to that type of function might be:
f_click * click;
So you might have:
struct s_clickable {
f_click * click;
};
struct s_button {
struct s_clickable clickable;
const char * caption;
};
Then, where you might have 'Sub Command1_Click' in Microsoft Visual
Basic, in C, you might do:
/* Declare using 'f_click' to ensure the type */
static f_click cmdbtn1_click;
/* Definition */
static int cmdbtn1_click(struct s_clickable * clickable) {
unsigned char * ptr;
struct s_button * button;
/*
* Non-standard 'container_of' and 'CONTAINING_RECORD'
* do something like this, but usually without the 'ptr'
* intermediary
*/
ptr = (void *) clickable;
ptr -= offsetof(struct s_button, clickable);
button = (void *) ptr;
printf("You pressed %s!\n", button->caption);
return 0;
}
struct s_button cmdbtn1 = {
/* clickable */
{ cmdbtn1_click },
/* caption */
"MyCommandButton1",
};
Then, as others have mentioned, you can have a "everything-clicked
event" and it can have a linked-list of subscribers. You can add
'&cmdbtn1.clickable' to that linked-list. The event can have an
associated function which walks the list and invokes
'list_item->clickable->click(&list_item->clickable)' for each item in
the list.
I hope this helps. :)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Call Backs Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-02-15 19:33 +0000
Re: Call Backs tom st denis <tom@iahu.ca> - 2012-02-15 11:36 -0800
Re: Call Backs James Kuyper <jameskuyper@verizon.net> - 2012-02-15 15:02 -0500
Re: Call Backs Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-02-15 12:27 -0800
Re: Call Backs Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-16 02:56 -0800
Re: Call Backs Lew Pitcher <lpitcher@teksavvy.com> - 2012-02-15 15:20 -0500
Re: Call Backs Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-02-15 12:24 -0800
Re: Call Backs Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 16:02 -0800
Re: Call Backs Shao Miller <sha0.miller@gmail.com> - 2012-02-16 01:21 -0500
Re: Call Backs Shao Miller <sha0.miller@gmail.com> - 2012-02-16 01:25 -0500
Re: Call Backs Willem <willem@toad.stack.nl> - 2012-02-17 21:58 +0000
csiph-web