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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_OSTREAM_METHODS_H
8 #define PXR_BASE_TF_OSTREAM_METHODS_H
9 
10 /// \file tf/ostreamMethods.h
11 /// \ingroup group_tf_DebuggingOutput
12 ///
13 /// Handy ostream output for various lib/tf and STL containers.
14 ///
15 /// These functions are useful when you need to quickly output various STL
16 /// containers. The stream operators are only available if the contained
17 /// types have stream operators.
18 ///
19 /// This facility should \e not be used to output data for later input: this
20 /// is essentially a "write-only" facility meant for diagnostics or
21 /// human-readable display; the formats described herein are subject to change
22 /// without notice.
23 
24 #include "pxr/pxr.h"
25 #include "pxr/base/tf/hashmap.h"
27 
28 #include <ostream>
29 #include <vector>
30 #include <list>
31 #include <map>
32 #include <set>
33 #include <type_traits>
34 #include <utility>
35 
37 
38 template <class T>
39 constexpr auto Tf_IsOstreamable_Impl(int) ->
40  decltype(std::declval<std::ostream &>() << std::declval<T>(), bool())
41 {
42  return true;
43 }
44 
45 template <class T>
46 constexpr bool Tf_IsOstreamable_Impl(...) {
47  return false;
48 }
49 
50 template <class T>
51 constexpr bool Tf_IsOstreamable() {
52  return Tf_IsOstreamable_Impl<T>(0);
53 }
54 
55 /// Output a TfSmallVector using [ ] as delimiters.
56 /// \ingroup group_tf_DebuggingOutput
57 template <class T, uint32_t N>
58 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
59 operator<<(std::ostream &out, const TfSmallVector<T, N> &v)
60 {
61  out << "[ ";
62  for (auto const &obj: v)
63  out << obj << " ";
64  out << "]";
65 
66  return out;
67 }
68 
70 
71 // These operator<< overloads need to go in the std namespace for
72 // Koenig lookup to work.
73 namespace std {
74 
75 /// Output an STL vector using [ ] as delimiters.
76 /// \ingroup group_tf_DebuggingOutput
77 template <class T>
78 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
79 operator<<(std::ostream &out, const std::vector<T> &v)
80 {
81  out << "[ ";
82  for (auto const &obj: v)
83  out << obj << " ";
84  out << "]";
85 
86  return out;
87 }
88 
89 /// Output an STL set using ( ) as delimiters.
90 /// \ingroup group_tf_DebuggingOutput
91 template <class T>
92 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
93 operator<<(std::ostream &out, const std::set<T> &v)
94 {
95  out << "( ";
96  for (auto const &obj: v)
97  out << obj << " ";
98  out << ")";
99 
100  return out;
101 }
102 
103 /// Output an STL list using { } as delimiters.
104 /// \ingroup group_tf_DebuggingOutput
105 template <class T>
106 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
107 operator<<(std::ostream &out, const std::list<T> &l)
108 {
109  out << "{ ";
110  for (auto const &obj: l)
111  out << obj << " ";
112  out << "}";
113 
114  return out;
115 }
116 
117 /// Output an TfHashMap using < > as delimiters.
118 /// \ingroup group_tf_DebuggingOutput
119 template <class K, class M, class H, class C, class A>
120 typename std::enable_if<
121  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
122 operator<<(std::ostream &out, const PXR_NS::TfHashMap<K, M, H, C, A> &h)
123 {
124  out << "< ";
125  for (auto const &p: h)
126  out << "<" << p.first << ": " << p.second << "> ";
127  out << ">";
128  return out;
129 }
130 
131 /// Output an STL map using < > as delimiters.
132 /// \ingroup group_tf_DebuggingOutput
133 template <class K, class M>
134 typename std::enable_if<
135  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
136 operator<<(std::ostream &out, const std::map<K, M> &h)
137 {
138  out << "< ";
139  for (auto const &p: h)
140  out << "<" << p.first << ": " << p.second << "> ";
141  out << ">";
142  return out;
143 }
144 
145 } // namespace std
146 
147 #endif // PXR_BASE_TF_OSTREAM_METHODS_H
const GLdouble * v
Definition: glcorearb.h:837
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
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:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74