Skip to content
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

Update ostream #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update ostream #69

wants to merge 1 commit into from

Conversation

pischky
Copy link

@pischky pischky commented Jan 4, 2022

allow use of F() macro for strings:
cout << F("Hello") << endl;

allow use of F() macro for Strings: 
cout << F("Hello") << endl;
@mike-matera
Copy link
Owner

Thank you so much! I just have a question about a couple of lines.

Copy link

@matthijskooijman matthijskooijman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an interesting change. I left some suggestions inline.

@@ -27,6 +27,11 @@
#include "cstdio"
#include "ostream_helpers"

#ifdef ARDUINO_ARCH_AVR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider leaving out these include guards, since pretty much every (non-AVR) Arduino core provides dummy version of the F() macro and pgm_read_byte and friends, so adding this code unconditionally makes user code that uses F() and ArduinoSTL more portable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but this code will not run on other than AVR. avr/pgmspace.h is required to read from flash.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but this code will not run on other than AVR. avr/pgmspace.h is required to read from flash.

Most cores actually provide a dummy avr/pgmspace.h, so portable sketches can still use F() (etc.) and compile on non-AVR too. E.g. see https://github.com/arduino/ArduinoCore-sam/tree/master/cores/arduino/avr and https://github.com/stm32duino/Arduino_Core_STM32/tree/main/cores/arduino/avr (better to just include Arduino.h instead, though, since that should also include pgmspace or its dummy).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. Code for SAM and STM should compile without complaning about missing F() macro

@@ -27,6 +27,11 @@
#include "cstdio"
#include "ostream_helpers"

#ifdef ARDUINO_ARCH_AVR
#include <avr/pgmspace.h> // typedef PGM_P, pgm_read_byte()
#include "WString.h" // class __FlashStringHelper

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to just #include <Arduino.h>, which should include both of these in a more portable way (at the expense of incuding more than maybe needed).

//stolen from Arduino Print.cpp
//sample use: cout << F("Hello") << endl;
template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
operator<<(basic_ostream<char,traits>& out, const __FlashStringHelper* c) //[PI: New] stolen from Arduino Print.cpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this specialization really needed? It looks like the implementation is identical to the generate charT version above, so it can probably be omitted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm....
I thing I do not understand what's your question. This is the trick for strings in flash memory.
AFAIK there must be different code for strings from RAM and strings from FLASH. For flash pgm_read_byte() hast to be used.
I have tested this on ATmega328P. May be we have to thing again for newer chips like ATmega4809. They are "Von Neumann" and do not need the pgm_read_byte().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that the implementation of this specialization:

ArduinoSTL/src/ostream

Lines 442 to 458 in f426344

#ifdef ARDUINO_ARCH_AVR
//support strings from flash memory [PI]
//stolen from Arduino Print.cpp
//sample use: cout << F("Hello") << endl;
template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
operator<<(basic_ostream<char,traits>& out, const __FlashStringHelper* c) //[PI: New] stolen from Arduino Print.cpp
{
typename basic_ostream<char,traits>::sentry s(out);
PGM_P p = reinterpret_cast<PGM_P>(c);
while (true) {
char x = pgm_read_byte(p++);
if (x == 0) break;
out.put(x);
}
return out;
}
#endif

Is identical to this specialization:

ArduinoSTL/src/ostream

Lines 415 to 431 in f426344

#ifdef ARDUINO_ARCH_AVR
//support strings from flash memory [PI]
//stolen from Arduino Print.cpp
//sample use: cout << F("Hello") << endl;
template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
operator<<(basic_ostream<charT,traits>& out, const __FlashStringHelper* c)
{
typename basic_ostream<charT,traits>::sentry s(out);
PGM_P p = reinterpret_cast<PGM_P>(c);
while (true) {
charT x = pgm_read_byte(p++);
if (x == 0) break;
out.put(x);
}
return out;
}
#endif

So I think the first one is redundant and should be caught by the second one (with charT = char)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be Mike just copied some of my code...
Should test again if Mikes code is working in my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants