Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: base/logging.h

Issue 2001783002: base: Support using (D)CHECK on scoped enums. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/logging.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_LOGGING_H_ 5 #ifndef BASE_LOGGING_H_
6 #define BASE_LOGGING_H_ 6 #define BASE_LOGGING_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <cassert> 10 #include <cassert>
11 #include <cstring> 11 #include <cstring>
12 #include <sstream> 12 #include <sstream>
13 #include <string> 13 #include <string>
14 #include <type_traits>
15 #include <utility>
14 16
15 #include "base/base_export.h" 17 #include "base/base_export.h"
16 #include "base/debug/debugger.h" 18 #include "base/debug/debugger.h"
17 #include "base/macros.h" 19 #include "base/macros.h"
18 #include "build/build_config.h" 20 #include "build/build_config.h"
19 21
20 // 22 //
21 // Optional message capabilities 23 // Optional message capabilities
22 // ----------------------------- 24 // -----------------------------
23 // Assertion failed messages and fatal errors are displayed in a dialog box 25 // Assertion failed messages and fatal errors are displayed in a dialog box
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 switch (0) case 0: default: \ 514 switch (0) case 0: default: \
513 if (logging::CheckOpResult true_if_passed = \ 515 if (logging::CheckOpResult true_if_passed = \
514 logging::Check##name##Impl((val1), (val2), \ 516 logging::Check##name##Impl((val1), (val2), \
515 #val1 " " #op " " #val2)) \ 517 #val1 " " #op " " #val2)) \
516 ; \ 518 ; \
517 else \ 519 else \
518 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() 520 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream()
519 521
520 #endif // !(OFFICIAL_BUILD && NDEBUG) 522 #endif // !(OFFICIAL_BUILD && NDEBUG)
521 523
524 // Uses expression SFINAE to detect whether using operator<< would work.
525 template <typename T, typename = void>
526 struct SupportsOstreamOperator : std::false_type {};
danakj 2016/05/20 21:54:16 Can you move this out of logging.h. template_util.
jbroman 2016/05/25 19:08:04 Put it in base::internal since it's internal to ba
527 template <typename T>
528 struct SupportsOstreamOperator<T,
529 decltype(void(std::declval<std::ostream&>()
jbroman 2016/05/20 19:56:35 "void" here because the type has to match the one
530 << std::declval<T>()))>
531 : std::true_type {};
532
522 // This formats a value for a failing CHECK_XX statement. Ordinarily, 533 // This formats a value for a failing CHECK_XX statement. Ordinarily,
523 // it uses the definition for operator<<, with a few special cases below. 534 // it uses the definition for operator<<, with a few special cases below.
535
524 template <typename T> 536 template <typename T>
525 inline void MakeCheckOpValueString(std::ostream* os, const T& v) { 537 inline typename std::enable_if<SupportsOstreamOperator<const T&>::value,
538 void>::type
539 MakeCheckOpValueString(std::ostream* os, const T& v) {
526 (*os) << v; 540 (*os) << v;
527 } 541 }
528 542
529 // We need an explicit specialization for std::nullptr_t. 543 // We need overloads for enums that don't support operator<<.
530 template <> 544 // (i.e. scoped enums where no operator<< overload was declared).
531 BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, 545 template <typename T>
532 const std::nullptr_t& p); 546 inline typename std::enable_if<!SupportsOstreamOperator<const T&>::value &&
danakj 2016/05/20 21:54:16 Random horrible thought: what would go wrong if we
jbroman 2016/05/25 19:08:04 We'd have to change the above condition to !std::i
547 std::is_enum<T>::value,
548 void>::type
549 MakeCheckOpValueString(std::ostream* os, const T& v) {
550 // TODO(jbroman): Use std::underlying_type here, once it's available in all
551 // our STL implementations.
552 static_assert(sizeof(T) <= sizeof(int64_t),
jbroman 2016/05/20 19:56:35 64 bits ought to be enough for anybody. :)
553 "enum can't be losslessly printed as int64_t");
554 (*os) << static_cast<int64_t>(v);
dcheng 2016/05/20 19:59:30 What if the enum's underlying type is uint64_t? =)
jbroman 2016/05/20 20:05:38 It'll do the obvious thing (values over 2^63 will
danakj 2016/05/20 21:08:23 Oh, that might actually be nice, I didn't know __u
jbroman 2016/05/25 19:08:04 Done, with the same semi-hacky condition Skia uses
555 }
556
557 // We need an explicit overload for std::nullptr_t.
558 BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p);
533 559
534 // Build the error message string. This is separate from the "Impl" 560 // Build the error message string. This is separate from the "Impl"
535 // function template because it is not performance critical and so can 561 // function template because it is not performance critical and so can
536 // be out of line, while the "Impl" code should be inline. Caller 562 // be out of line, while the "Impl" code should be inline. Caller
537 // takes ownership of the returned string. 563 // takes ownership of the returned string.
538 template<class t1, class t2> 564 template<class t1, class t2>
539 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { 565 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
540 std::ostringstream ss; 566 std::ostringstream ss;
541 ss << names << " ("; 567 ss << names << " (";
542 MakeCheckOpValueString(&ss, v1); 568 MakeCheckOpValueString(&ss, v1);
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 #elif NOTIMPLEMENTED_POLICY == 5 1005 #elif NOTIMPLEMENTED_POLICY == 5
980 #define NOTIMPLEMENTED() do {\ 1006 #define NOTIMPLEMENTED() do {\
981 static bool logged_once = false;\ 1007 static bool logged_once = false;\
982 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1008 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
983 logged_once = true;\ 1009 logged_once = true;\
984 } while(0);\ 1010 } while(0);\
985 EAT_STREAM_PARAMETERS 1011 EAT_STREAM_PARAMETERS
986 #endif 1012 #endif
987 1013
988 #endif // BASE_LOGGING_H_ 1014 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698