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 "itkMacro.h"
23#include "itkIndent.h"
24#ifndef itkIndent_h_complete
25# error "itk::Indent incomplete: itkIndent.h must not include itkMacro.h (keep ITKCommonExport.h, itkConfigure.h)"
26#endif
27
28#include <array>
29#include <iostream>
30#include <iterator>
31#include <vector>
32#include <list>
33#include <type_traits>
34
35
36namespace itk
37{
38// Forward declaration so itkPrintHelper.h can be safely included from
39// itkMacro.h without re-entering itkNumericTraits.h (which itself uses
40// macros defined later in itkMacro.h). Every PrintNumericTrait() call
41// site needs the full NumericTraits<T> specialization in scope, but those
42// sites already #include "itkNumericTraits.h" directly or transitively.
43template <typename T>
44class NumericTraits;
45} // namespace itk
46
48{
49
50// Forward declarations so the per-container bodies below see all overloads at
51// definition time. Required for nested cases like vector<list<T>>, where the
52// recursive `os << *it` is parsed before the list overload would otherwise be
53// declared, and ADL on std container types never reaches itk::print_helper.
54// PrintNumericTrait further down also dispatches through `os << value` and
55// must see these forward declarations to instantiate against std container
56// member types at the call site.
57template <typename T>
58std::ostream &
59operator<<(std::ostream & os, const std::vector<T> & v);
60
61template <typename T>
62std::ostream &
63operator<<(std::ostream & os, const std::list<T> & l);
64
65template <typename T, size_t VLength>
66std::ostream &
67operator<<(std::ostream & os, const std::array<T, VLength> & container);
68
69template <typename T, size_t VLength, typename = std::enable_if_t<!std::is_same_v<T, char>>>
70std::ostream &
71operator<<(std::ostream & os, const T (&arr)[VLength]);
72
99template <typename T>
100inline void
101PrintNumericTrait(std::ostream & os, const Indent & indent, const char * name, const T & value)
102{
103 os << indent << name << ": ";
104 if constexpr (std::is_same_v<T, typename NumericTraits<T>::PrintType>)
105 {
106 os << value;
107 }
108 else
109 {
110 os << static_cast<typename NumericTraits<T>::PrintType>(value);
111 }
112 os << std::endl;
113}
114
115template <typename T>
116std::ostream &
117operator<<(std::ostream & os, const std::vector<T> & v)
118{
119 if (v.empty())
120 {
121 return os << "[]";
122 }
123
124 os << '[';
125 // Manual loop so that an unqualified `os << *it` resolves overloads in
126 // itk::print_helper for nested containers; std::ostream_iterator inserts
127 // from inside namespace std and would not see them.
128 for (auto it = v.begin(); it != std::prev(v.end()); ++it)
129 {
130 os << *it << ", ";
131 }
132 return os << v.back() << ']';
133}
134
135template <typename T>
136std::ostream &
137operator<<(std::ostream & os, const std::list<T> & l)
138{
139 if (l.empty())
140 {
141 return os << "[]";
142 }
143
144 os << '[';
145 for (auto it = l.begin(); it != std::prev(l.end()); ++it)
146 {
147 os << *it << ", ";
148 }
149 return os << l.back() << ']';
150}
151
152template <typename T, size_t VLength>
153std::ostream &
154operator<<(std::ostream & os, [[maybe_unused]] const std::array<T, VLength> & container)
155{
156 if constexpr (VLength == 0)
157 {
158 return os << "()";
159 }
160 else
161 {
162 os << '(';
163 for (auto it = container.cbegin(); it != std::prev(container.cend()); ++it)
164 {
165 os << *it << ", ";
166 }
167 return os << container.back() << ')';
168 }
169}
170
171// Stream insertion operator for C-style arrays, excluding character arrays (strings)
172template <typename T, size_t VLength, typename>
173std::ostream &
174operator<<(std::ostream & os, const T (&arr)[VLength])
175{
176 if constexpr (VLength == 0)
177 {
178 return os << "()";
179 }
180
181 os << '(';
182 for (size_t i = 0; i < VLength - 1; ++i)
183 {
184 os << arr[i] << ", ";
185 }
186 return os << arr[VLength - 1] << ')';
187}
188
189} // namespace itk::print_helper
190
191#endif // itkPrintHelper_h
Control indentation during Print() invocation.
Definition itkIndent.h:53
Define additional traits for native types such as int or float.
void PrintNumericTrait(std::ostream &os, const Indent &indent, const char *name, const T &value)
Print "<name>: <value>\n" indented, matching ITK's PrintSelf style.
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....