Home | History | Annotate | Download | only in flatbuffers
      1 /*
      2  * Copyright 2017 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 #include "flatbuffers/flatbuffers.h"
     18 #include "flatbuffers/idl.h"
     19 #include "flatbuffers/util.h"
     20 #include <functional>
     21 #include <limits>
     22 #include <string>
     23 
     24 #ifndef FLATC_H_
     25 #define FLATC_H_
     26 
     27 namespace flatbuffers {
     28 
     29 class FlatCompiler {
     30  public:
     31   // Output generator for the various programming languages and formats we
     32   // support.
     33   struct Generator {
     34     typedef bool (*GenerateFn)(const flatbuffers::Parser &parser,
     35                                const std::string &path,
     36                                const std::string &file_name);
     37     typedef std::string (*MakeRuleFn)(const flatbuffers::Parser &parser,
     38                                       const std::string &path,
     39                                       const std::string &file_name);
     40 
     41     GenerateFn generate;
     42     const char *generator_opt_short;
     43     const char *generator_opt_long;
     44     const char *lang_name;
     45     bool schema_only;
     46     GenerateFn generateGRPC;
     47     flatbuffers::IDLOptions::Language lang;
     48     const char *generator_help;
     49     MakeRuleFn make_rule;
     50   };
     51 
     52   typedef void (*WarnFn)(const FlatCompiler *flatc,
     53                          const std::string &warn,
     54                          bool show_exe_name);
     55 
     56   typedef void (*ErrorFn)(const FlatCompiler *flatc,
     57                           const std::string &err,
     58                           bool usage, bool show_exe_name);
     59 
     60   // Parameters required to initialize the FlatCompiler.
     61   struct InitParams {
     62     InitParams()
     63         : generators(nullptr),
     64           num_generators(0),
     65           warn_fn(nullptr),
     66           error_fn(nullptr) {}
     67 
     68     const Generator* generators;
     69     size_t num_generators;
     70     WarnFn warn_fn;
     71     ErrorFn error_fn;
     72   };
     73 
     74   explicit FlatCompiler(const InitParams& params) : params_(params) {}
     75 
     76   int Compile(int argc, const char** argv);
     77 
     78   std::string GetUsageString(const char* program_name) const;
     79 
     80  private:
     81   void ParseFile(flatbuffers::Parser &parser,
     82                  const std::string &filename,
     83                  const std::string &contents,
     84                  std::vector<const char *> &include_directories) const;
     85 
     86   void Warn(const std::string &warn, bool show_exe_name = true) const;
     87 
     88   void Error(const std::string &err, bool usage = true,
     89              bool show_exe_name = true) const;
     90 
     91   InitParams params_;
     92 };
     93 
     94 
     95 }  // namespace flatbuffers
     96 
     97 #endif  // FLATC_H_
     98