-
-
Notifications
You must be signed in to change notification settings - Fork 713
ENH: Add stream insertion operators for std::list and C-style arrays #5616
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |||||||
|
|
||||||||
| #include <iostream> | ||||||||
| #include <iterator> | ||||||||
| #include <list> | ||||||||
| #include <type_traits> | ||||||||
|
|
||||||||
| // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112467 | ||||||||
| #if defined(ITK_WRAPPING_PARSER) && defined(__GNUC__) && !defined(__clang__) | ||||||||
|
|
@@ -50,6 +52,43 @@ operator<<(std::ostream & os, const std::vector<T> & v) | |||||||
| return os << v.back() << ')'; | ||||||||
| } | ||||||||
|
|
||||||||
| template <typename T> | ||||||||
| std::ostream & | ||||||||
| operator<<(std::ostream & os, const std::list<T> & l) | ||||||||
| { | ||||||||
| if (l.empty()) | ||||||||
| { | ||||||||
| return os << "()"; | ||||||||
| } | ||||||||
|
|
||||||||
| os << '('; | ||||||||
| auto it = l.begin(); | ||||||||
| auto last = std::prev(l.end()); | ||||||||
| for (; it != last; ++it) | ||||||||
| { | ||||||||
| os << *it << ", "; | ||||||||
| } | ||||||||
| return os << *it << ')'; | ||||||||
|
||||||||
| os << '('; | |
| std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", ")); | |
| return os << v.back() << ')'; |
So I would suggest using std::copy and std::ostream_iterator<T>(os, ", ") when printing std::list as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented both suggestions: now using std::copy with std::ostream_iterator<T>(os, ", ") and simplified the implementation to match the vector version. (commit 557d2e4)
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /*========================================================================= | ||
| * | ||
| * Copyright NumFOCUS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0.txt | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| *=========================================================================*/ | ||
|
|
||
| #include "itkPrintHelper.h" | ||
| #include "itkOffset.h" | ||
| #include "gtest/gtest.h" | ||
| #include <sstream> | ||
| #include <vector> | ||
| #include <list> | ||
|
|
||
| TEST(PrintHelper, Vector) | ||
| { | ||
| using namespace itk::print_helper; | ||
| std::vector<int> v{ 1, 2, 3, 4, 5 }; | ||
| std::ostringstream oss; | ||
| oss << v; | ||
| EXPECT_EQ(oss.str(), "(1, 2, 3, 4, 5)"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, EmptyVector) | ||
| { | ||
| using namespace itk::print_helper; | ||
| std::vector<int> v; | ||
| std::ostringstream oss; | ||
| oss << v; | ||
| EXPECT_EQ(oss.str(), "()"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, List) | ||
| { | ||
| using namespace itk::print_helper; | ||
| std::list<int> l{ 1, 2, 3, 4, 5 }; | ||
| std::ostringstream oss; | ||
| oss << l; | ||
| EXPECT_EQ(oss.str(), "(1, 2, 3, 4, 5)"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, EmptyList) | ||
| { | ||
| using namespace itk::print_helper; | ||
| std::list<int> l; | ||
| std::ostringstream oss; | ||
| oss << l; | ||
| EXPECT_EQ(oss.str(), "()"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, CStyleArray) | ||
| { | ||
| using namespace itk::print_helper; | ||
| int arr[5] = { 1, 2, 3, 4, 5 }; | ||
| std::ostringstream oss; | ||
| oss << arr; | ||
| EXPECT_EQ(oss.str(), "(1, 2, 3, 4, 5)"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, CStyleArraySingleElement) | ||
| { | ||
| using namespace itk::print_helper; | ||
| int arr[1] = { 42 }; | ||
| std::ostringstream oss; | ||
| oss << arr; | ||
| EXPECT_EQ(oss.str(), "(42)"); | ||
| } | ||
|
|
||
| TEST(PrintHelper, VectorOfOffsets) | ||
| { | ||
| using namespace itk::print_helper; | ||
| std::vector<itk::Offset<2>> v; | ||
| itk::Offset<2> o1{ { 1, 2 } }; | ||
| itk::Offset<2> o2{ { 3, 4 } }; | ||
| v.push_back(o1); | ||
| v.push_back(o2); | ||
| std::ostringstream oss; | ||
| oss << v; | ||
| EXPECT_EQ(oss.str(), "([1, 2], [3, 4])"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.