-
Notifications
You must be signed in to change notification settings - Fork 3k
callback - Add size-limited function-object overloads to Callback #2851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
retest uvisor |
/morph test-nightly |
@mbed-bot: TEST HOST_OSES=ALL |
Result: FAILUREYour command has finished executing! Here's what you wrote!
Outputmbed Build Number: 983 Build failed! |
This allows additional attributes to be attached to the internally generated type such as move and destructor operations with no increase in RAM footprint. The current overloads can't take advantage of this, but it does open the possibility for more powerful overloads that can provide these additional attributes. Changes to mbed-os memory consumption: .text .data .bss before 57887 2292 7692 after 57842 2292 7691
The callback class can now accept generalized function-objects: class Thing { public: int value; void operator()() { printf("hi! %d\n", value); } }; Callback<void()> cb(Thing(2)); However, with the intention of avoiding implicit dynamic-memory allocations, the Callback class is limited to a single word of storage. Exceeding this size will eliminate the function-object type from the overload set and fail to compile. Effort was invested to make this situation very clear to the user. Here is an example error message with noise removed: [ERROR] ./main.cpp: In function 'int main()': ./mbed-os/hal/api/Ticker.h:101:10: note: no known conversion for argument 1 from 'BigFunc' to 'mbed::Callback<void()>' The real benefit of this change is the ability for users to hook into the attributes of the Callback class. This mostly allows lifetime management of the function-objects from third-party libraries (such as the Event class from mbed-events). Note: The convenient `callback` function may become ambiguous if provided with a type that defines multiple incompatible `operator()` member functions.
6092cdf
to
161a2ec
Compare
Looks like I accidentally let gcc-specific struct initialiers in. Updated and tested locally with GCC and ARMCC. /morph test |
Result: FAILUREYour command has finished executing! Here's what you wrote!
Outputmbed Build Number: null Examples Build failed! |
/morph test |
Just FYI, I was making a change in the CI config that caused the above test failure, please ignore it! |
Result: SUCCESSYour command has finished executing! Here's what you wrote!
Outputmbed Build Number: 986 All builds and test passed! |
[Build 995] |
The callback class can now accept generalized function-objects:
However, with the intention of avoiding implicit dynamic-memory allocations, the Callback class is limited to a single word of storage. Exceeding this size will eliminate the function-object type from the
overload set and fail to compile.
Effort was invested to make this situation very clear to the user. Here is an example error message with noise removed:
The real benefit of this change is the ability for users to hook into the attributes of the Callback class. This mostly allows lifetime management of the function-objects from third-party libraries (such as
the Event class from mbed-events).
Note: The convenient
callback
function may become ambiguous if provided with a type that defines multiple incompatibleoperator()
member functions.Changes to mbed-os memory consumption:
Viewable diff of the zero-argument Callback:
https://www.diffchecker.com/bxWky06j
extra thanks to @pan- and @bogdanm for input on this patch
cc @pan-, @bogdanm, @sg-