Skip to content

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

Merged
merged 2 commits into from
Sep 28, 2016

Conversation

geky
Copy link
Contributor

@geky geky commented Sep 28, 2016

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.

Changes to mbed-os memory consumption:

.text .data .bss
before 57887 2292 7692
after 57842 2292 7691

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-

@mazimkhan
Copy link

retest uvisor

@sg- sg- added the needs: CI label Sep 28, 2016
@sg-
Copy link
Contributor

sg- commented Sep 28, 2016

/morph test-nightly

@sg-
Copy link
Contributor

sg- commented Sep 28, 2016

@mbed-bot: TEST

HOST_OSES=ALL
BUILD_TOOLCHAINS=ALL
TARGETS=ALL

@mbed-bot
Copy link

Result: FAILURE

Your command has finished executing! Here's what you wrote!

/morph test-nightly

Output

mbed 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.
@geky geky force-pushed the callback-function-objects branch from 6092cdf to 161a2ec Compare September 28, 2016 17:23
@geky
Copy link
Contributor Author

geky commented Sep 28, 2016

Looks like I accidentally let gcc-specific struct initialiers in.

Updated and tested locally with GCC and ARMCC.

/morph test

@mbed-bot
Copy link

Result: FAILURE

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: null

Examples Build failed!

@bridadan
Copy link
Contributor

/morph test

@bridadan
Copy link
Contributor

Just FYI, I was making a change in the CI config that caused the above test failure, please ignore it!

@mbed-bot
Copy link

Result: SUCCESS

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: 986

All builds and test passed!

@mbed-bot
Copy link

[Build 995]
SUCCESS: Building succeeded and tests were run! Be sure to check the test results

@sg- sg- merged commit 9e1b53f into ARMmbed:master Sep 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants