Skip to content

Commit cc57611

Browse files
author
Amanda Butler
authored
Move content from platform.md to Callback.md
Add content from #482 to live site.
1 parent d077a51 commit cc57611

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/reference/api/platform/Callback.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,38 @@ This is the technical reference for callbacks. You should read the [Callbacks](/
1313

1414
### Thread example with callbacks
1515

16-
The Callback API provides a convenient way to pass arguments to spawned threads.
16+
The Callback API provides a convenient way to pass arguments to spawned threads. This example uses a C function pointer in the callback.
1717

1818
[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/rtos_threading_with_callback/)](https://os.mbed.com/teams/mbed_example/code/rtos_threading_with_callback/file/d4b2a035ffe3/main.cpp)
1919

2020
### Sonar example
2121

22-
Here is an example that uses everything discussed in the [introduction to callbacks](/docs/v5.8/reference/platform.html#callbacks) document in the form of a minimal Sonar class.
22+
Here is an example that uses everything discussed in the [introduction to callbacks](/docs/development/reference/platform.html#callbacks) document in the form of a minimal Sonar class. This example uses a C++ class and method in the callback.
2323

2424
[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/callback-sonar-example/)](https://os.mbed.com/teams/mbed_example/code/callback-sonar-example/file/1713cdc51510/main.cpp)
2525

26+
### Calling callbacks
27+
28+
Callbacks overload the function call operator, so you can call a Callback like you would a normal function:
29+
30+
```c++
31+
void run_timer_event(Callback<void(float)> on_timer) {
32+
on_timer(1.0f);
33+
}
34+
```
35+
36+
The only thing to watch out for is that the Callback type has a null Callback, just like a null function pointer. Uninitialized callbacks are null and assert if you call them. If you want a call to always succeed, you need to check if it is null first.
37+
38+
``` c++
39+
void run_timer_event(Callback<void(float)> on_timer) {
40+
if (on_timer) {
41+
on_timer(1.0f);
42+
}
43+
}
44+
```
45+
46+
The Callback class is what’s known in C++ as a “Concrete Type”. That is, the Callback class is lightweight enough to be passed around like an int, pointer or other primitive type.
47+
2648
### Related content
2749

2850
- [Platform overview](/docs/v5.8/reference/platform.html).

0 commit comments

Comments
 (0)