Home | History | Annotate | Download | only in cc
      1 /* Copyright 2017 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 
     16 #ifndef TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_
     17 #define TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_
     18 
     19 #include <string>
     20 
     21 #include "tensorflow/core/lib/core/stringpiece.h"
     22 #include "tensorflow/core/platform/env.h"
     23 
     24 namespace tensorflow {
     25 
     26 // A utility class for writing source code, normally generated at
     27 // compile-time.
     28 //
     29 // Source writers are language-agnostic and therefore only expose generic
     30 // methods common to most languages. Extend or wrap this class to implement
     31 // language-specific features.
     32 //
     33 // Note: if you are looking to reuse this class for generating code in another
     34 // language than Java, please do by moving it at the '//tensorflow/core/lib/io'
     35 // level.
     36 class SourceWriter {
     37  public:
     38   virtual ~SourceWriter() = default;
     39 
     40   // Returns true if the writer is at the beginnig of a new line
     41   bool newline() const { return newline_; }
     42 
     43   // Appends a piece of code or text.
     44   //
     45   // It is expected that no newline character is present in the data provided,
     46   // otherwise Write() must be used.
     47   SourceWriter& Append(const StringPiece& str);
     48 
     49   // Writes a block of code or text.
     50   //
     51   // The data might potentially contain newline characters, therefore it will
     52   // be scanned to ensure that each line is indented and prefixed properly,
     53   // making it a bit slower than Append().
     54   SourceWriter& Write(const string& text);
     55 
     56   // Appends a newline character and start writing on a new line.
     57   SourceWriter& EndLine();
     58 
     59   // Indents following lines with white spaces.
     60   //
     61   // Indentation is cumulative, i.e. the provided tabulation is added to the
     62   // current indentation value. If the tabulation is negative, the operation
     63   // will outdent the source code, until the indentation reaches 0 again.
     64   //
     65   // For example, calling Indent(2) twice will indent code with 4 white
     66   // spaces. Then calling Indent(-2) will outdent the code back to 2 white
     67   // spaces.
     68   SourceWriter& Indent(int tab);
     69 
     70   // Prefixes following lines with provided character(s).
     71   //
     72   // A common use case of a prefix is for commenting or documenting the code.
     73   //
     74   // The prefix is written after the indentation, For example, invoking
     75   // Indent(2)->Prefix("//") will result in prefixing lines with "  //".
     76   //
     77   // An empty value ("") will remove any line prefix that was previously set.
     78   SourceWriter& Prefix(const char* line_prefix) {
     79     line_prefix_ = line_prefix;
     80     return *this;
     81   }
     82 
     83  protected:
     84   virtual void DoAppend(const StringPiece& str) = 0;
     85 
     86  private:
     87   string left_margin_;
     88   string line_prefix_;
     89   bool newline_ = true;
     90 };
     91 
     92 // A writer that outputs source code into a file.
     93 //
     94 // Note: the writer does not acquire the ownership of the file being passed in
     95 // parameter.
     96 class SourceFileWriter : public SourceWriter {
     97  public:
     98   explicit SourceFileWriter(WritableFile* file) : file_(file) {}
     99   virtual ~SourceFileWriter() = default;
    100 
    101  protected:
    102   void DoAppend(const StringPiece& str) override {
    103     TF_CHECK_OK(file_->Append(str));
    104   }
    105 
    106  private:
    107   WritableFile* file_;
    108 };
    109 
    110 // A writer that outputs source code into a string buffer.
    111 class SourceBufferWriter : public SourceWriter {
    112  public:
    113   SourceBufferWriter() : owns_buffer_(true), buffer_(new string()) {}
    114   explicit SourceBufferWriter(string* buffer)
    115       : owns_buffer_(false), buffer_(buffer) {}
    116   virtual ~SourceBufferWriter() {
    117     if (owns_buffer_) delete buffer_;
    118   }
    119   const string& str() { return *buffer_; }
    120 
    121  protected:
    122   void DoAppend(const StringPiece& str) override {
    123     buffer_->append(str.begin(), str.end());
    124   }
    125 
    126  private:
    127   bool owns_buffer_;
    128   string* buffer_;
    129 };
    130 
    131 }  // namespace tensorflow
    132 
    133 #endif  // TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_
    134