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 <iostream>
23#include <iterator>
24#include <list>
25#include <type_traits>
26
27// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112467
28#if defined(ITK_WRAPPING_PARSER) && defined(__GNUC__) && !defined(__clang__)
29# define __clang__
30# define ITK_CASTXML_GCC_VECTOR_WORKAROUND
31#endif
32#include <vector>
33#if defined(ITK_CASTXML_GCC_VECTOR_WORKAROUND)
34# undef __clang__
35# undef ITK_CASTXML_GCC_VECTOR_WORKAROUND
36#endif
37
39{
40
41template <typename T>
42std::ostream &
43operator<<(std::ostream & os, const std::vector<T> & v)
44{
45 if (v.empty())
46 {
47 return os << "[]";
48 }
49
50 os << '[';
51 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
52 return os << v.back() << ']';
53}
54
55template <typename T>
56std::ostream &
57operator<<(std::ostream & os, const std::list<T> & l)
58{
59 if (l.empty())
60 {
61 return os << "[]";
62 }
63
64 os << '[';
65 std::copy(l.begin(), std::prev(l.end()), std::ostream_iterator<T>(os, ", "));
66 return os << l.back() << ']';
67}
68
69// Stream insertion operator for C-style arrays, excluding character arrays (strings)
70template <typename T, size_t VLength, typename = std::enable_if_t<!std::is_same_v<T, char>>>
71std::ostream &
72operator<<(std::ostream & os, const T (&arr)[VLength])
73{
74 if constexpr (VLength == 0)
75 {
76 return os << "()";
77 }
78
79 os << '(';
80 for (size_t i = 0; i < VLength - 1; ++i)
81 {
82 os << arr[i] << ", ";
83 }
84 return os << arr[VLength - 1] << ')';
85}
86
87} // namespace itk::print_helper
88
89#endif // itkPrintHelper_h
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)