MCPcopy Create free account
hub / github.com/bytedance/Fastbot_Android / CodeWriter

Class CodeWriter

native/thirdpart/flatbuffers/code_generators.h:40–90  ·  view source on GitHub ↗

Utility class to assist in generating code through use of text templates. Example code: CodeWriter code("\t"); code.SetValue("NAME", "Foo"); code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }"; code.SetValue("NAME", "Bar"); code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }"; std::cout << code.ToString() << std::endl; Output: void Foo() { printf("%s", "Foo"); } void Bar() { printf("%s",

Source from the content-addressed store, hash-verified

38// void Foo() { printf("%s", "Foo"); }
39// void Bar() { printf("%s", "Bar"); }
40class CodeWriter {
41 public:
42 CodeWriter(std::string pad = std::string())
43 : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false) {}
44
45 // Clears the current "written" code.
46 void Clear() {
47 stream_.str("");
48 stream_.clear();
49 }
50
51 // Associates a key with a value. All subsequent calls to operator+=, where
52 // the specified key is contained in {{ and }} delimiters will be replaced by
53 // the given value.
54 void SetValue(const std::string &key, const std::string &value) {
55 value_map_[key] = value;
56 }
57
58 std::string GetValue(const std::string &key) const {
59 const auto it = value_map_.find(key);
60 return it == value_map_.end() ? "" : it->second;
61 }
62
63 // Appends the given text to the generated code as well as a newline
64 // character. Any text within {{ and }} delimiters is replaced by values
65 // previously stored in the CodeWriter by calling SetValue above. The newline
66 // will be suppressed if the text ends with the \\ character.
67 void operator+=(std::string text);
68
69 // Returns the current contents of the CodeWriter as a std::string.
70 std::string ToString() const { return stream_.str(); }
71
72 // Increase ident level for writing code
73 void IncrementIdentLevel() { cur_ident_lvl_++; }
74 // Decrease ident level for writing code
75 void DecrementIdentLevel() {
76 if (cur_ident_lvl_) cur_ident_lvl_--;
77 }
78
79 void SetPadding(const std::string &padding) { pad_ = padding; }
80
81 private:
82 std::map<std::string, std::string> value_map_;
83 std::stringstream stream_;
84 std::string pad_;
85 int cur_ident_lvl_;
86 bool ignore_ident_;
87
88 // Add ident padding (tab or space) based on ident level
89 void AppendIdent(std::stringstream &stream);
90};
91
92class BaseGenerator {
93 public:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected