Callback.h 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2015 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_CALLBACK_H
  17. #define MBED_CALLBACK_H
  18. #include <string.h>
  19. #include <stdint.h>
  20. #include <new>
  21. #include "platform/mbed_assert.h"
  22. #include "platform/mbed_toolchain.h"
  23. namespace mbed {
  24. /** \addtogroup platform */
  25. /** @{*/
  26. /**
  27. * \defgroup platform_Callback Callback class
  28. * @{
  29. */
  30. /** Callback class based on template specialization
  31. *
  32. * @note Synchronization level: Not protected
  33. */
  34. template <typename F>
  35. class Callback;
  36. // Internal sfinae declarations
  37. //
  38. // These are used to eliminate overloads based on type attributes
  39. // 1. Does a function object have a call operator
  40. // 2. Does a function object fit in the available storage
  41. //
  42. // These eliminations are handled cleanly by the compiler and avoid
  43. // massive and misleading error messages when confronted with an
  44. // invalid type (or worse, runtime failures)
  45. namespace detail {
  46. struct nil {};
  47. template <bool B, typename R = nil>
  48. struct enable_if {
  49. typedef R type;
  50. };
  51. template <typename R>
  52. struct enable_if<false, R> {};
  53. template <typename M, M>
  54. struct is_type {
  55. static const bool value = true;
  56. };
  57. }
  58. #define MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, M) \
  59. typename detail::enable_if< \
  60. detail::is_type<M, &F::operator()>::value && \
  61. sizeof(F) <= sizeof(uintptr_t) \
  62. >::type = detail::nil()
  63. /** Callback class based on template specialization
  64. *
  65. * @note Synchronization level: Not protected
  66. */
  67. template <typename R>
  68. class Callback<R()> {
  69. public:
  70. /** Create a Callback with a static function
  71. * @param func Static function to attach
  72. */
  73. Callback(R(*func)() = 0)
  74. {
  75. if (!func) {
  76. memset(this, 0, sizeof(Callback));
  77. } else {
  78. generate(func);
  79. }
  80. }
  81. /** Attach a Callback
  82. * @param func The Callback to attach
  83. */
  84. Callback(const Callback<R()> &func)
  85. {
  86. if (func._ops) {
  87. func._ops->move(this, &func);
  88. }
  89. _ops = func._ops;
  90. }
  91. /** Create a Callback with a member function
  92. * @param obj Pointer to object to invoke member function on
  93. * @param method Member function to attach
  94. */
  95. template<typename T, typename U>
  96. Callback(U *obj, R(T::*method)())
  97. {
  98. generate(method_context<T, R(T::*)()>(obj, method));
  99. }
  100. /** Create a Callback with a member function
  101. * @param obj Pointer to object to invoke member function on
  102. * @param method Member function to attach
  103. */
  104. template<typename T, typename U>
  105. Callback(const U *obj, R(T::*method)() const)
  106. {
  107. generate(method_context<const T, R(T::*)() const>(obj, method));
  108. }
  109. /** Create a Callback with a member function
  110. * @param obj Pointer to object to invoke member function on
  111. * @param method Member function to attach
  112. */
  113. template<typename T, typename U>
  114. Callback(volatile U *obj, R(T::*method)() volatile)
  115. {
  116. generate(method_context<volatile T, R(T::*)() volatile>(obj, method));
  117. }
  118. /** Create a Callback with a member function
  119. * @param obj Pointer to object to invoke member function on
  120. * @param method Member function to attach
  121. */
  122. template<typename T, typename U>
  123. Callback(const volatile U *obj, R(T::*method)() const volatile)
  124. {
  125. generate(method_context<const volatile T, R(T::*)() const volatile>(obj, method));
  126. }
  127. /** Create a Callback with a static function and bound pointer
  128. * @param func Static function to attach
  129. * @param arg Pointer argument to function
  130. */
  131. template<typename T, typename U>
  132. Callback(R(*func)(T *), U *arg)
  133. {
  134. generate(function_context<R(*)(T *), T>(func, arg));
  135. }
  136. /** Create a Callback with a static function and bound pointer
  137. * @param func Static function to attach
  138. * @param arg Pointer argument to function
  139. */
  140. template<typename T, typename U>
  141. Callback(R(*func)(const T *), const U *arg)
  142. {
  143. generate(function_context<R(*)(const T *), const T>(func, arg));
  144. }
  145. /** Create a Callback with a static function and bound pointer
  146. * @param func Static function to attach
  147. * @param arg Pointer argument to function
  148. */
  149. template<typename T, typename U>
  150. Callback(R(*func)(volatile T *), volatile U *arg)
  151. {
  152. generate(function_context<R(*)(volatile T *), volatile T>(func, arg));
  153. }
  154. /** Create a Callback with a static function and bound pointer
  155. * @param func Static function to attach
  156. * @param arg Pointer argument to function
  157. */
  158. template<typename T, typename U>
  159. Callback(R(*func)(const volatile T *), const volatile U *arg)
  160. {
  161. generate(function_context<R(*)(const volatile T *), const volatile T>(func, arg));
  162. }
  163. /** Create a Callback with a function object
  164. * @param f Function object to attach
  165. * @note The function object is limited to a single word of storage
  166. */
  167. template <typename F>
  168. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)()))
  169. {
  170. generate(f);
  171. }
  172. /** Create a Callback with a function object
  173. * @param f Function object to attach
  174. * @note The function object is limited to a single word of storage
  175. */
  176. template <typename F>
  177. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() const))
  178. {
  179. generate(f);
  180. }
  181. /** Create a Callback with a function object
  182. * @param f Function object to attach
  183. * @note The function object is limited to a single word of storage
  184. */
  185. template <typename F>
  186. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() volatile))
  187. {
  188. generate(f);
  189. }
  190. /** Create a Callback with a function object
  191. * @param f Function object to attach
  192. * @note The function object is limited to a single word of storage
  193. */
  194. template <typename F>
  195. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() const volatile))
  196. {
  197. generate(f);
  198. }
  199. /** Create a Callback with a static function and bound pointer
  200. * @param obj Pointer to object to bind to function
  201. * @param func Static function to attach
  202. * @deprecated
  203. * Arguments to callback have been reordered to Callback(func, arg)
  204. */
  205. template<typename T, typename U>
  206. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  207. "Arguments to callback have been reordered to Callback(func, arg)")
  208. Callback(U *obj, R(*func)(T *))
  209. {
  210. new (this) Callback(func, obj);
  211. }
  212. /** Create a Callback with a static function and bound pointer
  213. * @param obj Pointer to object to bind to function
  214. * @param func Static function to attach
  215. * @deprecated
  216. * Arguments to callback have been reordered to Callback(func, arg)
  217. */
  218. template<typename T, typename U>
  219. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  220. "Arguments to callback have been reordered to Callback(func, arg)")
  221. Callback(const U *obj, R(*func)(const T *))
  222. {
  223. new (this) Callback(func, obj);
  224. }
  225. /** Create a Callback with a static function and bound pointer
  226. * @param obj Pointer to object to bind to function
  227. * @param func Static function to attach
  228. * @deprecated
  229. * Arguments to callback have been reordered to Callback(func, arg)
  230. */
  231. template<typename T, typename U>
  232. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  233. "Arguments to callback have been reordered to Callback(func, arg)")
  234. Callback(volatile U *obj, R(*func)(volatile T *))
  235. {
  236. new (this) Callback(func, obj);
  237. }
  238. /** Create a Callback with a static function and bound pointer
  239. * @param obj Pointer to object to bind to function
  240. * @param func Static function to attach
  241. * @deprecated
  242. * Arguments to callback have been reordered to Callback(func, arg)
  243. */
  244. template<typename T, typename U>
  245. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  246. "Arguments to callback have been reordered to Callback(func, arg)")
  247. Callback(const volatile U *obj, R(*func)(const volatile T *))
  248. {
  249. new (this) Callback(func, obj);
  250. }
  251. /** Destroy a callback
  252. */
  253. ~Callback()
  254. {
  255. if (_ops) {
  256. _ops->dtor(this);
  257. }
  258. }
  259. /** Attach a static function
  260. * @param func Static function to attach
  261. * @deprecated
  262. * Replaced by simple assignment 'Callback cb = func'
  263. */
  264. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  265. "Replaced by simple assignment 'Callback cb = func")
  266. void attach(R(*func)())
  267. {
  268. this->~Callback();
  269. new (this) Callback(func);
  270. }
  271. /** Attach a Callback
  272. * @param func The Callback to attach
  273. * @deprecated
  274. * Replaced by simple assignment 'Callback cb = func'
  275. */
  276. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  277. "Replaced by simple assignment 'Callback cb = func")
  278. void attach(const Callback<R()> &func)
  279. {
  280. this->~Callback();
  281. new (this) Callback(func);
  282. }
  283. /** Attach a member function
  284. * @param obj Pointer to object to invoke member function on
  285. * @param method Member function to attach
  286. * @deprecated
  287. * Replaced by simple assignment 'Callback cb = func'
  288. */
  289. template<typename T, typename U>
  290. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  291. "Replaced by simple assignment 'Callback cb = func")
  292. void attach(U *obj, R(T::*method)())
  293. {
  294. this->~Callback();
  295. new (this) Callback(obj, method);
  296. }
  297. /** Attach a member function
  298. * @param obj Pointer to object to invoke member function on
  299. * @param method Member function to attach
  300. * @deprecated
  301. * Replaced by simple assignment 'Callback cb = func'
  302. */
  303. template<typename T, typename U>
  304. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  305. "Replaced by simple assignment 'Callback cb = func")
  306. void attach(const U *obj, R(T::*method)() const)
  307. {
  308. this->~Callback();
  309. new (this) Callback(obj, method);
  310. }
  311. /** Attach a member function
  312. * @param obj Pointer to object to invoke member function on
  313. * @param method Member function to attach
  314. * @deprecated
  315. * Replaced by simple assignment 'Callback cb = func'
  316. */
  317. template<typename T, typename U>
  318. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  319. "Replaced by simple assignment 'Callback cb = func")
  320. void attach(volatile U *obj, R(T::*method)() volatile)
  321. {
  322. this->~Callback();
  323. new (this) Callback(obj, method);
  324. }
  325. /** Attach a member function
  326. * @param obj Pointer to object to invoke member function on
  327. * @param method Member function to attach
  328. * @deprecated
  329. * Replaced by simple assignment 'Callback cb = func'
  330. */
  331. template<typename T, typename U>
  332. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  333. "Replaced by simple assignment 'Callback cb = func")
  334. void attach(const volatile U *obj, R(T::*method)() const volatile)
  335. {
  336. this->~Callback();
  337. new (this) Callback(obj, method);
  338. }
  339. /** Attach a static function with a bound pointer
  340. * @param func Static function to attach
  341. * @param arg Pointer argument to function
  342. * @deprecated
  343. * Replaced by simple assignment 'Callback cb = func'
  344. */
  345. template <typename T, typename U>
  346. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  347. "Replaced by simple assignment 'Callback cb = func")
  348. void attach(R(*func)(T *), U *arg)
  349. {
  350. this->~Callback();
  351. new (this) Callback(func, arg);
  352. }
  353. /** Attach a static function with a bound pointer
  354. * @param func Static function to attach
  355. * @param arg Pointer argument to function
  356. * @deprecated
  357. * Replaced by simple assignment 'Callback cb = func'
  358. */
  359. template <typename T, typename U>
  360. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  361. "Replaced by simple assignment 'Callback cb = func")
  362. void attach(R(*func)(const T *), const U *arg)
  363. {
  364. this->~Callback();
  365. new (this) Callback(func, arg);
  366. }
  367. /** Attach a static function with a bound pointer
  368. * @param func Static function to attach
  369. * @param arg Pointer argument to function
  370. * @deprecated
  371. * Replaced by simple assignment 'Callback cb = func'
  372. */
  373. template <typename T, typename U>
  374. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  375. "Replaced by simple assignment 'Callback cb = func")
  376. void attach(R(*func)(volatile T *), volatile U *arg)
  377. {
  378. this->~Callback();
  379. new (this) Callback(func, arg);
  380. }
  381. /** Attach a static function with a bound pointer
  382. * @param func Static function to attach
  383. * @param arg Pointer argument to function
  384. * @deprecated
  385. * Replaced by simple assignment 'Callback cb = func'
  386. */
  387. template <typename T, typename U>
  388. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  389. "Replaced by simple assignment 'Callback cb = func")
  390. void attach(R(*func)(const volatile T *), const volatile U *arg)
  391. {
  392. this->~Callback();
  393. new (this) Callback(func, arg);
  394. }
  395. /** Attach a function object
  396. * @param f Function object to attach
  397. * @note The function object is limited to a single word of storage
  398. * @deprecated
  399. * Replaced by simple assignment 'Callback cb = func'
  400. */
  401. template <typename F>
  402. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  403. "Replaced by simple assignment 'Callback cb = func")
  404. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)()))
  405. {
  406. this->~Callback();
  407. new (this) Callback(f);
  408. }
  409. /** Attach a function object
  410. * @param f Function object to attach
  411. * @note The function object is limited to a single word of storage
  412. * @deprecated
  413. * Replaced by simple assignment 'Callback cb = func'
  414. */
  415. template <typename F>
  416. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  417. "Replaced by simple assignment 'Callback cb = func")
  418. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() const))
  419. {
  420. this->~Callback();
  421. new (this) Callback(f);
  422. }
  423. /** Attach a function object
  424. * @param f Function object to attach
  425. * @note The function object is limited to a single word of storage
  426. * @deprecated
  427. * Replaced by simple assignment 'Callback cb = func'
  428. */
  429. template <typename F>
  430. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  431. "Replaced by simple assignment 'Callback cb = func")
  432. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() volatile))
  433. {
  434. this->~Callback();
  435. new (this) Callback(f);
  436. }
  437. /** Attach a function object
  438. * @param f Function object to attach
  439. * @note The function object is limited to a single word of storage
  440. * @deprecated
  441. * Replaced by simple assignment 'Callback cb = func'
  442. */
  443. template <typename F>
  444. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  445. "Replaced by simple assignment 'Callback cb = func")
  446. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)() const volatile))
  447. {
  448. this->~Callback();
  449. new (this) Callback(f);
  450. }
  451. /** Attach a static function with a bound pointer
  452. * @param obj Pointer to object to bind to function
  453. * @param func Static function to attach
  454. * @deprecated
  455. * Arguments to callback have been reordered to attach(func, arg)
  456. */
  457. template <typename T, typename U>
  458. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  459. "Arguments to callback have been reordered to attach(func, arg)")
  460. void attach(U *obj, R(*func)(T *))
  461. {
  462. this->~Callback();
  463. new (this) Callback(func, obj);
  464. }
  465. /** Attach a static function with a bound pointer
  466. * @param obj Pointer to object to bind to function
  467. * @param func Static function to attach
  468. * @deprecated
  469. * Arguments to callback have been reordered to attach(func, arg)
  470. */
  471. template <typename T, typename U>
  472. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  473. "Arguments to callback have been reordered to attach(func, arg)")
  474. void attach(const U *obj, R(*func)(const T *))
  475. {
  476. this->~Callback();
  477. new (this) Callback(func, obj);
  478. }
  479. /** Attach a static function with a bound pointer
  480. * @param obj Pointer to object to bind to function
  481. * @param func Static function to attach
  482. * @deprecated
  483. * Arguments to callback have been reordered to attach(func, arg)
  484. */
  485. template <typename T, typename U>
  486. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  487. "Arguments to callback have been reordered to attach(func, arg)")
  488. void attach(volatile U *obj, R(*func)(volatile T *))
  489. {
  490. this->~Callback();
  491. new (this) Callback(func, obj);
  492. }
  493. /** Attach a static function with a bound pointer
  494. * @param obj Pointer to object to bind to function
  495. * @param func Static function to attach
  496. * @deprecated
  497. * Arguments to callback have been reordered to attach(func, arg)
  498. */
  499. template <typename T, typename U>
  500. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  501. "Arguments to callback have been reordered to attach(func, arg)")
  502. void attach(const volatile U *obj, R(*func)(const volatile T *))
  503. {
  504. this->~Callback();
  505. new (this) Callback(func, obj);
  506. }
  507. /** Assign a callback
  508. */
  509. Callback &operator=(const Callback &that)
  510. {
  511. if (this != &that) {
  512. this->~Callback();
  513. new (this) Callback(that);
  514. }
  515. return *this;
  516. }
  517. /** Call the attached function
  518. */
  519. R call() const
  520. {
  521. MBED_ASSERT(_ops);
  522. return _ops->call(this);
  523. }
  524. /** Call the attached function
  525. */
  526. R operator()() const
  527. {
  528. return call();
  529. }
  530. /** Test if function has been attached
  531. */
  532. operator bool() const
  533. {
  534. return _ops;
  535. }
  536. /** Test for equality
  537. */
  538. friend bool operator==(const Callback &l, const Callback &r)
  539. {
  540. return memcmp(&l, &r, sizeof(Callback)) == 0;
  541. }
  542. /** Test for inequality
  543. */
  544. friend bool operator!=(const Callback &l, const Callback &r)
  545. {
  546. return !(l == r);
  547. }
  548. /** Static thunk for passing as C-style function
  549. * @param func Callback to call passed as void pointer
  550. * @return the value as determined by func which is of
  551. * type and determined by the signiture of func
  552. */
  553. static R thunk(void *func)
  554. {
  555. return static_cast<Callback *>(func)->call();
  556. }
  557. private:
  558. // Stored as pointer to function and pointer to optional object
  559. // Function pointer is stored as union of possible function types
  560. // to guarantee proper size and alignment
  561. struct _class;
  562. union {
  563. void (*_staticfunc)();
  564. void (*_boundfunc)(_class *);
  565. void (_class::*_methodfunc)();
  566. } _func;
  567. void *_obj;
  568. // Dynamically dispatched operations
  569. const struct ops {
  570. R(*call)(const void *);
  571. void (*move)(void *, const void *);
  572. void (*dtor)(void *);
  573. } *_ops;
  574. // Generate operations for function object
  575. template <typename F>
  576. void generate(const F &f)
  577. {
  578. static const ops ops = {
  579. &Callback::function_call<F>,
  580. &Callback::function_move<F>,
  581. &Callback::function_dtor<F>,
  582. };
  583. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  584. "Type F must not exceed the size of the Callback class");
  585. memset(this, 0, sizeof(Callback));
  586. new (this) F(f);
  587. _ops = &ops;
  588. }
  589. // Function attributes
  590. template <typename F>
  591. static R function_call(const void *p)
  592. {
  593. return (*(F *)p)();
  594. }
  595. template <typename F>
  596. static void function_move(void *d, const void *p)
  597. {
  598. new (d) F(*(F *)p);
  599. }
  600. template <typename F>
  601. static void function_dtor(void *p)
  602. {
  603. ((F *)p)->~F();
  604. }
  605. // Wrappers for functions with context
  606. template <typename O, typename M>
  607. struct method_context {
  608. M method;
  609. O *obj;
  610. method_context(O *obj, M method)
  611. : method(method), obj(obj) {}
  612. R operator()() const
  613. {
  614. return (obj->*method)();
  615. }
  616. };
  617. template <typename F, typename A>
  618. struct function_context {
  619. F func;
  620. A *arg;
  621. function_context(F func, A *arg)
  622. : func(func), arg(arg) {}
  623. R operator()() const
  624. {
  625. return func(arg);
  626. }
  627. };
  628. };
  629. /** Callback class based on template specialization
  630. *
  631. * @note Synchronization level: Not protected
  632. */
  633. template <typename R, typename A0>
  634. class Callback<R(A0)> {
  635. public:
  636. /** Create a Callback with a static function
  637. * @param func Static function to attach
  638. */
  639. Callback(R(*func)(A0) = 0)
  640. {
  641. if (!func) {
  642. memset(this, 0, sizeof(Callback));
  643. } else {
  644. generate(func);
  645. }
  646. }
  647. /** Attach a Callback
  648. * @param func The Callback to attach
  649. */
  650. Callback(const Callback<R(A0)> &func)
  651. {
  652. if (func._ops) {
  653. func._ops->move(this, &func);
  654. }
  655. _ops = func._ops;
  656. }
  657. /** Create a Callback with a member function
  658. * @param obj Pointer to object to invoke member function on
  659. * @param method Member function to attach
  660. */
  661. template<typename T, typename U>
  662. Callback(U *obj, R(T::*method)(A0))
  663. {
  664. generate(method_context<T, R(T::*)(A0)>(obj, method));
  665. }
  666. /** Create a Callback with a member function
  667. * @param obj Pointer to object to invoke member function on
  668. * @param method Member function to attach
  669. */
  670. template<typename T, typename U>
  671. Callback(const U *obj, R(T::*method)(A0) const)
  672. {
  673. generate(method_context<const T, R(T::*)(A0) const>(obj, method));
  674. }
  675. /** Create a Callback with a member function
  676. * @param obj Pointer to object to invoke member function on
  677. * @param method Member function to attach
  678. */
  679. template<typename T, typename U>
  680. Callback(volatile U *obj, R(T::*method)(A0) volatile)
  681. {
  682. generate(method_context<volatile T, R(T::*)(A0) volatile>(obj, method));
  683. }
  684. /** Create a Callback with a member function
  685. * @param obj Pointer to object to invoke member function on
  686. * @param method Member function to attach
  687. */
  688. template<typename T, typename U>
  689. Callback(const volatile U *obj, R(T::*method)(A0) const volatile)
  690. {
  691. generate(method_context<const volatile T, R(T::*)(A0) const volatile>(obj, method));
  692. }
  693. /** Create a Callback with a static function and bound pointer
  694. * @param func Static function to attach
  695. * @param arg Pointer argument to function
  696. */
  697. template<typename T, typename U>
  698. Callback(R(*func)(T *, A0), U *arg)
  699. {
  700. generate(function_context<R(*)(T *, A0), T>(func, arg));
  701. }
  702. /** Create a Callback with a static function and bound pointer
  703. * @param func Static function to attach
  704. * @param arg Pointer argument to function
  705. */
  706. template<typename T, typename U>
  707. Callback(R(*func)(const T *, A0), const U *arg)
  708. {
  709. generate(function_context<R(*)(const T *, A0), const T>(func, arg));
  710. }
  711. /** Create a Callback with a static function and bound pointer
  712. * @param func Static function to attach
  713. * @param arg Pointer argument to function
  714. */
  715. template<typename T, typename U>
  716. Callback(R(*func)(volatile T *, A0), volatile U *arg)
  717. {
  718. generate(function_context<R(*)(volatile T *, A0), volatile T>(func, arg));
  719. }
  720. /** Create a Callback with a static function and bound pointer
  721. * @param func Static function to attach
  722. * @param arg Pointer argument to function
  723. */
  724. template<typename T, typename U>
  725. Callback(R(*func)(const volatile T *, A0), const volatile U *arg)
  726. {
  727. generate(function_context<R(*)(const volatile T *, A0), const volatile T>(func, arg));
  728. }
  729. /** Create a Callback with a function object
  730. * @param f Function object to attach
  731. * @note The function object is limited to a single word of storage
  732. */
  733. template <typename F>
  734. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0)))
  735. {
  736. generate(f);
  737. }
  738. /** Create a Callback with a function object
  739. * @param f Function object to attach
  740. * @note The function object is limited to a single word of storage
  741. */
  742. template <typename F>
  743. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) const))
  744. {
  745. generate(f);
  746. }
  747. /** Create a Callback with a function object
  748. * @param f Function object to attach
  749. * @note The function object is limited to a single word of storage
  750. */
  751. template <typename F>
  752. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) volatile))
  753. {
  754. generate(f);
  755. }
  756. /** Create a Callback with a function object
  757. * @param f Function object to attach
  758. * @note The function object is limited to a single word of storage
  759. */
  760. template <typename F>
  761. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) const volatile))
  762. {
  763. generate(f);
  764. }
  765. /** Create a Callback with a static function and bound pointer
  766. * @param obj Pointer to object to bind to function
  767. * @param func Static function to attach
  768. * @deprecated
  769. * Arguments to callback have been reordered to Callback(func, arg)
  770. */
  771. template<typename T, typename U>
  772. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  773. "Arguments to callback have been reordered to Callback(func, arg)")
  774. Callback(U *obj, R(*func)(T *, A0))
  775. {
  776. new (this) Callback(func, obj);
  777. }
  778. /** Create a Callback with a static function and bound pointer
  779. * @param obj Pointer to object to bind to function
  780. * @param func Static function to attach
  781. * @deprecated
  782. * Arguments to callback have been reordered to Callback(func, arg)
  783. */
  784. template<typename T, typename U>
  785. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  786. "Arguments to callback have been reordered to Callback(func, arg)")
  787. Callback(const U *obj, R(*func)(const T *, A0))
  788. {
  789. new (this) Callback(func, obj);
  790. }
  791. /** Create a Callback with a static function and bound pointer
  792. * @param obj Pointer to object to bind to function
  793. * @param func Static function to attach
  794. * @deprecated
  795. * Arguments to callback have been reordered to Callback(func, arg)
  796. */
  797. template<typename T, typename U>
  798. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  799. "Arguments to callback have been reordered to Callback(func, arg)")
  800. Callback(volatile U *obj, R(*func)(volatile T *, A0))
  801. {
  802. new (this) Callback(func, obj);
  803. }
  804. /** Create a Callback with a static function and bound pointer
  805. * @param obj Pointer to object to bind to function
  806. * @param func Static function to attach
  807. * @deprecated
  808. * Arguments to callback have been reordered to Callback(func, arg)
  809. */
  810. template<typename T, typename U>
  811. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  812. "Arguments to callback have been reordered to Callback(func, arg)")
  813. Callback(const volatile U *obj, R(*func)(const volatile T *, A0))
  814. {
  815. new (this) Callback(func, obj);
  816. }
  817. /** Destroy a callback
  818. */
  819. ~Callback()
  820. {
  821. if (_ops) {
  822. _ops->dtor(this);
  823. }
  824. }
  825. /** Attach a static function
  826. * @param func Static function to attach
  827. * @deprecated
  828. * Replaced by simple assignment 'Callback cb = func'
  829. */
  830. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  831. "Replaced by simple assignment 'Callback cb = func")
  832. void attach(R(*func)(A0))
  833. {
  834. this->~Callback();
  835. new (this) Callback(func);
  836. }
  837. /** Attach a Callback
  838. * @param func The Callback to attach
  839. * @deprecated
  840. * Replaced by simple assignment 'Callback cb = func'
  841. */
  842. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  843. "Replaced by simple assignment 'Callback cb = func")
  844. void attach(const Callback<R(A0)> &func)
  845. {
  846. this->~Callback();
  847. new (this) Callback(func);
  848. }
  849. /** Attach a member function
  850. * @param obj Pointer to object to invoke member function on
  851. * @param method Member function to attach
  852. * @deprecated
  853. * Replaced by simple assignment 'Callback cb = func'
  854. */
  855. template<typename T, typename U>
  856. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  857. "Replaced by simple assignment 'Callback cb = func")
  858. void attach(U *obj, R(T::*method)(A0))
  859. {
  860. this->~Callback();
  861. new (this) Callback(obj, method);
  862. }
  863. /** Attach a member function
  864. * @param obj Pointer to object to invoke member function on
  865. * @param method Member function to attach
  866. * @deprecated
  867. * Replaced by simple assignment 'Callback cb = func'
  868. */
  869. template<typename T, typename U>
  870. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  871. "Replaced by simple assignment 'Callback cb = func")
  872. void attach(const U *obj, R(T::*method)(A0) const)
  873. {
  874. this->~Callback();
  875. new (this) Callback(obj, method);
  876. }
  877. /** Attach a member function
  878. * @param obj Pointer to object to invoke member function on
  879. * @param method Member function to attach
  880. * @deprecated
  881. * Replaced by simple assignment 'Callback cb = func'
  882. */
  883. template<typename T, typename U>
  884. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  885. "Replaced by simple assignment 'Callback cb = func")
  886. void attach(volatile U *obj, R(T::*method)(A0) volatile)
  887. {
  888. this->~Callback();
  889. new (this) Callback(obj, method);
  890. }
  891. /** Attach a member function
  892. * @param obj Pointer to object to invoke member function on
  893. * @param method Member function to attach
  894. * @deprecated
  895. * Replaced by simple assignment 'Callback cb = func'
  896. */
  897. template<typename T, typename U>
  898. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  899. "Replaced by simple assignment 'Callback cb = func")
  900. void attach(const volatile U *obj, R(T::*method)(A0) const volatile)
  901. {
  902. this->~Callback();
  903. new (this) Callback(obj, method);
  904. }
  905. /** Attach a static function with a bound pointer
  906. * @param func Static function to attach
  907. * @param arg Pointer argument to function
  908. * @deprecated
  909. * Replaced by simple assignment 'Callback cb = func'
  910. */
  911. template <typename T, typename U>
  912. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  913. "Replaced by simple assignment 'Callback cb = func")
  914. void attach(R(*func)(T *, A0), U *arg)
  915. {
  916. this->~Callback();
  917. new (this) Callback(func, arg);
  918. }
  919. /** Attach a static function with a bound pointer
  920. * @param func Static function to attach
  921. * @param arg Pointer argument to function
  922. * @deprecated
  923. * Replaced by simple assignment 'Callback cb = func'
  924. */
  925. template <typename T, typename U>
  926. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  927. "Replaced by simple assignment 'Callback cb = func")
  928. void attach(R(*func)(const T *, A0), const U *arg)
  929. {
  930. this->~Callback();
  931. new (this) Callback(func, arg);
  932. }
  933. /** Attach a static function with a bound pointer
  934. * @param func Static function to attach
  935. * @param arg Pointer argument to function
  936. * @deprecated
  937. * Replaced by simple assignment 'Callback cb = func'
  938. */
  939. template <typename T, typename U>
  940. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  941. "Replaced by simple assignment 'Callback cb = func")
  942. void attach(R(*func)(volatile T *, A0), volatile U *arg)
  943. {
  944. this->~Callback();
  945. new (this) Callback(func, arg);
  946. }
  947. /** Attach a static function with a bound pointer
  948. * @param func Static function to attach
  949. * @param arg Pointer argument to function
  950. * @deprecated
  951. * Replaced by simple assignment 'Callback cb = func'
  952. */
  953. template <typename T, typename U>
  954. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  955. "Replaced by simple assignment 'Callback cb = func")
  956. void attach(R(*func)(const volatile T *, A0), const volatile U *arg)
  957. {
  958. this->~Callback();
  959. new (this) Callback(func, arg);
  960. }
  961. /** Attach a function object
  962. * @param f Function object to attach
  963. * @note The function object is limited to a single word of storage
  964. * @deprecated
  965. * Replaced by simple assignment 'Callback cb = func'
  966. */
  967. template <typename F>
  968. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  969. "Replaced by simple assignment 'Callback cb = func")
  970. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0)))
  971. {
  972. this->~Callback();
  973. new (this) Callback(f);
  974. }
  975. /** Attach a function object
  976. * @param f Function object to attach
  977. * @note The function object is limited to a single word of storage
  978. * @deprecated
  979. * Replaced by simple assignment 'Callback cb = func'
  980. */
  981. template <typename F>
  982. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  983. "Replaced by simple assignment 'Callback cb = func")
  984. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) const))
  985. {
  986. this->~Callback();
  987. new (this) Callback(f);
  988. }
  989. /** Attach a function object
  990. * @param f Function object to attach
  991. * @note The function object is limited to a single word of storage
  992. * @deprecated
  993. * Replaced by simple assignment 'Callback cb = func'
  994. */
  995. template <typename F>
  996. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  997. "Replaced by simple assignment 'Callback cb = func")
  998. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) volatile))
  999. {
  1000. this->~Callback();
  1001. new (this) Callback(f);
  1002. }
  1003. /** Attach a function object
  1004. * @param f Function object to attach
  1005. * @note The function object is limited to a single word of storage
  1006. * @deprecated
  1007. * Replaced by simple assignment 'Callback cb = func'
  1008. */
  1009. template <typename F>
  1010. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1011. "Replaced by simple assignment 'Callback cb = func")
  1012. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0) const volatile))
  1013. {
  1014. this->~Callback();
  1015. new (this) Callback(f);
  1016. }
  1017. /** Attach a static function with a bound pointer
  1018. * @param obj Pointer to object to bind to function
  1019. * @param func Static function to attach
  1020. * @deprecated
  1021. * Arguments to callback have been reordered to attach(func, arg)
  1022. */
  1023. template <typename T, typename U>
  1024. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1025. "Arguments to callback have been reordered to attach(func, arg)")
  1026. void attach(U *obj, R(*func)(T *, A0))
  1027. {
  1028. this->~Callback();
  1029. new (this) Callback(func, obj);
  1030. }
  1031. /** Attach a static function with a bound pointer
  1032. * @param obj Pointer to object to bind to function
  1033. * @param func Static function to attach
  1034. * @deprecated
  1035. * Arguments to callback have been reordered to attach(func, arg)
  1036. */
  1037. template <typename T, typename U>
  1038. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1039. "Arguments to callback have been reordered to attach(func, arg)")
  1040. void attach(const U *obj, R(*func)(const T *, A0))
  1041. {
  1042. this->~Callback();
  1043. new (this) Callback(func, obj);
  1044. }
  1045. /** Attach a static function with a bound pointer
  1046. * @param obj Pointer to object to bind to function
  1047. * @param func Static function to attach
  1048. * @deprecated
  1049. * Arguments to callback have been reordered to attach(func, arg)
  1050. */
  1051. template <typename T, typename U>
  1052. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1053. "Arguments to callback have been reordered to attach(func, arg)")
  1054. void attach(volatile U *obj, R(*func)(volatile T *, A0))
  1055. {
  1056. this->~Callback();
  1057. new (this) Callback(func, obj);
  1058. }
  1059. /** Attach a static function with a bound pointer
  1060. * @param obj Pointer to object to bind to function
  1061. * @param func Static function to attach
  1062. * @deprecated
  1063. * Arguments to callback have been reordered to attach(func, arg)
  1064. */
  1065. template <typename T, typename U>
  1066. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1067. "Arguments to callback have been reordered to attach(func, arg)")
  1068. void attach(const volatile U *obj, R(*func)(const volatile T *, A0))
  1069. {
  1070. this->~Callback();
  1071. new (this) Callback(func, obj);
  1072. }
  1073. /** Assign a callback
  1074. */
  1075. Callback &operator=(const Callback &that)
  1076. {
  1077. if (this != &that) {
  1078. this->~Callback();
  1079. new (this) Callback(that);
  1080. }
  1081. return *this;
  1082. }
  1083. /** Call the attached function
  1084. */
  1085. R call(A0 a0) const
  1086. {
  1087. MBED_ASSERT(_ops);
  1088. return _ops->call(this, a0);
  1089. }
  1090. /** Call the attached function
  1091. */
  1092. R operator()(A0 a0) const
  1093. {
  1094. return call(a0);
  1095. }
  1096. /** Test if function has been attached
  1097. */
  1098. operator bool() const
  1099. {
  1100. return _ops;
  1101. }
  1102. /** Test for equality
  1103. */
  1104. friend bool operator==(const Callback &l, const Callback &r)
  1105. {
  1106. return memcmp(&l, &r, sizeof(Callback)) == 0;
  1107. }
  1108. /** Test for inequality
  1109. */
  1110. friend bool operator!=(const Callback &l, const Callback &r)
  1111. {
  1112. return !(l == r);
  1113. }
  1114. /** Static thunk for passing as C-style function
  1115. * @param func Callback to call passed as void pointer
  1116. * @param a0 An argument to be called with function func
  1117. * @return the value as determined by func which is of
  1118. * type and determined by the signiture of func
  1119. */
  1120. static R thunk(void *func, A0 a0)
  1121. {
  1122. return static_cast<Callback *>(func)->call(a0);
  1123. }
  1124. private:
  1125. // Stored as pointer to function and pointer to optional object
  1126. // Function pointer is stored as union of possible function types
  1127. // to guarantee proper size and alignment
  1128. struct _class;
  1129. union {
  1130. void (*_staticfunc)(A0);
  1131. void (*_boundfunc)(_class *, A0);
  1132. void (_class::*_methodfunc)(A0);
  1133. } _func;
  1134. void *_obj;
  1135. // Dynamically dispatched operations
  1136. const struct ops {
  1137. R(*call)(const void *, A0);
  1138. void (*move)(void *, const void *);
  1139. void (*dtor)(void *);
  1140. } *_ops;
  1141. // Generate operations for function object
  1142. template <typename F>
  1143. void generate(const F &f)
  1144. {
  1145. static const ops ops = {
  1146. &Callback::function_call<F>,
  1147. &Callback::function_move<F>,
  1148. &Callback::function_dtor<F>,
  1149. };
  1150. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  1151. "Type F must not exceed the size of the Callback class");
  1152. memset(this, 0, sizeof(Callback));
  1153. new (this) F(f);
  1154. _ops = &ops;
  1155. }
  1156. // Function attributes
  1157. template <typename F>
  1158. static R function_call(const void *p, A0 a0)
  1159. {
  1160. return (*(F *)p)(a0);
  1161. }
  1162. template <typename F>
  1163. static void function_move(void *d, const void *p)
  1164. {
  1165. new (d) F(*(F *)p);
  1166. }
  1167. template <typename F>
  1168. static void function_dtor(void *p)
  1169. {
  1170. ((F *)p)->~F();
  1171. }
  1172. // Wrappers for functions with context
  1173. template <typename O, typename M>
  1174. struct method_context {
  1175. M method;
  1176. O *obj;
  1177. method_context(O *obj, M method)
  1178. : method(method), obj(obj) {}
  1179. R operator()(A0 a0) const
  1180. {
  1181. return (obj->*method)(a0);
  1182. }
  1183. };
  1184. template <typename F, typename A>
  1185. struct function_context {
  1186. F func;
  1187. A *arg;
  1188. function_context(F func, A *arg)
  1189. : func(func), arg(arg) {}
  1190. R operator()(A0 a0) const
  1191. {
  1192. return func(arg, a0);
  1193. }
  1194. };
  1195. };
  1196. /** Callback class based on template specialization
  1197. *
  1198. * @note Synchronization level: Not protected
  1199. */
  1200. template <typename R, typename A0, typename A1>
  1201. class Callback<R(A0, A1)> {
  1202. public:
  1203. /** Create a Callback with a static function
  1204. * @param func Static function to attach
  1205. */
  1206. Callback(R(*func)(A0, A1) = 0)
  1207. {
  1208. if (!func) {
  1209. memset(this, 0, sizeof(Callback));
  1210. } else {
  1211. generate(func);
  1212. }
  1213. }
  1214. /** Attach a Callback
  1215. * @param func The Callback to attach
  1216. */
  1217. Callback(const Callback<R(A0, A1)> &func)
  1218. {
  1219. if (func._ops) {
  1220. func._ops->move(this, &func);
  1221. }
  1222. _ops = func._ops;
  1223. }
  1224. /** Create a Callback with a member function
  1225. * @param obj Pointer to object to invoke member function on
  1226. * @param method Member function to attach
  1227. */
  1228. template<typename T, typename U>
  1229. Callback(U *obj, R(T::*method)(A0, A1))
  1230. {
  1231. generate(method_context<T, R(T::*)(A0, A1)>(obj, method));
  1232. }
  1233. /** Create a Callback with a member function
  1234. * @param obj Pointer to object to invoke member function on
  1235. * @param method Member function to attach
  1236. */
  1237. template<typename T, typename U>
  1238. Callback(const U *obj, R(T::*method)(A0, A1) const)
  1239. {
  1240. generate(method_context<const T, R(T::*)(A0, A1) const>(obj, method));
  1241. }
  1242. /** Create a Callback with a member function
  1243. * @param obj Pointer to object to invoke member function on
  1244. * @param method Member function to attach
  1245. */
  1246. template<typename T, typename U>
  1247. Callback(volatile U *obj, R(T::*method)(A0, A1) volatile)
  1248. {
  1249. generate(method_context<volatile T, R(T::*)(A0, A1) volatile>(obj, method));
  1250. }
  1251. /** Create a Callback with a member function
  1252. * @param obj Pointer to object to invoke member function on
  1253. * @param method Member function to attach
  1254. */
  1255. template<typename T, typename U>
  1256. Callback(const volatile U *obj, R(T::*method)(A0, A1) const volatile)
  1257. {
  1258. generate(method_context<const volatile T, R(T::*)(A0, A1) const volatile>(obj, method));
  1259. }
  1260. /** Create a Callback with a static function and bound pointer
  1261. * @param func Static function to attach
  1262. * @param arg Pointer argument to function
  1263. */
  1264. template<typename T, typename U>
  1265. Callback(R(*func)(T *, A0, A1), U *arg)
  1266. {
  1267. generate(function_context<R(*)(T *, A0, A1), T>(func, arg));
  1268. }
  1269. /** Create a Callback with a static function and bound pointer
  1270. * @param func Static function to attach
  1271. * @param arg Pointer argument to function
  1272. */
  1273. template<typename T, typename U>
  1274. Callback(R(*func)(const T *, A0, A1), const U *arg)
  1275. {
  1276. generate(function_context<R(*)(const T *, A0, A1), const T>(func, arg));
  1277. }
  1278. /** Create a Callback with a static function and bound pointer
  1279. * @param func Static function to attach
  1280. * @param arg Pointer argument to function
  1281. */
  1282. template<typename T, typename U>
  1283. Callback(R(*func)(volatile T *, A0, A1), volatile U *arg)
  1284. {
  1285. generate(function_context<R(*)(volatile T *, A0, A1), volatile T>(func, arg));
  1286. }
  1287. /** Create a Callback with a static function and bound pointer
  1288. * @param func Static function to attach
  1289. * @param arg Pointer argument to function
  1290. */
  1291. template<typename T, typename U>
  1292. Callback(R(*func)(const volatile T *, A0, A1), const volatile U *arg)
  1293. {
  1294. generate(function_context<R(*)(const volatile T *, A0, A1), const volatile T>(func, arg));
  1295. }
  1296. /** Create a Callback with a function object
  1297. * @param f Function object to attach
  1298. * @note The function object is limited to a single word of storage
  1299. */
  1300. template <typename F>
  1301. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1)))
  1302. {
  1303. generate(f);
  1304. }
  1305. /** Create a Callback with a function object
  1306. * @param f Function object to attach
  1307. * @note The function object is limited to a single word of storage
  1308. */
  1309. template <typename F>
  1310. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) const))
  1311. {
  1312. generate(f);
  1313. }
  1314. /** Create a Callback with a function object
  1315. * @param f Function object to attach
  1316. * @note The function object is limited to a single word of storage
  1317. */
  1318. template <typename F>
  1319. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) volatile))
  1320. {
  1321. generate(f);
  1322. }
  1323. /** Create a Callback with a function object
  1324. * @param f Function object to attach
  1325. * @note The function object is limited to a single word of storage
  1326. */
  1327. template <typename F>
  1328. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) const volatile))
  1329. {
  1330. generate(f);
  1331. }
  1332. /** Create a Callback with a static function and bound pointer
  1333. * @param obj Pointer to object to bind to function
  1334. * @param func Static function to attach
  1335. * @deprecated
  1336. * Arguments to callback have been reordered to Callback(func, arg)
  1337. */
  1338. template<typename T, typename U>
  1339. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1340. "Arguments to callback have been reordered to Callback(func, arg)")
  1341. Callback(U *obj, R(*func)(T *, A0, A1))
  1342. {
  1343. new (this) Callback(func, obj);
  1344. }
  1345. /** Create a Callback with a static function and bound pointer
  1346. * @param obj Pointer to object to bind to function
  1347. * @param func Static function to attach
  1348. * @deprecated
  1349. * Arguments to callback have been reordered to Callback(func, arg)
  1350. */
  1351. template<typename T, typename U>
  1352. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1353. "Arguments to callback have been reordered to Callback(func, arg)")
  1354. Callback(const U *obj, R(*func)(const T *, A0, A1))
  1355. {
  1356. new (this) Callback(func, obj);
  1357. }
  1358. /** Create a Callback with a static function and bound pointer
  1359. * @param obj Pointer to object to bind to function
  1360. * @param func Static function to attach
  1361. * @deprecated
  1362. * Arguments to callback have been reordered to Callback(func, arg)
  1363. */
  1364. template<typename T, typename U>
  1365. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1366. "Arguments to callback have been reordered to Callback(func, arg)")
  1367. Callback(volatile U *obj, R(*func)(volatile T *, A0, A1))
  1368. {
  1369. new (this) Callback(func, obj);
  1370. }
  1371. /** Create a Callback with a static function and bound pointer
  1372. * @param obj Pointer to object to bind to function
  1373. * @param func Static function to attach
  1374. * @deprecated
  1375. * Arguments to callback have been reordered to Callback(func, arg)
  1376. */
  1377. template<typename T, typename U>
  1378. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1379. "Arguments to callback have been reordered to Callback(func, arg)")
  1380. Callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1))
  1381. {
  1382. new (this) Callback(func, obj);
  1383. }
  1384. /** Destroy a callback
  1385. */
  1386. ~Callback()
  1387. {
  1388. if (_ops) {
  1389. _ops->dtor(this);
  1390. }
  1391. }
  1392. /** Attach a static function
  1393. * @param func Static function to attach
  1394. * @deprecated
  1395. * Replaced by simple assignment 'Callback cb = func'
  1396. */
  1397. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1398. "Replaced by simple assignment 'Callback cb = func")
  1399. void attach(R(*func)(A0, A1))
  1400. {
  1401. this->~Callback();
  1402. new (this) Callback(func);
  1403. }
  1404. /** Attach a Callback
  1405. * @param func The Callback to attach
  1406. * @deprecated
  1407. * Replaced by simple assignment 'Callback cb = func'
  1408. */
  1409. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1410. "Replaced by simple assignment 'Callback cb = func")
  1411. void attach(const Callback<R(A0, A1)> &func)
  1412. {
  1413. this->~Callback();
  1414. new (this) Callback(func);
  1415. }
  1416. /** Attach a member function
  1417. * @param obj Pointer to object to invoke member function on
  1418. * @param method Member function to attach
  1419. * @deprecated
  1420. * Replaced by simple assignment 'Callback cb = func'
  1421. */
  1422. template<typename T, typename U>
  1423. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1424. "Replaced by simple assignment 'Callback cb = func")
  1425. void attach(U *obj, R(T::*method)(A0, A1))
  1426. {
  1427. this->~Callback();
  1428. new (this) Callback(obj, method);
  1429. }
  1430. /** Attach a member function
  1431. * @param obj Pointer to object to invoke member function on
  1432. * @param method Member function to attach
  1433. * @deprecated
  1434. * Replaced by simple assignment 'Callback cb = func'
  1435. */
  1436. template<typename T, typename U>
  1437. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1438. "Replaced by simple assignment 'Callback cb = func")
  1439. void attach(const U *obj, R(T::*method)(A0, A1) const)
  1440. {
  1441. this->~Callback();
  1442. new (this) Callback(obj, method);
  1443. }
  1444. /** Attach a member function
  1445. * @param obj Pointer to object to invoke member function on
  1446. * @param method Member function to attach
  1447. * @deprecated
  1448. * Replaced by simple assignment 'Callback cb = func'
  1449. */
  1450. template<typename T, typename U>
  1451. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1452. "Replaced by simple assignment 'Callback cb = func")
  1453. void attach(volatile U *obj, R(T::*method)(A0, A1) volatile)
  1454. {
  1455. this->~Callback();
  1456. new (this) Callback(obj, method);
  1457. }
  1458. /** Attach a member function
  1459. * @param obj Pointer to object to invoke member function on
  1460. * @param method Member function to attach
  1461. * @deprecated
  1462. * Replaced by simple assignment 'Callback cb = func'
  1463. */
  1464. template<typename T, typename U>
  1465. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1466. "Replaced by simple assignment 'Callback cb = func")
  1467. void attach(const volatile U *obj, R(T::*method)(A0, A1) const volatile)
  1468. {
  1469. this->~Callback();
  1470. new (this) Callback(obj, method);
  1471. }
  1472. /** Attach a static function with a bound pointer
  1473. * @param func Static function to attach
  1474. * @param arg Pointer argument to function
  1475. * @deprecated
  1476. * Replaced by simple assignment 'Callback cb = func'
  1477. */
  1478. template <typename T, typename U>
  1479. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1480. "Replaced by simple assignment 'Callback cb = func")
  1481. void attach(R(*func)(T *, A0, A1), U *arg)
  1482. {
  1483. this->~Callback();
  1484. new (this) Callback(func, arg);
  1485. }
  1486. /** Attach a static function with a bound pointer
  1487. * @param func Static function to attach
  1488. * @param arg Pointer argument to function
  1489. * @deprecated
  1490. * Replaced by simple assignment 'Callback cb = func'
  1491. */
  1492. template <typename T, typename U>
  1493. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1494. "Replaced by simple assignment 'Callback cb = func")
  1495. void attach(R(*func)(const T *, A0, A1), const U *arg)
  1496. {
  1497. this->~Callback();
  1498. new (this) Callback(func, arg);
  1499. }
  1500. /** Attach a static function with a bound pointer
  1501. * @param func Static function to attach
  1502. * @param arg Pointer argument to function
  1503. * @deprecated
  1504. * Replaced by simple assignment 'Callback cb = func'
  1505. */
  1506. template <typename T, typename U>
  1507. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1508. "Replaced by simple assignment 'Callback cb = func")
  1509. void attach(R(*func)(volatile T *, A0, A1), volatile U *arg)
  1510. {
  1511. this->~Callback();
  1512. new (this) Callback(func, arg);
  1513. }
  1514. /** Attach a static function with a bound pointer
  1515. * @param func Static function to attach
  1516. * @param arg Pointer argument to function
  1517. * @deprecated
  1518. * Replaced by simple assignment 'Callback cb = func'
  1519. */
  1520. template <typename T, typename U>
  1521. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1522. "Replaced by simple assignment 'Callback cb = func")
  1523. void attach(R(*func)(const volatile T *, A0, A1), const volatile U *arg)
  1524. {
  1525. this->~Callback();
  1526. new (this) Callback(func, arg);
  1527. }
  1528. /** Attach a function object
  1529. * @param f Function object to attach
  1530. * @note The function object is limited to a single word of storage
  1531. * @deprecated
  1532. * Replaced by simple assignment 'Callback cb = func'
  1533. */
  1534. template <typename F>
  1535. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1536. "Replaced by simple assignment 'Callback cb = func")
  1537. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1)))
  1538. {
  1539. this->~Callback();
  1540. new (this) Callback(f);
  1541. }
  1542. /** Attach a function object
  1543. * @param f Function object to attach
  1544. * @note The function object is limited to a single word of storage
  1545. * @deprecated
  1546. * Replaced by simple assignment 'Callback cb = func'
  1547. */
  1548. template <typename F>
  1549. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1550. "Replaced by simple assignment 'Callback cb = func")
  1551. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) const))
  1552. {
  1553. this->~Callback();
  1554. new (this) Callback(f);
  1555. }
  1556. /** Attach a function object
  1557. * @param f Function object to attach
  1558. * @note The function object is limited to a single word of storage
  1559. * @deprecated
  1560. * Replaced by simple assignment 'Callback cb = func'
  1561. */
  1562. template <typename F>
  1563. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1564. "Replaced by simple assignment 'Callback cb = func")
  1565. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) volatile))
  1566. {
  1567. this->~Callback();
  1568. new (this) Callback(f);
  1569. }
  1570. /** Attach a function object
  1571. * @param f Function object to attach
  1572. * @note The function object is limited to a single word of storage
  1573. * @deprecated
  1574. * Replaced by simple assignment 'Callback cb = func'
  1575. */
  1576. template <typename F>
  1577. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1578. "Replaced by simple assignment 'Callback cb = func")
  1579. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1) const volatile))
  1580. {
  1581. this->~Callback();
  1582. new (this) Callback(f);
  1583. }
  1584. /** Attach a static function with a bound pointer
  1585. * @param obj Pointer to object to bind to function
  1586. * @param func Static function to attach
  1587. * @deprecated
  1588. * Arguments to callback have been reordered to attach(func, arg)
  1589. */
  1590. template <typename T, typename U>
  1591. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1592. "Arguments to callback have been reordered to attach(func, arg)")
  1593. void attach(U *obj, R(*func)(T *, A0, A1))
  1594. {
  1595. this->~Callback();
  1596. new (this) Callback(func, obj);
  1597. }
  1598. /** Attach a static function with a bound pointer
  1599. * @param obj Pointer to object to bind to function
  1600. * @param func Static function to attach
  1601. * @deprecated
  1602. * Arguments to callback have been reordered to attach(func, arg)
  1603. */
  1604. template <typename T, typename U>
  1605. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1606. "Arguments to callback have been reordered to attach(func, arg)")
  1607. void attach(const U *obj, R(*func)(const T *, A0, A1))
  1608. {
  1609. this->~Callback();
  1610. new (this) Callback(func, obj);
  1611. }
  1612. /** Attach a static function with a bound pointer
  1613. * @param obj Pointer to object to bind to function
  1614. * @param func Static function to attach
  1615. * @deprecated
  1616. * Arguments to callback have been reordered to attach(func, arg)
  1617. */
  1618. template <typename T, typename U>
  1619. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1620. "Arguments to callback have been reordered to attach(func, arg)")
  1621. void attach(volatile U *obj, R(*func)(volatile T *, A0, A1))
  1622. {
  1623. this->~Callback();
  1624. new (this) Callback(func, obj);
  1625. }
  1626. /** Attach a static function with a bound pointer
  1627. * @param obj Pointer to object to bind to function
  1628. * @param func Static function to attach
  1629. * @deprecated
  1630. * Arguments to callback have been reordered to attach(func, arg)
  1631. */
  1632. template <typename T, typename U>
  1633. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1634. "Arguments to callback have been reordered to attach(func, arg)")
  1635. void attach(const volatile U *obj, R(*func)(const volatile T *, A0, A1))
  1636. {
  1637. this->~Callback();
  1638. new (this) Callback(func, obj);
  1639. }
  1640. /** Assign a callback
  1641. */
  1642. Callback &operator=(const Callback &that)
  1643. {
  1644. if (this != &that) {
  1645. this->~Callback();
  1646. new (this) Callback(that);
  1647. }
  1648. return *this;
  1649. }
  1650. /** Call the attached function
  1651. */
  1652. R call(A0 a0, A1 a1) const
  1653. {
  1654. MBED_ASSERT(_ops);
  1655. return _ops->call(this, a0, a1);
  1656. }
  1657. /** Call the attached function
  1658. */
  1659. R operator()(A0 a0, A1 a1) const
  1660. {
  1661. return call(a0, a1);
  1662. }
  1663. /** Test if function has been attached
  1664. */
  1665. operator bool() const
  1666. {
  1667. return _ops;
  1668. }
  1669. /** Test for equality
  1670. */
  1671. friend bool operator==(const Callback &l, const Callback &r)
  1672. {
  1673. return memcmp(&l, &r, sizeof(Callback)) == 0;
  1674. }
  1675. /** Test for inequality
  1676. */
  1677. friend bool operator!=(const Callback &l, const Callback &r)
  1678. {
  1679. return !(l == r);
  1680. }
  1681. /** Static thunk for passing as C-style function
  1682. * @param func Callback to call passed as void pointer
  1683. * @param a0 An argument to be called with function func
  1684. * @param a1 An argument to be called with function func
  1685. * @return the value as determined by func which is of
  1686. * type and determined by the signiture of func
  1687. */
  1688. static R thunk(void *func, A0 a0, A1 a1)
  1689. {
  1690. return static_cast<Callback *>(func)->call(a0, a1);
  1691. }
  1692. private:
  1693. // Stored as pointer to function and pointer to optional object
  1694. // Function pointer is stored as union of possible function types
  1695. // to guarantee proper size and alignment
  1696. struct _class;
  1697. union {
  1698. void (*_staticfunc)(A0, A1);
  1699. void (*_boundfunc)(_class *, A0, A1);
  1700. void (_class::*_methodfunc)(A0, A1);
  1701. } _func;
  1702. void *_obj;
  1703. // Dynamically dispatched operations
  1704. const struct ops {
  1705. R(*call)(const void *, A0, A1);
  1706. void (*move)(void *, const void *);
  1707. void (*dtor)(void *);
  1708. } *_ops;
  1709. // Generate operations for function object
  1710. template <typename F>
  1711. void generate(const F &f)
  1712. {
  1713. static const ops ops = {
  1714. &Callback::function_call<F>,
  1715. &Callback::function_move<F>,
  1716. &Callback::function_dtor<F>,
  1717. };
  1718. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  1719. "Type F must not exceed the size of the Callback class");
  1720. memset(this, 0, sizeof(Callback));
  1721. new (this) F(f);
  1722. _ops = &ops;
  1723. }
  1724. // Function attributes
  1725. template <typename F>
  1726. static R function_call(const void *p, A0 a0, A1 a1)
  1727. {
  1728. return (*(F *)p)(a0, a1);
  1729. }
  1730. template <typename F>
  1731. static void function_move(void *d, const void *p)
  1732. {
  1733. new (d) F(*(F *)p);
  1734. }
  1735. template <typename F>
  1736. static void function_dtor(void *p)
  1737. {
  1738. ((F *)p)->~F();
  1739. }
  1740. // Wrappers for functions with context
  1741. template <typename O, typename M>
  1742. struct method_context {
  1743. M method;
  1744. O *obj;
  1745. method_context(O *obj, M method)
  1746. : method(method), obj(obj) {}
  1747. R operator()(A0 a0, A1 a1) const
  1748. {
  1749. return (obj->*method)(a0, a1);
  1750. }
  1751. };
  1752. template <typename F, typename A>
  1753. struct function_context {
  1754. F func;
  1755. A *arg;
  1756. function_context(F func, A *arg)
  1757. : func(func), arg(arg) {}
  1758. R operator()(A0 a0, A1 a1) const
  1759. {
  1760. return func(arg, a0, a1);
  1761. }
  1762. };
  1763. };
  1764. /** Callback class based on template specialization
  1765. *
  1766. * @note Synchronization level: Not protected
  1767. */
  1768. template <typename R, typename A0, typename A1, typename A2>
  1769. class Callback<R(A0, A1, A2)> {
  1770. public:
  1771. /** Create a Callback with a static function
  1772. * @param func Static function to attach
  1773. */
  1774. Callback(R(*func)(A0, A1, A2) = 0)
  1775. {
  1776. if (!func) {
  1777. memset(this, 0, sizeof(Callback));
  1778. } else {
  1779. generate(func);
  1780. }
  1781. }
  1782. /** Attach a Callback
  1783. * @param func The Callback to attach
  1784. */
  1785. Callback(const Callback<R(A0, A1, A2)> &func)
  1786. {
  1787. if (func._ops) {
  1788. func._ops->move(this, &func);
  1789. }
  1790. _ops = func._ops;
  1791. }
  1792. /** Create a Callback with a member function
  1793. * @param obj Pointer to object to invoke member function on
  1794. * @param method Member function to attach
  1795. */
  1796. template<typename T, typename U>
  1797. Callback(U *obj, R(T::*method)(A0, A1, A2))
  1798. {
  1799. generate(method_context<T, R(T::*)(A0, A1, A2)>(obj, method));
  1800. }
  1801. /** Create a Callback with a member function
  1802. * @param obj Pointer to object to invoke member function on
  1803. * @param method Member function to attach
  1804. */
  1805. template<typename T, typename U>
  1806. Callback(const U *obj, R(T::*method)(A0, A1, A2) const)
  1807. {
  1808. generate(method_context<const T, R(T::*)(A0, A1, A2) const>(obj, method));
  1809. }
  1810. /** Create a Callback with a member function
  1811. * @param obj Pointer to object to invoke member function on
  1812. * @param method Member function to attach
  1813. */
  1814. template<typename T, typename U>
  1815. Callback(volatile U *obj, R(T::*method)(A0, A1, A2) volatile)
  1816. {
  1817. generate(method_context<volatile T, R(T::*)(A0, A1, A2) volatile>(obj, method));
  1818. }
  1819. /** Create a Callback with a member function
  1820. * @param obj Pointer to object to invoke member function on
  1821. * @param method Member function to attach
  1822. */
  1823. template<typename T, typename U>
  1824. Callback(const volatile U *obj, R(T::*method)(A0, A1, A2) const volatile)
  1825. {
  1826. generate(method_context<const volatile T, R(T::*)(A0, A1, A2) const volatile>(obj, method));
  1827. }
  1828. /** Create a Callback with a static function and bound pointer
  1829. * @param func Static function to attach
  1830. * @param arg Pointer argument to function
  1831. */
  1832. template<typename T, typename U>
  1833. Callback(R(*func)(T *, A0, A1, A2), U *arg)
  1834. {
  1835. generate(function_context<R(*)(T *, A0, A1, A2), T>(func, arg));
  1836. }
  1837. /** Create a Callback with a static function and bound pointer
  1838. * @param func Static function to attach
  1839. * @param arg Pointer argument to function
  1840. */
  1841. template<typename T, typename U>
  1842. Callback(R(*func)(const T *, A0, A1, A2), const U *arg)
  1843. {
  1844. generate(function_context<R(*)(const T *, A0, A1, A2), const T>(func, arg));
  1845. }
  1846. /** Create a Callback with a static function and bound pointer
  1847. * @param func Static function to attach
  1848. * @param arg Pointer argument to function
  1849. */
  1850. template<typename T, typename U>
  1851. Callback(R(*func)(volatile T *, A0, A1, A2), volatile U *arg)
  1852. {
  1853. generate(function_context<R(*)(volatile T *, A0, A1, A2), volatile T>(func, arg));
  1854. }
  1855. /** Create a Callback with a static function and bound pointer
  1856. * @param func Static function to attach
  1857. * @param arg Pointer argument to function
  1858. */
  1859. template<typename T, typename U>
  1860. Callback(R(*func)(const volatile T *, A0, A1, A2), const volatile U *arg)
  1861. {
  1862. generate(function_context<R(*)(const volatile T *, A0, A1, A2), const volatile T>(func, arg));
  1863. }
  1864. /** Create a Callback with a function object
  1865. * @param f Function object to attach
  1866. * @note The function object is limited to a single word of storage
  1867. */
  1868. template <typename F>
  1869. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2)))
  1870. {
  1871. generate(f);
  1872. }
  1873. /** Create a Callback with a function object
  1874. * @param f Function object to attach
  1875. * @note The function object is limited to a single word of storage
  1876. */
  1877. template <typename F>
  1878. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) const))
  1879. {
  1880. generate(f);
  1881. }
  1882. /** Create a Callback with a function object
  1883. * @param f Function object to attach
  1884. * @note The function object is limited to a single word of storage
  1885. */
  1886. template <typename F>
  1887. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) volatile))
  1888. {
  1889. generate(f);
  1890. }
  1891. /** Create a Callback with a function object
  1892. * @param f Function object to attach
  1893. * @note The function object is limited to a single word of storage
  1894. */
  1895. template <typename F>
  1896. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) const volatile))
  1897. {
  1898. generate(f);
  1899. }
  1900. /** Create a Callback with a static function and bound pointer
  1901. * @param obj Pointer to object to bind to function
  1902. * @param func Static function to attach
  1903. * @deprecated
  1904. * Arguments to callback have been reordered to Callback(func, arg)
  1905. */
  1906. template<typename T, typename U>
  1907. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1908. "Arguments to callback have been reordered to Callback(func, arg)")
  1909. Callback(U *obj, R(*func)(T *, A0, A1, A2))
  1910. {
  1911. new (this) Callback(func, obj);
  1912. }
  1913. /** Create a Callback with a static function and bound pointer
  1914. * @param obj Pointer to object to bind to function
  1915. * @param func Static function to attach
  1916. * @deprecated
  1917. * Arguments to callback have been reordered to Callback(func, arg)
  1918. */
  1919. template<typename T, typename U>
  1920. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1921. "Arguments to callback have been reordered to Callback(func, arg)")
  1922. Callback(const U *obj, R(*func)(const T *, A0, A1, A2))
  1923. {
  1924. new (this) Callback(func, obj);
  1925. }
  1926. /** Create a Callback with a static function and bound pointer
  1927. * @param obj Pointer to object to bind to function
  1928. * @param func Static function to attach
  1929. * @deprecated
  1930. * Arguments to callback have been reordered to Callback(func, arg)
  1931. */
  1932. template<typename T, typename U>
  1933. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1934. "Arguments to callback have been reordered to Callback(func, arg)")
  1935. Callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2))
  1936. {
  1937. new (this) Callback(func, obj);
  1938. }
  1939. /** Create a Callback with a static function and bound pointer
  1940. * @param obj Pointer to object to bind to function
  1941. * @param func Static function to attach
  1942. * @deprecated
  1943. * Arguments to callback have been reordered to Callback(func, arg)
  1944. */
  1945. template<typename T, typename U>
  1946. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  1947. "Arguments to callback have been reordered to Callback(func, arg)")
  1948. Callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2))
  1949. {
  1950. new (this) Callback(func, obj);
  1951. }
  1952. /** Destroy a callback
  1953. */
  1954. ~Callback()
  1955. {
  1956. if (_ops) {
  1957. _ops->dtor(this);
  1958. }
  1959. }
  1960. /** Attach a static function
  1961. * @param func Static function to attach
  1962. * @deprecated
  1963. * Replaced by simple assignment 'Callback cb = func'
  1964. */
  1965. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1966. "Replaced by simple assignment 'Callback cb = func")
  1967. void attach(R(*func)(A0, A1, A2))
  1968. {
  1969. this->~Callback();
  1970. new (this) Callback(func);
  1971. }
  1972. /** Attach a Callback
  1973. * @param func The Callback to attach
  1974. * @deprecated
  1975. * Replaced by simple assignment 'Callback cb = func'
  1976. */
  1977. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1978. "Replaced by simple assignment 'Callback cb = func")
  1979. void attach(const Callback<R(A0, A1, A2)> &func)
  1980. {
  1981. this->~Callback();
  1982. new (this) Callback(func);
  1983. }
  1984. /** Attach a member function
  1985. * @param obj Pointer to object to invoke member function on
  1986. * @param method Member function to attach
  1987. * @deprecated
  1988. * Replaced by simple assignment 'Callback cb = func'
  1989. */
  1990. template<typename T, typename U>
  1991. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  1992. "Replaced by simple assignment 'Callback cb = func")
  1993. void attach(U *obj, R(T::*method)(A0, A1, A2))
  1994. {
  1995. this->~Callback();
  1996. new (this) Callback(obj, method);
  1997. }
  1998. /** Attach a member function
  1999. * @param obj Pointer to object to invoke member function on
  2000. * @param method Member function to attach
  2001. * @deprecated
  2002. * Replaced by simple assignment 'Callback cb = func'
  2003. */
  2004. template<typename T, typename U>
  2005. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2006. "Replaced by simple assignment 'Callback cb = func")
  2007. void attach(const U *obj, R(T::*method)(A0, A1, A2) const)
  2008. {
  2009. this->~Callback();
  2010. new (this) Callback(obj, method);
  2011. }
  2012. /** Attach a member function
  2013. * @param obj Pointer to object to invoke member function on
  2014. * @param method Member function to attach
  2015. * @deprecated
  2016. * Replaced by simple assignment 'Callback cb = func'
  2017. */
  2018. template<typename T, typename U>
  2019. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2020. "Replaced by simple assignment 'Callback cb = func")
  2021. void attach(volatile U *obj, R(T::*method)(A0, A1, A2) volatile)
  2022. {
  2023. this->~Callback();
  2024. new (this) Callback(obj, method);
  2025. }
  2026. /** Attach a member function
  2027. * @param obj Pointer to object to invoke member function on
  2028. * @param method Member function to attach
  2029. * @deprecated
  2030. * Replaced by simple assignment 'Callback cb = func'
  2031. */
  2032. template<typename T, typename U>
  2033. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2034. "Replaced by simple assignment 'Callback cb = func")
  2035. void attach(const volatile U *obj, R(T::*method)(A0, A1, A2) const volatile)
  2036. {
  2037. this->~Callback();
  2038. new (this) Callback(obj, method);
  2039. }
  2040. /** Attach a static function with a bound pointer
  2041. * @param func Static function to attach
  2042. * @param arg Pointer argument to function
  2043. * @deprecated
  2044. * Replaced by simple assignment 'Callback cb = func'
  2045. */
  2046. template <typename T, typename U>
  2047. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2048. "Replaced by simple assignment 'Callback cb = func")
  2049. void attach(R(*func)(T *, A0, A1, A2), U *arg)
  2050. {
  2051. this->~Callback();
  2052. new (this) Callback(func, arg);
  2053. }
  2054. /** Attach a static function with a bound pointer
  2055. * @param func Static function to attach
  2056. * @param arg Pointer argument to function
  2057. * @deprecated
  2058. * Replaced by simple assignment 'Callback cb = func'
  2059. */
  2060. template <typename T, typename U>
  2061. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2062. "Replaced by simple assignment 'Callback cb = func")
  2063. void attach(R(*func)(const T *, A0, A1, A2), const U *arg)
  2064. {
  2065. this->~Callback();
  2066. new (this) Callback(func, arg);
  2067. }
  2068. /** Attach a static function with a bound pointer
  2069. * @param func Static function to attach
  2070. * @param arg Pointer argument to function
  2071. * @deprecated
  2072. * Replaced by simple assignment 'Callback cb = func'
  2073. */
  2074. template <typename T, typename U>
  2075. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2076. "Replaced by simple assignment 'Callback cb = func")
  2077. void attach(R(*func)(volatile T *, A0, A1, A2), volatile U *arg)
  2078. {
  2079. this->~Callback();
  2080. new (this) Callback(func, arg);
  2081. }
  2082. /** Attach a static function with a bound pointer
  2083. * @param func Static function to attach
  2084. * @param arg Pointer argument to function
  2085. * @deprecated
  2086. * Replaced by simple assignment 'Callback cb = func'
  2087. */
  2088. template <typename T, typename U>
  2089. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2090. "Replaced by simple assignment 'Callback cb = func")
  2091. void attach(R(*func)(const volatile T *, A0, A1, A2), const volatile U *arg)
  2092. {
  2093. this->~Callback();
  2094. new (this) Callback(func, arg);
  2095. }
  2096. /** Attach a function object
  2097. * @param f Function object to attach
  2098. * @note The function object is limited to a single word of storage
  2099. * @deprecated
  2100. * Replaced by simple assignment 'Callback cb = func'
  2101. */
  2102. template <typename F>
  2103. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2104. "Replaced by simple assignment 'Callback cb = func")
  2105. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2)))
  2106. {
  2107. this->~Callback();
  2108. new (this) Callback(f);
  2109. }
  2110. /** Attach a function object
  2111. * @param f Function object to attach
  2112. * @note The function object is limited to a single word of storage
  2113. * @deprecated
  2114. * Replaced by simple assignment 'Callback cb = func'
  2115. */
  2116. template <typename F>
  2117. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2118. "Replaced by simple assignment 'Callback cb = func")
  2119. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) const))
  2120. {
  2121. this->~Callback();
  2122. new (this) Callback(f);
  2123. }
  2124. /** Attach a function object
  2125. * @param f Function object to attach
  2126. * @note The function object is limited to a single word of storage
  2127. * @deprecated
  2128. * Replaced by simple assignment 'Callback cb = func'
  2129. */
  2130. template <typename F>
  2131. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2132. "Replaced by simple assignment 'Callback cb = func")
  2133. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) volatile))
  2134. {
  2135. this->~Callback();
  2136. new (this) Callback(f);
  2137. }
  2138. /** Attach a function object
  2139. * @param f Function object to attach
  2140. * @note The function object is limited to a single word of storage
  2141. * @deprecated
  2142. * Replaced by simple assignment 'Callback cb = func'
  2143. */
  2144. template <typename F>
  2145. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2146. "Replaced by simple assignment 'Callback cb = func")
  2147. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2) const volatile))
  2148. {
  2149. this->~Callback();
  2150. new (this) Callback(f);
  2151. }
  2152. /** Attach a static function with a bound pointer
  2153. * @param obj Pointer to object to bind to function
  2154. * @param func Static function to attach
  2155. * @deprecated
  2156. * Arguments to callback have been reordered to attach(func, arg)
  2157. */
  2158. template <typename T, typename U>
  2159. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2160. "Arguments to callback have been reordered to attach(func, arg)")
  2161. void attach(U *obj, R(*func)(T *, A0, A1, A2))
  2162. {
  2163. this->~Callback();
  2164. new (this) Callback(func, obj);
  2165. }
  2166. /** Attach a static function with a bound pointer
  2167. * @param obj Pointer to object to bind to function
  2168. * @param func Static function to attach
  2169. * @deprecated
  2170. * Arguments to callback have been reordered to attach(func, arg)
  2171. */
  2172. template <typename T, typename U>
  2173. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2174. "Arguments to callback have been reordered to attach(func, arg)")
  2175. void attach(const U *obj, R(*func)(const T *, A0, A1, A2))
  2176. {
  2177. this->~Callback();
  2178. new (this) Callback(func, obj);
  2179. }
  2180. /** Attach a static function with a bound pointer
  2181. * @param obj Pointer to object to bind to function
  2182. * @param func Static function to attach
  2183. * @deprecated
  2184. * Arguments to callback have been reordered to attach(func, arg)
  2185. */
  2186. template <typename T, typename U>
  2187. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2188. "Arguments to callback have been reordered to attach(func, arg)")
  2189. void attach(volatile U *obj, R(*func)(volatile T *, A0, A1, A2))
  2190. {
  2191. this->~Callback();
  2192. new (this) Callback(func, obj);
  2193. }
  2194. /** Attach a static function with a bound pointer
  2195. * @param obj Pointer to object to bind to function
  2196. * @param func Static function to attach
  2197. * @deprecated
  2198. * Arguments to callback have been reordered to attach(func, arg)
  2199. */
  2200. template <typename T, typename U>
  2201. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2202. "Arguments to callback have been reordered to attach(func, arg)")
  2203. void attach(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2))
  2204. {
  2205. this->~Callback();
  2206. new (this) Callback(func, obj);
  2207. }
  2208. /** Assign a callback
  2209. */
  2210. Callback &operator=(const Callback &that)
  2211. {
  2212. if (this != &that) {
  2213. this->~Callback();
  2214. new (this) Callback(that);
  2215. }
  2216. return *this;
  2217. }
  2218. /** Call the attached function
  2219. */
  2220. R call(A0 a0, A1 a1, A2 a2) const
  2221. {
  2222. MBED_ASSERT(_ops);
  2223. return _ops->call(this, a0, a1, a2);
  2224. }
  2225. /** Call the attached function
  2226. */
  2227. R operator()(A0 a0, A1 a1, A2 a2) const
  2228. {
  2229. return call(a0, a1, a2);
  2230. }
  2231. /** Test if function has been attached
  2232. */
  2233. operator bool() const
  2234. {
  2235. return _ops;
  2236. }
  2237. /** Test for equality
  2238. */
  2239. friend bool operator==(const Callback &l, const Callback &r)
  2240. {
  2241. return memcmp(&l, &r, sizeof(Callback)) == 0;
  2242. }
  2243. /** Test for inequality
  2244. */
  2245. friend bool operator!=(const Callback &l, const Callback &r)
  2246. {
  2247. return !(l == r);
  2248. }
  2249. /** Static thunk for passing as C-style function
  2250. * @param func Callback to call passed as void pointer
  2251. * @param a0 An argument to be called with function func
  2252. * @param a1 An argument to be called with function func
  2253. * @param a2 An argument to be called with function func
  2254. * @return the value as determined by func which is of
  2255. * type and determined by the signiture of func
  2256. */
  2257. static R thunk(void *func, A0 a0, A1 a1, A2 a2)
  2258. {
  2259. return static_cast<Callback *>(func)->call(a0, a1, a2);
  2260. }
  2261. private:
  2262. // Stored as pointer to function and pointer to optional object
  2263. // Function pointer is stored as union of possible function types
  2264. // to guarantee proper size and alignment
  2265. struct _class;
  2266. union {
  2267. void (*_staticfunc)(A0, A1, A2);
  2268. void (*_boundfunc)(_class *, A0, A1, A2);
  2269. void (_class::*_methodfunc)(A0, A1, A2);
  2270. } _func;
  2271. void *_obj;
  2272. // Dynamically dispatched operations
  2273. const struct ops {
  2274. R(*call)(const void *, A0, A1, A2);
  2275. void (*move)(void *, const void *);
  2276. void (*dtor)(void *);
  2277. } *_ops;
  2278. // Generate operations for function object
  2279. template <typename F>
  2280. void generate(const F &f)
  2281. {
  2282. static const ops ops = {
  2283. &Callback::function_call<F>,
  2284. &Callback::function_move<F>,
  2285. &Callback::function_dtor<F>,
  2286. };
  2287. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  2288. "Type F must not exceed the size of the Callback class");
  2289. memset(this, 0, sizeof(Callback));
  2290. new (this) F(f);
  2291. _ops = &ops;
  2292. }
  2293. // Function attributes
  2294. template <typename F>
  2295. static R function_call(const void *p, A0 a0, A1 a1, A2 a2)
  2296. {
  2297. return (*(F *)p)(a0, a1, a2);
  2298. }
  2299. template <typename F>
  2300. static void function_move(void *d, const void *p)
  2301. {
  2302. new (d) F(*(F *)p);
  2303. }
  2304. template <typename F>
  2305. static void function_dtor(void *p)
  2306. {
  2307. ((F *)p)->~F();
  2308. }
  2309. // Wrappers for functions with context
  2310. template <typename O, typename M>
  2311. struct method_context {
  2312. M method;
  2313. O *obj;
  2314. method_context(O *obj, M method)
  2315. : method(method), obj(obj) {}
  2316. R operator()(A0 a0, A1 a1, A2 a2) const
  2317. {
  2318. return (obj->*method)(a0, a1, a2);
  2319. }
  2320. };
  2321. template <typename F, typename A>
  2322. struct function_context {
  2323. F func;
  2324. A *arg;
  2325. function_context(F func, A *arg)
  2326. : func(func), arg(arg) {}
  2327. R operator()(A0 a0, A1 a1, A2 a2) const
  2328. {
  2329. return func(arg, a0, a1, a2);
  2330. }
  2331. };
  2332. };
  2333. /** Callback class based on template specialization
  2334. *
  2335. * @note Synchronization level: Not protected
  2336. */
  2337. template <typename R, typename A0, typename A1, typename A2, typename A3>
  2338. class Callback<R(A0, A1, A2, A3)> {
  2339. public:
  2340. /** Create a Callback with a static function
  2341. * @param func Static function to attach
  2342. */
  2343. Callback(R(*func)(A0, A1, A2, A3) = 0)
  2344. {
  2345. if (!func) {
  2346. memset(this, 0, sizeof(Callback));
  2347. } else {
  2348. generate(func);
  2349. }
  2350. }
  2351. /** Attach a Callback
  2352. * @param func The Callback to attach
  2353. */
  2354. Callback(const Callback<R(A0, A1, A2, A3)> &func)
  2355. {
  2356. if (func._ops) {
  2357. func._ops->move(this, &func);
  2358. }
  2359. _ops = func._ops;
  2360. }
  2361. /** Create a Callback with a member function
  2362. * @param obj Pointer to object to invoke member function on
  2363. * @param method Member function to attach
  2364. */
  2365. template<typename T, typename U>
  2366. Callback(U *obj, R(T::*method)(A0, A1, A2, A3))
  2367. {
  2368. generate(method_context<T, R(T::*)(A0, A1, A2, A3)>(obj, method));
  2369. }
  2370. /** Create a Callback with a member function
  2371. * @param obj Pointer to object to invoke member function on
  2372. * @param method Member function to attach
  2373. */
  2374. template<typename T, typename U>
  2375. Callback(const U *obj, R(T::*method)(A0, A1, A2, A3) const)
  2376. {
  2377. generate(method_context<const T, R(T::*)(A0, A1, A2, A3) const>(obj, method));
  2378. }
  2379. /** Create a Callback with a member function
  2380. * @param obj Pointer to object to invoke member function on
  2381. * @param method Member function to attach
  2382. */
  2383. template<typename T, typename U>
  2384. Callback(volatile U *obj, R(T::*method)(A0, A1, A2, A3) volatile)
  2385. {
  2386. generate(method_context<volatile T, R(T::*)(A0, A1, A2, A3) volatile>(obj, method));
  2387. }
  2388. /** Create a Callback with a member function
  2389. * @param obj Pointer to object to invoke member function on
  2390. * @param method Member function to attach
  2391. */
  2392. template<typename T, typename U>
  2393. Callback(const volatile U *obj, R(T::*method)(A0, A1, A2, A3) const volatile)
  2394. {
  2395. generate(method_context<const volatile T, R(T::*)(A0, A1, A2, A3) const volatile>(obj, method));
  2396. }
  2397. /** Create a Callback with a static function and bound pointer
  2398. * @param func Static function to attach
  2399. * @param arg Pointer argument to function
  2400. */
  2401. template<typename T, typename U>
  2402. Callback(R(*func)(T *, A0, A1, A2, A3), U *arg)
  2403. {
  2404. generate(function_context<R(*)(T *, A0, A1, A2, A3), T>(func, arg));
  2405. }
  2406. /** Create a Callback with a static function and bound pointer
  2407. * @param func Static function to attach
  2408. * @param arg Pointer argument to function
  2409. */
  2410. template<typename T, typename U>
  2411. Callback(R(*func)(const T *, A0, A1, A2, A3), const U *arg)
  2412. {
  2413. generate(function_context<R(*)(const T *, A0, A1, A2, A3), const T>(func, arg));
  2414. }
  2415. /** Create a Callback with a static function and bound pointer
  2416. * @param func Static function to attach
  2417. * @param arg Pointer argument to function
  2418. */
  2419. template<typename T, typename U>
  2420. Callback(R(*func)(volatile T *, A0, A1, A2, A3), volatile U *arg)
  2421. {
  2422. generate(function_context<R(*)(volatile T *, A0, A1, A2, A3), volatile T>(func, arg));
  2423. }
  2424. /** Create a Callback with a static function and bound pointer
  2425. * @param func Static function to attach
  2426. * @param arg Pointer argument to function
  2427. */
  2428. template<typename T, typename U>
  2429. Callback(R(*func)(const volatile T *, A0, A1, A2, A3), const volatile U *arg)
  2430. {
  2431. generate(function_context<R(*)(const volatile T *, A0, A1, A2, A3), const volatile T>(func, arg));
  2432. }
  2433. /** Create a Callback with a function object
  2434. * @param f Function object to attach
  2435. * @note The function object is limited to a single word of storage
  2436. */
  2437. template <typename F>
  2438. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3)))
  2439. {
  2440. generate(f);
  2441. }
  2442. /** Create a Callback with a function object
  2443. * @param f Function object to attach
  2444. * @note The function object is limited to a single word of storage
  2445. */
  2446. template <typename F>
  2447. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) const))
  2448. {
  2449. generate(f);
  2450. }
  2451. /** Create a Callback with a function object
  2452. * @param f Function object to attach
  2453. * @note The function object is limited to a single word of storage
  2454. */
  2455. template <typename F>
  2456. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) volatile))
  2457. {
  2458. generate(f);
  2459. }
  2460. /** Create a Callback with a function object
  2461. * @param f Function object to attach
  2462. * @note The function object is limited to a single word of storage
  2463. */
  2464. template <typename F>
  2465. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) const volatile))
  2466. {
  2467. generate(f);
  2468. }
  2469. /** Create a Callback with a static function and bound pointer
  2470. * @param obj Pointer to object to bind to function
  2471. * @param func Static function to attach
  2472. * @deprecated
  2473. * Arguments to callback have been reordered to Callback(func, arg)
  2474. */
  2475. template<typename T, typename U>
  2476. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2477. "Arguments to callback have been reordered to Callback(func, arg)")
  2478. Callback(U *obj, R(*func)(T *, A0, A1, A2, A3))
  2479. {
  2480. new (this) Callback(func, obj);
  2481. }
  2482. /** Create a Callback with a static function and bound pointer
  2483. * @param obj Pointer to object to bind to function
  2484. * @param func Static function to attach
  2485. * @deprecated
  2486. * Arguments to callback have been reordered to Callback(func, arg)
  2487. */
  2488. template<typename T, typename U>
  2489. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2490. "Arguments to callback have been reordered to Callback(func, arg)")
  2491. Callback(const U *obj, R(*func)(const T *, A0, A1, A2, A3))
  2492. {
  2493. new (this) Callback(func, obj);
  2494. }
  2495. /** Create a Callback with a static function and bound pointer
  2496. * @param obj Pointer to object to bind to function
  2497. * @param func Static function to attach
  2498. * @deprecated
  2499. * Arguments to callback have been reordered to Callback(func, arg)
  2500. */
  2501. template<typename T, typename U>
  2502. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2503. "Arguments to callback have been reordered to Callback(func, arg)")
  2504. Callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3))
  2505. {
  2506. new (this) Callback(func, obj);
  2507. }
  2508. /** Create a Callback with a static function and bound pointer
  2509. * @param obj Pointer to object to bind to function
  2510. * @param func Static function to attach
  2511. * @deprecated
  2512. * Arguments to callback have been reordered to Callback(func, arg)
  2513. */
  2514. template<typename T, typename U>
  2515. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2516. "Arguments to callback have been reordered to Callback(func, arg)")
  2517. Callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3))
  2518. {
  2519. new (this) Callback(func, obj);
  2520. }
  2521. /** Destroy a callback
  2522. */
  2523. ~Callback()
  2524. {
  2525. if (_ops) {
  2526. _ops->dtor(this);
  2527. }
  2528. }
  2529. /** Attach a static function
  2530. * @param func Static function to attach
  2531. * @deprecated
  2532. * Replaced by simple assignment 'Callback cb = func'
  2533. */
  2534. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2535. "Replaced by simple assignment 'Callback cb = func")
  2536. void attach(R(*func)(A0, A1, A2, A3))
  2537. {
  2538. this->~Callback();
  2539. new (this) Callback(func);
  2540. }
  2541. /** Attach a Callback
  2542. * @param func The Callback to attach
  2543. * @deprecated
  2544. * Replaced by simple assignment 'Callback cb = func'
  2545. */
  2546. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2547. "Replaced by simple assignment 'Callback cb = func")
  2548. void attach(const Callback<R(A0, A1, A2, A3)> &func)
  2549. {
  2550. this->~Callback();
  2551. new (this) Callback(func);
  2552. }
  2553. /** Attach a member function
  2554. * @param obj Pointer to object to invoke member function on
  2555. * @param method Member function to attach
  2556. * @deprecated
  2557. * Replaced by simple assignment 'Callback cb = func'
  2558. */
  2559. template<typename T, typename U>
  2560. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2561. "Replaced by simple assignment 'Callback cb = func")
  2562. void attach(U *obj, R(T::*method)(A0, A1, A2, A3))
  2563. {
  2564. this->~Callback();
  2565. new (this) Callback(obj, method);
  2566. }
  2567. /** Attach a member function
  2568. * @param obj Pointer to object to invoke member function on
  2569. * @param method Member function to attach
  2570. * @deprecated
  2571. * Replaced by simple assignment 'Callback cb = func'
  2572. */
  2573. template<typename T, typename U>
  2574. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2575. "Replaced by simple assignment 'Callback cb = func")
  2576. void attach(const U *obj, R(T::*method)(A0, A1, A2, A3) const)
  2577. {
  2578. this->~Callback();
  2579. new (this) Callback(obj, method);
  2580. }
  2581. /** Attach a member function
  2582. * @param obj Pointer to object to invoke member function on
  2583. * @param method Member function to attach
  2584. * @deprecated
  2585. * Replaced by simple assignment 'Callback cb = func'
  2586. */
  2587. template<typename T, typename U>
  2588. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2589. "Replaced by simple assignment 'Callback cb = func")
  2590. void attach(volatile U *obj, R(T::*method)(A0, A1, A2, A3) volatile)
  2591. {
  2592. this->~Callback();
  2593. new (this) Callback(obj, method);
  2594. }
  2595. /** Attach a member function
  2596. * @param obj Pointer to object to invoke member function on
  2597. * @param method Member function to attach
  2598. * @deprecated
  2599. * Replaced by simple assignment 'Callback cb = func'
  2600. */
  2601. template<typename T, typename U>
  2602. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2603. "Replaced by simple assignment 'Callback cb = func")
  2604. void attach(const volatile U *obj, R(T::*method)(A0, A1, A2, A3) const volatile)
  2605. {
  2606. this->~Callback();
  2607. new (this) Callback(obj, method);
  2608. }
  2609. /** Attach a static function with a bound pointer
  2610. * @param func Static function to attach
  2611. * @param arg Pointer argument to function
  2612. * @deprecated
  2613. * Replaced by simple assignment 'Callback cb = func'
  2614. */
  2615. template <typename T, typename U>
  2616. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2617. "Replaced by simple assignment 'Callback cb = func")
  2618. void attach(R(*func)(T *, A0, A1, A2, A3), U *arg)
  2619. {
  2620. this->~Callback();
  2621. new (this) Callback(func, arg);
  2622. }
  2623. /** Attach a static function with a bound pointer
  2624. * @param func Static function to attach
  2625. * @param arg Pointer argument to function
  2626. * @deprecated
  2627. * Replaced by simple assignment 'Callback cb = func'
  2628. */
  2629. template <typename T, typename U>
  2630. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2631. "Replaced by simple assignment 'Callback cb = func")
  2632. void attach(R(*func)(const T *, A0, A1, A2, A3), const U *arg)
  2633. {
  2634. this->~Callback();
  2635. new (this) Callback(func, arg);
  2636. }
  2637. /** Attach a static function with a bound pointer
  2638. * @param func Static function to attach
  2639. * @param arg Pointer argument to function
  2640. * @deprecated
  2641. * Replaced by simple assignment 'Callback cb = func'
  2642. */
  2643. template <typename T, typename U>
  2644. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2645. "Replaced by simple assignment 'Callback cb = func")
  2646. void attach(R(*func)(volatile T *, A0, A1, A2, A3), volatile U *arg)
  2647. {
  2648. this->~Callback();
  2649. new (this) Callback(func, arg);
  2650. }
  2651. /** Attach a static function with a bound pointer
  2652. * @param func Static function to attach
  2653. * @param arg Pointer argument to function
  2654. * @deprecated
  2655. * Replaced by simple assignment 'Callback cb = func'
  2656. */
  2657. template <typename T, typename U>
  2658. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2659. "Replaced by simple assignment 'Callback cb = func")
  2660. void attach(R(*func)(const volatile T *, A0, A1, A2, A3), const volatile U *arg)
  2661. {
  2662. this->~Callback();
  2663. new (this) Callback(func, arg);
  2664. }
  2665. /** Attach a function object
  2666. * @param f Function object to attach
  2667. * @note The function object is limited to a single word of storage
  2668. * @deprecated
  2669. * Replaced by simple assignment 'Callback cb = func'
  2670. */
  2671. template <typename F>
  2672. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2673. "Replaced by simple assignment 'Callback cb = func")
  2674. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3)))
  2675. {
  2676. this->~Callback();
  2677. new (this) Callback(f);
  2678. }
  2679. /** Attach a function object
  2680. * @param f Function object to attach
  2681. * @note The function object is limited to a single word of storage
  2682. * @deprecated
  2683. * Replaced by simple assignment 'Callback cb = func'
  2684. */
  2685. template <typename F>
  2686. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2687. "Replaced by simple assignment 'Callback cb = func")
  2688. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) const))
  2689. {
  2690. this->~Callback();
  2691. new (this) Callback(f);
  2692. }
  2693. /** Attach a function object
  2694. * @param f Function object to attach
  2695. * @note The function object is limited to a single word of storage
  2696. * @deprecated
  2697. * Replaced by simple assignment 'Callback cb = func'
  2698. */
  2699. template <typename F>
  2700. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2701. "Replaced by simple assignment 'Callback cb = func")
  2702. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) volatile))
  2703. {
  2704. this->~Callback();
  2705. new (this) Callback(f);
  2706. }
  2707. /** Attach a function object
  2708. * @param f Function object to attach
  2709. * @note The function object is limited to a single word of storage
  2710. * @deprecated
  2711. * Replaced by simple assignment 'Callback cb = func'
  2712. */
  2713. template <typename F>
  2714. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  2715. "Replaced by simple assignment 'Callback cb = func")
  2716. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3) const volatile))
  2717. {
  2718. this->~Callback();
  2719. new (this) Callback(f);
  2720. }
  2721. /** Attach a static function with a bound pointer
  2722. * @param obj Pointer to object to bind to function
  2723. * @param func Static function to attach
  2724. * @deprecated
  2725. * Arguments to callback have been reordered to attach(func, arg)
  2726. */
  2727. template <typename T, typename U>
  2728. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2729. "Arguments to callback have been reordered to attach(func, arg)")
  2730. void attach(U *obj, R(*func)(T *, A0, A1, A2, A3))
  2731. {
  2732. this->~Callback();
  2733. new (this) Callback(func, obj);
  2734. }
  2735. /** Attach a static function with a bound pointer
  2736. * @param obj Pointer to object to bind to function
  2737. * @param func Static function to attach
  2738. * @deprecated
  2739. * Arguments to callback have been reordered to attach(func, arg)
  2740. */
  2741. template <typename T, typename U>
  2742. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2743. "Arguments to callback have been reordered to attach(func, arg)")
  2744. void attach(const U *obj, R(*func)(const T *, A0, A1, A2, A3))
  2745. {
  2746. this->~Callback();
  2747. new (this) Callback(func, obj);
  2748. }
  2749. /** Attach a static function with a bound pointer
  2750. * @param obj Pointer to object to bind to function
  2751. * @param func Static function to attach
  2752. * @deprecated
  2753. * Arguments to callback have been reordered to attach(func, arg)
  2754. */
  2755. template <typename T, typename U>
  2756. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2757. "Arguments to callback have been reordered to attach(func, arg)")
  2758. void attach(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3))
  2759. {
  2760. this->~Callback();
  2761. new (this) Callback(func, obj);
  2762. }
  2763. /** Attach a static function with a bound pointer
  2764. * @param obj Pointer to object to bind to function
  2765. * @param func Static function to attach
  2766. * @deprecated
  2767. * Arguments to callback have been reordered to attach(func, arg)
  2768. */
  2769. template <typename T, typename U>
  2770. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  2771. "Arguments to callback have been reordered to attach(func, arg)")
  2772. void attach(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3))
  2773. {
  2774. this->~Callback();
  2775. new (this) Callback(func, obj);
  2776. }
  2777. /** Assign a callback
  2778. */
  2779. Callback &operator=(const Callback &that)
  2780. {
  2781. if (this != &that) {
  2782. this->~Callback();
  2783. new (this) Callback(that);
  2784. }
  2785. return *this;
  2786. }
  2787. /** Call the attached function
  2788. */
  2789. R call(A0 a0, A1 a1, A2 a2, A3 a3) const
  2790. {
  2791. MBED_ASSERT(_ops);
  2792. return _ops->call(this, a0, a1, a2, a3);
  2793. }
  2794. /** Call the attached function
  2795. */
  2796. R operator()(A0 a0, A1 a1, A2 a2, A3 a3) const
  2797. {
  2798. return call(a0, a1, a2, a3);
  2799. }
  2800. /** Test if function has been attached
  2801. */
  2802. operator bool() const
  2803. {
  2804. return _ops;
  2805. }
  2806. /** Test for equality
  2807. */
  2808. friend bool operator==(const Callback &l, const Callback &r)
  2809. {
  2810. return memcmp(&l, &r, sizeof(Callback)) == 0;
  2811. }
  2812. /** Test for inequality
  2813. */
  2814. friend bool operator!=(const Callback &l, const Callback &r)
  2815. {
  2816. return !(l == r);
  2817. }
  2818. /** Static thunk for passing as C-style function
  2819. * @param func Callback to call passed as void pointer
  2820. * @param a0 An argument to be called with function func
  2821. * @param a1 An argument to be called with function func
  2822. * @param a2 An argument to be called with function func
  2823. * @param a3 An argument to be called with function func
  2824. * @return the value as determined by func which is of
  2825. * type and determined by the signiture of func
  2826. */
  2827. static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3)
  2828. {
  2829. return static_cast<Callback *>(func)->call(a0, a1, a2, a3);
  2830. }
  2831. private:
  2832. // Stored as pointer to function and pointer to optional object
  2833. // Function pointer is stored as union of possible function types
  2834. // to guarantee proper size and alignment
  2835. struct _class;
  2836. union {
  2837. void (*_staticfunc)(A0, A1, A2, A3);
  2838. void (*_boundfunc)(_class *, A0, A1, A2, A3);
  2839. void (_class::*_methodfunc)(A0, A1, A2, A3);
  2840. } _func;
  2841. void *_obj;
  2842. // Dynamically dispatched operations
  2843. const struct ops {
  2844. R(*call)(const void *, A0, A1, A2, A3);
  2845. void (*move)(void *, const void *);
  2846. void (*dtor)(void *);
  2847. } *_ops;
  2848. // Generate operations for function object
  2849. template <typename F>
  2850. void generate(const F &f)
  2851. {
  2852. static const ops ops = {
  2853. &Callback::function_call<F>,
  2854. &Callback::function_move<F>,
  2855. &Callback::function_dtor<F>,
  2856. };
  2857. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  2858. "Type F must not exceed the size of the Callback class");
  2859. memset(this, 0, sizeof(Callback));
  2860. new (this) F(f);
  2861. _ops = &ops;
  2862. }
  2863. // Function attributes
  2864. template <typename F>
  2865. static R function_call(const void *p, A0 a0, A1 a1, A2 a2, A3 a3)
  2866. {
  2867. return (*(F *)p)(a0, a1, a2, a3);
  2868. }
  2869. template <typename F>
  2870. static void function_move(void *d, const void *p)
  2871. {
  2872. new (d) F(*(F *)p);
  2873. }
  2874. template <typename F>
  2875. static void function_dtor(void *p)
  2876. {
  2877. ((F *)p)->~F();
  2878. }
  2879. // Wrappers for functions with context
  2880. template <typename O, typename M>
  2881. struct method_context {
  2882. M method;
  2883. O *obj;
  2884. method_context(O *obj, M method)
  2885. : method(method), obj(obj) {}
  2886. R operator()(A0 a0, A1 a1, A2 a2, A3 a3) const
  2887. {
  2888. return (obj->*method)(a0, a1, a2, a3);
  2889. }
  2890. };
  2891. template <typename F, typename A>
  2892. struct function_context {
  2893. F func;
  2894. A *arg;
  2895. function_context(F func, A *arg)
  2896. : func(func), arg(arg) {}
  2897. R operator()(A0 a0, A1 a1, A2 a2, A3 a3) const
  2898. {
  2899. return func(arg, a0, a1, a2, a3);
  2900. }
  2901. };
  2902. };
  2903. /** Callback class based on template specialization
  2904. *
  2905. * @note Synchronization level: Not protected
  2906. */
  2907. template <typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  2908. class Callback<R(A0, A1, A2, A3, A4)> {
  2909. public:
  2910. /** Create a Callback with a static function
  2911. * @param func Static function to attach
  2912. */
  2913. Callback(R(*func)(A0, A1, A2, A3, A4) = 0)
  2914. {
  2915. if (!func) {
  2916. memset(this, 0, sizeof(Callback));
  2917. } else {
  2918. generate(func);
  2919. }
  2920. }
  2921. /** Attach a Callback
  2922. * @param func The Callback to attach
  2923. */
  2924. Callback(const Callback<R(A0, A1, A2, A3, A4)> &func)
  2925. {
  2926. if (func._ops) {
  2927. func._ops->move(this, &func);
  2928. }
  2929. _ops = func._ops;
  2930. }
  2931. /** Create a Callback with a member function
  2932. * @param obj Pointer to object to invoke member function on
  2933. * @param method Member function to attach
  2934. */
  2935. template<typename T, typename U>
  2936. Callback(U *obj, R(T::*method)(A0, A1, A2, A3, A4))
  2937. {
  2938. generate(method_context<T, R(T::*)(A0, A1, A2, A3, A4)>(obj, method));
  2939. }
  2940. /** Create a Callback with a member function
  2941. * @param obj Pointer to object to invoke member function on
  2942. * @param method Member function to attach
  2943. */
  2944. template<typename T, typename U>
  2945. Callback(const U *obj, R(T::*method)(A0, A1, A2, A3, A4) const)
  2946. {
  2947. generate(method_context<const T, R(T::*)(A0, A1, A2, A3, A4) const>(obj, method));
  2948. }
  2949. /** Create a Callback with a member function
  2950. * @param obj Pointer to object to invoke member function on
  2951. * @param method Member function to attach
  2952. */
  2953. template<typename T, typename U>
  2954. Callback(volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) volatile)
  2955. {
  2956. generate(method_context<volatile T, R(T::*)(A0, A1, A2, A3, A4) volatile>(obj, method));
  2957. }
  2958. /** Create a Callback with a member function
  2959. * @param obj Pointer to object to invoke member function on
  2960. * @param method Member function to attach
  2961. */
  2962. template<typename T, typename U>
  2963. Callback(const volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) const volatile)
  2964. {
  2965. generate(method_context<const volatile T, R(T::*)(A0, A1, A2, A3, A4) const volatile>(obj, method));
  2966. }
  2967. /** Create a Callback with a static function and bound pointer
  2968. * @param func Static function to attach
  2969. * @param arg Pointer argument to function
  2970. */
  2971. template<typename T, typename U>
  2972. Callback(R(*func)(T *, A0, A1, A2, A3, A4), U *arg)
  2973. {
  2974. generate(function_context<R(*)(T *, A0, A1, A2, A3, A4), T>(func, arg));
  2975. }
  2976. /** Create a Callback with a static function and bound pointer
  2977. * @param func Static function to attach
  2978. * @param arg Pointer argument to function
  2979. */
  2980. template<typename T, typename U>
  2981. Callback(R(*func)(const T *, A0, A1, A2, A3, A4), const U *arg)
  2982. {
  2983. generate(function_context<R(*)(const T *, A0, A1, A2, A3, A4), const T>(func, arg));
  2984. }
  2985. /** Create a Callback with a static function and bound pointer
  2986. * @param func Static function to attach
  2987. * @param arg Pointer argument to function
  2988. */
  2989. template<typename T, typename U>
  2990. Callback(R(*func)(volatile T *, A0, A1, A2, A3, A4), volatile U *arg)
  2991. {
  2992. generate(function_context<R(*)(volatile T *, A0, A1, A2, A3, A4), volatile T>(func, arg));
  2993. }
  2994. /** Create a Callback with a static function and bound pointer
  2995. * @param func Static function to attach
  2996. * @param arg Pointer argument to function
  2997. */
  2998. template<typename T, typename U>
  2999. Callback(R(*func)(const volatile T *, A0, A1, A2, A3, A4), const volatile U *arg)
  3000. {
  3001. generate(function_context<R(*)(const volatile T *, A0, A1, A2, A3, A4), const volatile T>(func, arg));
  3002. }
  3003. /** Create a Callback with a function object
  3004. * @param f Function object to attach
  3005. * @note The function object is limited to a single word of storage
  3006. */
  3007. template <typename F>
  3008. Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4)))
  3009. {
  3010. generate(f);
  3011. }
  3012. /** Create a Callback with a function object
  3013. * @param f Function object to attach
  3014. * @note The function object is limited to a single word of storage
  3015. */
  3016. template <typename F>
  3017. Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) const))
  3018. {
  3019. generate(f);
  3020. }
  3021. /** Create a Callback with a function object
  3022. * @param f Function object to attach
  3023. * @note The function object is limited to a single word of storage
  3024. */
  3025. template <typename F>
  3026. Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) volatile))
  3027. {
  3028. generate(f);
  3029. }
  3030. /** Create a Callback with a function object
  3031. * @param f Function object to attach
  3032. * @note The function object is limited to a single word of storage
  3033. */
  3034. template <typename F>
  3035. Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) const volatile))
  3036. {
  3037. generate(f);
  3038. }
  3039. /** Create a Callback with a static function and bound pointer
  3040. * @param obj Pointer to object to bind to function
  3041. * @param func Static function to attach
  3042. * @deprecated
  3043. * Arguments to callback have been reordered to Callback(func, arg)
  3044. */
  3045. template<typename T, typename U>
  3046. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3047. "Arguments to callback have been reordered to Callback(func, arg)")
  3048. Callback(U *obj, R(*func)(T *, A0, A1, A2, A3, A4))
  3049. {
  3050. new (this) Callback(func, obj);
  3051. }
  3052. /** Create a Callback with a static function and bound pointer
  3053. * @param obj Pointer to object to bind to function
  3054. * @param func Static function to attach
  3055. * @deprecated
  3056. * Arguments to callback have been reordered to Callback(func, arg)
  3057. */
  3058. template<typename T, typename U>
  3059. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3060. "Arguments to callback have been reordered to Callback(func, arg)")
  3061. Callback(const U *obj, R(*func)(const T *, A0, A1, A2, A3, A4))
  3062. {
  3063. new (this) Callback(func, obj);
  3064. }
  3065. /** Create a Callback with a static function and bound pointer
  3066. * @param obj Pointer to object to bind to function
  3067. * @param func Static function to attach
  3068. * @deprecated
  3069. * Arguments to callback have been reordered to Callback(func, arg)
  3070. */
  3071. template<typename T, typename U>
  3072. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3073. "Arguments to callback have been reordered to Callback(func, arg)")
  3074. Callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3, A4))
  3075. {
  3076. new (this) Callback(func, obj);
  3077. }
  3078. /** Create a Callback with a static function and bound pointer
  3079. * @param obj Pointer to object to bind to function
  3080. * @param func Static function to attach
  3081. * @deprecated
  3082. * Arguments to callback have been reordered to Callback(func, arg)
  3083. */
  3084. template<typename T, typename U>
  3085. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3086. "Arguments to callback have been reordered to Callback(func, arg)")
  3087. Callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3, A4))
  3088. {
  3089. new (this) Callback(func, obj);
  3090. }
  3091. /** Destroy a callback
  3092. */
  3093. ~Callback()
  3094. {
  3095. if (_ops) {
  3096. _ops->dtor(this);
  3097. }
  3098. }
  3099. /** Attach a static function
  3100. * @param func Static function to attach
  3101. * @deprecated
  3102. * Replaced by simple assignment 'Callback cb = func'
  3103. */
  3104. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3105. "Replaced by simple assignment 'Callback cb = func")
  3106. void attach(R(*func)(A0, A1, A2, A3, A4))
  3107. {
  3108. this->~Callback();
  3109. new (this) Callback(func);
  3110. }
  3111. /** Attach a Callback
  3112. * @param func The Callback to attach
  3113. * @deprecated
  3114. * Replaced by simple assignment 'Callback cb = func'
  3115. */
  3116. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3117. "Replaced by simple assignment 'Callback cb = func")
  3118. void attach(const Callback<R(A0, A1, A2, A3, A4)> &func)
  3119. {
  3120. this->~Callback();
  3121. new (this) Callback(func);
  3122. }
  3123. /** Attach a member function
  3124. * @param obj Pointer to object to invoke member function on
  3125. * @param method Member function to attach
  3126. * @deprecated
  3127. * Replaced by simple assignment 'Callback cb = func'
  3128. */
  3129. template<typename T, typename U>
  3130. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3131. "Replaced by simple assignment 'Callback cb = func")
  3132. void attach(U *obj, R(T::*method)(A0, A1, A2, A3, A4))
  3133. {
  3134. this->~Callback();
  3135. new (this) Callback(obj, method);
  3136. }
  3137. /** Attach a member function
  3138. * @param obj Pointer to object to invoke member function on
  3139. * @param method Member function to attach
  3140. * @deprecated
  3141. * Replaced by simple assignment 'Callback cb = func'
  3142. */
  3143. template<typename T, typename U>
  3144. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3145. "Replaced by simple assignment 'Callback cb = func")
  3146. void attach(const U *obj, R(T::*method)(A0, A1, A2, A3, A4) const)
  3147. {
  3148. this->~Callback();
  3149. new (this) Callback(obj, method);
  3150. }
  3151. /** Attach a member function
  3152. * @param obj Pointer to object to invoke member function on
  3153. * @param method Member function to attach
  3154. * @deprecated
  3155. * Replaced by simple assignment 'Callback cb = func'
  3156. */
  3157. template<typename T, typename U>
  3158. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3159. "Replaced by simple assignment 'Callback cb = func")
  3160. void attach(volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) volatile)
  3161. {
  3162. this->~Callback();
  3163. new (this) Callback(obj, method);
  3164. }
  3165. /** Attach a member function
  3166. * @param obj Pointer to object to invoke member function on
  3167. * @param method Member function to attach
  3168. * @deprecated
  3169. * Replaced by simple assignment 'Callback cb = func'
  3170. */
  3171. template<typename T, typename U>
  3172. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3173. "Replaced by simple assignment 'Callback cb = func")
  3174. void attach(const volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) const volatile)
  3175. {
  3176. this->~Callback();
  3177. new (this) Callback(obj, method);
  3178. }
  3179. /** Attach a static function with a bound pointer
  3180. * @param func Static function to attach
  3181. * @param arg Pointer argument to function
  3182. * @deprecated
  3183. * Replaced by simple assignment 'Callback cb = func'
  3184. */
  3185. template <typename T, typename U>
  3186. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3187. "Replaced by simple assignment 'Callback cb = func")
  3188. void attach(R(*func)(T *, A0, A1, A2, A3, A4), U *arg)
  3189. {
  3190. this->~Callback();
  3191. new (this) Callback(func, arg);
  3192. }
  3193. /** Attach a static function with a bound pointer
  3194. * @param func Static function to attach
  3195. * @param arg Pointer argument to function
  3196. * @deprecated
  3197. * Replaced by simple assignment 'Callback cb = func'
  3198. */
  3199. template <typename T, typename U>
  3200. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3201. "Replaced by simple assignment 'Callback cb = func")
  3202. void attach(R(*func)(const T *, A0, A1, A2, A3, A4), const U *arg)
  3203. {
  3204. this->~Callback();
  3205. new (this) Callback(func, arg);
  3206. }
  3207. /** Attach a static function with a bound pointer
  3208. * @param func Static function to attach
  3209. * @param arg Pointer argument to function
  3210. * @deprecated
  3211. * Replaced by simple assignment 'Callback cb = func'
  3212. */
  3213. template <typename T, typename U>
  3214. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3215. "Replaced by simple assignment 'Callback cb = func")
  3216. void attach(R(*func)(volatile T *, A0, A1, A2, A3, A4), volatile U *arg)
  3217. {
  3218. this->~Callback();
  3219. new (this) Callback(func, arg);
  3220. }
  3221. /** Attach a static function with a bound pointer
  3222. * @param func Static function to attach
  3223. * @param arg Pointer argument to function
  3224. * @deprecated
  3225. * Replaced by simple assignment 'Callback cb = func'
  3226. */
  3227. template <typename T, typename U>
  3228. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3229. "Replaced by simple assignment 'Callback cb = func")
  3230. void attach(R(*func)(const volatile T *, A0, A1, A2, A3, A4), const volatile U *arg)
  3231. {
  3232. this->~Callback();
  3233. new (this) Callback(func, arg);
  3234. }
  3235. /** Attach a function object
  3236. * @param f Function object to attach
  3237. * @note The function object is limited to a single word of storage
  3238. * @deprecated
  3239. * Replaced by simple assignment 'Callback cb = func'
  3240. */
  3241. template <typename F>
  3242. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3243. "Replaced by simple assignment 'Callback cb = func")
  3244. void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4)))
  3245. {
  3246. this->~Callback();
  3247. new (this) Callback(f);
  3248. }
  3249. /** Attach a function object
  3250. * @param f Function object to attach
  3251. * @note The function object is limited to a single word of storage
  3252. * @deprecated
  3253. * Replaced by simple assignment 'Callback cb = func'
  3254. */
  3255. template <typename F>
  3256. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3257. "Replaced by simple assignment 'Callback cb = func")
  3258. void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) const))
  3259. {
  3260. this->~Callback();
  3261. new (this) Callback(f);
  3262. }
  3263. /** Attach a function object
  3264. * @param f Function object to attach
  3265. * @note The function object is limited to a single word of storage
  3266. * @deprecated
  3267. * Replaced by simple assignment 'Callback cb = func'
  3268. */
  3269. template <typename F>
  3270. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3271. "Replaced by simple assignment 'Callback cb = func")
  3272. void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) volatile))
  3273. {
  3274. this->~Callback();
  3275. new (this) Callback(f);
  3276. }
  3277. /** Attach a function object
  3278. * @param f Function object to attach
  3279. * @note The function object is limited to a single word of storage
  3280. * @deprecated
  3281. * Replaced by simple assignment 'Callback cb = func'
  3282. */
  3283. template <typename F>
  3284. MBED_DEPRECATED_SINCE("mbed-os-5.4",
  3285. "Replaced by simple assignment 'Callback cb = func")
  3286. void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(A0, A1, A2, A3, A4) const volatile))
  3287. {
  3288. this->~Callback();
  3289. new (this) Callback(f);
  3290. }
  3291. /** Attach a static function with a bound pointer
  3292. * @param obj Pointer to object to bind to function
  3293. * @param func Static function to attach
  3294. * @deprecated
  3295. * Arguments to callback have been reordered to attach(func, arg)
  3296. */
  3297. template <typename T, typename U>
  3298. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3299. "Arguments to callback have been reordered to attach(func, arg)")
  3300. void attach(U *obj, R(*func)(T *, A0, A1, A2, A3, A4))
  3301. {
  3302. this->~Callback();
  3303. new (this) Callback(func, obj);
  3304. }
  3305. /** Attach a static function with a bound pointer
  3306. * @param obj Pointer to object to bind to function
  3307. * @param func Static function to attach
  3308. * @deprecated
  3309. * Arguments to callback have been reordered to attach(func, arg)
  3310. */
  3311. template <typename T, typename U>
  3312. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3313. "Arguments to callback have been reordered to attach(func, arg)")
  3314. void attach(const U *obj, R(*func)(const T *, A0, A1, A2, A3, A4))
  3315. {
  3316. this->~Callback();
  3317. new (this) Callback(func, obj);
  3318. }
  3319. /** Attach a static function with a bound pointer
  3320. * @param obj Pointer to object to bind to function
  3321. * @param func Static function to attach
  3322. * @deprecated
  3323. * Arguments to callback have been reordered to attach(func, arg)
  3324. */
  3325. template <typename T, typename U>
  3326. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3327. "Arguments to callback have been reordered to attach(func, arg)")
  3328. void attach(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3, A4))
  3329. {
  3330. this->~Callback();
  3331. new (this) Callback(func, obj);
  3332. }
  3333. /** Attach a static function with a bound pointer
  3334. * @param obj Pointer to object to bind to function
  3335. * @param func Static function to attach
  3336. * @deprecated
  3337. * Arguments to callback have been reordered to attach(func, arg)
  3338. */
  3339. template <typename T, typename U>
  3340. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3341. "Arguments to callback have been reordered to attach(func, arg)")
  3342. void attach(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3, A4))
  3343. {
  3344. this->~Callback();
  3345. new (this) Callback(func, obj);
  3346. }
  3347. /** Assign a callback
  3348. */
  3349. Callback &operator=(const Callback &that)
  3350. {
  3351. if (this != &that) {
  3352. this->~Callback();
  3353. new (this) Callback(that);
  3354. }
  3355. return *this;
  3356. }
  3357. /** Call the attached function
  3358. */
  3359. R call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const
  3360. {
  3361. MBED_ASSERT(_ops);
  3362. return _ops->call(this, a0, a1, a2, a3, a4);
  3363. }
  3364. /** Call the attached function
  3365. */
  3366. R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const
  3367. {
  3368. return call(a0, a1, a2, a3, a4);
  3369. }
  3370. /** Test if function has been attached
  3371. */
  3372. operator bool() const
  3373. {
  3374. return _ops;
  3375. }
  3376. /** Test for equality
  3377. */
  3378. friend bool operator==(const Callback &l, const Callback &r)
  3379. {
  3380. return memcmp(&l, &r, sizeof(Callback)) == 0;
  3381. }
  3382. /** Test for inequality
  3383. */
  3384. friend bool operator!=(const Callback &l, const Callback &r)
  3385. {
  3386. return !(l == r);
  3387. }
  3388. /** Static thunk for passing as C-style function
  3389. * @param func Callback to call passed as void pointer
  3390. * @param a0 An argument to be called with function func
  3391. * @param a1 An argument to be called with function func
  3392. * @param a2 An argument to be called with function func
  3393. * @param a3 An argument to be called with function func
  3394. * @param a4 An argument to be called with function func
  3395. * @return the value as determined by func which is of
  3396. * type and determined by the signiture of func
  3397. */
  3398. static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
  3399. {
  3400. return static_cast<Callback *>(func)->call(a0, a1, a2, a3, a4);
  3401. }
  3402. private:
  3403. // Stored as pointer to function and pointer to optional object
  3404. // Function pointer is stored as union of possible function types
  3405. // to guarantee proper size and alignment
  3406. struct _class;
  3407. union {
  3408. void (*_staticfunc)(A0, A1, A2, A3, A4);
  3409. void (*_boundfunc)(_class *, A0, A1, A2, A3, A4);
  3410. void (_class::*_methodfunc)(A0, A1, A2, A3, A4);
  3411. } _func;
  3412. void *_obj;
  3413. // Dynamically dispatched operations
  3414. const struct ops {
  3415. R(*call)(const void *, A0, A1, A2, A3, A4);
  3416. void (*move)(void *, const void *);
  3417. void (*dtor)(void *);
  3418. } *_ops;
  3419. // Generate operations for function object
  3420. template <typename F>
  3421. void generate(const F &f)
  3422. {
  3423. static const ops ops = {
  3424. &Callback::function_call<F>,
  3425. &Callback::function_move<F>,
  3426. &Callback::function_dtor<F>,
  3427. };
  3428. MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
  3429. "Type F must not exceed the size of the Callback class");
  3430. memset(this, 0, sizeof(Callback));
  3431. new (this) F(f);
  3432. _ops = &ops;
  3433. }
  3434. // Function attributes
  3435. template <typename F>
  3436. static R function_call(const void *p, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
  3437. {
  3438. return (*(F *)p)(a0, a1, a2, a3, a4);
  3439. }
  3440. template <typename F>
  3441. static void function_move(void *d, const void *p)
  3442. {
  3443. new (d) F(*(F *)p);
  3444. }
  3445. template <typename F>
  3446. static void function_dtor(void *p)
  3447. {
  3448. ((F *)p)->~F();
  3449. }
  3450. // Wrappers for functions with context
  3451. template <typename O, typename M>
  3452. struct method_context {
  3453. M method;
  3454. O *obj;
  3455. method_context(O *obj, M method)
  3456. : method(method), obj(obj) {}
  3457. R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const
  3458. {
  3459. return (obj->*method)(a0, a1, a2, a3, a4);
  3460. }
  3461. };
  3462. template <typename F, typename A>
  3463. struct function_context {
  3464. F func;
  3465. A *arg;
  3466. function_context(F func, A *arg)
  3467. : func(func), arg(arg) {}
  3468. R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const
  3469. {
  3470. return func(arg, a0, a1, a2, a3, a4);
  3471. }
  3472. };
  3473. };
  3474. // Internally used event type
  3475. typedef Callback<void(int)> event_callback_t;
  3476. /** Create a callback class with type infered from the arguments
  3477. *
  3478. * @param func Static function to attach
  3479. * @return Callback with infered type
  3480. */
  3481. template <typename R>
  3482. Callback<R()> callback(R(*func)() = 0)
  3483. {
  3484. return Callback<R()>(func);
  3485. }
  3486. /** Create a callback class with type infered from the arguments
  3487. *
  3488. * @param func Static function to attach
  3489. * @return Callback with infered type
  3490. */
  3491. template <typename R>
  3492. Callback<R()> callback(const Callback<R()> &func)
  3493. {
  3494. return Callback<R()>(func);
  3495. }
  3496. /** Create a callback class with type infered from the arguments
  3497. *
  3498. * @param obj Optional pointer to object to bind to function
  3499. * @param method Member function to attach
  3500. * @return Callback with infered type
  3501. */
  3502. template<typename T, typename U, typename R>
  3503. Callback<R()> callback(U *obj, R(T::*method)())
  3504. {
  3505. return Callback<R()>(obj, method);
  3506. }
  3507. /** Create a callback class with type infered from the arguments
  3508. *
  3509. * @param obj Optional pointer to object to bind to function
  3510. * @param method Member function to attach
  3511. * @return Callback with infered type
  3512. */
  3513. template<typename T, typename U, typename R>
  3514. Callback<R()> callback(const U *obj, R(T::*method)() const)
  3515. {
  3516. return Callback<R()>(obj, method);
  3517. }
  3518. /** Create a callback class with type infered from the arguments
  3519. *
  3520. * @param obj Optional pointer to object to bind to function
  3521. * @param method Member function to attach
  3522. * @return Callback with infered type
  3523. */
  3524. template<typename T, typename U, typename R>
  3525. Callback<R()> callback(volatile U *obj, R(T::*method)() volatile)
  3526. {
  3527. return Callback<R()>(obj, method);
  3528. }
  3529. /** Create a callback class with type infered from the arguments
  3530. *
  3531. * @param obj Optional pointer to object to bind to function
  3532. * @param method Member function to attach
  3533. * @return Callback with infered type
  3534. */
  3535. template<typename T, typename U, typename R>
  3536. Callback<R()> callback(const volatile U *obj, R(T::*method)() const volatile)
  3537. {
  3538. return Callback<R()>(obj, method);
  3539. }
  3540. /** Create a callback class with type infered from the arguments
  3541. *
  3542. * @param func Static function to attach
  3543. * @param arg Pointer argument to function
  3544. * @return Callback with infered type
  3545. */
  3546. template <typename T, typename U, typename R>
  3547. Callback<R()> callback(R(*func)(T *), U *arg)
  3548. {
  3549. return Callback<R()>(func, arg);
  3550. }
  3551. /** Create a callback class with type infered from the arguments
  3552. *
  3553. * @param func Static function to attach
  3554. * @param arg Pointer argument to function
  3555. * @return Callback with infered type
  3556. */
  3557. template <typename T, typename U, typename R>
  3558. Callback<R()> callback(R(*func)(const T *), const U *arg)
  3559. {
  3560. return Callback<R()>(func, arg);
  3561. }
  3562. /** Create a callback class with type infered from the arguments
  3563. *
  3564. * @param func Static function to attach
  3565. * @param arg Pointer argument to function
  3566. * @return Callback with infered type
  3567. */
  3568. template <typename T, typename U, typename R>
  3569. Callback<R()> callback(R(*func)(volatile T *), volatile U *arg)
  3570. {
  3571. return Callback<R()>(func, arg);
  3572. }
  3573. /** Create a callback class with type infered from the arguments
  3574. *
  3575. * @param func Static function to attach
  3576. * @param arg Pointer argument to function
  3577. * @return Callback with infered type
  3578. */
  3579. template <typename T, typename U, typename R>
  3580. Callback<R()> callback(R(*func)(const volatile T *), const volatile U *arg)
  3581. {
  3582. return Callback<R()>(func, arg);
  3583. }
  3584. /** Create a callback class with type infered from the arguments
  3585. *
  3586. * @param obj Optional pointer to object to bind to function
  3587. * @param func Static function to attach
  3588. * @return Callback with infered type
  3589. * @deprecated
  3590. * Arguments to callback have been reordered to callback(func, arg)
  3591. */
  3592. template <typename T, typename U, typename R>
  3593. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3594. "Arguments to callback have been reordered to callback(func, arg)")
  3595. Callback<R()> callback(U *obj, R(*func)(T *))
  3596. {
  3597. return Callback<R()>(func, obj);
  3598. }
  3599. /** Create a callback class with type infered from the arguments
  3600. *
  3601. * @param obj Optional pointer to object to bind to function
  3602. * @param func Static function to attach
  3603. * @return Callback with infered type
  3604. * @deprecated
  3605. * Arguments to callback have been reordered to callback(func, arg)
  3606. */
  3607. template <typename T, typename U, typename R>
  3608. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3609. "Arguments to callback have been reordered to callback(func, arg)")
  3610. Callback<R()> callback(const U *obj, R(*func)(const T *))
  3611. {
  3612. return Callback<R()>(func, obj);
  3613. }
  3614. /** Create a callback class with type infered from the arguments
  3615. *
  3616. * @param obj Optional pointer to object to bind to function
  3617. * @param func Static function to attach
  3618. * @return Callback with infered type
  3619. * @deprecated
  3620. * Arguments to callback have been reordered to callback(func, arg)
  3621. */
  3622. template <typename T, typename U, typename R>
  3623. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3624. "Arguments to callback have been reordered to callback(func, arg)")
  3625. Callback<R()> callback(volatile U *obj, R(*func)(volatile T *))
  3626. {
  3627. return Callback<R()>(func, obj);
  3628. }
  3629. /** Create a callback class with type infered from the arguments
  3630. *
  3631. * @param obj Optional pointer to object to bind to function
  3632. * @param func Static function to attach
  3633. * @return Callback with infered type
  3634. * @deprecated
  3635. * Arguments to callback have been reordered to callback(func, arg)
  3636. */
  3637. template <typename T, typename U, typename R>
  3638. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3639. "Arguments to callback have been reordered to callback(func, arg)")
  3640. Callback<R()> callback(const volatile U *obj, R(*func)(const volatile T *))
  3641. {
  3642. return Callback<R()>(func, obj);
  3643. }
  3644. /** Create a callback class with type infered from the arguments
  3645. *
  3646. * @param func Static function to attach
  3647. * @return Callback with infered type
  3648. */
  3649. template <typename R, typename A0>
  3650. Callback<R(A0)> callback(R(*func)(A0) = 0)
  3651. {
  3652. return Callback<R(A0)>(func);
  3653. }
  3654. /** Create a callback class with type infered from the arguments
  3655. *
  3656. * @param func Static function to attach
  3657. * @return Callback with infered type
  3658. */
  3659. template <typename R, typename A0>
  3660. Callback<R(A0)> callback(const Callback<R(A0)> &func)
  3661. {
  3662. return Callback<R(A0)>(func);
  3663. }
  3664. /** Create a callback class with type infered from the arguments
  3665. *
  3666. * @param obj Optional pointer to object to bind to function
  3667. * @param method Member function to attach
  3668. * @return Callback with infered type
  3669. */
  3670. template<typename T, typename U, typename R, typename A0>
  3671. Callback<R(A0)> callback(U *obj, R(T::*method)(A0))
  3672. {
  3673. return Callback<R(A0)>(obj, method);
  3674. }
  3675. /** Create a callback class with type infered from the arguments
  3676. *
  3677. * @param obj Optional pointer to object to bind to function
  3678. * @param method Member function to attach
  3679. * @return Callback with infered type
  3680. */
  3681. template<typename T, typename U, typename R, typename A0>
  3682. Callback<R(A0)> callback(const U *obj, R(T::*method)(A0) const)
  3683. {
  3684. return Callback<R(A0)>(obj, method);
  3685. }
  3686. /** Create a callback class with type infered from the arguments
  3687. *
  3688. * @param obj Optional pointer to object to bind to function
  3689. * @param method Member function to attach
  3690. * @return Callback with infered type
  3691. */
  3692. template<typename T, typename U, typename R, typename A0>
  3693. Callback<R(A0)> callback(volatile U *obj, R(T::*method)(A0) volatile)
  3694. {
  3695. return Callback<R(A0)>(obj, method);
  3696. }
  3697. /** Create a callback class with type infered from the arguments
  3698. *
  3699. * @param obj Optional pointer to object to bind to function
  3700. * @param method Member function to attach
  3701. * @return Callback with infered type
  3702. */
  3703. template<typename T, typename U, typename R, typename A0>
  3704. Callback<R(A0)> callback(const volatile U *obj, R(T::*method)(A0) const volatile)
  3705. {
  3706. return Callback<R(A0)>(obj, method);
  3707. }
  3708. /** Create a callback class with type infered from the arguments
  3709. *
  3710. * @param func Static function to attach
  3711. * @param arg Pointer argument to function
  3712. * @return Callback with infered type
  3713. */
  3714. template <typename T, typename U, typename R, typename A0>
  3715. Callback<R(A0)> callback(R(*func)(T *, A0), U *arg)
  3716. {
  3717. return Callback<R(A0)>(func, arg);
  3718. }
  3719. /** Create a callback class with type infered from the arguments
  3720. *
  3721. * @param func Static function to attach
  3722. * @param arg Pointer argument to function
  3723. * @return Callback with infered type
  3724. */
  3725. template <typename T, typename U, typename R, typename A0>
  3726. Callback<R(A0)> callback(R(*func)(const T *, A0), const U *arg)
  3727. {
  3728. return Callback<R(A0)>(func, arg);
  3729. }
  3730. /** Create a callback class with type infered from the arguments
  3731. *
  3732. * @param func Static function to attach
  3733. * @param arg Pointer argument to function
  3734. * @return Callback with infered type
  3735. */
  3736. template <typename T, typename U, typename R, typename A0>
  3737. Callback<R(A0)> callback(R(*func)(volatile T *, A0), volatile U *arg)
  3738. {
  3739. return Callback<R(A0)>(func, arg);
  3740. }
  3741. /** Create a callback class with type infered from the arguments
  3742. *
  3743. * @param func Static function to attach
  3744. * @param arg Pointer argument to function
  3745. * @return Callback with infered type
  3746. */
  3747. template <typename T, typename U, typename R, typename A0>
  3748. Callback<R(A0)> callback(R(*func)(const volatile T *, A0), const volatile U *arg)
  3749. {
  3750. return Callback<R(A0)>(func, arg);
  3751. }
  3752. /** Create a callback class with type infered from the arguments
  3753. *
  3754. * @param obj Optional pointer to object to bind to function
  3755. * @param func Static function to attach
  3756. * @return Callback with infered type
  3757. * @deprecated
  3758. * Arguments to callback have been reordered to callback(func, arg)
  3759. */
  3760. template <typename T, typename U, typename R, typename A0>
  3761. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3762. "Arguments to callback have been reordered to callback(func, arg)")
  3763. Callback<R(A0)> callback(U *obj, R(*func)(T *, A0))
  3764. {
  3765. return Callback<R(A0)>(func, obj);
  3766. }
  3767. /** Create a callback class with type infered from the arguments
  3768. *
  3769. * @param obj Optional pointer to object to bind to function
  3770. * @param func Static function to attach
  3771. * @return Callback with infered type
  3772. * @deprecated
  3773. * Arguments to callback have been reordered to callback(func, arg)
  3774. */
  3775. template <typename T, typename U, typename R, typename A0>
  3776. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3777. "Arguments to callback have been reordered to callback(func, arg)")
  3778. Callback<R(A0)> callback(const U *obj, R(*func)(const T *, A0))
  3779. {
  3780. return Callback<R(A0)>(func, obj);
  3781. }
  3782. /** Create a callback class with type infered from the arguments
  3783. *
  3784. * @param obj Optional pointer to object to bind to function
  3785. * @param func Static function to attach
  3786. * @return Callback with infered type
  3787. * @deprecated
  3788. * Arguments to callback have been reordered to callback(func, arg)
  3789. */
  3790. template <typename T, typename U, typename R, typename A0>
  3791. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3792. "Arguments to callback have been reordered to callback(func, arg)")
  3793. Callback<R(A0)> callback(volatile U *obj, R(*func)(volatile T *, A0))
  3794. {
  3795. return Callback<R(A0)>(func, obj);
  3796. }
  3797. /** Create a callback class with type infered from the arguments
  3798. *
  3799. * @param obj Optional pointer to object to bind to function
  3800. * @param func Static function to attach
  3801. * @return Callback with infered type
  3802. * @deprecated
  3803. * Arguments to callback have been reordered to callback(func, arg)
  3804. */
  3805. template <typename T, typename U, typename R, typename A0>
  3806. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3807. "Arguments to callback have been reordered to callback(func, arg)")
  3808. Callback<R(A0)> callback(const volatile U *obj, R(*func)(const volatile T *, A0))
  3809. {
  3810. return Callback<R(A0)>(func, obj);
  3811. }
  3812. /** Create a callback class with type infered from the arguments
  3813. *
  3814. * @param func Static function to attach
  3815. * @return Callback with infered type
  3816. */
  3817. template <typename R, typename A0, typename A1>
  3818. Callback<R(A0, A1)> callback(R(*func)(A0, A1) = 0)
  3819. {
  3820. return Callback<R(A0, A1)>(func);
  3821. }
  3822. /** Create a callback class with type infered from the arguments
  3823. *
  3824. * @param func Static function to attach
  3825. * @return Callback with infered type
  3826. */
  3827. template <typename R, typename A0, typename A1>
  3828. Callback<R(A0, A1)> callback(const Callback<R(A0, A1)> &func)
  3829. {
  3830. return Callback<R(A0, A1)>(func);
  3831. }
  3832. /** Create a callback class with type infered from the arguments
  3833. *
  3834. * @param obj Optional pointer to object to bind to function
  3835. * @param method Member function to attach
  3836. * @return Callback with infered type
  3837. */
  3838. template<typename T, typename U, typename R, typename A0, typename A1>
  3839. Callback<R(A0, A1)> callback(U *obj, R(T::*method)(A0, A1))
  3840. {
  3841. return Callback<R(A0, A1)>(obj, method);
  3842. }
  3843. /** Create a callback class with type infered from the arguments
  3844. *
  3845. * @param obj Optional pointer to object to bind to function
  3846. * @param method Member function to attach
  3847. * @return Callback with infered type
  3848. */
  3849. template<typename T, typename U, typename R, typename A0, typename A1>
  3850. Callback<R(A0, A1)> callback(const U *obj, R(T::*method)(A0, A1) const)
  3851. {
  3852. return Callback<R(A0, A1)>(obj, method);
  3853. }
  3854. /** Create a callback class with type infered from the arguments
  3855. *
  3856. * @param obj Optional pointer to object to bind to function
  3857. * @param method Member function to attach
  3858. * @return Callback with infered type
  3859. */
  3860. template<typename T, typename U, typename R, typename A0, typename A1>
  3861. Callback<R(A0, A1)> callback(volatile U *obj, R(T::*method)(A0, A1) volatile)
  3862. {
  3863. return Callback<R(A0, A1)>(obj, method);
  3864. }
  3865. /** Create a callback class with type infered from the arguments
  3866. *
  3867. * @param obj Optional pointer to object to bind to function
  3868. * @param method Member function to attach
  3869. * @return Callback with infered type
  3870. */
  3871. template<typename T, typename U, typename R, typename A0, typename A1>
  3872. Callback<R(A0, A1)> callback(const volatile U *obj, R(T::*method)(A0, A1) const volatile)
  3873. {
  3874. return Callback<R(A0, A1)>(obj, method);
  3875. }
  3876. /** Create a callback class with type infered from the arguments
  3877. *
  3878. * @param func Static function to attach
  3879. * @param arg Pointer argument to function
  3880. * @return Callback with infered type
  3881. */
  3882. template <typename T, typename U, typename R, typename A0, typename A1>
  3883. Callback<R(A0, A1)> callback(R(*func)(T *, A0, A1), U *arg)
  3884. {
  3885. return Callback<R(A0, A1)>(func, arg);
  3886. }
  3887. /** Create a callback class with type infered from the arguments
  3888. *
  3889. * @param func Static function to attach
  3890. * @param arg Pointer argument to function
  3891. * @return Callback with infered type
  3892. */
  3893. template <typename T, typename U, typename R, typename A0, typename A1>
  3894. Callback<R(A0, A1)> callback(R(*func)(const T *, A0, A1), const U *arg)
  3895. {
  3896. return Callback<R(A0, A1)>(func, arg);
  3897. }
  3898. /** Create a callback class with type infered from the arguments
  3899. *
  3900. * @param func Static function to attach
  3901. * @param arg Pointer argument to function
  3902. * @return Callback with infered type
  3903. */
  3904. template <typename T, typename U, typename R, typename A0, typename A1>
  3905. Callback<R(A0, A1)> callback(R(*func)(volatile T *, A0, A1), volatile U *arg)
  3906. {
  3907. return Callback<R(A0, A1)>(func, arg);
  3908. }
  3909. /** Create a callback class with type infered from the arguments
  3910. *
  3911. * @param func Static function to attach
  3912. * @param arg Pointer argument to function
  3913. * @return Callback with infered type
  3914. */
  3915. template <typename T, typename U, typename R, typename A0, typename A1>
  3916. Callback<R(A0, A1)> callback(R(*func)(const volatile T *, A0, A1), const volatile U *arg)
  3917. {
  3918. return Callback<R(A0, A1)>(func, arg);
  3919. }
  3920. /** Create a callback class with type infered from the arguments
  3921. *
  3922. * @param obj Optional pointer to object to bind to function
  3923. * @param func Static function to attach
  3924. * @return Callback with infered type
  3925. * @deprecated
  3926. * Arguments to callback have been reordered to callback(func, arg)
  3927. */
  3928. template <typename T, typename U, typename R, typename A0, typename A1>
  3929. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3930. "Arguments to callback have been reordered to callback(func, arg)")
  3931. Callback<R(A0, A1)> callback(U *obj, R(*func)(T *, A0, A1))
  3932. {
  3933. return Callback<R(A0, A1)>(func, obj);
  3934. }
  3935. /** Create a callback class with type infered from the arguments
  3936. *
  3937. * @param obj Optional pointer to object to bind to function
  3938. * @param func Static function to attach
  3939. * @return Callback with infered type
  3940. * @deprecated
  3941. * Arguments to callback have been reordered to callback(func, arg)
  3942. */
  3943. template <typename T, typename U, typename R, typename A0, typename A1>
  3944. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3945. "Arguments to callback have been reordered to callback(func, arg)")
  3946. Callback<R(A0, A1)> callback(const U *obj, R(*func)(const T *, A0, A1))
  3947. {
  3948. return Callback<R(A0, A1)>(func, obj);
  3949. }
  3950. /** Create a callback class with type infered from the arguments
  3951. *
  3952. * @param obj Optional pointer to object to bind to function
  3953. * @param func Static function to attach
  3954. * @return Callback with infered type
  3955. * @deprecated
  3956. * Arguments to callback have been reordered to callback(func, arg)
  3957. */
  3958. template <typename T, typename U, typename R, typename A0, typename A1>
  3959. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3960. "Arguments to callback have been reordered to callback(func, arg)")
  3961. Callback<R(A0, A1)> callback(volatile U *obj, R(*func)(volatile T *, A0, A1))
  3962. {
  3963. return Callback<R(A0, A1)>(func, obj);
  3964. }
  3965. /** Create a callback class with type infered from the arguments
  3966. *
  3967. * @param obj Optional pointer to object to bind to function
  3968. * @param func Static function to attach
  3969. * @return Callback with infered type
  3970. * @deprecated
  3971. * Arguments to callback have been reordered to callback(func, arg)
  3972. */
  3973. template <typename T, typename U, typename R, typename A0, typename A1>
  3974. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  3975. "Arguments to callback have been reordered to callback(func, arg)")
  3976. Callback<R(A0, A1)> callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1))
  3977. {
  3978. return Callback<R(A0, A1)>(func, obj);
  3979. }
  3980. /** Create a callback class with type infered from the arguments
  3981. *
  3982. * @param func Static function to attach
  3983. * @return Callback with infered type
  3984. */
  3985. template <typename R, typename A0, typename A1, typename A2>
  3986. Callback<R(A0, A1, A2)> callback(R(*func)(A0, A1, A2) = 0)
  3987. {
  3988. return Callback<R(A0, A1, A2)>(func);
  3989. }
  3990. /** Create a callback class with type infered from the arguments
  3991. *
  3992. * @param func Static function to attach
  3993. * @return Callback with infered type
  3994. */
  3995. template <typename R, typename A0, typename A1, typename A2>
  3996. Callback<R(A0, A1, A2)> callback(const Callback<R(A0, A1, A2)> &func)
  3997. {
  3998. return Callback<R(A0, A1, A2)>(func);
  3999. }
  4000. /** Create a callback class with type infered from the arguments
  4001. *
  4002. * @param obj Optional pointer to object to bind to function
  4003. * @param method Member function to attach
  4004. * @return Callback with infered type
  4005. */
  4006. template<typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4007. Callback<R(A0, A1, A2)> callback(U *obj, R(T::*method)(A0, A1, A2))
  4008. {
  4009. return Callback<R(A0, A1, A2)>(obj, method);
  4010. }
  4011. /** Create a callback class with type infered from the arguments
  4012. *
  4013. * @param obj Optional pointer to object to bind to function
  4014. * @param method Member function to attach
  4015. * @return Callback with infered type
  4016. */
  4017. template<typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4018. Callback<R(A0, A1, A2)> callback(const U *obj, R(T::*method)(A0, A1, A2) const)
  4019. {
  4020. return Callback<R(A0, A1, A2)>(obj, method);
  4021. }
  4022. /** Create a callback class with type infered from the arguments
  4023. *
  4024. * @param obj Optional pointer to object to bind to function
  4025. * @param method Member function to attach
  4026. * @return Callback with infered type
  4027. */
  4028. template<typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4029. Callback<R(A0, A1, A2)> callback(volatile U *obj, R(T::*method)(A0, A1, A2) volatile)
  4030. {
  4031. return Callback<R(A0, A1, A2)>(obj, method);
  4032. }
  4033. /** Create a callback class with type infered from the arguments
  4034. *
  4035. * @param obj Optional pointer to object to bind to function
  4036. * @param method Member function to attach
  4037. * @return Callback with infered type
  4038. */
  4039. template<typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4040. Callback<R(A0, A1, A2)> callback(const volatile U *obj, R(T::*method)(A0, A1, A2) const volatile)
  4041. {
  4042. return Callback<R(A0, A1, A2)>(obj, method);
  4043. }
  4044. /** Create a callback class with type infered from the arguments
  4045. *
  4046. * @param func Static function to attach
  4047. * @param arg Pointer argument to function
  4048. * @return Callback with infered type
  4049. */
  4050. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4051. Callback<R(A0, A1, A2)> callback(R(*func)(T *, A0, A1, A2), U *arg)
  4052. {
  4053. return Callback<R(A0, A1, A2)>(func, arg);
  4054. }
  4055. /** Create a callback class with type infered from the arguments
  4056. *
  4057. * @param func Static function to attach
  4058. * @param arg Pointer argument to function
  4059. * @return Callback with infered type
  4060. */
  4061. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4062. Callback<R(A0, A1, A2)> callback(R(*func)(const T *, A0, A1, A2), const U *arg)
  4063. {
  4064. return Callback<R(A0, A1, A2)>(func, arg);
  4065. }
  4066. /** Create a callback class with type infered from the arguments
  4067. *
  4068. * @param func Static function to attach
  4069. * @param arg Pointer argument to function
  4070. * @return Callback with infered type
  4071. */
  4072. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4073. Callback<R(A0, A1, A2)> callback(R(*func)(volatile T *, A0, A1, A2), volatile U *arg)
  4074. {
  4075. return Callback<R(A0, A1, A2)>(func, arg);
  4076. }
  4077. /** Create a callback class with type infered from the arguments
  4078. *
  4079. * @param func Static function to attach
  4080. * @param arg Pointer argument to function
  4081. * @return Callback with infered type
  4082. */
  4083. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4084. Callback<R(A0, A1, A2)> callback(R(*func)(const volatile T *, A0, A1, A2), const volatile U *arg)
  4085. {
  4086. return Callback<R(A0, A1, A2)>(func, arg);
  4087. }
  4088. /** Create a callback class with type infered from the arguments
  4089. *
  4090. * @param obj Optional pointer to object to bind to function
  4091. * @param func Static function to attach
  4092. * @return Callback with infered type
  4093. * @deprecated
  4094. * Arguments to callback have been reordered to callback(func, arg)
  4095. */
  4096. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4097. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4098. "Arguments to callback have been reordered to callback(func, arg)")
  4099. Callback<R(A0, A1, A2)> callback(U *obj, R(*func)(T *, A0, A1, A2))
  4100. {
  4101. return Callback<R(A0, A1, A2)>(func, obj);
  4102. }
  4103. /** Create a callback class with type infered from the arguments
  4104. *
  4105. * @param obj Optional pointer to object to bind to function
  4106. * @param func Static function to attach
  4107. * @return Callback with infered type
  4108. * @deprecated
  4109. * Arguments to callback have been reordered to callback(func, arg)
  4110. */
  4111. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4112. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4113. "Arguments to callback have been reordered to callback(func, arg)")
  4114. Callback<R(A0, A1, A2)> callback(const U *obj, R(*func)(const T *, A0, A1, A2))
  4115. {
  4116. return Callback<R(A0, A1, A2)>(func, obj);
  4117. }
  4118. /** Create a callback class with type infered from the arguments
  4119. *
  4120. * @param obj Optional pointer to object to bind to function
  4121. * @param func Static function to attach
  4122. * @return Callback with infered type
  4123. * @deprecated
  4124. * Arguments to callback have been reordered to callback(func, arg)
  4125. */
  4126. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4127. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4128. "Arguments to callback have been reordered to callback(func, arg)")
  4129. Callback<R(A0, A1, A2)> callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2))
  4130. {
  4131. return Callback<R(A0, A1, A2)>(func, obj);
  4132. }
  4133. /** Create a callback class with type infered from the arguments
  4134. *
  4135. * @param obj Optional pointer to object to bind to function
  4136. * @param func Static function to attach
  4137. * @return Callback with infered type
  4138. * @deprecated
  4139. * Arguments to callback have been reordered to callback(func, arg)
  4140. */
  4141. template <typename T, typename U, typename R, typename A0, typename A1, typename A2>
  4142. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4143. "Arguments to callback have been reordered to callback(func, arg)")
  4144. Callback<R(A0, A1, A2)> callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2))
  4145. {
  4146. return Callback<R(A0, A1, A2)>(func, obj);
  4147. }
  4148. /** Create a callback class with type infered from the arguments
  4149. *
  4150. * @param func Static function to attach
  4151. * @return Callback with infered type
  4152. */
  4153. template <typename R, typename A0, typename A1, typename A2, typename A3>
  4154. Callback<R(A0, A1, A2, A3)> callback(R(*func)(A0, A1, A2, A3) = 0)
  4155. {
  4156. return Callback<R(A0, A1, A2, A3)>(func);
  4157. }
  4158. /** Create a callback class with type infered from the arguments
  4159. *
  4160. * @param func Static function to attach
  4161. * @return Callback with infered type
  4162. */
  4163. template <typename R, typename A0, typename A1, typename A2, typename A3>
  4164. Callback<R(A0, A1, A2, A3)> callback(const Callback<R(A0, A1, A2, A3)> &func)
  4165. {
  4166. return Callback<R(A0, A1, A2, A3)>(func);
  4167. }
  4168. /** Create a callback class with type infered from the arguments
  4169. *
  4170. * @param obj Optional pointer to object to bind to function
  4171. * @param method Member function to attach
  4172. * @return Callback with infered type
  4173. */
  4174. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4175. Callback<R(A0, A1, A2, A3)> callback(U *obj, R(T::*method)(A0, A1, A2, A3))
  4176. {
  4177. return Callback<R(A0, A1, A2, A3)>(obj, method);
  4178. }
  4179. /** Create a callback class with type infered from the arguments
  4180. *
  4181. * @param obj Optional pointer to object to bind to function
  4182. * @param method Member function to attach
  4183. * @return Callback with infered type
  4184. */
  4185. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4186. Callback<R(A0, A1, A2, A3)> callback(const U *obj, R(T::*method)(A0, A1, A2, A3) const)
  4187. {
  4188. return Callback<R(A0, A1, A2, A3)>(obj, method);
  4189. }
  4190. /** Create a callback class with type infered from the arguments
  4191. *
  4192. * @param obj Optional pointer to object to bind to function
  4193. * @param method Member function to attach
  4194. * @return Callback with infered type
  4195. */
  4196. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4197. Callback<R(A0, A1, A2, A3)> callback(volatile U *obj, R(T::*method)(A0, A1, A2, A3) volatile)
  4198. {
  4199. return Callback<R(A0, A1, A2, A3)>(obj, method);
  4200. }
  4201. /** Create a callback class with type infered from the arguments
  4202. *
  4203. * @param obj Optional pointer to object to bind to function
  4204. * @param method Member function to attach
  4205. * @return Callback with infered type
  4206. */
  4207. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4208. Callback<R(A0, A1, A2, A3)> callback(const volatile U *obj, R(T::*method)(A0, A1, A2, A3) const volatile)
  4209. {
  4210. return Callback<R(A0, A1, A2, A3)>(obj, method);
  4211. }
  4212. /** Create a callback class with type infered from the arguments
  4213. *
  4214. * @param func Static function to attach
  4215. * @param arg Pointer argument to function
  4216. * @return Callback with infered type
  4217. */
  4218. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4219. Callback<R(A0, A1, A2, A3)> callback(R(*func)(T *, A0, A1, A2, A3), U *arg)
  4220. {
  4221. return Callback<R(A0, A1, A2, A3)>(func, arg);
  4222. }
  4223. /** Create a callback class with type infered from the arguments
  4224. *
  4225. * @param func Static function to attach
  4226. * @param arg Pointer argument to function
  4227. * @return Callback with infered type
  4228. */
  4229. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4230. Callback<R(A0, A1, A2, A3)> callback(R(*func)(const T *, A0, A1, A2, A3), const U *arg)
  4231. {
  4232. return Callback<R(A0, A1, A2, A3)>(func, arg);
  4233. }
  4234. /** Create a callback class with type infered from the arguments
  4235. *
  4236. * @param func Static function to attach
  4237. * @param arg Pointer argument to function
  4238. * @return Callback with infered type
  4239. */
  4240. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4241. Callback<R(A0, A1, A2, A3)> callback(R(*func)(volatile T *, A0, A1, A2, A3), volatile U *arg)
  4242. {
  4243. return Callback<R(A0, A1, A2, A3)>(func, arg);
  4244. }
  4245. /** Create a callback class with type infered from the arguments
  4246. *
  4247. * @param func Static function to attach
  4248. * @param arg Pointer argument to function
  4249. * @return Callback with infered type
  4250. */
  4251. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4252. Callback<R(A0, A1, A2, A3)> callback(R(*func)(const volatile T *, A0, A1, A2, A3), const volatile U *arg)
  4253. {
  4254. return Callback<R(A0, A1, A2, A3)>(func, arg);
  4255. }
  4256. /** Create a callback class with type infered from the arguments
  4257. *
  4258. * @param obj Optional pointer to object to bind to function
  4259. * @param func Static function to attach
  4260. * @return Callback with infered type
  4261. * @deprecated
  4262. * Arguments to callback have been reordered to callback(func, arg)
  4263. */
  4264. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4265. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4266. "Arguments to callback have been reordered to callback(func, arg)")
  4267. Callback<R(A0, A1, A2, A3)> callback(U *obj, R(*func)(T *, A0, A1, A2, A3))
  4268. {
  4269. return Callback<R(A0, A1, A2, A3)>(func, obj);
  4270. }
  4271. /** Create a callback class with type infered from the arguments
  4272. *
  4273. * @param obj Optional pointer to object to bind to function
  4274. * @param func Static function to attach
  4275. * @return Callback with infered type
  4276. * @deprecated
  4277. * Arguments to callback have been reordered to callback(func, arg)
  4278. */
  4279. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4280. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4281. "Arguments to callback have been reordered to callback(func, arg)")
  4282. Callback<R(A0, A1, A2, A3)> callback(const U *obj, R(*func)(const T *, A0, A1, A2, A3))
  4283. {
  4284. return Callback<R(A0, A1, A2, A3)>(func, obj);
  4285. }
  4286. /** Create a callback class with type infered from the arguments
  4287. *
  4288. * @param obj Optional pointer to object to bind to function
  4289. * @param func Static function to attach
  4290. * @return Callback with infered type
  4291. * @deprecated
  4292. * Arguments to callback have been reordered to callback(func, arg)
  4293. */
  4294. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4295. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4296. "Arguments to callback have been reordered to callback(func, arg)")
  4297. Callback<R(A0, A1, A2, A3)> callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3))
  4298. {
  4299. return Callback<R(A0, A1, A2, A3)>(func, obj);
  4300. }
  4301. /** Create a callback class with type infered from the arguments
  4302. *
  4303. * @param obj Optional pointer to object to bind to function
  4304. * @param func Static function to attach
  4305. * @return Callback with infered type
  4306. * @deprecated
  4307. * Arguments to callback have been reordered to callback(func, arg)
  4308. */
  4309. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3>
  4310. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4311. "Arguments to callback have been reordered to callback(func, arg)")
  4312. Callback<R(A0, A1, A2, A3)> callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3))
  4313. {
  4314. return Callback<R(A0, A1, A2, A3)>(func, obj);
  4315. }
  4316. /** Create a callback class with type infered from the arguments
  4317. *
  4318. * @param func Static function to attach
  4319. * @return Callback with infered type
  4320. */
  4321. template <typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4322. Callback<R(A0, A1, A2, A3, A4)> callback(R(*func)(A0, A1, A2, A3, A4) = 0)
  4323. {
  4324. return Callback<R(A0, A1, A2, A3, A4)>(func);
  4325. }
  4326. /** Create a callback class with type infered from the arguments
  4327. *
  4328. * @param func Static function to attach
  4329. * @return Callback with infered type
  4330. */
  4331. template <typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4332. Callback<R(A0, A1, A2, A3, A4)> callback(const Callback<R(A0, A1, A2, A3, A4)> &func)
  4333. {
  4334. return Callback<R(A0, A1, A2, A3, A4)>(func);
  4335. }
  4336. /** Create a callback class with type infered from the arguments
  4337. *
  4338. * @param obj Optional pointer to object to bind to function
  4339. * @param method Member function to attach
  4340. * @return Callback with infered type
  4341. */
  4342. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4343. Callback<R(A0, A1, A2, A3, A4)> callback(U *obj, R(T::*method)(A0, A1, A2, A3, A4))
  4344. {
  4345. return Callback<R(A0, A1, A2, A3, A4)>(obj, method);
  4346. }
  4347. /** Create a callback class with type infered from the arguments
  4348. *
  4349. * @param obj Optional pointer to object to bind to function
  4350. * @param method Member function to attach
  4351. * @return Callback with infered type
  4352. */
  4353. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4354. Callback<R(A0, A1, A2, A3, A4)> callback(const U *obj, R(T::*method)(A0, A1, A2, A3, A4) const)
  4355. {
  4356. return Callback<R(A0, A1, A2, A3, A4)>(obj, method);
  4357. }
  4358. /** Create a callback class with type infered from the arguments
  4359. *
  4360. * @param obj Optional pointer to object to bind to function
  4361. * @param method Member function to attach
  4362. * @return Callback with infered type
  4363. */
  4364. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4365. Callback<R(A0, A1, A2, A3, A4)> callback(volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) volatile)
  4366. {
  4367. return Callback<R(A0, A1, A2, A3, A4)>(obj, method);
  4368. }
  4369. /** Create a callback class with type infered from the arguments
  4370. *
  4371. * @param obj Optional pointer to object to bind to function
  4372. * @param method Member function to attach
  4373. * @return Callback with infered type
  4374. */
  4375. template<typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4376. Callback<R(A0, A1, A2, A3, A4)> callback(const volatile U *obj, R(T::*method)(A0, A1, A2, A3, A4) const volatile)
  4377. {
  4378. return Callback<R(A0, A1, A2, A3, A4)>(obj, method);
  4379. }
  4380. /** Create a callback class with type infered from the arguments
  4381. *
  4382. * @param func Static function to attach
  4383. * @param arg Pointer argument to function
  4384. * @return Callback with infered type
  4385. */
  4386. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4387. Callback<R(A0, A1, A2, A3, A4)> callback(R(*func)(T *, A0, A1, A2, A3, A4), U *arg)
  4388. {
  4389. return Callback<R(A0, A1, A2, A3, A4)>(func, arg);
  4390. }
  4391. /** Create a callback class with type infered from the arguments
  4392. *
  4393. * @param func Static function to attach
  4394. * @param arg Pointer argument to function
  4395. * @return Callback with infered type
  4396. */
  4397. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4398. Callback<R(A0, A1, A2, A3, A4)> callback(R(*func)(const T *, A0, A1, A2, A3, A4), const U *arg)
  4399. {
  4400. return Callback<R(A0, A1, A2, A3, A4)>(func, arg);
  4401. }
  4402. /** Create a callback class with type infered from the arguments
  4403. *
  4404. * @param func Static function to attach
  4405. * @param arg Pointer argument to function
  4406. * @return Callback with infered type
  4407. */
  4408. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4409. Callback<R(A0, A1, A2, A3, A4)> callback(R(*func)(volatile T *, A0, A1, A2, A3, A4), volatile U *arg)
  4410. {
  4411. return Callback<R(A0, A1, A2, A3, A4)>(func, arg);
  4412. }
  4413. /** Create a callback class with type infered from the arguments
  4414. *
  4415. * @param func Static function to attach
  4416. * @param arg Pointer argument to function
  4417. * @return Callback with infered type
  4418. */
  4419. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4420. Callback<R(A0, A1, A2, A3, A4)> callback(R(*func)(const volatile T *, A0, A1, A2, A3, A4), const volatile U *arg)
  4421. {
  4422. return Callback<R(A0, A1, A2, A3, A4)>(func, arg);
  4423. }
  4424. /** Create a callback class with type infered from the arguments
  4425. *
  4426. * @param obj Optional pointer to object to bind to function
  4427. * @param func Static function to attach
  4428. * @return Callback with infered type
  4429. * @deprecated
  4430. * Arguments to callback have been reordered to callback(func, arg)
  4431. */
  4432. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4433. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4434. "Arguments to callback have been reordered to callback(func, arg)")
  4435. Callback<R(A0, A1, A2, A3, A4)> callback(U *obj, R(*func)(T *, A0, A1, A2, A3, A4))
  4436. {
  4437. return Callback<R(A0, A1, A2, A3, A4)>(func, obj);
  4438. }
  4439. /** Create a callback class with type infered from the arguments
  4440. *
  4441. * @param obj Optional pointer to object to bind to function
  4442. * @param func Static function to attach
  4443. * @return Callback with infered type
  4444. * @deprecated
  4445. * Arguments to callback have been reordered to callback(func, arg)
  4446. */
  4447. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4448. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4449. "Arguments to callback have been reordered to callback(func, arg)")
  4450. Callback<R(A0, A1, A2, A3, A4)> callback(const U *obj, R(*func)(const T *, A0, A1, A2, A3, A4))
  4451. {
  4452. return Callback<R(A0, A1, A2, A3, A4)>(func, obj);
  4453. }
  4454. /** Create a callback class with type infered from the arguments
  4455. *
  4456. * @param obj Optional pointer to object to bind to function
  4457. * @param func Static function to attach
  4458. * @return Callback with infered type
  4459. * @deprecated
  4460. * Arguments to callback have been reordered to callback(func, arg)
  4461. */
  4462. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4463. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4464. "Arguments to callback have been reordered to callback(func, arg)")
  4465. Callback<R(A0, A1, A2, A3, A4)> callback(volatile U *obj, R(*func)(volatile T *, A0, A1, A2, A3, A4))
  4466. {
  4467. return Callback<R(A0, A1, A2, A3, A4)>(func, obj);
  4468. }
  4469. /** Create a callback class with type infered from the arguments
  4470. *
  4471. * @param obj Optional pointer to object to bind to function
  4472. * @param func Static function to attach
  4473. * @return Callback with infered type
  4474. * @deprecated
  4475. * Arguments to callback have been reordered to callback(func, arg)
  4476. */
  4477. template <typename T, typename U, typename R, typename A0, typename A1, typename A2, typename A3, typename A4>
  4478. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  4479. "Arguments to callback have been reordered to callback(func, arg)")
  4480. Callback<R(A0, A1, A2, A3, A4)> callback(const volatile U *obj, R(*func)(const volatile T *, A0, A1, A2, A3, A4))
  4481. {
  4482. return Callback<R(A0, A1, A2, A3, A4)>(func, obj);
  4483. }
  4484. /**@}*/
  4485. /**@}*/
  4486. } // namespace mbed
  4487. #endif