1 /* 2 * Copyright 2023 Google Inc. All rights reserved. 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 17 #ifndef FLATBUFFERS_CODE_GENERATOR_H_ 18 #define FLATBUFFERS_CODE_GENERATOR_H_ 19 20 #include <string> 21 22 #include "flatbuffers/idl.h" 23 24 namespace flatbuffers { 25 26 struct CodeGenOptions { 27 std::string output_path; 28 }; 29 30 // A code generator interface for producing converting flatbuffer schema into 31 // code. 32 class CodeGenerator { 33 public: 34 virtual ~CodeGenerator() = default; 35 36 enum Status { 37 OK = 0, 38 ERROR = 1, 39 FAILED_VERIFICATION = 2, 40 NOT_IMPLEMENTED = 3 41 }; 42 43 std::string status_detail; 44 45 // Generate code from the provided `parser`. 46 // 47 // DEPRECATED: prefer using the other overload of GenerateCode for bfbs. 48 virtual Status GenerateCode(const Parser &parser, const std::string &path, 49 const std::string &filename) = 0; 50 51 // Generate code from the provided `parser` and place it in the output. GenerateCodeString(const Parser & parser,const std::string & filename,std::string & output)52 virtual Status GenerateCodeString(const Parser &parser, 53 const std::string &filename, 54 std::string &output) { 55 (void)parser; 56 (void)filename; 57 (void)output; 58 return Status::NOT_IMPLEMENTED; 59 } 60 61 // Generate code from the provided `buffer` of given `length`. The buffer is a 62 // serialized reflection.fbs. 63 virtual Status GenerateCode(const uint8_t *buffer, int64_t length, 64 const CodeGenOptions &options) = 0; 65 66 virtual Status GenerateMakeRule(const Parser &parser, const std::string &path, 67 const std::string &filename, 68 std::string &output) = 0; 69 70 virtual Status GenerateGrpcCode(const Parser &parser, const std::string &path, 71 const std::string &filename) = 0; 72 73 virtual Status GenerateRootFile(const Parser &parser, 74 const std::string &path) = 0; 75 76 virtual bool IsSchemaOnly() const = 0; 77 78 virtual bool SupportsBfbsGeneration() const = 0; 79 80 virtual bool SupportsRootFileGeneration() const = 0; 81 82 virtual IDLOptions::Language Language() const = 0; 83 84 virtual std::string LanguageName() const = 0; 85 86 protected: 87 CodeGenerator() = default; 88 89 private: 90 // Copying is not supported. 91 CodeGenerator(const CodeGenerator &) = delete; 92 CodeGenerator &operator=(const CodeGenerator &) = delete; 93 }; 94 95 } // namespace flatbuffers 96 97 #endif // FLATBUFFERS_CODE_GENERATOR_H_ 98