OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_SYNC_UTIL_FAST_DUMP_H_ | |
6 #define CHROME_BROWSER_SYNC_UTIL_FAST_DUMP_H_ | |
7 #pragma once | |
8 | |
9 #include <ostream> | |
10 #include <streambuf> | |
11 #include <string> | |
12 | |
13 #include "base/string_number_conversions.h" | |
14 | |
15 using std::ostream; | |
16 using std::streambuf; | |
17 using std::string; | |
18 | |
19 // This seems totally gratuitous, and it would be if std::ostream was fast, but | |
20 // std::ostream is slow. It's slow because it creates a ostream::sentry (mutex) | |
21 // for every little << operator. When you want to dump a whole lot of stuff | |
22 // under a single mutex lock, use this FastDump class. | |
23 namespace browser_sync { | |
24 class FastDump { | |
25 public: | |
26 explicit FastDump(ostream* outs) : sentry_(*outs), out_(outs->rdbuf()) { | |
27 } | |
28 ostream::sentry sentry_; | |
29 streambuf* const out_; | |
30 }; | |
31 | |
32 inline FastDump& operator << (FastDump& dump, int64 n) { | |
33 string numbuf(base::Int64ToString(n)); | |
34 const char* number = numbuf.c_str(); | |
35 dump.out_->sputn(number, numbuf.length()); | |
36 return dump; | |
37 } | |
38 | |
39 inline FastDump& operator << (FastDump& dump, int32 n) { | |
40 string numbuf(base::IntToString(n)); | |
41 const char* number = numbuf.c_str(); | |
42 dump.out_->sputn(number, numbuf.length()); | |
43 return dump; | |
44 } | |
45 | |
46 inline FastDump& operator << (FastDump& dump, const char* s) { | |
47 dump.out_->sputn(s, strlen(s)); | |
48 return dump; | |
49 } | |
50 | |
51 inline FastDump& operator << (FastDump& dump, const string& s) { | |
52 dump.out_->sputn(s.data(), s.size()); | |
53 return dump; | |
54 } | |
55 | |
56 } // namespace browser_sync | |
57 | |
58 #endif // CHROME_BROWSER_SYNC_UTIL_FAST_DUMP_H_ | |
OLD | NEW |