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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/ostream
Original file line number Diff line number Diff line change
Expand Up @@ -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

#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).

#endif

#pragma GCC visibility push(default)

namespace std {
Expand Down Expand Up @@ -407,6 +412,24 @@ namespace std {
return out;
}

#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

// partial specializations
template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
operator<<(basic_ostream<char,traits>& out, const char* c)
Expand All @@ -416,6 +439,24 @@ namespace std {
return out;
}

#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

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.

{
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

#ifdef __UCLIBCXX_HAS_WCHAR__
template<class traits> _UCXXEXPORT basic_ostream<wchar_t,traits>&
operator<<(basic_ostream<wchar_t,traits>& out, const char* c)
Expand Down