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