C++ wrappers

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Re: C++ wrappers

Postby Korken » Tue Jun 14, 2016 9:53 pm

For people wondering about C++ and embedded development (and a lot of myth busting, such as memory hog, slow, etc):
http://embedded.fm/episodes/137

Take an hour to hear it, it is really worth your time.

The gist of it is, do not use the full power of C++ and know what "C-code" your C++ would be equal to.
Take advantage of C++'s stricter type checking, operator overloading, templates, etc.
Use some tricks to force the compiler to not use dynamic memory to catch allocation "bugs".

Giovanni wrote:I think that the core should stay written in C and provide a C++ wrapper, current approach. I don't see advantages in writing directly in C++ and it would impact portability.

I agree with you. There is no reason to port it as long as the wrappers plays nice with C++.

helmut
Posts: 18
Joined: Thu Apr 07, 2016 9:06 am
Has thanked: 1 time
Been thanked: 3 times

Re: C++ wrappers

Postby helmut » Tue Jun 21, 2016 12:07 pm

Hi,

to allow me to post the next version of the C++ wrappers patch, please answer the questions 4 and 9
from the previous posts.

Thx Helmut

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ wrappers

Postby Giovanni » Tue Jun 21, 2016 1:25 pm

Hi,

4) OSAL is not part of RT, the wrapper should not use OSAL.
9) Use coding conventions from the article I mentioned before.

Giovanni

helmut
Posts: 18
Joined: Thu Apr 07, 2016 9:06 am
Has thanked: 1 time
Been thanked: 3 times

Re: C++ wrappers

Postby helmut » Tue Jun 21, 2016 4:16 pm

Hi,

attached please find a rebased and beautified version of the c++-wrappers.
Hopefully all issues should be addressed, please review.
I've squashed more or less all individual commits into one large, as they did not
support reviewing anyway, so its more or less a rewrite of ch.cpp (deleted and moved to) ch.hpp.

Helmut
Attachments
improve-cpp-wrappers.patch.tar.gz
(18.65 KiB) Downloaded 223 times

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ wrappers

Postby Giovanni » Tue Jun 21, 2016 9:41 pm

Hi,

Thank you, I will review and probably merge tomorrow.

Giovanni

User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Re: C++ wrappers

Postby Korken » Sun Jul 24, 2016 10:34 pm

Hi!

Just a check, has this been included yet?
I was thinking of testing the new wrappers but trunk seems to have the old one?

// Emil

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ wrappers

Postby Giovanni » Sun Jul 24, 2016 10:59 pm

Not yet, I am a bit busy of late.

You can easily test them by just replacing the files. Some feedback would help having them included earlier.

Giovanni

User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Re: C++ wrappers

Postby Korken » Sun Jul 24, 2016 11:20 pm

Already on it! :)
I will come back tomorrow with comments.

User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Re: C++ wrappers

Postby Korken » Mon Jul 25, 2016 10:28 am

Okey, I had a look and here are my comments!
Have a look and lets discuss :) Overall it looks really good only a few minor things I found.

1. A lot of "static inline", should generally be avoided. Why is this used here and not only inline? (static inline is more of a C-thing)

2. Mempool does not use templates, but void pointers. It is extended by ObjectPool, which is the desired usage, does MemPool want to be viewed as it is using unformated memory?

3. Why was BaseStaticThread removed? It was a concise way to create a thread with minimal chance for errors by using the template argument to create working areas etc.

4. A lot of prototypes omit "const" qualifier (sizes, time periods, etc), should probably be made const to convey the message of constants and to help the compiler.

5. A lot of the standard macro definitions could be converted for C++ usage with "constexpr" to guarantee that the compiler calculates the final value at compile time.
Should this be included perhaps?

6. The code breaks the 80 column width limit here and there.

7. The demo should add -std=c++11 since the wrapper is designed for C++11.

8. In the syscalls_cpp.cpp, when the usage of the heap is disabled the usage of new and delete should produce errors, maybe add something like this (recommended to have it call an unimplemented function to cause a link-time error):

Code: Select all

/**
 * @brief   Define non-existing function to catch the usage of new.
 */
extern void you_tried_to_use_new(void);

/**
 * @brief   Define non-existing function to catch the usage of delete.
 */
extern void you_tried_to_use_delete(void);

/**
 * @brief   Redefinition of new to stop the usage of it.
 */
void* operator new(size_t size) {
  (void)size;
  you_tried_to_use_new();
  return NULL;
}

void* operator new (size_t size, void* ptr) {
  (void)size;
  (void)ptr;
  you_tried_to_use_new();
  return NULL;
}

void* operator new[](size_t size) {
  (void)size;
  you_tried_to_use_new();
  return NULL;
}

void* operator new[](size_t size, void* ptr) {
  (void)size;
  (void)ptr;
  you_tried_to_use_new();
  return NULL;
}

/**
 * @brief   Redefinition of delete to stop the usage of it.
 */
void operator delete(void* ptr) {
  (void)ptr;
  you_tried_to_use_delete();
}

void operator delete[](void* ptr) {
  (void)ptr;
  you_tried_to_use_delete();
}

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ wrappers

Postby Giovanni » Mon Jul 25, 2016 10:33 am

Excellent feedback.

I am not very into C++ so I will wait for you experts to reach a consensus :)

Just keep this going, it is an important part.

Giovanni


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 22 guests