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.
C++ wrappers
- 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
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?
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 251 times
- Giovanni
- Site Admin
- Posts: 14457
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: C++ wrappers
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
It is supposed to be a "wrapper" around existing functionality not a rewrite.
Giovanni
- 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
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.
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 236 times
Re: C++ wrappers
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.
- 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
This is indeed possible. You can quite simply do it:
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.
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 231 times
- 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
I just have one more thing I'd like to discuss:
and
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.
Code: Select all
class System final { ... }
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.
Re: C++ wrappers
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.
- 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
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).
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 63 guests