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 
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 
52 #include <boost/type_traits/has_left_shift.hpp>
53 
54 PXR_NAMESPACE_OPEN_SCOPE
55 
56 template <class T>
57 constexpr bool Tf_IsOstreamable() {
58  return boost::has_left_shift<
59  std::ostream &, /* << */ T, /* -> */ std::ostream &>::value;
60 }
61 
64 template <class T, uint32_t N>
65 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
66 operator<<(std::ostream &out, const TfSmallVector<T, N> &v)
67 {
68  out << "[ ";
69  for (auto const &obj: v)
70  out << obj << " ";
71  out << "]";
72 
73  return out;
74 }
75 
76 PXR_NAMESPACE_CLOSE_SCOPE
77 
78 // These operator<< overloads need to go in the std namespace for
79 // Koenig lookup to work.
80 namespace std {
81 
84 template <class T>
85 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
86 operator<<(std::ostream &out, const std::vector<T> &v)
87 {
88  out << "[ ";
89  for (auto const &obj: v)
90  out << obj << " ";
91  out << "]";
92 
93  return out;
94 }
95 
98 template <class T>
99 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
100 operator<<(std::ostream &out, const std::set<T> &v)
101 {
102  out << "( ";
103  for (auto const &obj: v)
104  out << obj << " ";
105  out << ")";
106 
107  return out;
108 }
109 
112 template <class T>
113 typename std::enable_if<PXR_NS::Tf_IsOstreamable<T>(), std::ostream &>::type
114 operator<<(std::ostream &out, const std::list<T> &l)
115 {
116  out << "{ ";
117  for (auto const &obj: l)
118  out << obj << " ";
119  out << "}";
120 
121  return out;
122 }
123 
126 template <class K, class M, class H, class C, class A>
127 typename std::enable_if<
128  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
129 operator<<(std::ostream &out, const PXR_NS::TfHashMap<K, M, H, C, A> &h)
130 {
131  out << "< ";
132  for (auto const &p: h)
133  out << "<" << p.first << ": " << p.second << "> ";
134  out << ">";
135  return out;
136 }
137 
140 template <class K, class M>
141 typename std::enable_if<
142  PXR_NS::Tf_IsOstreamable<K>() && PXR_NS::Tf_IsOstreamable<M>(), std::ostream &>::type
143 operator<<(std::ostream &out, const std::map<K, M> &h)
144 {
145  out << "< ";
146  for (auto const &p: h)
147  out << "<" << p.first << ": " << p.second << "> ";
148  out << ">";
149  return out;
150 }
151 
152 } // namespace std
153 
154 #endif // PXR_BASE_TF_OSTREAM_METHODS_H
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:177