option to generate one file for C#
authorAmol Deshpande <amol.deshpande@outlook.com>
Sun, 7 Jun 2015 02:35:12 +0000 (19:35 -0700)
committerAmol Deshpande <amol.deshpande@outlook.com>
Sun, 7 Jun 2015 02:35:12 +0000 (19:35 -0700)
include/flatbuffers/idl.h
src/flatc.cpp
src/idl_gen_general.cpp

index 2e9cfb5..016066f 100644 (file)
@@ -406,6 +406,7 @@ struct GeneratorOptions {
   bool prefixed_enums;
   bool include_dependence_headers;
   bool mutable_buffer;
+  bool one_file;
 
   // Possible options for the more general generator below.
   enum Language { kJava, kCSharp, kGo, kMAX };
@@ -418,6 +419,7 @@ struct GeneratorOptions {
                        output_enum_identifiers(true), prefixed_enums(true),
                        include_dependence_headers(false),
                        mutable_buffer(false),
+                       one_file(false),
                        lang(GeneratorOptions::kJava) {}
 };
 
index 5eb0fda..a332a53 100755 (executable)
@@ -93,6 +93,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
       "  --gen-includes  Generate include statements for included schemas the\n"
       "                  generated file depends on (C++).\n"
       "  --gen-mutable   Generate accessors that can mutate buffers in-place.\n"
+      "  --gen-onefile   Generate single output file for C#\n"
       "  --raw-binary    Allow binaries without file_indentifier to be read.\n"
       "                  This may crash flatc given a mismatched schema.\n"
       "  --proto         Input is a .proto, translate to .fbs.\n"
@@ -140,7 +141,9 @@ int main(int argc, const char *argv[]) {
         opts.mutable_buffer = true;
       } else if(arg == "--gen-includes") {
         opts.include_dependence_headers = true;
-      } else if(arg == "--raw-binary") {
+      }else if (arg == "--gen-onefile") {
+        opts.one_file = true;
+      }else if (arg == "--raw-binary") {
         raw_binary = true;
       } else if(arg == "--") {  // Separator between text and binary inputs.
         binary_files_from = filenames.size();
index 9e34a2e..268de64 100644 (file)
@@ -858,8 +858,8 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
 // Save out the generated code for a single class while adding
 // declaration boilerplate.
 static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
-                      const Definition &def, const std::string &classcode,
-                      const std::string &path, bool needs_includes) {
+                      const std::string &defname, const std::string &classcode,
+                      const std::string &path, bool needs_includes, bool onefile) {
   if (!classcode.length()) return true;
 
   std::string namespace_general;
@@ -870,7 +870,10 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
       namespace_general += ".";
     }
     namespace_general += *it;
-    namespace_dir += *it + kPathSeparator;
+    if (!onefile) {
+      namespace_dir += *it + kPathSeparator;
+    }
+
   }
   EnsureDirExists(namespace_dir);
 
@@ -880,34 +883,48 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
   if (needs_includes) code += lang.includes;
   code += classcode;
   code += lang.namespace_end;
-  auto filename = namespace_dir + def.name + lang.file_extension;
+  auto filename = namespace_dir + defname + lang.file_extension;
   return SaveFile(filename.c_str(), code, false);
 }
 
 bool GenerateGeneral(const Parser &parser,
                      const std::string &path,
-                     const std::string & /*file_name*/,
+                     const std::string & file_name,
                      const GeneratorOptions &opts) {
 
   assert(opts.lang <= GeneratorOptions::kMAX);
   auto lang = language_parameters[opts.lang];
+  std::string one_file_code;
 
   for (auto it = parser.enums_.vec.begin();
        it != parser.enums_.vec.end(); ++it) {
     std::string enumcode;
     GenEnum(lang, **it, &enumcode);
-    if (!SaveClass(lang, parser, **it, enumcode, path, false))
-      return false;
+    if (opts.one_file) {
+      one_file_code = enumcode;
+    }
+    else {
+      if (!SaveClass(lang, parser, (**it).name, enumcode, path, false, false))
+        return false;
+    }
   }
 
   for (auto it = parser.structs_.vec.begin();
        it != parser.structs_.vec.end(); ++it) {
     std::string declcode;
     GenStruct(lang, parser, **it, &declcode);
-    if (!SaveClass(lang, parser, **it, declcode, path, true))
-      return false;
+    if (opts.one_file) {
+      one_file_code += declcode;
+    }
+    else {
+      if (!SaveClass(lang, parser, (**it).name, declcode, path, true, false))
+        return false;
+    }
   }
 
+  if (opts.one_file) {
+    return SaveClass(lang, parser, file_name, one_file_code,path, true, true);
+  }
   return true;
 }