HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ostreamMethods.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_OSTREAM_METHODS_H
25 #define PXR_BASE_TF_OSTREAM_METHODS_H
26 
27 /// \file tf/ostreamMethods.h
28 /// \ingroup group_tf_DebuggingOutput
29 ///
30 /// Handy ostream output for various lib/tf and STL containers.
31 ///
32 /// These functions are useful when you need to quickly output various STL
33 /// containers. The stream operators are only available if the contained
34 /// types have stream operators.
35 ///
36 /// This facility should \e not be used to output data for later input: this
37 /// is essentially a "write-only" facility meant for diagnostics or
38 /// human-readable display; the formats described herein are subject to change
39 /// without notice.
40 
41 #include "pxr/pxr.h"
42 #include "pxr/base/tf/hashmap.h"
44 
45 #include <ostream>
46 #include <vector>
47 #include <list>
48 #include <map>
49 #include <set>
50 #include <type_traits>
51 #include <utility>
52 
54 
55 template <class T>
56 constexpr auto Tf_IsOstreamable_Impl(int) ->
57  decltype(std::declval<std::ostream &>() << std::declval<T>(), bool())
58 {
59  return true;
60 }
61 
62 template <class T>
63 constexpr bool Tf_IsOstreamable_Impl(...) {
64  return false;
65 }
66 
67 template <class T>
68 constexpr bool Tf_IsOstreamable() {
69  return Tf_IsOstreamable_Impl<T>(0);
70 }
71 
72 /// Output a TfSmallVector using [ ] as delimiters.
73 /// \ingroup group_tf_DebuggingOutput
74 template <class T, uint32_t N>
75 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
76 operator<<(std::ostream &out, const TfSmallVector<T, N> &v)
77 {
78  out << "[ ";
79  for (auto const &obj: v)
80  out << obj << " ";
81  out << "]";
82 
83  return out;
84 }
85 
87 
88 // These operator<< overloads need to go in the std namespace for
89 // Koenig lookup to work.
90 namespace std {
91 
92 /// Output an STL vector using [ ] as delimiters.
93 /// \ingroup group_tf_DebuggingOutput
94 template <class T>
95 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
96 operator<<(std::ostream &out, const std::vector<T> &v)
97 {
98  out << "[ ";
99  for (auto const &obj: v)
100  out << obj << " ";
101  out << "]";
102 
103  return out;
104 }
105 
106 /// Output an STL set using ( ) as delimiters.
107 /// \ingroup group_tf_DebuggingOutput
108 template <class T>
109 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
110 operator<<(std::ostream &out, const std::set<T> &v)
111 {
112  out << "( ";
113  for (auto const &obj: v)
114  out << obj << " ";
115  out << ")";
116 
117  return out;
118 }
119 
120 /// Output an STL list using { } as delimiters.
121 /// \ingroup group_tf_DebuggingOutput
122 template <class T>
123 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
124 operator<<(std::ostream &out, const std::list<T> &l)
125 {
126  out << "{ ";
127  for (auto const &obj: l)
128  out << obj << " ";
129  out << "}";
130 
131  return out;
132 }
133 
134 /// Output an TfHashMap using < > as delimiters.
135 /// \ingroup group_tf_DebuggingOutput
136 template <class K, class M, class H, class C, class A>
137 typename std::enable_if<
138  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
139 operator<<(std::ostream &out, const PXR_NS::TfHashMap<K, M, H, C, A> &h)
140 {
141  out << "< ";
142  for (auto const &p: h)
143  out << "<" << p.first << ": " << p.second << "> ";
144  out << ">";
145  return out;
146 }
147 
148 /// Output an STL map using < > as delimiters.
149 /// \ingroup group_tf_DebuggingOutput
150 template <class K, class M>
151 typename std::enable_if<
152  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
153 operator<<(std::ostream &out, const std::map<K, M> &h)
154 {
155  out << "< ";
156  for (auto const &p: h)
157  out << "<" << p.first << ": " << p.second << "> ";
158  out << ">";
159  return out;
160 }
161 
162 } // namespace std
163 
164 #endif // PXR_BASE_TF_OSTREAM_METHODS_H
const GLdouble * v
Definition: glcorearb.h:837
PXR_NAMESPACE_OPEN_SCOPE constexpr auto Tf_IsOstreamable_Impl(int) -> decltype(std::declval< std::ostream & >()<< std::declval< T >(), bool())
constexpr bool Tf_IsOstreamable()
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
type
Definition: core.h:1059