Fix VC build by adding GOOGLE_GLOG_DLL_DECL
[platform/upstream/glog.git] / src / glog / stl_logging.h.in
1 // Copyright (c) 2003, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Stream output operators for STL containers; to be used for logging *only*.
31 // Inclusion of this file lets you do:
32 //
33 // list<string> x;
34 // LOG(INFO) << "data: " << x;
35 // vector<int> v1, v2;
36 // CHECK_EQ(v1, v2);
37 //
38 // If you want to use this header file with hash maps or slist, you
39 // need to define macros before including this file:
40 //
41 // - GLOG_STL_LOGGING_FOR_UNORDERED     - <unordered_map> and <unordered_set>
42 // - GLOG_STL_LOGGING_FOR_TR1_UNORDERED - <tr1/unordered_(map|set)>
43 // - GLOG_STL_LOGGING_FOR_EXT_HASH      - <ext/hash_(map|set)>
44 // - GLOG_STL_LOGGING_FOR_EXT_SLIST     - <ext/slist>
45 //
46
47 #ifndef UTIL_GTL_STL_LOGGING_INL_H_
48 #define UTIL_GTL_STL_LOGGING_INL_H_
49
50 #if !@ac_cv_cxx_using_operator@
51 # error We do not support stl_logging for this compiler
52 #endif
53
54 #include <deque>
55 #include <list>
56 #include <map>
57 #include <ostream>
58 #include <set>
59 #include <utility>
60 #include <vector>
61
62 #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
63 # include <unordered_map>
64 # include <unordered_set>
65 #endif
66
67 #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
68 # include <tr1/unordered_map>
69 # include <tr1/unordered_set>
70 #endif
71
72 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
73 # include <ext/hash_set>
74 # include <ext/hash_map>
75 #endif
76 #ifdef GLOG_STL_LOGGING_FOR_EXT_SLIST
77 # include <ext/slist>
78 #endif
79
80 // Forward declare these two, and define them after all the container streams
81 // operators so that we can recurse from pair -> container -> container -> pair
82 // properly.
83 template<class First, class Second>
84 std::ostream& operator<<(std::ostream& out, const std::pair<First, Second>& p);
85
86 @ac_google_start_namespace@
87
88 template<class Iter>
89 void PrintSequence(std::ostream& out, Iter begin, Iter end);
90
91 @ac_google_end_namespace@
92
93 #define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
94 template<class T1, class T2> \
95 inline std::ostream& operator<<(std::ostream& out, \
96                                 const Sequence<T1, T2>& seq) { \
97   @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
98   return out; \
99 }
100
101 OUTPUT_TWO_ARG_CONTAINER(std::vector)
102 OUTPUT_TWO_ARG_CONTAINER(std::deque)
103 OUTPUT_TWO_ARG_CONTAINER(std::list)
104 #ifdef GLOG_STL_LOGGING_FOR_EXT_SLIST
105 OUTPUT_TWO_ARG_CONTAINER(__gnu_cxx::slist)
106 #endif
107
108 #undef OUTPUT_TWO_ARG_CONTAINER
109
110 #define OUTPUT_THREE_ARG_CONTAINER(Sequence) \
111 template<class T1, class T2, class T3> \
112 inline std::ostream& operator<<(std::ostream& out, \
113                                 const Sequence<T1, T2, T3>& seq) { \
114   @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
115   return out; \
116 }
117
118 OUTPUT_THREE_ARG_CONTAINER(std::set)
119 OUTPUT_THREE_ARG_CONTAINER(std::multiset)
120
121 #undef OUTPUT_THREE_ARG_CONTAINER
122
123 #define OUTPUT_FOUR_ARG_CONTAINER(Sequence) \
124 template<class T1, class T2, class T3, class T4> \
125 inline std::ostream& operator<<(std::ostream& out, \
126                                 const Sequence<T1, T2, T3, T4>& seq) { \
127   @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
128   return out; \
129 }
130
131 OUTPUT_FOUR_ARG_CONTAINER(std::map)
132 OUTPUT_FOUR_ARG_CONTAINER(std::multimap)
133 #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
134 OUTPUT_FOUR_ARG_CONTAINER(std::unordered_set)
135 OUTPUT_FOUR_ARG_CONTAINER(std::unordered_multiset)
136 #endif
137 #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
138 OUTPUT_FOUR_ARG_CONTAINER(std::tr1::unordered_set)
139 OUTPUT_FOUR_ARG_CONTAINER(std::tr1::unordered_multiset)
140 #endif
141 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
142 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_set)
143 OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_multiset)
144 #endif
145
146 #undef OUTPUT_FOUR_ARG_CONTAINER
147
148 #define OUTPUT_FIVE_ARG_CONTAINER(Sequence) \
149 template<class T1, class T2, class T3, class T4, class T5> \
150 inline std::ostream& operator<<(std::ostream& out, \
151                                 const Sequence<T1, T2, T3, T4, T5>& seq) { \
152   @ac_google_namespace@::PrintSequence(out, seq.begin(), seq.end()); \
153   return out; \
154 }
155
156 #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
157 OUTPUT_FIVE_ARG_CONTAINER(std::unordered_map)
158 OUTPUT_FIVE_ARG_CONTAINER(std::unordered_multimap)
159 #endif
160 #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
161 OUTPUT_FIVE_ARG_CONTAINER(std::tr1::unordered_map)
162 OUTPUT_FIVE_ARG_CONTAINER(std::tr1::unordered_multimap)
163 #endif
164 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
165 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_map)
166 OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap)
167 #endif
168
169 #undef OUTPUT_FIVE_ARG_CONTAINER
170
171 template<class First, class Second>
172 inline std::ostream& operator<<(std::ostream& out,
173                                 const std::pair<First, Second>& p) {
174   out << '(' << p.first << ", " << p.second << ')';
175   return out;
176 }
177
178 @ac_google_start_namespace@
179
180 template<class Iter>
181 inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
182   // Output at most 100 elements -- appropriate if used for logging.
183   for (int i = 0; begin != end && i < 100; ++i, ++begin) {
184     if (i > 0) out << ' ';
185     out << *begin;
186   }
187   if (begin != end) {
188     out << " ...";
189   }
190 }
191
192 @ac_google_end_namespace@
193
194 // Note that this is technically undefined behavior! We are adding things into
195 // the std namespace for a reason though -- we are providing new operations on
196 // types which are themselves defined with this namespace. Without this, these
197 // operator overloads cannot be found via ADL. If these definitions are not
198 // found via ADL, they must be #included before they're used, which requires
199 // this header to be included before apparently independent other headers.
200 //
201 // For example, base/logging.h defines various template functions to implement
202 // CHECK_EQ(x, y) and stream x and y into the log in the event the check fails.
203 // It does so via the function template MakeCheckOpValueString:
204 //   template<class T>
205 //   void MakeCheckOpValueString(strstream* ss, const T& v) {
206 //     (*ss) << v;
207 //   }
208 // Because 'glog/logging.h' is included before 'glog/stl_logging.h',
209 // subsequent CHECK_EQ(v1, v2) for vector<...> typed variable v1 and v2 can only
210 // find these operator definitions via ADL.
211 //
212 // Even this solution has problems -- it may pull unintended operators into the
213 // namespace as well, allowing them to also be found via ADL, and creating code
214 // that only works with a particular order of includes. Long term, we need to
215 // move all of the *definitions* into namespace std, bet we need to ensure no
216 // one references them first. This lets us take that step. We cannot define them
217 // in both because that would create ambiguous overloads when both are found.
218 namespace std { using ::operator<<; }
219
220 #endif  // UTIL_GTL_STL_LOGGING_INL_H_