1 // Copyright 2003 Google, Inc.
2 // All Rights Reserved.
4 // Stream output operators for STL containers; to be used for logging *only*.
5 // Inclusion of this file lets you do:
8 // LOG(INFO) << "data: " << x;
12 // Note that if you want to use these operators from the non-global namespace,
13 // you may get an error since they are not in namespace std (and they are not
14 // in namespace std since that would result in undefined behavior). You may
17 // using ::operator<<;
19 // to fix these errors.
21 #ifndef UTIL_GTL_STL_LOGGING_INL_H_
22 #define UTIL_GTL_STL_LOGGING_INL_H_
24 #if !@ac_cv_cxx_using_operator@
25 # error We do not support stl_logging for this compiler
37 # include <ext/hash_set>
38 # include <ext/hash_map>
42 template<class First, class Second>
43 inline std::ostream& operator<<(std::ostream& out,
44 const std::pair<First, Second>& p) {
45 out << '(' << p.first << ", " << p.second << ')';
49 @ac_google_start_namespace@
52 inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
54 // Output at most 100 elements -- appropriate if used for logging.
55 for (int i = 0; begin != end && i < 100; ++i, ++begin) {
56 if (i > 0) out << ' ';
64 @ac_google_end_namespace@
66 #define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
67 template<class T1, class T2> \
68 inline std::ostream& operator<<(std::ostream& out, \
69 const Sequence<T1, T2>& seq) { \
70 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
74 OUTPUT_TWO_ARG_CONTAINER(std::vector)
75 OUTPUT_TWO_ARG_CONTAINER(std::deque)
76 OUTPUT_TWO_ARG_CONTAINER(std::list)
78 OUTPUT_TWO_ARG_CONTAINER(__gnu_cxx::slist)
81 #undef OUTPUT_TWO_ARG_CONTAINER
83 #define OUTPUT_THREE_ARG_CONTAINER(Sequence) \
84 template<class T1, class T2, class T3> \
85 inline std::ostream& operator<<(std::ostream& out, \
86 const Sequence<T1, T2, T3>& seq) { \
87 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
91 OUTPUT_THREE_ARG_CONTAINER(std::set)
92 OUTPUT_THREE_ARG_CONTAINER(std::multiset)
94 #undef OUTPUT_THREE_ARG_CONTAINER
96 #define OUTPUT_FOUR_ARG_CONTAINER(Sequence) \
97 template<class T1, class T2, class T3, class T4> \
98 inline std::ostream& operator<<(std::ostream& out, \
99 const Sequence<T1, T2, T3, T4>& seq) { \
100 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
104 OUTPUT_FOUR_ARG_CONTAINER(std::map)
105 OUTPUT_FOUR_ARG_CONTAINER(std::multimap)
107 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_set)
108 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_multiset)
111 #undef OUTPUT_FOUR_ARG_CONTAINER
113 #define OUTPUT_FIVE_ARG_CONTAINER(Sequence) \
114 template<class T1, class T2, class T3, class T4, class T5> \
115 inline std::ostream& operator<<(std::ostream& out, \
116 const Sequence<T1, T2, T3, T4, T5>& seq) { \
117 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
122 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_map)
123 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap)
126 #undef OUTPUT_FIVE_ARG_CONTAINER
128 #endif // UTIL_GTL_STL_LOGGING_INL_H_