Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added stream insertion operator.#181

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jfjlaros wants to merge1 commit intoarduino:master
base:master
Choose a base branch
Loading
fromjfjlaros:stream_print
Open
Show file tree
Hide file tree
Changes fromall 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

Some comments aren't visible on the classic Files Changed page.

31 changes: 30 additions & 1 deletionapi/Stream.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -128,6 +128,35 @@ class Stream : public Print

#undef NO_IGNORE_CHAR

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

// Helper function that creates a `Format` object.
template <class T>
Format<T> format(T const data, int const modifier) {
Format<T> fmt {data, modifier};
return fmt;
}

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

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

}

using arduino::Stream;
using arduino::Stream;
using arduino::operator <<;
1 change: 1 addition & 0 deletionstest/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff 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
53 changes: 53 additions & 0 deletionstest/src/Stream/test_insertion_operator.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*/

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

#include <catch.hpp>

#include <StreamMock.h>

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

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

TEST_CASE ("Testing 'format' helper function", "[Stream-insertion-operator-02]") {
arduino::Format<char> fmt {arduino::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 because `StreamMock` has its own << operator.
REQUIRE(mock.available() == 6);

char buf[10] {};
mock.readBytes(buf, 6);
REQUIRE(not strncmp(buf, "a12b34", 6));
}

TEST_CASE ("Testing insertion operator with modifiers", "[Stream-insertion-operator-04]") {
StreamMock mock;
mock << arduino::format(1.2, 4);
REQUIRE(mock.available() == 6);

char buf[10] {};
mock.readBytes(buf, 6);
REQUIRE(not strncmp(buf, "1.2000", 6));

mock << arduino::format(12, BIN);
REQUIRE(mock.available() == 4);

mock.readBytes(buf, 4);
REQUIRE(not strncmp(buf, "1100", 4));
}

[8]ページ先頭

©2009-2025 Movatter.jp