Mistake on this page?
Report an issue in GitHub or email us
Callback.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_CALLBACK_H
18 #define MBED_CALLBACK_H
19 
20 #include <string.h>
21 #include <stdint.h>
22 #include <new>
23 #include "platform/mbed_assert.h"
24 #include "platform/mbed_toolchain.h"
25 
26 namespace mbed {
27 /** \addtogroup platform-public-api */
28 /** @{*/
29 /**
30  * \defgroup platform_Callback Callback class
31  * @{
32  */
33 
34 /** Callback class based on template specialization
35  *
36  * @note Synchronization level: Not protected
37  */
38 template <typename F>
39 class Callback;
40 
41 // Internal sfinae declarations
42 //
43 // These are used to eliminate overloads based on type attributes
44 // 1. Does a function object have a call operator
45 // 2. Does a function object fit in the available storage
46 //
47 // These eliminations are handled cleanly by the compiler and avoid
48 // massive and misleading error messages when confronted with an
49 // invalid type (or worse, runtime failures)
50 namespace detail {
51 struct nil {};
52 
53 template <bool B, typename R = nil>
54 struct enable_if {
55  typedef R type;
56 };
57 
58 template <typename R>
59 struct enable_if<false, R> {};
60 
61 template <typename M, M>
62 struct is_type {
63  static const bool value = true;
64 };
65 }
66 
67 #define MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, M) \
68  typename detail::enable_if< \
69  detail::is_type<M, &F::operator()>::value && \
70  sizeof(F) <= sizeof(uintptr_t) \
71  >::type = detail::nil()
72 
73 /** Callback class based on template specialization
74  *
75  * @note Synchronization level: Not protected
76  */
77 template <typename R, typename... ArgTs>
78 class Callback<R(ArgTs...)> {
79 public:
80  /** Create a Callback with a static function
81  * @param func Static function to attach
82  */
83  Callback(R(*func)(ArgTs...) = 0)
84  {
85  if (!func) {
86  memset(this, 0, sizeof(Callback));
87  } else {
88  generate(func);
89  }
90  }
91 
92  /** Attach a Callback
93  * @param func The Callback to attach
94  */
95  Callback(const Callback<R(ArgTs...)> &func)
96  {
97  memset(this, 0, sizeof(Callback));
98  if (func._ops) {
99  func._ops->move(this, &func);
100  }
101  _ops = func._ops;
102  }
103 
104  /** Create a Callback with a member function
105  * @param obj Pointer to object to invoke member function on
106  * @param method Member function to attach
107  */
108  template<typename T, typename U>
109  Callback(U *obj, R(T::*method)(ArgTs...))
110  {
111  generate(method_context<T, R(T::*)(ArgTs...)>(obj, method));
112  }
113 
114  /** Create a Callback with a member function
115  * @param obj Pointer to object to invoke member function on
116  * @param method Member function to attach
117  */
118  template<typename T, typename U>
119  Callback(const U *obj, R(T::*method)(ArgTs...) const)
120  {
121  generate(method_context<const T, R(T::*)(ArgTs...) const>(obj, method));
122  }
123 
124  /** Create a Callback with a member function
125  * @param obj Pointer to object to invoke member function on
126  * @param method Member function to attach
127  */
128  template<typename T, typename U>
129  Callback(volatile U *obj, R(T::*method)(ArgTs...) volatile)
130  {
131  generate(method_context<volatile T, R(T::*)(ArgTs...) volatile>(obj, method));
132  }
133 
134  /** Create a Callback with a member function
135  * @param obj Pointer to object to invoke member function on
136  * @param method Member function to attach
137  */
138  template<typename T, typename U>
139  Callback(const volatile U *obj, R(T::*method)(ArgTs...) const volatile)
140  {
141  generate(method_context<const volatile T, R(T::*)(ArgTs...) const volatile>(obj, method));
142  }
143 
144  /** Create a Callback with a static function and bound pointer
145  * @param func Static function to attach
146  * @param arg Pointer argument to function
147  */
148  template<typename T, typename U>
149  Callback(R(*func)(T *, ArgTs...), U *arg)
150  {
151  generate(function_context<R(*)(T *, ArgTs...), T>(func, arg));
152  }
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 T *, ArgTs...), const U *arg)
160  {
161  generate(function_context<R(*)(const T *, ArgTs...), const T>(func, arg));
162  }
163 
164  /** Create a Callback with a static function and bound pointer
165  * @param func Static function to attach
166  * @param arg Pointer argument to function
167  */
168  template<typename T, typename U>
169  Callback(R(*func)(volatile T *, ArgTs...), volatile U *arg)
170  {
171  generate(function_context<R(*)(volatile T *, ArgTs...), volatile T>(func, arg));
172  }
173 
174  /** Create a Callback with a static function and bound pointer
175  * @param func Static function to attach
176  * @param arg Pointer argument to function
177  */
178  template<typename T, typename U>
179  Callback(R(*func)(const volatile T *, ArgTs...), const volatile U *arg)
180  {
181  generate(function_context<R(*)(const volatile T *, ArgTs...), const volatile T>(func, arg));
182  }
183 
184  /** Create a Callback with a function object
185  * @param f Function object to attach
186  * @note The function object is limited to a single word of storage
187  */
188  template <typename F>
189  Callback(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...)))
190  {
191  generate(f);
192  }
193 
194  /** Create a Callback with a function object
195  * @param f Function object to attach
196  * @note The function object is limited to a single word of storage
197  */
198  template <typename F>
199  Callback(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) const))
200  {
201  generate(f);
202  }
203 
204  /** Create a Callback with a function object
205  * @param f Function object to attach
206  * @note The function object is limited to a single word of storage
207  */
208  template <typename F>
209  Callback(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) volatile))
210  {
211  generate(f);
212  }
213 
214  /** Create a Callback with a function object
215  * @param f Function object to attach
216  * @note The function object is limited to a single word of storage
217  */
218  template <typename F>
219  Callback(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) const volatile))
220  {
221  generate(f);
222  }
223 
224  /** Create a Callback with a static function and bound pointer
225  * @param obj Pointer to object to bind to function
226  * @param func Static function to attach
227  * @deprecated
228  * Arguments to callback have been reordered to Callback(func, arg)
229  */
230  template<typename T, typename U>
231  MBED_DEPRECATED_SINCE("mbed-os-5.1",
232  "Arguments to callback have been reordered to Callback(func, arg)")
233  Callback(U *obj, R(*func)(T *, ArgTs...))
234  {
235  new (this) Callback(func, obj);
236  }
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 U *obj, R(*func)(const T *, ArgTs...))
248  {
249  new (this) Callback(func, obj);
250  }
251 
252  /** Create a Callback with a static function and bound pointer
253  * @param obj Pointer to object to bind to function
254  * @param func Static function to attach
255  * @deprecated
256  * Arguments to callback have been reordered to Callback(func, arg)
257  */
258  template<typename T, typename U>
259  MBED_DEPRECATED_SINCE("mbed-os-5.1",
260  "Arguments to callback have been reordered to Callback(func, arg)")
261  Callback(volatile U *obj, R(*func)(volatile T *, ArgTs...))
262  {
263  new (this) Callback(func, obj);
264  }
265 
266  /** Create a Callback with a static function and bound pointer
267  * @param obj Pointer to object to bind to function
268  * @param func Static function to attach
269  * @deprecated
270  * Arguments to callback have been reordered to Callback(func, arg)
271  */
272  template<typename T, typename U>
273  MBED_DEPRECATED_SINCE("mbed-os-5.1",
274  "Arguments to callback have been reordered to Callback(func, arg)")
275  Callback(const volatile U *obj, R(*func)(const volatile T *, ArgTs...))
276  {
277  new (this) Callback(func, obj);
278  }
279 
280  /** Destroy a callback
281  */
283  {
284  if (_ops) {
285  _ops->dtor(this);
286  }
287  }
288 
289  /** Attach a static function
290  * @param func Static function to attach
291  * @deprecated
292  * Replaced by simple assignment 'Callback cb = func'
293  */
294  MBED_DEPRECATED_SINCE("mbed-os-5.4",
295  "Replaced by simple assignment 'Callback cb = func")
296  void attach(R(*func)(ArgTs...))
297  {
298  this->~Callback();
299  new (this) Callback(func);
300  }
301 
302  /** Attach a Callback
303  * @param func The Callback to attach
304  * @deprecated
305  * Replaced by simple assignment 'Callback cb = func'
306  */
307  MBED_DEPRECATED_SINCE("mbed-os-5.4",
308  "Replaced by simple assignment 'Callback cb = func")
309  void attach(const Callback<R(ArgTs...)> &func)
310  {
311  this->~Callback();
312  new (this) Callback(func);
313  }
314 
315  /** Attach a member function
316  * @param obj Pointer to object to invoke member function on
317  * @param method Member function to attach
318  * @deprecated
319  * Replaced by simple assignment 'Callback cb = func'
320  */
321  template<typename T, typename U>
322  MBED_DEPRECATED_SINCE("mbed-os-5.4",
323  "Replaced by simple assignment 'Callback cb = func")
324  void attach(U *obj, R(T::*method)(ArgTs...))
325  {
326  this->~Callback();
327  new (this) Callback(obj, method);
328  }
329 
330  /** Attach a member function
331  * @param obj Pointer to object to invoke member function on
332  * @param method Member function to attach
333  * @deprecated
334  * Replaced by simple assignment 'Callback cb = func'
335  */
336  template<typename T, typename U>
337  MBED_DEPRECATED_SINCE("mbed-os-5.4",
338  "Replaced by simple assignment 'Callback cb = func")
339  void attach(const U *obj, R(T::*method)(ArgTs...) const)
340  {
341  this->~Callback();
342  new (this) Callback(obj, method);
343  }
344 
345  /** Attach a member function
346  * @param obj Pointer to object to invoke member function on
347  * @param method Member function to attach
348  * @deprecated
349  * Replaced by simple assignment 'Callback cb = func'
350  */
351  template<typename T, typename U>
352  MBED_DEPRECATED_SINCE("mbed-os-5.4",
353  "Replaced by simple assignment 'Callback cb = func")
354  void attach(volatile U *obj, R(T::*method)(ArgTs...) volatile)
355  {
356  this->~Callback();
357  new (this) Callback(obj, method);
358  }
359 
360  /** Attach a member function
361  * @param obj Pointer to object to invoke member function on
362  * @param method Member function to attach
363  * @deprecated
364  * Replaced by simple assignment 'Callback cb = func'
365  */
366  template<typename T, typename U>
367  MBED_DEPRECATED_SINCE("mbed-os-5.4",
368  "Replaced by simple assignment 'Callback cb = func")
369  void attach(const volatile U *obj, R(T::*method)(ArgTs...) const volatile)
370  {
371  this->~Callback();
372  new (this) Callback(obj, method);
373  }
374 
375  /** Attach a static function with a bound pointer
376  * @param func Static function to attach
377  * @param arg Pointer argument to function
378  * @deprecated
379  * Replaced by simple assignment 'Callback cb = func'
380  */
381  template <typename T, typename U>
382  MBED_DEPRECATED_SINCE("mbed-os-5.4",
383  "Replaced by simple assignment 'Callback cb = func")
384  void attach(R(*func)(T *, ArgTs...), U *arg)
385  {
386  this->~Callback();
387  new (this) Callback(func, arg);
388  }
389 
390  /** Attach a static function with a bound pointer
391  * @param func Static function to attach
392  * @param arg Pointer argument to function
393  * @deprecated
394  * Replaced by simple assignment 'Callback cb = func'
395  */
396  template <typename T, typename U>
397  MBED_DEPRECATED_SINCE("mbed-os-5.4",
398  "Replaced by simple assignment 'Callback cb = func")
399  void attach(R(*func)(const T *, ArgTs...), const U *arg)
400  {
401  this->~Callback();
402  new (this) Callback(func, arg);
403  }
404 
405  /** Attach a static function with a bound pointer
406  * @param func Static function to attach
407  * @param arg Pointer argument to function
408  * @deprecated
409  * Replaced by simple assignment 'Callback cb = func'
410  */
411  template <typename T, typename U>
412  MBED_DEPRECATED_SINCE("mbed-os-5.4",
413  "Replaced by simple assignment 'Callback cb = func")
414  void attach(R(*func)(volatile T *, ArgTs...), volatile U *arg)
415  {
416  this->~Callback();
417  new (this) Callback(func, arg);
418  }
419 
420  /** Attach a static function with a bound pointer
421  * @param func Static function to attach
422  * @param arg Pointer argument to function
423  * @deprecated
424  * Replaced by simple assignment 'Callback cb = func'
425  */
426  template <typename T, typename U>
427  MBED_DEPRECATED_SINCE("mbed-os-5.4",
428  "Replaced by simple assignment 'Callback cb = func")
429  void attach(R(*func)(const volatile T *, ArgTs...), const volatile U *arg)
430  {
431  this->~Callback();
432  new (this) Callback(func, arg);
433  }
434 
435  /** Attach a function object
436  * @param f Function object to attach
437  * @note The function object is limited to a single word of storage
438  * @deprecated
439  * Replaced by simple assignment 'Callback cb = func'
440  */
441  template <typename F>
442  MBED_DEPRECATED_SINCE("mbed-os-5.4",
443  "Replaced by simple assignment 'Callback cb = func")
444  void attach(F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...)))
445  {
446  this->~Callback();
447  new (this) Callback(f);
448  }
449 
450  /** Attach a function object
451  * @param f Function object to attach
452  * @note The function object is limited to a single word of storage
453  * @deprecated
454  * Replaced by simple assignment 'Callback cb = func'
455  */
456  template <typename F>
457  MBED_DEPRECATED_SINCE("mbed-os-5.4",
458  "Replaced by simple assignment 'Callback cb = func")
459  void attach(const F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) const))
460  {
461  this->~Callback();
462  new (this) Callback(f);
463  }
464 
465  /** Attach a function object
466  * @param f Function object to attach
467  * @note The function object is limited to a single word of storage
468  * @deprecated
469  * Replaced by simple assignment 'Callback cb = func'
470  */
471  template <typename F>
472  MBED_DEPRECATED_SINCE("mbed-os-5.4",
473  "Replaced by simple assignment 'Callback cb = func")
474  void attach(volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) volatile))
475  {
476  this->~Callback();
477  new (this) Callback(f);
478  }
479 
480  /** Attach a function object
481  * @param f Function object to attach
482  * @note The function object is limited to a single word of storage
483  * @deprecated
484  * Replaced by simple assignment 'Callback cb = func'
485  */
486  template <typename F>
487  MBED_DEPRECATED_SINCE("mbed-os-5.4",
488  "Replaced by simple assignment 'Callback cb = func")
489  void attach(const volatile F f, MBED_ENABLE_IF_CALLBACK_COMPATIBLE(F, R(F::*)(ArgTs...) const volatile))
490  {
491  this->~Callback();
492  new (this) Callback(f);
493  }
494 
495  /** Attach a static function with a bound pointer
496  * @param obj Pointer to object to bind to function
497  * @param func Static function to attach
498  * @deprecated
499  * Arguments to callback have been reordered to attach(func, arg)
500  */
501  template <typename T, typename U>
502  MBED_DEPRECATED_SINCE("mbed-os-5.1",
503  "Arguments to callback have been reordered to attach(func, arg)")
504  void attach(U *obj, R(*func)(T *, ArgTs...))
505  {
506  this->~Callback();
507  new (this) Callback(func, obj);
508  }
509 
510  /** Attach a static function with a bound pointer
511  * @param obj Pointer to object to bind to function
512  * @param func Static function to attach
513  * @deprecated
514  * Arguments to callback have been reordered to attach(func, arg)
515  */
516  template <typename T, typename U>
517  MBED_DEPRECATED_SINCE("mbed-os-5.1",
518  "Arguments to callback have been reordered to attach(func, arg)")
519  void attach(const U *obj, R(*func)(const T *, ArgTs...))
520  {
521  this->~Callback();
522  new (this) Callback(func, obj);
523  }
524 
525  /** Attach a static function with a bound pointer
526  * @param obj Pointer to object to bind to function
527  * @param func Static function to attach
528  * @deprecated
529  * Arguments to callback have been reordered to attach(func, arg)
530  */
531  template <typename T, typename U>
532  MBED_DEPRECATED_SINCE("mbed-os-5.1",
533  "Arguments to callback have been reordered to attach(func, arg)")
534  void attach(volatile U *obj, R(*func)(volatile T *, ArgTs...))
535  {
536  this->~Callback();
537  new (this) Callback(func, obj);
538  }
539 
540  /** Attach a static function with a bound pointer
541  * @param obj Pointer to object to bind to function
542  * @param func Static function to attach
543  * @deprecated
544  * Arguments to callback have been reordered to attach(func, arg)
545  */
546  template <typename T, typename U>
547  MBED_DEPRECATED_SINCE("mbed-os-5.1",
548  "Arguments to callback have been reordered to attach(func, arg)")
549  void attach(const volatile U *obj, R(*func)(const volatile T *, ArgTs...))
550  {
551  this->~Callback();
552  new (this) Callback(func, obj);
553  }
554 
555  /** Assign a callback
556  */
558  {
559  if (this != &that) {
560  this->~Callback();
561  new (this) Callback(that);
562  }
563 
564  return *this;
565  }
566 
567  /** Call the attached function
568  */
569  R call(ArgTs... args) const
570  {
571  MBED_ASSERT(_ops);
572  return _ops->call(this, args...);
573  }
574 
575  /** Call the attached function
576  */
577  R operator()(ArgTs... args) const
578  {
579  return call(args...);
580  }
581 
582  /** Test if function has been attached
583  */
584  operator bool() const
585  {
586  return _ops;
587  }
588 
589  /** Test for equality
590  */
591  friend bool operator==(const Callback &l, const Callback &r)
592  {
593  return memcmp(&l, &r, sizeof(Callback)) == 0;
594  }
595 
596  /** Test for inequality
597  */
598  friend bool operator!=(const Callback &l, const Callback &r)
599  {
600  return !(l == r);
601  }
602 
603  /** Static thunk for passing as C-style function
604  * @param func Callback to call passed as void pointer
605  * @param args Arguments to be called with function func
606  * @return the value as determined by func which is of
607  * type and determined by the signature of func
608  */
609  static R thunk(void *func, ArgTs... args)
610  {
611  return static_cast<Callback *>(func)->call(args...);
612  }
613 
614 private:
615  // Stored as pointer to function and pointer to optional object
616  // Function pointer is stored as union of possible function types
617  // to guarantee proper size and alignment
618  struct _class;
619  union {
620  void (*_staticfunc)(ArgTs...);
621  void (*_boundfunc)(_class *, ArgTs...);
622  void (_class::*_methodfunc)(ArgTs...);
623  } _func;
624  void *_obj;
625 
626  // Dynamically dispatched operations
627  const struct ops {
628  R(*call)(const void *, ArgTs...);
629  void (*move)(void *, const void *);
630  void (*dtor)(void *);
631  } *_ops;
632 
633  // Generate operations for function object
634  template <typename F>
635  void generate(const F &f)
636  {
637  static const ops ops = {
638  &Callback::function_call<F>,
639  &Callback::function_move<F>,
640  &Callback::function_dtor<F>,
641  };
642 
643  MBED_STATIC_ASSERT(sizeof(Callback) - sizeof(_ops) >= sizeof(F),
644  "Type F must not exceed the size of the Callback class");
645  memset(this, 0, sizeof(Callback));
646  new (this) F(f);
647  _ops = &ops;
648  }
649 
650  // Function attributes
651  template <typename F>
652  static R function_call(const void *p, ArgTs... args)
653  {
654  return (*(F *)p)(args...);
655  }
656 
657  template <typename F>
658  static void function_move(void *d, const void *p)
659  {
660  new (d) F(*(F *)p);
661  }
662 
663  template <typename F>
664  static void function_dtor(void *p)
665  {
666  ((F *)p)->~F();
667  }
668 
669  // Wrappers for functions with context
670  template <typename O, typename M>
671  struct method_context {
672  M method;
673  O *obj;
674 
675  method_context(O *obj, M method)
676  : method(method), obj(obj) {}
677 
678  R operator()(ArgTs... args) const
679  {
680  return (obj->*method)(args...);
681  }
682  };
683 
684  template <typename F, typename A>
685  struct function_context {
686  F func;
687  A *arg;
688 
689  function_context(F func, A *arg)
690  : func(func), arg(arg) {}
691 
692  R operator()(ArgTs... args) const
693  {
694  return func(arg, args...);
695  }
696  };
697 };
698 
699 // Internally used event type
701 
702 
703 /** Create a callback class with type inferred from the arguments
704  *
705  * @param func Static function to attach
706  * @return Callback with inferred type
707  */
708 template <typename R, typename... ArgTs>
709 Callback<R(ArgTs...)> callback(R(*func)(ArgTs...) = 0)
710 {
711  return Callback<R(ArgTs...)>(func);
712 }
713 
714 /** Create a callback class with type inferred from the arguments
715  *
716  * @param func Static function to attach
717  * @return Callback with inferred type
718  */
719 template <typename R, typename... ArgTs>
720 Callback<R(ArgTs...)> callback(const Callback<R(ArgTs...)> &func)
721 {
722  return Callback<R(ArgTs...)>(func);
723 }
724 
725 /** Create a callback class with type inferred from the arguments
726  *
727  * @param obj Optional pointer to object to bind to function
728  * @param method Member function to attach
729  * @return Callback with inferred type
730  */
731 template<typename T, typename U, typename R, typename... ArgTs>
732 Callback<R(ArgTs...)> callback(U *obj, R(T::*method)(ArgTs...))
733 {
734  return Callback<R(ArgTs...)>(obj, method);
735 }
736 
737 /** Create a callback class with type inferred from the arguments
738  *
739  * @param obj Optional pointer to object to bind to function
740  * @param method Member function to attach
741  * @return Callback with inferred type
742  */
743 template<typename T, typename U, typename R, typename... ArgTs>
744 Callback<R(ArgTs...)> callback(const U *obj, R(T::*method)(ArgTs...) const)
745 {
746  return Callback<R(ArgTs...)>(obj, method);
747 }
748 
749 /** Create a callback class with type inferred from the arguments
750  *
751  * @param obj Optional pointer to object to bind to function
752  * @param method Member function to attach
753  * @return Callback with inferred type
754  */
755 template<typename T, typename U, typename R, typename... ArgTs>
756 Callback<R(ArgTs...)> callback(volatile U *obj, R(T::*method)(ArgTs...) volatile)
757 {
758  return Callback<R(ArgTs...)>(obj, method);
759 }
760 
761 /** Create a callback class with type inferred from the arguments
762  *
763  * @param obj Optional pointer to object to bind to function
764  * @param method Member function to attach
765  * @return Callback with inferred type
766  */
767 template<typename T, typename U, typename R, typename... ArgTs>
768 Callback<R(ArgTs...)> callback(const volatile U *obj, R(T::*method)(ArgTs...) const volatile)
769 {
770  return Callback<R(ArgTs...)>(obj, method);
771 }
772 
773 /** Create a callback class with type inferred from the arguments
774  *
775  * @param func Static function to attach
776  * @param arg Pointer argument to function
777  * @return Callback with inferred type
778  */
779 template <typename T, typename U, typename R, typename... ArgTs>
780 Callback<R(ArgTs...)> callback(R(*func)(T *, ArgTs...), U *arg)
781 {
782  return Callback<R(ArgTs...)>(func, arg);
783 }
784 
785 /** Create a callback class with type inferred from the arguments
786  *
787  * @param func Static function to attach
788  * @param arg Pointer argument to function
789  * @return Callback with inferred type
790  */
791 template <typename T, typename U, typename R, typename... ArgTs>
792 Callback<R(ArgTs...)> callback(R(*func)(const T *, ArgTs...), const U *arg)
793 {
794  return Callback<R(ArgTs...)>(func, arg);
795 }
796 
797 /** Create a callback class with type inferred from the arguments
798  *
799  * @param func Static function to attach
800  * @param arg Pointer argument to function
801  * @return Callback with inferred type
802  */
803 template <typename T, typename U, typename R, typename... ArgTs>
804 Callback<R(ArgTs...)> callback(R(*func)(volatile T *, ArgTs...), volatile U *arg)
805 {
806  return Callback<R(ArgTs...)>(func, arg);
807 }
808 
809 /** Create a callback class with type inferred from the arguments
810  *
811  * @param func Static function to attach
812  * @param arg Pointer argument to function
813  * @return Callback with inferred type
814  */
815 template <typename T, typename U, typename R, typename... ArgTs>
816 Callback<R(ArgTs...)> callback(R(*func)(const volatile T *, ArgTs...), const volatile U *arg)
817 {
818  return Callback<R(ArgTs...)>(func, arg);
819 }
820 
821 /** Create a callback class with type inferred from the arguments
822  *
823  * @param obj Optional pointer to object to bind to function
824  * @param func Static function to attach
825  * @return Callback with inferred type
826  * @deprecated
827  * Arguments to callback have been reordered to callback(func, arg)
828  */
829 template <typename T, typename U, typename R, typename... ArgTs>
830 MBED_DEPRECATED_SINCE("mbed-os-5.1",
831  "Arguments to callback have been reordered to callback(func, arg)")
832 Callback<R(ArgTs...)> callback(U *obj, R(*func)(T *, ArgTs...))
833 {
834  return Callback<R(ArgTs...)>(func, obj);
835 }
836 
837 /** Create a callback class with type inferred from the arguments
838  *
839  * @param obj Optional pointer to object to bind to function
840  * @param func Static function to attach
841  * @return Callback with inferred type
842  * @deprecated
843  * Arguments to callback have been reordered to callback(func, arg)
844  */
845 template <typename T, typename U, typename R, typename... ArgTs>
846 MBED_DEPRECATED_SINCE("mbed-os-5.1",
847  "Arguments to callback have been reordered to callback(func, arg)")
848 Callback<R(ArgTs...)> callback(const U *obj, R(*func)(const T *, ArgTs...))
849 {
850  return Callback<R(ArgTs...)>(func, obj);
851 }
852 
853 /** Create a callback class with type inferred from the arguments
854  *
855  * @param obj Optional pointer to object to bind to function
856  * @param func Static function to attach
857  * @return Callback with inferred type
858  * @deprecated
859  * Arguments to callback have been reordered to callback(func, arg)
860  */
861 template <typename T, typename U, typename R, typename... ArgTs>
862 MBED_DEPRECATED_SINCE("mbed-os-5.1",
863  "Arguments to callback have been reordered to callback(func, arg)")
864 Callback<R(ArgTs...)> callback(volatile U *obj, R(*func)(volatile T *, ArgTs...))
865 {
866  return Callback<R(ArgTs...)>(func, obj);
867 }
868 
869 /** Create a callback class with type inferred from the arguments
870  *
871  * @param obj Optional pointer to object to bind to function
872  * @param func Static function to attach
873  * @return Callback with inferred type
874  * @deprecated
875  * Arguments to callback have been reordered to callback(func, arg)
876  */
877 template <typename T, typename U, typename R, typename... ArgTs>
878 MBED_DEPRECATED_SINCE("mbed-os-5.1",
879  "Arguments to callback have been reordered to callback(func, arg)")
880 Callback<R(ArgTs...)> callback(const volatile U *obj, R(*func)(const volatile T *, ArgTs...))
881 {
882  return Callback<R(ArgTs...)>(func, obj);
883 }
884 
885 /**@}*/
886 
887 /**@}*/
888 
889 } // namespace mbed
890 
891 #endif
R call(ArgTs...args) const
Call the attached function.
Definition: Callback.h:569
Callback & operator=(const Callback &that)
Assign a callback.
Definition: Callback.h:557
Callback(const F f,)
Create a Callback with a function object.
Definition: Callback.h:199
static R thunk(void *func, ArgTs...args)
Static thunk for passing as C-style function.
Definition: Callback.h:609
Callback(const U *obj, R(T::*method)(ArgTs...) const)
Create a Callback with a member function.
Definition: Callback.h:119
Callback(R(*func)(volatile T *, ArgTs...), volatile U *arg)
Create a Callback with a static function and bound pointer.
Definition: Callback.h:169
Callback(R(*func)(const T *, ArgTs...), const U *arg)
Create a Callback with a static function and bound pointer.
Definition: Callback.h:159
Callback(R(*func)(ArgTs...)=0)
Create a Callback with a static function.
Definition: Callback.h:83
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:65
friend bool operator==(const Callback &l, const Callback &r)
Test for equality.
Definition: Callback.h:591
Callback(const Callback< R(ArgTs...)> &func)
Attach a Callback.
Definition: Callback.h:95
~Callback()
Destroy a callback.
Definition: Callback.h:282
Callback(volatile F f,)
Create a Callback with a function object.
Definition: Callback.h:209
Callback(R(*func)(const volatile T *, ArgTs...), const volatile U *arg)
Create a Callback with a static function and bound pointer.
Definition: Callback.h:179
R operator()(ArgTs...args) const
Call the attached function.
Definition: Callback.h:577
Callback(U *obj, R(T::*method)(ArgTs...))
Create a Callback with a member function.
Definition: Callback.h:109
Callback< R(ArgTs...)> callback(const volatile U *obj, R(*func)(const volatile T *, ArgTs...))
Create a callback class with type inferred from the arguments.
Definition: Callback.h:880
Callback(const volatile F f,)
Create a Callback with a function object.
Definition: Callback.h:219
Callback(const volatile U *obj, R(T::*method)(ArgTs...) const volatile)
Create a Callback with a member function.
Definition: Callback.h:139
Callback(R(*func)(T *, ArgTs...), U *arg)
Create a Callback with a static function and bound pointer.
Definition: Callback.h:149
Callback(volatile U *obj, R(T::*method)(ArgTs...) volatile)
Create a Callback with a member function.
Definition: Callback.h:129
Callback(F f,)
Create a Callback with a function object.
Definition: Callback.h:189
Callback class based on template specialization.
Definition: Callback.h:39
#define MBED_STATIC_ASSERT(expr, msg)
MBED_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is fal...
Definition: mbed_assert.h:110
friend bool operator!=(const Callback &l, const Callback &r)
Test for inequality.
Definition: Callback.h:598
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.