You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
-[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
-
voiddosomething(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
-
classThing {
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
// 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>
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
-
structdosomething_arguments {
124
-
Thing *thing;
125
-
int arg1;
126
-
int arg2;
127
-
};
128
-
129
-
// Create a function that calls “dosomething” with the arguments
Callbacks overload the function call operator, so you can call a Callback like you would a normal function:
143
29
@@ -177,6 +63,3 @@ public:
177
63
}
178
64
```
179
65
180
-
<h4id="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