Lines 5-8 were replaced by lines 5-8 |
- typedef struct { |
- char *class; |
- /* ... message data ... */ |
- } <message class>; |
+ typedef struct { |
+ char *class; |
+ /* ... message data ... */ |
+ } <message class>; |
Line 12 was replaced by lines 12-28 |
- Example: |
+ In our example we have a module ''nodehandling''. |
+ |
+ Extract from nodehandling.h: |
+ {{{ |
+ typedef struct { |
+ char *class; |
+ SConnection *pCon; |
+ Hdb *node; |
+ ValueStruct *value; |
+ } SetCallbackMsg; |
+ |
+ SetCallbackMsg *SetClassCast(void *msg); |
+ }}} |
+ |
+ SetClassCast returns the message if it is a SetCallbackMsg or NULL otherwise. |
+ |
+ Extract from nodehandling.c: |
Line 14 was replaced by lines 30-37 |
- static char *setCallbackClass="set callback"; |
+ static char *setCallbackClass="set callback"; |
+ |
+ SetCallbackMsg *SetClassCast(void *msg) { |
+ if (SetCallbackMsg *)msg)->class == SetCallbackClass) { |
+ return msg; |
+ } |
+ return NULL; |
+ } |
Lines 16-21 were replaced by line 39 |
- typedef struct { |
- char *class; |
- SConnection *pCon; |
- Hdb *node; |
- ValueStruct *value; |
- } SetCallbackMsg; |
+ ... |
Line 23 was replaced by line 41 |
- ... |
+ /* calling the callback */ |
Removed lines 25-26 |
- /* calling the callback */ |
- { |
Line 33 was replaced by lines 49-75 |
- } |
+ }}} |
+ |
+ Extract from a module implementing a callback: |
+ {{{ |
+ int MySetCallback(Hdb *node, void *msg, void *data) { |
+ SetCallbackMsg *setMsg = SetClassCast(msg); |
+ |
+ if (! setMsg) return 0; /* not handled */ |
+ |
+ /* do the set */ |
+ |
+ return 1; |
+ } |
+ |
+ ... |
+ |
+ void KillMyData(MyData *data) { |
+ /* free resources */ |
+ } |
+ ... |
+ |
+ MyData *data; |
+ Callback *cb; |
+ |
+ cb = MakeCallback(MySetCallback, data, (KillFunc)KillMyData); |
+ AppendCallback(node, cb); |
+ }}} |