Skip to content

Commit 4944f85

Browse files
committed
Added stream insertion operator.
1 parent 844e4bf commit 4944f85

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

api/Stream.h

+28-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ class Stream : public Print
128128

129129
#undef NO_IGNORE_CHAR
130130

131+
template <class T>
132+
struct Format { // Structure to store a value and a modifier.
133+
T data;
134+
int modifier;
135+
};
136+
137+
template <class T>
138+
Format<T> format(T const data, int const modifier) {
139+
Format<T> fmt {data, modifier};
140+
return fmt;
141+
}
142+
143+
template <class T>
144+
Stream& operator <<(Stream& stream, T const data) { // Stream insertion operator for plain data types.
145+
stream.print(data);
146+
return stream;
147+
}
148+
149+
template <class T>
150+
Stream& operator <<(Stream& stream, Format<T> const& parameters) { // Stream insertion operator with modifiers (e.g., BIN, HEX, number of digits, etc.).
151+
stream.print(parameters.data, parameters.modifier);
152+
return stream;
153+
}
154+
131155
}
132156

133-
using arduino::Stream;
157+
using arduino::Stream;
158+
using arduino::Format;
159+
using arduino::format;
160+
using arduino::operator <<;

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(TEST_SRCS
5555
src/Stream/test_find.cpp
5656
src/Stream/test_findUntil.cpp
5757
src/Stream/test_getTimeout.cpp
58+
src/Stream/test_insertion_operator.cpp
5859
src/Stream/test_parseFloat.cpp
5960
src/Stream/test_parseInt.cpp
6061
src/Stream/test_readBytes.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <StreamMock.h>
12+
13+
/**************************************************************************************
14+
* TEST CODE
15+
**************************************************************************************/
16+
17+
TEST_CASE ("Testing 'Format' initialisation", "[Stream-insertion-operator-01]") {
18+
Format<char> fmt {'a', 2};
19+
REQUIRE(fmt.data == 'a');
20+
REQUIRE(fmt.modifier == 2);
21+
}
22+
23+
TEST_CASE ("Testing 'format' helper function", "[Stream-insertion-operator-02]") {
24+
Format<char> fmt {format('a', 2)};
25+
REQUIRE(fmt.data == 'a');
26+
REQUIRE(fmt.modifier == 2);
27+
}
28+
29+
TEST_CASE ("Testing basic insertion operator", "[Stream-insertion-operator-03]") {
30+
StreamMock mock;
31+
mock << 'a' << 12 << 'b' << 34; // Note we cannot test C strings as `StreamMock` has its own << operator.
32+
REQUIRE(mock.available() == 6);
33+
}
34+
35+
TEST_CASE ("Testing insertion operator with modifiers", "[Stream-insertion-operator-03]") {
36+
StreamMock mock;
37+
mock << format(1.2, 4); // Expands to `1.2000`.
38+
REQUIRE(mock.available() == 6);
39+
mock << format(12, BIN); // Expands to `1100`.
40+
REQUIRE(mock.available() == 10);
41+
}

0 commit comments

Comments
 (0)