Callbacks description
-
Upload
abhipankaj -
Category
Documents
-
view
244 -
download
0
Transcript of Callbacks description
-
7/25/2019 Callbacks description
1/12
NATIONALENERGYRESEARCHSCIENTIFICCOMPUTINGCENTER
harles Le ett
CallbacksCallbacks
May 2 2003
-
7/25/2019 Callbacks description
2/12
2Charles Le ett
CallbacksCallbacks
In C, callbacks are easy - one can store the address
of a callback functon n a !arable.
In C"", t#s trcker - $hle one can get the addressof global functons, statc ob%ects, and statc
&e&ber functons of ob%ects, t#s &ore dffcult toe'tract the actual address of a non-statc &e&berfuncton of a non-statc ob%ect.
Most general(ed callback fra&e$orks n C"" &akeuse of nhertance to defne an access )ont to anob%ect#s &e&ber functons.
-
7/25/2019 Callbacks description
3/12
3Charles Le ett
libsigc++libsigc++
C"" lbrary *not %ust header fles+
Can handle &e&ber functon of any ob%ect der!ed fro& gCb%ect functon ob%ect der!ed fro& gCb%ect statc, global or frend functon statc functon ob%ect &e&ber functon to a statc ob%ect
three se)arate enttes n!ol!ed n &echans& sender rece!er so&eone &akng connecton bet$een the t$o
-
7/25/2019 Callbacks description
4/12
-
7/25/2019 Callbacks description
5/12
5Charles Le ett
Signal ImplementationSignal Implementation
gnals are C"" functon ob%ects
1 connecton fro& a sgnal to a *&e&ber+ functon&atchng sgnal5s nterface can be &ade
If the functon s a &e&ber functon
Callng a sgnal looks e'actly lke callng a nor&al C""functon
signal buttonPressed;
void my_function(int param1, float param2);buttonPressed.connect(slot(my_function));
!y"lass myob#ect;
buttonPressed.connect(slot(myob#ect,!y"lass$$my_function));
buttonPressed(1%, 2%.%);
-
7/25/2019 Callbacks description
6/12
6Charles Le ett
Mismatched argument types and countsMismatched argument types and counts
Can also connect functons to sgnals $th dfferent
argu&ents or return ty)es usng ada)ters bygeneratng ne$ slot ob%ects that $ra) around ane'stng slot
&
slot1, 2 etra);
slot2);
slot2);
-
-
7/25/2019 Callbacks description
7/127Charles Le ett
Handling Return ValuesHandling Return Values
6hat s the return !alue of a gnal that n turn
calls &any dfferent callbacks that each return adfferent !alue7 lbrary offers a Marshal ob%ect, taken as an e'tra
te&)late argu&ent to a sgnal, that allo$s)rocessng of return codes.
/he &arshallng functon $ll be called $th thereturn !alue of each slot connected to the sgnal asthey are called. /he e&sson )rocess can besto))ed by the &arshallng functon returnng true.
-
7/25/2019 Callbacks description
8/128Charles Le ett
Callbacks Without InheritanceCallbacks Without Inheritance
6hat f you need to )erfor& a callback to a &e&ber
functon of a class that sn#t statc, and doesn#tnhert fro& a base class7 /he )roble& s that there s no $ay n C"" to
dentfy the e'act address of a &e&ber functon.
ee&s $erd doesn#t t7 /he co&)ler kno$s theaddress - t has to be able to call t, but there s no$ay to ds)lay ths !alue.
r s there7 6e can use bnd*+, to bnd a &e&ber functon
declaraton to an nstantated ob%ect, and then callt, but $e need a $ay of un8uely dentfyng each
such bound functon.
-
7/25/2019 Callbacks description
9/129Charles Le ett
Binding FunctionsBinding Functions
It#s easer to use the boostbnd*+ functon than
the stl bnd, but t essentally does the sa&e thngclass "/ &public$float fcn(int _i) & return _i0.; --;
& "/ cb1; boost$$function< float,int > bfcn
(boost$$bind("/$$fcn, cb1, _1));
cout
-
7/25/2019 Callbacks description
10/1210Charles Le ett
Binding FunctionsBinding Functions
9ut once t#s bound to a boostfuncton, there#s no
$ay to dentfy the ob%ects that $ent nto t.
6e need to fnd a $ay to un8uely dentfy anfuncton:ob%ect )ar, and assocate t $th the
boostfuncton.
;asy to dentfy the ob%ect - %ust use the address. 9ut $hat about the functon7 Can#t )rnt out
C9=fcn - t#s not statc& cout
-
7/25/2019 Callbacks description
11/1211Charles Le ett
Function ddressesFunction ddresses
/urns out, that )rntf s &uch s&arter than cout
/hs s actually de)endent on the co&)ler used. gcc2.? can#t dstngush bet$een the non-!rtual
functons, but gcc 3.2 can
class "/ &public$ float fcn1(int) &-; float fcn2(int) &-; virtual float fcn(int) &-; virtual float fcn3(int) &-;
-;& printf (45 5 5 56n7, "/$$fcn1, "/$$fcn2 "/$$fcn, "/$$fcn3);-
output>1 8 9%323 9%3283
-
7/25/2019 Callbacks description
12/1212Charles Le ett
!utting it all together!utting it all together
o$ $e can un8uely dentfy any &e&ber functon,
usng the address of the ob%ect, and the address ofthe functon returned by s)rntf. 6e can thenassocate ths nfor&aton $th the address of theboostfuncton, and nd!dually call any functon at$ll, $thout ha!ng to resort to nhertance or
statc functons.