Skip to content

Commit 641fbe4

Browse files
authored
Refactor FlatC to receive FlatCOptions (#7770)
* Refactor FlatC to receive `FlatCOptions` * switch to c++11 unique_ptr
1 parent 5638a6a commit 641fbe4

File tree

4 files changed

+224
-147
lines changed

4 files changed

+224
-147
lines changed

include/flatbuffers/flatc.h

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <functional>
2121
#include <limits>
22+
#include <list>
2223
#include <string>
2324

2425
#include "flatbuffers/bfbs_generator.h"
@@ -31,6 +32,30 @@ namespace flatbuffers {
3132
extern void LogCompilerWarn(const std::string &warn);
3233
extern void LogCompilerError(const std::string &err);
3334

35+
struct FlatCOptions {
36+
IDLOptions opts;
37+
38+
std::string program_name;
39+
40+
std::string output_path;
41+
42+
std::vector<std::string> filenames;
43+
44+
std::list<std::string> include_directories_storage;
45+
std::vector<const char *> include_directories;
46+
std::vector<const char *> conform_include_directories;
47+
std::vector<bool> generator_enabled;
48+
size_t binary_files_from = std::numeric_limits<size_t>::max();
49+
std::string conform_to_schema;
50+
std::string annotate_schema;
51+
bool any_generator = false;
52+
bool print_make_rules = false;
53+
bool raw_binary = false;
54+
bool schema_binary = false;
55+
bool grpc_enabled = false;
56+
bool requires_bfbs = false;
57+
};
58+
3459
struct FlatCOption {
3560
std::string short_opt;
3661
std::string long_opt;
@@ -85,15 +110,18 @@ class FlatCompiler {
85110

86111
explicit FlatCompiler(const InitParams &params) : params_(params) {}
87112

88-
int Compile(int argc, const char **argv);
113+
int Compile(const FlatCOptions &options);
114+
115+
std::string GetShortUsageString(const std::string& program_name) const;
116+
std::string GetUsageString(const std::string& program_name) const;
89117

90-
std::string GetShortUsageString(const char *program_name) const;
91-
std::string GetUsageString(const char *program_name) const;
118+
// Parse the FlatC options from command line arguments.
119+
FlatCOptions ParseFromCommandLineArguments(int argc, const char **argv);
92120

93121
private:
94122
void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
95123
const std::string &contents,
96-
std::vector<const char *> &include_directories) const;
124+
const std::vector<const char *> &include_directories) const;
97125

98126
void LoadBinarySchema(Parser &parser, const std::string &filename,
99127
const std::string &contents);
@@ -105,9 +133,16 @@ class FlatCompiler {
105133

106134
void AnnotateBinaries(const uint8_t *binary_schema,
107135
uint64_t binary_schema_size,
108-
const std::string & schema_filename,
136+
const std::string &schema_filename,
109137
const std::vector<std::string> &binary_files);
110138

139+
void ValidateOptions(const FlatCOptions &options);
140+
141+
Parser GetConformParser(const FlatCOptions &options);
142+
143+
std::unique_ptr<Parser> GenerateCode(const FlatCOptions &options,
144+
Parser &conform_parser);
145+
111146
InitParams params_;
112147
};
113148

include/flatbuffers/idl.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ struct FieldDef : public Definition {
298298
presence(kDefault),
299299
nested_flatbuffer(nullptr),
300300
padding(0),
301-
sibling_union_field(nullptr){}
301+
sibling_union_field(nullptr) {}
302302

303303
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
304304
const Parser &parser) const;
@@ -803,7 +803,7 @@ struct ParserState {
803803
FLATBUFFERS_ASSERT(cursor_ && line_start_ && cursor_ >= line_start_);
804804
return static_cast<int64_t>(cursor_ - line_start_);
805805
}
806-
806+
807807
const char *prev_cursor_;
808808
const char *cursor_;
809809
const char *line_start_;
@@ -910,6 +910,13 @@ class Parser : public ParserState {
910910
known_attributes_["private"] = true;
911911
}
912912

913+
// Copying is not allowed
914+
Parser(const Parser &) = delete;
915+
Parser &operator=(const Parser &) = delete;
916+
917+
Parser(Parser &&) = default;
918+
Parser &operator=(Parser &&) = default;
919+
913920
~Parser() {
914921
for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
915922
delete *it;

0 commit comments

Comments
 (0)