Skip to content

Commit 82adc95

Browse files
Add explicit Print::write(char) (#6101)
W/o this change, file::write('a'); tries to use the template and fails since char is a basic type. The reason it is needed is due to pre 2.5.x behavior, File::write(char) silently was cast to File::write(uint8_t). With the template write, though, this is not performed. * Add Print::write tests and add'l overrides Ensure that print::write does something sane and doesn't cause a compile time error about templates when used for Files. Test using SPIFFS file since Print is an abstract type.
1 parent 48fc8af commit 82adc95

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

Diff for: cores/esp8266/Print.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ class Print {
6363
return write((const uint8_t *) buffer, size);
6464
}
6565
// These handle ambiguity for write(0) case, because (0) can be a pointer or an integer
66-
size_t write(short t) { return write((uint8_t)t); }
67-
size_t write(unsigned short t) { return write((uint8_t)t); }
68-
size_t write(int t) { return write((uint8_t)t); }
69-
size_t write(unsigned int t) { return write((uint8_t)t); }
70-
size_t write(long t) { return write((uint8_t)t); }
71-
size_t write(unsigned long t) { return write((uint8_t)t); }
66+
inline size_t write(short t) { return write((uint8_t)t); }
67+
inline size_t write(unsigned short t) { return write((uint8_t)t); }
68+
inline size_t write(int t) { return write((uint8_t)t); }
69+
inline size_t write(unsigned int t) { return write((uint8_t)t); }
70+
inline size_t write(long t) { return write((uint8_t)t); }
71+
inline size_t write(unsigned long t) { return write((uint8_t)t); }
72+
// Enable write(char) to fall through to write(uint8_t)
73+
inline size_t write(char c) { return write((uint8_t) c); }
74+
inline size_t write(int8_t c) { return write((uint8_t) c); }
7275

7376
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
7477
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));

Diff for: libraries/Wire/Wire.h

-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ class TwoWire : public Stream
8080
void onReceive( void (*)(size_t) ); // legacy esp8266 backward compatibility
8181
void onRequest( void (*)(void) );
8282

83-
inline size_t write(unsigned long n) { return write((uint8_t)n); }
84-
inline size_t write(long n) { return write((uint8_t)n); }
85-
inline size_t write(unsigned int n) { return write((uint8_t)n); }
86-
inline size_t write(int n) { return write((uint8_t)n); }
8783
using Print::write;
8884
};
8985

Diff for: tests/host/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ TEST_CPP_FILES := \
127127
core/test_pgmspace.cpp \
128128
core/test_md5builder.cpp \
129129
core/test_string.cpp \
130-
core/test_PolledTimeout.cpp
130+
core/test_PolledTimeout.cpp \
131+
core/test_Print.cpp
131132

132133
PREINCLUDES := \
133134
-include common/mock.h \

Diff for: tests/host/core/test_Print.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
test_pgmspace.cpp - pgmspace tests
3+
Copyright © 2016 Ivan Grokhotkov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
*/
15+
16+
#include <catch.hpp>
17+
#include <string.h>
18+
#include <FS.h>
19+
#include "../common/spiffs_mock.h"
20+
#include <spiffs/spiffs.h>
21+
22+
// Use a SPIFFS file because we can't instantiate a virtual class like Print
23+
TEST_CASE("Print::write overrides all compile properly", "[core][Print]")
24+
{
25+
SPIFFS_MOCK_DECLARE(64, 8, 512, "");
26+
REQUIRE(SPIFFS.begin());
27+
auto p = SPIFFS.open("test.bin", "w");
28+
REQUIRE(p);
29+
uint8_t uint8 = 1;
30+
uint16_t uint16 = 2;
31+
uint32_t uint32 = 3;
32+
size_t size = 4;
33+
int8_t int8 = 1;
34+
int16_t int16 = 2;
35+
int32_t int32 = 3;
36+
char c = 'h';
37+
int i = 10;
38+
long l = 11;
39+
unsigned char uc = 20;
40+
unsigned int ui = 21;
41+
unsigned long ul = 22;
42+
p.write(uint8);
43+
p.write(uint16);
44+
p.write(uint32);
45+
p.write(size);
46+
p.write(int8);
47+
p.write(int16);
48+
p.write(int32);
49+
p.write(c);
50+
p.write(i);
51+
p.write(l);
52+
p.write(uc);
53+
p.write(ui);
54+
p.write(ul);
55+
p.write(0);
56+
p.write(1);
57+
p.close();
58+
59+
p = SPIFFS.open("test.bin", "r");
60+
REQUIRE(p);
61+
uint8_t buff[16];
62+
int len = p.read(buff, 16);
63+
p.close();
64+
REQUIRE(len == 15);
65+
REQUIRE(buff[0] == 1);
66+
REQUIRE(buff[1] == 2);
67+
REQUIRE(buff[2] == 3);
68+
REQUIRE(buff[3] == 4);
69+
REQUIRE(buff[4] == 1);
70+
REQUIRE(buff[5] == 2);
71+
REQUIRE(buff[6] == 3);
72+
REQUIRE(buff[7] == 'h');
73+
REQUIRE(buff[8] == 10);
74+
REQUIRE(buff[9] == 11);
75+
REQUIRE(buff[10] == 20);
76+
REQUIRE(buff[11] == 21);
77+
REQUIRE(buff[12] == 22);
78+
REQUIRE(buff[13] == 0);
79+
REQUIRE(buff[14] == 1);
80+
}

0 commit comments

Comments
 (0)