1 // Copyright (c) 2003, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Stream output operators for STL containers; to be used for logging *only*.
31 // Inclusion of this file lets you do:
34 // LOG(INFO) << "data: " << x;
35 // vector<int> v1, v2;
38 // Note that if you want to use these operators from the non-global namespace,
39 // you may get an error since they are not in namespace std (and they are not
40 // in namespace std since that would result in undefined behavior). You may
43 // using ::operator<<;
45 // to fix these errors.
47 #ifndef UTIL_GTL_STL_LOGGING_INL_H_
48 #define UTIL_GTL_STL_LOGGING_INL_H_
50 #if !@ac_cv_cxx_using_operator@
51 # error We do not support stl_logging for this compiler
63 # include <ext/hash_set>
64 # include <ext/hash_map>
68 template<class First, class Second>
69 inline std::ostream& operator<<(std::ostream& out,
70 const std::pair<First, Second>& p) {
71 out << '(' << p.first << ", " << p.second << ')';
75 @ac_google_start_namespace@
78 inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
80 // Output at most 100 elements -- appropriate if used for logging.
81 for (int i = 0; begin != end && i < 100; ++i, ++begin) {
82 if (i > 0) out << ' ';
90 @ac_google_end_namespace@
92 #define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
93 template<class T1, class T2> \
94 inline std::ostream& operator<<(std::ostream& out, \
95 const Sequence<T1, T2>& seq) { \
96 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
100 OUTPUT_TWO_ARG_CONTAINER(std::vector)
101 OUTPUT_TWO_ARG_CONTAINER(std::deque)
102 OUTPUT_TWO_ARG_CONTAINER(std::list)
104 OUTPUT_TWO_ARG_CONTAINER(__gnu_cxx::slist)
107 #undef OUTPUT_TWO_ARG_CONTAINER
109 #define OUTPUT_THREE_ARG_CONTAINER(Sequence) \
110 template<class T1, class T2, class T3> \
111 inline std::ostream& operator<<(std::ostream& out, \
112 const Sequence<T1, T2, T3>& seq) { \
113 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
117 OUTPUT_THREE_ARG_CONTAINER(std::set)
118 OUTPUT_THREE_ARG_CONTAINER(std::multiset)
120 #undef OUTPUT_THREE_ARG_CONTAINER
122 #define OUTPUT_FOUR_ARG_CONTAINER(Sequence) \
123 template<class T1, class T2, class T3, class T4> \
124 inline std::ostream& operator<<(std::ostream& out, \
125 const Sequence<T1, T2, T3, T4>& seq) { \
126 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
130 OUTPUT_FOUR_ARG_CONTAINER(std::map)
131 OUTPUT_FOUR_ARG_CONTAINER(std::multimap)
133 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_set)
134 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_multiset)
137 #undef OUTPUT_FOUR_ARG_CONTAINER
139 #define OUTPUT_FIVE_ARG_CONTAINER(Sequence) \
140 template<class T1, class T2, class T3, class T4, class T5> \
141 inline std::ostream& operator<<(std::ostream& out, \
142 const Sequence<T1, T2, T3, T4, T5>& seq) { \
143 @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
148 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_map)
149 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap)
152 #undef OUTPUT_FIVE_ARG_CONTAINER
154 #endif // UTIL_GTL_STL_LOGGING_INL_H_