Skip to content

Commit 0ba84b7

Browse files
committed
Remove redundant examples
1 parent 00b0a48 commit 0ba84b7

File tree

1 file changed

+3
-120
lines changed

1 file changed

+3
-120
lines changed

docs/reference/api/platform/Callback.md

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -13,131 +13,17 @@ 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/development/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-
### Related content
27-
28-
- [Platform overview](/docs/development/reference/platform.html).
29-
- [Introduction to callbacks](/docs/development/reference/platform.html#callbacks).
30-
31-
#### Create callbacks
32-
33-
First, you need to understand the syntax of the Callback type. The Callback type is a templated type parameterized by a C++ function declaration:
34-
35-
``` c++
36-
// Callback</*return type*/(/*parameters*/)> cb;
37-
Callback<int(float)> cb; // A callback that takes in a float and returns an int
38-
Callback<void(float)> cb; // A callback that takes in a float and returns nothing
39-
Callback<int()> cb; // A callback that takes in nothing and returns an int
40-
Callback<void(float, float)> cb; // A callback that takes in two floats and returns nothing
41-
```
42-
43-
You can create a Callback directly from a C function or function pointer with the same type:
44-
45-
``` c++
46-
void dosomething(int) {
47-
// do something
48-
}
49-
50-
Callback<void(int)> cb(dosomething);
51-
```
52-
53-
If an API provides a function that takes in a callback, you can pass in a C function or function pointer with the same type:
54-
55-
``` c++
56-
class ADC {
57-
// ADC can pass an analog value to the callback
58-
void attach(Callback<void(float)> cb);
59-
};
60-
61-
void dosomething(float f) {
62-
// do something
63-
}
64-
65-
ADC adc;
66-
adc.attach(dosomething);
67-
```
68-
69-
But what about state? The Callback type also supports passing a state pointer for a function. This state can be either a pointer to an object that is passed to a member function, or a pointer passed to a C-style function.
70-
71-
Because this form of creating Callbacks requires two arguments, you need to create the Callback explicitly using the Callback constructor. The Callback also comes with the lowercase callback function, which creates callbacks based on the arguments type and avoids the need to repeat the template type.
72-
73-
You can create a callback with a member function.
74-
75-
``` c++
76-
class Thing {
77-
int state;
78-
void catinthehat(int i) {
79-
state = // do something
80-
}
81-
}
82-
83-
// We can create a Callback with the Callback constructor
84-
Thing thing1;
85-
adc.attach(Callback<void(int)>(&thing1, &Thing::catinthehat));
86-
87-
// Or we can create a Callback with the callback function to avoid repeating ourselves
88-
Thing thing2;
89-
adc.attach(callback(&thing2, &Thing::catinthehat));
90-
```
91-
92-
Or you can pass the state to a C-style function.
93-
94-
``` c++
95-
struct thing_t {
96-
int state;
97-
}
98-
99-
void catinthehat(thing_t *thing, float f) {
100-
thing->state = // do something
101-
}
102-
103-
// We can create a Callback with the Callback constructor
104-
thing_t thing1;
105-
adc.attach(Callback<void(int)>(catinthehat, &thing1));
106-
107-
// Or we can create a Callback with the callback function to avoid repeating ourselves
108-
thing_t thing2;
109-
adc.attach(callback(catinthehat, &thing2));
110-
```
111-
112-
<span class="notes">**Note:** This state is restricted to a single pointer. This means you can’t bind both an object and argument to a callback.</span>
113-
114-
``` c++
115-
// Does not work
116-
adc.attach(callback(&thing, &Thing::dosomething, &arg));
117-
```
118-
119-
If you need to pass multiple arguments to a callback and you can’t store the arguments in the class, you can create a struct that contains all of the arguments and pass a pointer to that. However, you need to handle the memory allocation yourself.
120-
121-
``` c++
122-
// Create a struct that contains all of the state needed for “dosomething”
123-
struct dosomething_arguments {
124-
Thing *thing;
125-
int arg1;
126-
int arg2;
127-
};
128-
129-
// Create a function that calls “dosomething” with the arguments
130-
void dosomething_with_arguments(struct dosomething_arguments *args) {
131-
args->thing->dosomething(args->arg1, args->arg2);
132-
}
133-
134-
135-
// Allocate arguments and pass to callback
136-
struct dosomething_arguments args = { &thing, arg1, arg2 };
137-
adc.attach(callback(dosomething_with_arguments, &args)); // yes
138-
```
139-
140-
#### Call callbacks
26+
### Calling callbacks
14127

14228
Callbacks overload the function call operator, so you can call a Callback like you would a normal function:
14329

@@ -177,6 +63,3 @@ public:
17763
}
17864
```
17965

180-
<h4 id="the-importance-of-state">The importance of state</h4>
181-
182-
A callback is a user provided function that a user may pass to an API. The callback allows the API to execute the user’s code in its own context. You can find more information on how to use callbacks in the [technical callback documentation](/docs/development/reference/callback.html).

0 commit comments

Comments
 (0)