C++ wrappers

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome.
User avatar
Korken
Posts: 256
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 3:25 pm

Please disregard "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)".
I was stupid :) Static inline is needed here.

I have added const everywhere it made sense and the additional new and delete functions to catch non-wanted usage of new/delete and combined the patches.
Please have a look. I will look into adding standard macros as constexpr functions instead.
Attachments
cpp-wrapper.zip
(20.86 KiB) Downloaded 33 times

User avatar
Korken
Posts: 256
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 4:25 pm

I added an example implementation of time macros as constexpr.
In the small G++ example that only uses the time macros twice reduced in size by 16 bytes and constant are guaranteed to be calculated at compile-time if possible.

Though, to not get conflict with the original macros 2 choices are available:
1. As I did now, and slightly changed from the original name.
2. #undef the original macros in the ch.hpp, I personally like this better as C macros should not be used in the C++ case.
What do you guys think?
Attachments
cpp-wrapper-timemacros.zip
(21.43 KiB) Downloaded 14 times

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

Re: C++ wrappers

Postby Giovanni » Mon Jul 25, 2016 4:39 pm

Macros can change, so undef them only if it is acceptable for me to not update the C++ wrapper.

It is supposed to be a "wrapper" around existing functionality not a rewrite.

Giovanni

User avatar
Korken
Posts: 256
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 4:48 pm

Very true.
Then I'd propose to just skip that part. :)
It is nothing critical - the main functionality of the wrapper is, and it removes the need to support from your side.

EDIT: Forgot the patch.
Attachments
cpp-wrapper.zip
(20.84 KiB) Downloaded 12 times

steved
Posts: 456
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 3 times
Been thanked: 34 times

Re: C++ wrappers

Postby steved » Tue Jul 26, 2016 8:35 am

Korken wrote:Though, to not get conflict with the original macros 2 choices are available:
1. As I did now, and slightly changed from the original name.
2. #undef the original macros in the ch.hpp, I personally like this better as C macros should not be used in the C++ case.
What do you guys think?

Is conditional compilation within the time macros an option? Use the original macros with the C compiler, and the 'new' ones with the C++ compiler? It would mean there's a little bit of C++ within the C-language core, albeit effectively disabled.

User avatar
Korken
Posts: 256
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 Jul 26, 2016 8:52 am

This is indeed possible. You can quite simply do it:

Code: Select all

#ifdef __cplusplus
   /* constexpr version */
#else
   /* Macro version */
#endif

But, we do not want to break original C code that is dependent on the Macros.
This is why I did the #undef version, as it is in the ch.hpp (after ch.h has been used), so it would not affect the drivers.


On a different note, I have now been testing the wrapper a lot and it seems to work just fine!
I added a few more things to get rid of warnings when compiling in C++14 and merged the patches, see attached patch.
From my side it is ready for first inclusion, and then we can do minor updates as new ideas/features are identified. :)
Attachments
cpp-wrapper-patch-20160726.zip
(20.85 KiB) Downloaded 11 times

User avatar
Korken
Posts: 256
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 Jul 26, 2016 9:06 am

I just have one more thing I'd like to discuss:

Code: Select all

class System final { ... }
and

Code: Select all

class Core final { ... }

Generally, you should not use static methods (as I was touching in 1.), and rather I'd go here with namespaced functions.
As these functions does not need to know anything in between each other (it is only a translation to C code) I would change to the use of namespaces.

This thread talks about this with good points: http://stackoverflow.com/questions/1434 ... on-a-class

What do you guys think?
Personally, I do not use static members unless I absolutely have to.

User avatar
Korken
Posts: 256
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 » Wed Jul 27, 2016 6:34 pm

Any comments on this?
Surely I cannot be the only one using C++ here. :)

steved
Posts: 456
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 3 times
Been thanked: 34 times

Re: C++ wrappers

Postby steved » Fri Jul 29, 2016 1:25 pm

Korken wrote:Any comments on this?
Surely I cannot be the only one using C++ here. :)

I've had a quick look, but posting as a patch does make it a bit tricky to read.

I've barely used C++ with Chibi, but have used certain aspects of it extensively on another embedded platform (with rather more memory than any of the ST devices), also one where the underlying OS is written in C. There are areas where C++ undoubtedly scores over plain C, primarily where classes and inheritance are useful. I've not felt the need to investigate C++ streams and template files (although maybe I should). I have found that interfacing to the OS from C++ can get a bit messy, so anything that makes that easier in Chibi is welcome. I also use static interfaces a fair amount, primarily as an API to a number of like objects.

User avatar
Korken
Posts: 256
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 » Fri Jul 29, 2016 3:32 pm

To have an easy read, clone the ChibiOS and apply the patch.
Then, browse to the C++ wrapper hand ch.hpp (has most of the goodness).


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 1 guest