ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkPrintHelper.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef itkPrintHelper_h
20#define itkPrintHelper_h
21
22#include <array>
23#include <iostream>
24#include <iterator>
25#include <list>
26#include <type_traits>
27
28// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112467
29#if defined(ITK_WRAPPING_PARSER) && defined(__GNUC__) && !defined(__clang__)
30# define __clang__
31# define ITK_CASTXML_GCC_VECTOR_WORKAROUND
32#endif
33#include <vector>
34#if defined(ITK_CASTXML_GCC_VECTOR_WORKAROUND)
35# undef __clang__
36# undef ITK_CASTXML_GCC_VECTOR_WORKAROUND
37#endif
38
40{
41
42template <typename T>
43std::ostream &
44operator<<(std::ostream & os, const std::vector<T> & v)
45{
46 if (v.empty())
47 {
48 return os << "[]";
49 }
50
51 os << '[';
52 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
53 return os << v.back() << ']';
54}
55
56template <typename T>
57std::ostream &
58operator<<(std::ostream & os, const std::list<T> & l)
59{
60 if (l.empty())
61 {
62 return os << "[]";
63 }
64
65 os << '[';
66 std::copy(l.begin(), std::prev(l.end()), std::ostream_iterator<T>(os, ", "));
67 return os << l.back() << ']';
68}
69
70template <typename T, size_t VLength>
71std::ostream &
72operator<<(std::ostream & os, [[maybe_unused]] const std::array<T, VLength> & container)
73{
74 if constexpr (VLength == 0)
75 {
76 return os << "()";
77 }
78 else
79 {
80 os << '(';
81 std::copy(container.cbegin(), std::prev(container.cend()), std::ostream_iterator<T>(os, ", "));
82 return os << container.back() << ')';
83 }
84}
85
86// Stream insertion operator for C-style arrays, excluding character arrays (strings)
87template <typename T, size_t VLength, typename = std::enable_if_t<!std::is_same_v<T, char>>>
88std::ostream &
89operator<<(std::ostream & os, const T (&arr)[VLength])
90{
91 if constexpr (VLength == 0)
92 {
93 return os << "()";
94 }
95
96 os << '(';
97 for (size_t i = 0; i < VLength - 1; ++i)
98 {
99 os << arr[i] << ", ";
100 }
101 return os << arr[VLength - 1] << ')';
102}
103
104} // namespace itk::print_helper
105
106#endif // itkPrintHelper_h
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)