stl_logging.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // This file is automatically generated from src/glog/stl_logging.h.in
  2. // using src/windows/preprocess.sh.
  3. // DO NOT EDIT!
  4. // Copyright (c) 2003, Google Inc.
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. // Stream output operators for STL containers; to be used for logging *only*.
  34. // Inclusion of this file lets you do:
  35. //
  36. // list<string> x;
  37. // LOG(INFO) << "data: " << x;
  38. // vector<int> v1, v2;
  39. // CHECK_EQ(v1, v2);
  40. //
  41. // If you want to use this header file with hash_compare maps or slist, you
  42. // need to define macros before including this file:
  43. //
  44. // - GLOG_STL_LOGGING_FOR_UNORDERED - <unordered_map> and <unordered_set>
  45. // - GLOG_STL_LOGGING_FOR_TR1_UNORDERED - <tr1/unordered_(map|set)>
  46. // - GLOG_STL_LOGGING_FOR_EXT_HASH - <ext/hash_(map|set)>
  47. // - GLOG_STL_LOGGING_FOR_EXT_SLIST - <ext/slist>
  48. //
  49. #ifndef UTIL_GTL_STL_LOGGING_INL_H_
  50. #define UTIL_GTL_STL_LOGGING_INL_H_
  51. #if !1
  52. # error We do not support stl_logging for this compiler
  53. #endif
  54. #include <deque>
  55. #include <list>
  56. #include <map>
  57. #include <ostream>
  58. #include <set>
  59. #include <utility>
  60. #include <vector>
  61. #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
  62. # include <unordered_map>
  63. # include <unordered_set>
  64. #endif
  65. #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
  66. # include <tr1/unordered_map>
  67. # include <tr1/unordered_set>
  68. #endif
  69. #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
  70. # include <ext/hash_set>
  71. # include <ext/hash_map>
  72. #endif
  73. #ifdef GLOG_STL_LOGGING_FOR_EXT_SLIST
  74. # include <ext/slist>
  75. #endif
  76. // Forward declare these two, and define them after all the container streams
  77. // operators so that we can recurse from pair -> container -> container -> pair
  78. // properly.
  79. template<class First, class Second>
  80. std::ostream& operator<<(std::ostream& out, const std::pair<First, Second>& p);
  81. namespace google {
  82. template<class Iter>
  83. void PrintSequence(std::ostream& out, Iter begin, Iter end);
  84. }
  85. #define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
  86. template<class T1, class T2> \
  87. inline std::ostream& operator<<(std::ostream& out, \
  88. const Sequence<T1, T2>& seq) { \
  89. google::PrintSequence(out, seq.begin(), seq.end()); \
  90. return out; \
  91. }
  92. OUTPUT_TWO_ARG_CONTAINER(std::vector)
  93. OUTPUT_TWO_ARG_CONTAINER(std::deque)
  94. OUTPUT_TWO_ARG_CONTAINER(std::list)
  95. #ifdef GLOG_STL_LOGGING_FOR_EXT_SLIST
  96. OUTPUT_TWO_ARG_CONTAINER(__gnu_cxx::slist)
  97. #endif
  98. #undef OUTPUT_TWO_ARG_CONTAINER
  99. #define OUTPUT_THREE_ARG_CONTAINER(Sequence) \
  100. template<class T1, class T2, class T3> \
  101. inline std::ostream& operator<<(std::ostream& out, \
  102. const Sequence<T1, T2, T3>& seq) { \
  103. google::PrintSequence(out, seq.begin(), seq.end()); \
  104. return out; \
  105. }
  106. OUTPUT_THREE_ARG_CONTAINER(std::set)
  107. OUTPUT_THREE_ARG_CONTAINER(std::multiset)
  108. #undef OUTPUT_THREE_ARG_CONTAINER
  109. #define OUTPUT_FOUR_ARG_CONTAINER(Sequence) \
  110. template<class T1, class T2, class T3, class T4> \
  111. inline std::ostream& operator<<(std::ostream& out, \
  112. const Sequence<T1, T2, T3, T4>& seq) { \
  113. google::PrintSequence(out, seq.begin(), seq.end()); \
  114. return out; \
  115. }
  116. OUTPUT_FOUR_ARG_CONTAINER(std::map)
  117. OUTPUT_FOUR_ARG_CONTAINER(std::multimap)
  118. #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
  119. OUTPUT_FOUR_ARG_CONTAINER(std::unordered_set)
  120. OUTPUT_FOUR_ARG_CONTAINER(std::unordered_multiset)
  121. #endif
  122. #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
  123. OUTPUT_FOUR_ARG_CONTAINER(std::tr1::unordered_set)
  124. OUTPUT_FOUR_ARG_CONTAINER(std::tr1::unordered_multiset)
  125. #endif
  126. #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
  127. OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_set)
  128. OUTPUT_FOUR_ARG_CONTAINER(__gnu_cxx::hash_multiset)
  129. #endif
  130. #undef OUTPUT_FOUR_ARG_CONTAINER
  131. #define OUTPUT_FIVE_ARG_CONTAINER(Sequence) \
  132. template<class T1, class T2, class T3, class T4, class T5> \
  133. inline std::ostream& operator<<(std::ostream& out, \
  134. const Sequence<T1, T2, T3, T4, T5>& seq) { \
  135. google::PrintSequence(out, seq.begin(), seq.end()); \
  136. return out; \
  137. }
  138. #ifdef GLOG_STL_LOGGING_FOR_UNORDERED
  139. OUTPUT_FIVE_ARG_CONTAINER(std::unordered_map)
  140. OUTPUT_FIVE_ARG_CONTAINER(std::unordered_multimap)
  141. #endif
  142. #ifdef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
  143. OUTPUT_FIVE_ARG_CONTAINER(std::tr1::unordered_map)
  144. OUTPUT_FIVE_ARG_CONTAINER(std::tr1::unordered_multimap)
  145. #endif
  146. #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
  147. OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_map)
  148. OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap)
  149. #endif
  150. #undef OUTPUT_FIVE_ARG_CONTAINER
  151. template<class First, class Second>
  152. inline std::ostream& operator<<(std::ostream& out,
  153. const std::pair<First, Second>& p) {
  154. out << '(' << p.first << ", " << p.second << ')';
  155. return out;
  156. }
  157. namespace google {
  158. template<class Iter>
  159. inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
  160. // Output at most 100 elements -- appropriate if used for logging.
  161. for (int i = 0; begin != end && i < 100; ++i, ++begin) {
  162. if (i > 0) out << ' ';
  163. out << *begin;
  164. }
  165. if (begin != end) {
  166. out << " ...";
  167. }
  168. }
  169. }
  170. // Note that this is technically undefined behavior! We are adding things into
  171. // the std namespace for a reason though -- we are providing new operations on
  172. // types which are themselves defined with this namespace. Without this, these
  173. // operator overloads cannot be found via ADL. If these definitions are not
  174. // found via ADL, they must be #included before they're used, which requires
  175. // this header to be included before apparently independent other headers.
  176. //
  177. // For example, base/logging.h defines various template functions to implement
  178. // CHECK_EQ(x, y) and stream x and y into the log in the event the check fails.
  179. // It does so via the function template MakeCheckOpValueString:
  180. // template<class T>
  181. // void MakeCheckOpValueString(strstream* ss, const T& v) {
  182. // (*ss) << v;
  183. // }
  184. // Because 'glog/logging.h' is included before 'glog/stl_logging.h',
  185. // subsequent CHECK_EQ(v1, v2) for vector<...> typed variable v1 and v2 can only
  186. // find these operator definitions via ADL.
  187. //
  188. // Even this solution has problems -- it may pull unintended operators into the
  189. // namespace as well, allowing them to also be found via ADL, and creating code
  190. // that only works with a particular order of includes. Long term, we need to
  191. // move all of the *definitions* into namespace std, bet we need to ensure no
  192. // one references them first. This lets us take that step. We cannot define them
  193. // in both because that would create ambiguous overloads when both are found.
  194. namespace std { using ::operator<<; }
  195. #endif // UTIL_GTL_STL_LOGGING_INL_H_