Skip to content

Commit 5993485

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

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

api/Stream.h

+29-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ 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+
144+
template <class T>
145+
Stream& operator <<(Stream& stream, T const data) { // Stream insertion operator for plain data types.
146+
stream.print(data);
147+
return stream;
148+
}
149+
150+
template <class T>
151+
Stream& operator <<(Stream& stream, Format<T> const& parameters) { // Stream insertion operator with modifiers (e.g., BIN, HEX, number of digits, etc.).
152+
stream.print(parameters.data, parameters.modifier);
153+
return stream;
154+
}
155+
131156
}
132157

133-
using arduino::Stream;
158+
using arduino::Stream;
159+
using arduino::Format;
160+
using arduino::format;
161+
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,42 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <MillisFake.h>
12+
#include <StreamMock.h>
13+
14+
/**************************************************************************************
15+
* TEST CODE
16+
**************************************************************************************/
17+
18+
TEST_CASE ("Testing 'Format' initialisation", "[Stream-insertion-operator-01]") {
19+
Format<char> fmt {'a', 2};
20+
REQUIRE(fmt.data == 'a');
21+
REQUIRE(fmt.modifier == 2);
22+
}
23+
24+
TEST_CASE ("Testing 'format' helper function", "[Stream-insertion-operator-02]") {
25+
Format<char> fmt {format('a', 2)};
26+
REQUIRE(fmt.data == 'a');
27+
REQUIRE(fmt.modifier == 2);
28+
}
29+
30+
TEST_CASE ("Testing basic insertion operator", "[Stream-insertion-operator-03]") {
31+
StreamMock mock;
32+
mock << 'a' << 12 << 'b' << 34; // Note we cannot test C strings as `StreamMock` has its own << operator.
33+
REQUIRE(mock.available() == 6);
34+
}
35+
36+
TEST_CASE ("Testing insertion operator with modifiers", "[Stream-insertion-operator-03]") {
37+
StreamMock mock;
38+
mock << format(1.2, 4); // Expands to `1.2000`.
39+
REQUIRE(mock.available() == 6);
40+
mock << format(12, BIN); // Expands to `1100`.
41+
REQUIRE(mock.available() == 10);
42+
}

0 commit comments

Comments
 (0)