blob: d890d52118d43621c33792e354dbcdb47b0cd7dc [file] [log] [blame]
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070018
19#include <memory>
Jeongik Cha9a7f21f2019-02-13 14:42:47 +090020#include <ostream>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070021#include <string>
22
23#include <stdio.h>
24
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/macros.h>
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070026
27namespace android {
28namespace aidl {
29
Jiyong Parkb78e15b2018-07-04 20:31:03 +090030class CodeWriter;
31using CodeWriterPtr = std::unique_ptr<CodeWriter>;
32
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070033class CodeWriter {
34 public:
Jiyong Parkb78e15b2018-07-04 20:31:03 +090035 // Get a CodeWriter that writes to a file. When filename is "-",
36 // it is written to stdout.
37 static CodeWriterPtr ForFile(const std::string& filename);
38 // Get a CodeWriter that writes to a string buffer.
39 // The buffer gets updated only after Close() is called or the CodeWriter
40 // is deleted -- much like a real file.
41 static CodeWriterPtr ForString(std::string* buf);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070042 // Write a formatted string to this writer in the usual printf sense.
43 // Returns false on error.
Steven Morelandd3721d62019-12-09 13:04:16 -080044 virtual bool Write(const char* format, ...) __attribute__((format(printf, 2, 3)));
Steven Moreland201a3162019-03-06 12:04:46 -080045 void Indent();
46 void Dedent();
Jiyong Parkb78e15b2018-07-04 20:31:03 +090047 virtual bool Close();
48 virtual ~CodeWriter() = default;
49 CodeWriter() = default;
50
Jiyong Park22b4ecd2018-07-23 23:34:42 +090051 CodeWriter& operator<<(const char* s);
52 CodeWriter& operator<<(const std::string& str);
53
Jiyong Parka755dc72018-06-29 13:52:24 +090054 private:
Jiyong Parkb78e15b2018-07-04 20:31:03 +090055 CodeWriter(std::unique_ptr<std::ostream> ostream);
Jiyong Parka755dc72018-06-29 13:52:24 +090056 std::string ApplyIndent(const std::string& str);
Jiyong Parkb78e15b2018-07-04 20:31:03 +090057 const std::unique_ptr<std::ostream> ostream_;
Jiyong Parka755dc72018-06-29 13:52:24 +090058 int indent_level_ {0};
59 bool start_of_line_ {true};
Jiyong Parkb78e15b2018-07-04 20:31:03 +090060};
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070061
62} // namespace aidl
63} // namespace android