Commit 6df9e1c5 authored by reynolma2's avatar reynolma2 Committed by Wouter van Oortmerssen

Update idl_gen_general.cpp

There is a bug in creating a C# table when it includes a field of type 'BOOL'. The problem is that the generate C# code is as follows:
  "bool SampleValue = 0;"
This will fail to compile, because in C# this fails, it needs to be generated as:
 "bool SampleValue = false;"

The error is in line ~510

Change-Id: I77f6eea0f269b0540dbeb462602fc447cb69237d
parent 6a012634
...@@ -167,6 +167,12 @@ static std::string GenTypeGet(const LanguageParameters &lang, ...@@ -167,6 +167,12 @@ static std::string GenTypeGet(const LanguageParameters &lang,
: GenTypePointer(lang, type); : GenTypePointer(lang, type);
} }
static std::string GenDefaultValue(const Value &value) {
return value.type.base_type == BASE_TYPE_BOOL
? (value.constant == "0" ? "false" : "true")
: value.constant;
}
static void GenEnum(const LanguageParameters &lang, EnumDef &enum_def, static void GenEnum(const LanguageParameters &lang, EnumDef &enum_def,
std::string *code_ptr) { std::string *code_ptr) {
std::string &code = *code_ptr; std::string &code = *code_ptr;
...@@ -392,9 +398,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, ...@@ -392,9 +398,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
} else { } else {
code += offset_prefix + getter; code += offset_prefix + getter;
code += "(o + bb_pos) : " + default_cast; code += "(o + bb_pos) : " + default_cast;
code += field.value.type.base_type == BASE_TYPE_BOOL code += GenDefaultValue(field.value);
? (field.value.constant == "0" ? "false" : "true")
: field.value.constant;
} }
} else { } else {
switch (field.value.type.base_type) { switch (field.value.type.base_type) {
...@@ -506,8 +510,9 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, ...@@ -506,8 +510,9 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
code += field.name; code += field.name;
// Java doesn't have defaults, which means this method must always // Java doesn't have defaults, which means this method must always
// supply all arguments, and thus won't compile when fields are added. // supply all arguments, and thus won't compile when fields are added.
if (lang.language != GeneratorOptions::kJava) if (lang.language != GeneratorOptions::kJava) {
code += " = " + field.value.constant; code += " = " + GenDefaultValue(field.value);
}
} }
code += ") {\n builder."; code += ") {\n builder.";
code += FunctionStart(lang, 'S') + "tartObject("; code += FunctionStart(lang, 'S') + "tartObject(";
...@@ -554,12 +559,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, ...@@ -554,12 +559,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd"; code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd";
code += GenMethod(lang, field.value.type) + "("; code += GenMethod(lang, field.value.type) + "(";
code += NumToString(it - struct_def.fields.vec.begin()) + ", "; code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
code += argname + ", "; code += argname + ", " + GenDefaultValue(field.value);
if (field.value.type.base_type == BASE_TYPE_BOOL) {
code += field.value.constant == "0" ? "false" : "true";
} else {
code += field.value.constant;
}
code += "); }\n"; code += "); }\n";
if (field.value.type.base_type == BASE_TYPE_VECTOR) { if (field.value.type.base_type == BASE_TYPE_VECTOR) {
auto vector_type = field.value.type.VectorType(); auto vector_type = field.value.type.VectorType();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment