Skip to content

Commit

Permalink
Added stream insertion operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Feb 7, 2023
1 parent 844e4bf commit 5993485
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
30 changes: 29 additions & 1 deletion api/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ class Stream : public Print

#undef NO_IGNORE_CHAR

template <class T>
struct Format { // Structure to store a value and a modifier.
T data;
int modifier;
};

template <class T>
Format<T> format(T const data, int const modifier) {
Format<T> fmt {data, modifier};
return fmt;
}


template <class T>
Stream& operator <<(Stream& stream, T const data) { // Stream insertion operator for plain data types.
stream.print(data);
return stream;
}

template <class T>
Stream& operator <<(Stream& stream, Format<T> const& parameters) { // Stream insertion operator with modifiers (e.g., BIN, HEX, number of digits, etc.).
stream.print(parameters.data, parameters.modifier);
return stream;
}

}

using arduino::Stream;
using arduino::Stream;
using arduino::Format;
using arduino::format;
using arduino::operator <<;
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(TEST_SRCS
src/Stream/test_find.cpp
src/Stream/test_findUntil.cpp
src/Stream/test_getTimeout.cpp
src/Stream/test_insertion_operator.cpp
src/Stream/test_parseFloat.cpp
src/Stream/test_parseInt.cpp
src/Stream/test_readBytes.cpp
Expand Down
42 changes: 42 additions & 0 deletions test/src/Stream/test_insertion_operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <catch.hpp>

#include <MillisFake.h>
#include <StreamMock.h>

/**************************************************************************************
* TEST CODE
**************************************************************************************/

TEST_CASE ("Testing 'Format' initialisation", "[Stream-insertion-operator-01]") {
Format<char> fmt {'a', 2};
REQUIRE(fmt.data == 'a');
REQUIRE(fmt.modifier == 2);
}

TEST_CASE ("Testing 'format' helper function", "[Stream-insertion-operator-02]") {
Format<char> fmt {format('a', 2)};
REQUIRE(fmt.data == 'a');
REQUIRE(fmt.modifier == 2);
}

TEST_CASE ("Testing basic insertion operator", "[Stream-insertion-operator-03]") {
StreamMock mock;
mock << 'a' << 12 << 'b' << 34; // Note we cannot test C strings as `StreamMock` has its own << operator.
REQUIRE(mock.available() == 6);
}

TEST_CASE ("Testing insertion operator with modifiers", "[Stream-insertion-operator-03]") {
StreamMock mock;
mock << format(1.2, 4); // Expands to `1.2000`.
REQUIRE(mock.available() == 6);
mock << format(12, BIN); // Expands to `1100`.
REQUIRE(mock.available() == 10);
}

0 comments on commit 5993485

Please sign in to comment.