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 <vector>
26#include <list>
27#include <type_traits>
28
29
31{
32
33template <typename T>
34std::ostream &
35operator<<(std::ostream & os, const std::vector<T> & v)
36{
37 if (v.empty())
38 {
39 return os << "[]";
40 }
41
42 os << '[';
43 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
44 return os << v.back() << ']';
45}
46
47template <typename T>
48std::ostream &
49operator<<(std::ostream & os, const std::list<T> & l)
50{
51 if (l.empty())
52 {
53 return os << "[]";
54 }
55
56 os << '[';
57 std::copy(l.begin(), std::prev(l.end()), std::ostream_iterator<T>(os, ", "));
58 return os << l.back() << ']';
59}
60
61template <typename T, size_t VLength>
62std::ostream &
63operator<<(std::ostream & os, [[maybe_unused]] const std::array<T, VLength> & container)
64{
65 if constexpr (VLength == 0)
66 {
67 return os << "()";
68 }
69 else
70 {
71 os << '(';
72 std::copy(container.cbegin(), std::prev(container.cend()), std::ostream_iterator<T>(os, ", "));
73 return os << container.back() << ')';
74 }
75}
76
77// Stream insertion operator for C-style arrays, excluding character arrays (strings)
78template <typename T, size_t VLength, typename = std::enable_if_t<!std::is_same_v<T, char>>>
79std::ostream &
80operator<<(std::ostream & os, const T (&arr)[VLength])
81{
82 if constexpr (VLength == 0)
83 {
84 return os << "()";
85 }
86
87 os << '(';
88 for (size_t i = 0; i < VLength - 1; ++i)
89 {
90 os << arr[i] << ", ";
91 }
92 return os << arr[VLength - 1] << ')';
93}
94
95} // namespace itk::print_helper
96
97#endif // itkPrintHelper_h
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)