OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 5 #ifndef TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
7 | 7 |
8 #include "llvm/Support/raw_ostream.h" | 8 #include "llvm/Support/raw_ostream.h" |
9 | 9 |
10 // Helper to write information for the points-to graph. | 10 // Helper to write information for the points-to graph. |
11 class JsonWriter { | 11 class JsonWriter { |
12 public: | 12 public: |
13 static JsonWriter* from(std::unique_ptr<llvm::raw_ostream> os) { | 13 static JsonWriter* from(std::unique_ptr<llvm::raw_ostream> os) { |
14 return os ? new JsonWriter(std::move(os)) : 0; | 14 return os ? new JsonWriter(std::move(os)) : 0; |
15 } | 15 } |
16 void OpenList() { | 16 void OpenList() { |
17 Separator(); | 17 Separator(); |
18 *os_ << "["; | 18 *os_ << "["; |
19 state_.push(false); | 19 state_.push(false); |
20 } | 20 } |
21 void OpenList(const std::string key) { | 21 void OpenList(const std::string& key) { |
22 Write(key); | 22 Write(key); |
23 *os_ << ":"; | 23 *os_ << ":"; |
24 OpenList(); | 24 OpenList(); |
25 } | 25 } |
26 void CloseList() { | 26 void CloseList() { |
27 *os_ << "]"; | 27 *os_ << "]"; |
28 state_.pop(); | 28 state_.pop(); |
29 } | 29 } |
30 void OpenObject() { | 30 void OpenObject() { |
31 Separator(); | 31 Separator(); |
32 *os_ << "{"; | 32 *os_ << "{"; |
33 state_.push(false); | 33 state_.push(false); |
34 } | 34 } |
35 void CloseObject() { | 35 void CloseObject() { |
36 *os_ << "}\n"; | 36 *os_ << "}\n"; |
37 state_.pop(); | 37 state_.pop(); |
38 } | 38 } |
39 void Write(const size_t val) { | 39 void Write(const size_t val) { |
40 Separator(); | 40 Separator(); |
41 *os_ << val; | 41 *os_ << val; |
42 } | 42 } |
43 void Write(const std::string val) { | 43 void Write(const std::string& val) { |
44 Separator(); | 44 Separator(); |
45 *os_ << "\"" << val << "\""; | 45 *os_ << "\"" << val << "\""; |
46 } | 46 } |
47 void Write(const std::string key, const size_t val) { | 47 void Write(const std::string& key, const size_t val) { |
48 Separator(); | 48 Separator(); |
49 *os_ << "\"" << key << "\":" << val; | 49 *os_ << "\"" << key << "\":" << val; |
50 } | 50 } |
51 void Write(const std::string key, const std::string val) { | 51 void Write(const std::string& key, const std::string& val) { |
52 Separator(); | 52 Separator(); |
53 *os_ << "\"" << key << "\":\"" << val << "\""; | 53 *os_ << "\"" << key << "\":\"" << val << "\""; |
54 } | 54 } |
55 private: | 55 private: |
56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {} | 56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {} |
57 void Separator() { | 57 void Separator() { |
58 if (state_.empty()) | 58 if (state_.empty()) |
59 return; | 59 return; |
60 if (state_.top()) { | 60 if (state_.top()) { |
61 *os_ << ","; | 61 *os_ << ","; |
62 return; | 62 return; |
63 } | 63 } |
64 state_.top() = true; | 64 state_.top() = true; |
65 } | 65 } |
66 std::unique_ptr<llvm::raw_ostream> os_; | 66 std::unique_ptr<llvm::raw_ostream> os_; |
67 std::stack<bool> state_; | 67 std::stack<bool> state_; |
68 }; | 68 }; |
69 | 69 |
70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
OLD | NEW |