Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / clang / blink_gc_plugin / JsonWriter.h
blob54a87aae59e7cbf69fd48f345b977892088d51ba
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 // Helper to write information for the points-to graph.
11 class JsonWriter {
12 public:
13 static JsonWriter* from(llvm::raw_fd_ostream* os) {
14 return os ? new JsonWriter(os) : 0;
16 ~JsonWriter() {
17 os_.close();
19 void OpenList() {
20 Separator();
21 os_ << "[";
22 state_.push(false);
24 void OpenList(const std::string key) {
25 Write(key);
26 os_ << ":";
27 OpenList();
29 void CloseList() {
30 os_ << "]";
31 state_.pop();
33 void OpenObject() {
34 Separator();
35 os_ << "{";
36 state_.push(false);
38 void CloseObject() {
39 os_ << "}\n";
40 state_.pop();
42 void Write(const size_t val) {
43 Separator();
44 os_ << val;
46 void Write(const std::string val) {
47 Separator();
48 os_ << "\"" << val << "\"";
50 void Write(const std::string key, const size_t val) {
51 Separator();
52 os_ << "\"" << key << "\":" << val;
54 void Write(const std::string key, const std::string val) {
55 Separator();
56 os_ << "\"" << key << "\":\"" << val << "\"";
58 private:
59 JsonWriter(llvm::raw_fd_ostream* os) : os_(*os) {}
60 void Separator() {
61 if (state_.empty())
62 return;
63 if (state_.top()) {
64 os_ << ",";
65 return;
67 state_.top() = true;
69 llvm::raw_fd_ostream& os_;
70 std::stack<bool> state_;
73 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_