Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / tools / clang / blink_gc_plugin / JsonWriter.h
blob09504b3aaf9feb7e011d0740959aab6c61624a89
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
3 // found in the LICENSE file.
5 #ifndef TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
8 #include "llvm/Support/raw_ostream.h"
10 // TODO(hans): Remove this #ifdef after Clang is rolled past r234897.
11 #ifdef LLVM_FORCE_HEAD_REVISION
12 #define JSON_WRITER_STREAM std::unique_ptr<llvm::raw_ostream>
13 #else
14 #define JSON_WRITER_STREAM llvm::raw_fd_ostream*
15 #endif
17 // Helper to write information for the points-to graph.
18 class JsonWriter {
19 public:
20 static JsonWriter* from(JSON_WRITER_STREAM os) {
21 return os ? new JsonWriter(std::move(os)) : 0;
23 #ifndef LLVM_FORCE_HEAD_REVISION
24 ~JsonWriter() {
25 delete os_;
27 #endif
28 void OpenList() {
29 Separator();
30 *os_ << "[";
31 state_.push(false);
33 void OpenList(const std::string key) {
34 Write(key);
35 *os_ << ":";
36 OpenList();
38 void CloseList() {
39 *os_ << "]";
40 state_.pop();
42 void OpenObject() {
43 Separator();
44 *os_ << "{";
45 state_.push(false);
47 void CloseObject() {
48 *os_ << "}\n";
49 state_.pop();
51 void Write(const size_t val) {
52 Separator();
53 *os_ << val;
55 void Write(const std::string val) {
56 Separator();
57 *os_ << "\"" << val << "\"";
59 void Write(const std::string key, const size_t val) {
60 Separator();
61 *os_ << "\"" << key << "\":" << val;
63 void Write(const std::string key, const std::string val) {
64 Separator();
65 *os_ << "\"" << key << "\":\"" << val << "\"";
67 private:
68 JsonWriter(JSON_WRITER_STREAM os) : os_(std::move(os)) {}
69 void Separator() {
70 if (state_.empty())
71 return;
72 if (state_.top()) {
73 *os_ << ",";
74 return;
76 state_.top() = true;
78 JSON_WRITER_STREAM os_;
79 std::stack<bool> state_;
82 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_