xref: /aosp_15_r20/external/tensorflow/tensorflow/c/experimental/ops/gen/cpp/renderers/renderer.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_CPP_RENDERERS_RENDERER_H_
16 #define TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_CPP_RENDERERS_RENDERER_H_
17 
18 #include "absl/strings/substitute.h"
19 #include "tensorflow/c/experimental/ops/gen/cpp/renderers/renderer_context.h"
20 #include "tensorflow/core/platform/types.h"
21 
22 namespace tensorflow {
23 namespace generator {
24 namespace cpp {
25 
26 class Renderer {
27  public:
28   explicit Renderer(RendererContext context);
29 
30  protected:
31   // Append a blank line.
32   Renderer &BlankLine();
33 
34   // Append a line of source code, left-justified (not indented).
35   // Use for preprocessors directives ("#include"), namespaces, etc.
36   Renderer &CodeLine(const string &text);
37   template <typename... Args>
CodeLine(absl::string_view text,const Args &...args)38   Renderer CodeLine(absl::string_view text, const Args &...args) {
39     return CodeLine(absl::Substitute(text, args...));
40   }
41 
42   // Append a multiline string of source code, left-justified (not indented).
43   // Note: Trims leading/trailing whitespace including newlines, making this
44   //       method convenient for multiline raw strings.
45   // Newlines ('\n') are allowed/expected.
46   Renderer &CodeLines(const string &text);
47   template <typename... Args>
CodeLines(absl::string_view text,const Args &...args)48   Renderer CodeLines(absl::string_view text, const Args &...args) {
49     return CodeLines(absl::Substitute(text, args...));
50   }
51 
52   // Indent and append a C++ statement.
53   // Note: do *not* include a trailing semicolon in the statement text.
54   Renderer &Statement(const string &text);
55   template <typename... Args>
Statement(absl::string_view text,const Args &...args)56   Renderer Statement(absl::string_view text, const Args &...args) {
57     return Statement(absl::Substitute(text, args...));
58   }
59 
60   // Indent and append a call to a TF method returning a Status to check.
61   // Note: do *not* include a trailing semicolon in the statement text.
62   Renderer &TFStatement(const string &text);
63   template <typename... Args>
TFStatement(absl::string_view text,const Args &...args)64   Renderer TFStatement(absl::string_view text, const Args &...args) {
65     return TFStatement(absl::Substitute(text, args...));
66   }
67 
68   // Indent and append a C++ single-line style comment (using '//').
69   Renderer &CommentLine(const string &text = "");
70   template <typename... Args>
CommentLine(absl::string_view text,const Args &...args)71   Renderer CommentLine(absl::string_view text, const Args &...args) {
72     return CommentLine(absl::Substitute(text, args...));
73   }
74 
75   // Append a line of code which starts a new block: trailing with '{') and
76   // indenting.
77   Renderer &BlockOpen(const string &text);
78   template <typename... Args>
BlockOpen(absl::string_view text,const Args &...args)79   Renderer BlockOpen(absl::string_view text, const Args &...args) {
80     return BlockOpen(absl::Substitute(text, args...));
81   }
82 
83   // Append a line of code ending a block: unindenting and adding '}'.
84   // Note: optional trailing text is often a comment, e.g. '// namespace xyz'.
85   Renderer &BlockClose(const string &text = "");
86   template <typename... Args>
BlockClose(absl::string_view text,const Args &...args)87   Renderer BlockClose(absl::string_view text, const Args &...args) {
88     return BlockClose(absl::Substitute(text, args...));
89   }
90 
91  protected:
92   RendererContext context_;
93 };
94 
95 }  // namespace cpp
96 }  // namespace generator
97 }  // namespace tensorflow
98 
99 #endif  // TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_CPP_RENDERERS_RENDERER_H_
100