has_method support for primitive fields in java runtime. Changed: idl.h, FlatBufferBu...
authorEdward <ed.foux@gmail.com>
Mon, 19 Aug 2019 19:46:48 +0000 (12:46 -0700)
committerWouter van Oortmerssen <aardappel@gmail.com>
Mon, 19 Aug 2019 19:46:48 +0000 (12:46 -0700)
* has_method support for primitive fields in java runtime

* adding the new flag to flatc

* addressing the review comments

include/flatbuffers/idl.h
java/com/google/flatbuffers/FlatBufferBuilder.java
src/flatc.cpp
src/idl_gen_general.cpp
src/idl_parser.cpp

index 44e3566..fe68b0d 100644 (file)
@@ -528,6 +528,7 @@ struct IDLOptions {
   bool size_prefixed;
   std::string root_type;
   bool force_defaults;
+  bool java_primitive_has_method;
   std::vector<std::string> cpp_includes;
 
   // Possible options for the more general generator below.
@@ -602,6 +603,7 @@ struct IDLOptions {
         protobuf_ascii_alike(false),
         size_prefixed(false),
         force_defaults(false),
+        java_primitive_has_method(false),
         lang(IDLOptions::kJava),
         mini_reflect(IDLOptions::kNone),
         lang_to_generate(0),
@@ -927,6 +929,8 @@ class Parser : public ParserState {
 
 extern std::string MakeCamel(const std::string &in, bool first = true);
 
+extern std::string MakeScreamingCamel(const std::string &in);
+
 // Generate text (JSON) from a given FlatBuffer, and a given Parser
 // object that has been populated with the corresponding schema.
 // If ident_step is 0, no indentation will be generated. Additionally,
index f224610..4fe1c42 100644 (file)
@@ -199,6 +199,17 @@ public class FlatBufferBuilder {
         }
     }
 
+   /**
+   * Helper function to test if a field is present in the table
+   *
+   * @param table Flatbuffer table
+   * @param offset virtual table offset
+   * @return true if the filed is present
+   */
+   public static boolean isFieldPresent(Table table, int offset) {
+     return table.__offset(offset) != 0;
+   }
+
     /**
      * Reset the FlatBufferBuilder by purging all data that it holds.
      */
index e1236bd..99a442d 100644 (file)
@@ -319,6 +319,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
         opts.force_defaults = true;
       } else if (arg == "--force-empty") {
         opts.set_empty_to_null = false;
+      } else if (arg == "--java-primitive-has-method") {
+        opts.java_primitive_has_method = true;
       } else {
         for (size_t i = 0; i < params_.num_generators; ++i) {
           if (arg == params_.generators[i].generator_opt_long ||
index 8dca792..8994e55 100644 (file)
@@ -1323,6 +1323,15 @@ class GeneralGenerator : public BaseGenerator {
           }
         }
       }
+      if (parser_.opts.java_primitive_has_method &&
+          IsScalar(field.value.type.base_type) && !struct_def.fixed) {
+        auto vt_offset_constant = "  public static final int VT_" +
+                                  MakeScreamingCamel(field.name) + " = " +
+                                  NumToString(field.value.offset) + ";";
+
+        code += vt_offset_constant;
+        code += "\n";
+      }
     }
     code += "\n";
     flatbuffers::FieldDef *key_field = nullptr;
index 31b315c..c906d82 100644 (file)
@@ -102,6 +102,18 @@ std::string MakeCamel(const std::string &in, bool first) {
   return s;
 }
 
+// Convert an underscore_based_identifier in to screaming snake case.
+std::string MakeScreamingCamel(const std::string &in) {
+  std::string s;
+  for (size_t i = 0; i < in.length(); i++) {
+    if (in[i] != '_')
+      s += static_cast<char>(toupper(in[i]));
+    else
+      s += in[i];
+  }
+  return s;
+}
+
 void DeserializeDoc( std::vector<std::string> &doc,
                      const Vector<Offset<String>> *documentation) {
   if (documentation == nullptr) return;