-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
Add variadic print(String...) function #58
Comments
Seems I got it working pretty good: // Addition by NicoHood
template <typename First, typename Second, typename Third, typename... Rest>
inline size_t print(const First& first, const Second& second, const Third& third, const Rest&... rest) {
// A Wrapper is needed here in order to not call the
// Formatted functions if the input was three integers
// If you input only two integers the formatted
// Functions have higher priority
return printWrapper(first, second, third, rest...);
}
template <typename First>
inline size_t printWrapper(const First& first) {
return print(first);
}
template <typename First, typename... Rest>
inline size_t printWrapper(const First& first, const Rest&... rest) {
size_t r = 0;
r+=print(first);
r+=printWrapper(rest...); // recursive call using pack expansion syntax
return r;
}
template <typename First, typename Second, typename Third, typename... Rest>
inline size_t println(const First& first, const Second& second, const Third& third, const Rest&... rest) {
// A Wrapper is needed here in order to not call the
// Formatted functions if the input was three integers
// If you input only two integers the formatted
// Functions have higher priority
return printlnWrapper(first, second, third, rest...);
}
template <typename First>
inline size_t printlnWrapper(const First& first) {
return println(first);
}
template <typename First, typename... Rest>
inline size_t printlnWrapper(const First& first, const Rest&... rest) {
size_t r = 0;
r+=print(first);
r+=printlnWrapper(rest...); // recursive call using pack expansion syntax
return r;
} Code to test: Serial.println("1", "2", 6, "3"," Word " , "4", '!', "5");
Serial.println();
Serial.println(44, 44, 44);
Serial.println();
Serial.println(44, HEX);
Serial.println();
// this produces the same flash size:
// Serial.println("1", "2", "3", "4", "5");
Serial.println("1");
Serial.println("2");
Serial.println("3");
Serial.println("4");
Serial.println("5"); Edit: One could also adapt this to other functions like: Edit2: Just a thought: Maybe we can also change the DEC, HEX, BIN into an enum and then be able to print 2 integers and a formatted integer at the same time. Maybe, not tested. |
At first glance, this seems like a great addition and I'm sure there are some further improvements we can look at. However this code will cause the current API to become C++11 only. Even if the code is wrapped in define guards, the reference will become separated unless a C++98 compatible version is provided using variadic functions. |
I do have an idea though! I give it a test when I'm home in a few hours. |
Ooh, yeahh!! +1!! |
For some reason the print (without ln) has no higher priority on 1-2 function parameters. Meaning it will result in a loop and don't print anything. That is not a problem though, we can force the function to take at least 3 arguments to it wont bother at all. So if you input more than 2 args (>=3) it will print them in a row. No matter what type it is (int, float, String, char). Formatted printing will not work I think. I updated the code above. Should I create a PR to test this code? Edit: @Chris--A Since we now have c++11, why not use it? I see no problem with that. Its enabled anyways. |
With the current pinMode layout it does not work with some overhead. At least I wasnt able to make it smaller. The problem is that the last argument is important for the other inputs. Here is a working, but more expensive example: #include "Arduino.h"
// Addition by NicoHood
template <typename First, typename Second>
inline uint8_t pinModeWrapper(const First& first, const Second& second) {
pinModeWrapper(first, second);
return second;
}
template <typename First, typename... Rest>
inline uint8_t pinModeWrapper(const First& first, const Rest&... rest) {
uint8_t mode = pinModeWrapper(rest...);
pinModeWrapper(first, mode);
return mode;
}
template <typename First, typename Second, typename Third, typename... Rest>
inline void pinMode(const First& first, const Second& second, const Third& third, const Rest&... rest) {
uint8_t mode = pinModeWrapper(second, third, rest...);
pinMode(first, mode);
} |
I agree it should be used, however as it hasn't been released yet (an IDE with C++11 enabled) making the API version incompatible is quite restrictive. This should be discussed first I think (with the users, not dev team). Many people chose to use the older IDE when breaking changes for IDE 1.0 were released, and subsequently many libraries written after 1.0 contained checks for WProgram.h to cater for the many people that used out dated third-party forks, or simply didn't want to upgrade. Adding in this breaking change will prevent people from testing libraries/code for the older IDE's (by only a few months) and give many people further reason to not upgrade. As this code is just a convenience and doesn't allow a result that wasn't already possible, careful consideration should be put into this addition. |
Hum? C++11 is in the current 1.6.6 release merged. This is a minor feature, nothing so much new. Its just a nice addition for the 1.6.x branch. Doesnt mean you cannot stick to the old version. And that there is a gap between 1.0.x and 1.6.x we already know and it will grow from update to update. That is progress in development. And this tiny addition shouldnt keep us from adding it. They changed the whole USB core and I think this wasnt backported to 1.0.x, if they ever release 1.0.7. So the whole USB code will be more or less incompatible without some fixes. So I dont get the problem of adding this. |
The current release tag is 1.6.5, 1.6.6 hasn't been released. As minor as it is, if I enable gnu++98, the API no longer is compatible. It doesn't mean you can't use it, it means you cannot even compile the code. At the very minimum, like the other C++11 code already implemented in the API, it should be disabled for C++98. It should also be noted that many people can't use the latest releases due to their OS or other restrictions not supporting it (Mac has a minimum version now if I recall). But either way, the code will eventually have to look at moving forward and forgetting the past, it just needs to be done carefully. |
I am working with the nightly build. And this has c++11 and 1.6.6 will also have c++11 |
Yes, however this is not the point I'm arguing. |
no idea if this helps, but:
Having this, you can concat a variable number of strings (tokens) to 1 long string and then do with the complete resulting string whatever you want. OT: |
@Chris--A, wrapping these methods in proper #ifdefs would solve the "it doesn't compile", I guess. But I'm not sure why this would be needed? Is there any good reason to disable C++11 mode for the Arduino core? There are probably other compiler flags that, if you change them, will break the compilation? Another point that has not been mentioned is conflicts with the current
Should this print "10016", or "64"? It seems the conflict is minor, but I'm not sure which version will be favored by the compiler, and it is fundamental - if you have an any-number-of-arguments version, you can't really use extra arguments for specifying the formats.
It helps if you use markdown for your code blocks. I added ``` around it to make it a proper code block now. |
The current template above only works for >=3 parameters and wont do integer formating. Otherwise we'd have to use enums for the HEX and DEC definitions and exclude the enums in the template (and I dont know how to do that or if its possible). Thatswhy the function is kept for >=3 args which should not conflict. |
I'm afraid that if Using enums for |
What we could do is:
|
Why not sidestep the Chuck |
Good Idea! Or maybe we also add something like printAnything? |
better then implement a variadic "real" printf() subsidiary instead, feat. then also That actually would finally make the Sketch print command more ANSI C stdio.h compatible ! |
I'm quite fond of this idea, and have added a similar function to my The code produced ends up smaller than the equivalent written using multiple An example usage:
|
yes, I also found your lib, it's excellent, especially your printf() implementation! |
@vogonjeltz Trying not to send this issue spiraling off topic, however just thought I'd let you know, as of version 1.1.5, the precision parameter is now available (lib manager should update within the hour). A test:
Result:
|
+1 |
What happened to this idea? Is requiring C++11 still a turn-off? I was thinking on a similar approach, exploiting inlining (I haven't tried it yet though). This is how my
Properties of this code:
In short, I think it's an elegant solution as it simplifies the code, it's backwards compatible with old code, and doesn't have a negative impact on performance nor resource usage. If C++11 is still a problem, I guess |
By the way, and I think this is the most important point: C++ has rules for resolving overloaded functions. So if you do http://en.cppreference.com/w/cpp/language/overload_resolution#Best_viable_function In any case, my previous code was made in a "lazy" and "timid" way, making as few changes as possible and keeping everything as backwards compatible as possible, hoping that that'll promote its adoption. It is true that using an enum or a special class for the base (so that you could use |
Just found this:
http://stackoverflow.com/questions/10044449/a-function-with-variable-number-of-arguments-with-known-types-the-c11-way
Add this to Print.h:
Now you can do stuff like this:
Serial.println("Hey", "this", "is", "a", "Test");
Every word is printed in a new line. You can also add this for Serial.print (without newline).
Maybe this can be optimized to only print the last input with a new line. An overload with two arguments is needed here I guess.
Not sure if its perfect c++, but this would give us the option to print infinite strings at once.
Maybe one can generalize this even more to also print numbers (without formating of course, just DEC).
It takes a lot of flash though. But maybe someone can optimize this further? (Now that we also have c++11 with the merged PR)
Any comments on that?
Edit: Maybe this is also relevant. This uses the same size as if there was no template.
It can also handle numbers (without format).
https://msdn.microsoft.com/en-us/library/dn439779.aspx
Another example (ln now only adds a new line at the end):
The text was updated successfully, but these errors were encountered: