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"
43 
44 #include <ostream>
45 #include <vector>
46 #include <list>
47 #include <map>
48 #include <set>
49 #include <type_traits>
50 
51 #include <hboost/type_traits/has_left_shift.hpp>
52 
54 
55 template <class T>
56 constexpr bool Tf_IsOstreamable() {
57  return hboost::has_left_shift<
58  std::ostream &, /* << */ T, /* -> */ std::ostream &>::value;
59 }
60 
62 
63 // These operator<< overloads need to go in the std namespace for
64 // Koenig lookup to work.
65 namespace std {
66 
67 /// Output an STL vector using [ ] as delimiters.
68 /// \ingroup group_tf_DebuggingOutput
69 template <class T>
70 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
71 operator<<(std::ostream &out, const std::vector<T> &v)
72 {
73  out << "[ ";
74  for (auto const &obj: v)
75  out << obj << " ";
76  out << "]";
77 
78  return out;
79 }
80 
81 /// Output an STL set using ( ) as delimiters.
82 /// \ingroup group_tf_DebuggingOutput
83 template <class T>
84 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
85 operator<<(std::ostream &out, const std::set<T> &v)
86 {
87  out << "( ";
88  for (auto const &obj: v)
89  out << obj << " ";
90  out << ")";
91 
92  return out;
93 }
94 
95 /// Output an STL list using { } as delimiters.
96 /// \ingroup group_tf_DebuggingOutput
97 template <class T>
98 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
99 operator<<(std::ostream &out, const std::list<T> &l)
100 {
101  out << "{ ";
102  for (auto const &obj: l)
103  out << obj << " ";
104  out << "}";
105 
106  return out;
107 }
108 
109 /// Output an TfHashMap using < > as delimiters.
110 /// \ingroup group_tf_DebuggingOutput
111 template <class K, class M, class H, class C, class A>
112 typename std::enable_if<
113  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
114 operator<<(std::ostream &out, const PXR_NS::TfHashMap<K, M, H, C, A> &h)
115 {
116  out << "< ";
117  for (auto const &p: h)
118  out << "<" << p.first << ": " << p.second << "> ";
119  out << ">";
120  return out;
121 }
122 
123 /// Output an STL map using < > as delimiters.
124 /// \ingroup group_tf_DebuggingOutput
125 template <class K, class M>
126 typename std::enable_if<
127  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
128 operator<<(std::ostream &out, const std::map<K, M> &h)
129 {
130  out << "< ";
131  for (auto const &p: h)
132  out << "<" << p.first << ": " << p.second << "> ";
133  out << ">";
134  return out;
135 }
136 
137 } // namespace std
138 
139 #endif // PXR_BASE_TF_OSTREAM_METHODS_H
GLdouble l
Definition: glew.h:9122
PXR_NAMESPACE_OPEN_SCOPE constexpr bool Tf_IsOstreamable()
const GLdouble * v
Definition: glew.h:1391
GLhandleARB obj
Definition: glew.h:6236
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
GLfloat GLfloat p
Definition: glew.h:16321
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLsizei const GLfloat * value
Definition: glew.h:1849