Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 5 months ago.
Inline member functions
I'm writing a library for a TFT display. I want everything to execute as fast as possible, so I am declaring the member functions for writing to the display with _forceinline as shown below, so that the compiler puts the body of the function in place of the function call.
__forceinline void SomeClass::WriteCommand(unsigned char cmd) { // Output 8-bit data on port _DB _DB.write(cmd); // Toggle chip select low _NCS.write(0); // Toggle clock low _DNC_SCL.write(0); // Toggle write low _NWR_SCL.write(0); // Toggle write high _NWR_SCL.write(1); // Toggle clock high _DNC_SCL.write(1); // Write complete };
This is very effective at making things happen faster. The problem is, if I want to have another display library inheriting from my class, the inline functions aren't available to it. Is there any way of having these inline functions available to inheriting classes so I can get the same speed for any inheriting display libraries?
Thanks for your help!
2 Answers
11 years, 5 months ago.
You should put the function's body inside the class declaration, then it will be available to all callers. There's no need for
in such case.__forceinline
class SomeClass { .... void WriteCommand(unsigned char cmd) { // Output 8-bit data on port _DB _DB.write(cmd); // Toggle chip select low _NCS.write(0); // Toggle clock low _DNC_SCL.write(0); // Toggle write low _NWR_SCL.write(0); // Toggle write high _NWR_SCL.write(1); // Toggle clock high _DNC_SCL.write(1); // Write complete }; ... };
11 years, 5 months ago.
Hi Igor,
Thanks for this. I've actually found that if I put the function's body inside the class declaration AND use _forceinline the code runs 25% faster. Maybe without the _forceinline the compiler decides not to inline some of the functions?
Anyway, problem solved. Thanks for your help.
Tim