summaryrefslogtreecommitdiff
path: root/src/idl_gen_general.cpp
diff options
context:
space:
mode:
authorMaor Itzkovitch <maor.tt@gmail.com>2015-08-01 12:38:47 +0300
committerMaor Itzkovitch <maor.tt@gmail.com>2015-08-01 12:38:47 +0300
commit81c2b185efcc729777d86815577cd6ebdd7e15c4 (patch)
tree93e8d37b6ad0393c0d52c4163ffeea638547cf00 /src/idl_gen_general.cpp
parent6e192fa4087aaafd17a050ee5da3d4a043352055 (diff)
downloadflatbuffers-81c2b185efcc729777d86815577cd6ebdd7e15c4.tar.gz
flatbuffers-81c2b185efcc729777d86815577cd6ebdd7e15c4.tar.bz2
flatbuffers-81c2b185efcc729777d86815577cd6ebdd7e15c4.zip
support for scalar mutators
Diffstat (limited to 'src/idl_gen_general.cpp')
-rw-r--r--src/idl_gen_general.cpp39
1 files changed, 37 insertions, 2 deletions
diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp
index a5b7af77..0c733f34 100644
--- a/src/idl_gen_general.cpp
+++ b/src/idl_gen_general.cpp
@@ -428,6 +428,20 @@ static std::string GenGetter(const LanguageParameters &lang,
}
}
+static std::string GenSetter(const LanguageParameters &lang,
+ const Type &type) {
+ switch (type.base_type) {
+ case BASE_TYPE_STRUCT: return "";
+ default: {
+ std::string setter = "bb." + FunctionStart(lang, 'P') + "ut";
+ if (GenTypeBasic(lang, type) != "byte") {
+ setter += MakeCamel(GenTypeGet(lang, type));
+ }
+ return setter;
+ }
+ }
+}
+
// Returns the method name for use with add/put calls.
static std::string GenMethod(const LanguageParameters &lang, const Type &type) {
return IsScalar(type.base_type)
@@ -493,7 +507,8 @@ static void GenStructBody(const LanguageParameters &lang,
}
static void GenStruct(const LanguageParameters &lang, const Parser &parser,
- StructDef &struct_def, std::string *code_ptr) {
+ StructDef &struct_def, const GeneratorOptions &opts,
+ std::string *code_ptr) {
if (struct_def.generated) return;
std::string &code = *code_ptr;
@@ -698,6 +713,26 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
InlineSize(field.value.type.VectorType()));
code += "); }\n";
}
+
+ // generate mutators for scalar fields
+ if (opts.mutable_buffer) {
+ std::string mutator_prefix = MakeCamel("mutate", lang.first_camel_upper);
+ if (IsScalar(field.value.type.base_type)) {
+ code += " public ";
+ code += struct_def.fixed ? "void " : lang.bool_type;
+ code += mutator_prefix + MakeCamel(field.name, true) + "(";
+ code += GenTypeBasic(lang, field.value.type);
+ code += " " + field.name + ") { ";
+ if (struct_def.fixed) {
+ code += GenSetter(lang, field.value.type) + "(bb_pos + ";
+ code += NumToString(field.value.offset) + "); }\n";
+ } else {
+ code += "int o = __offset(" + NumToString(field.value.offset) + ");";
+ code += " if (o != 0) { " + GenSetter(lang, field.value.type);
+ code += "(o + bb_pos); return true } else { return false } }\n";
+ }
+ }
+ }
}
code += "\n";
if (struct_def.fixed) {
@@ -914,7 +949,7 @@ bool GenerateGeneral(const Parser &parser,
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
- GenStruct(lang, parser, **it, &declcode);
+ GenStruct(lang, parser, **it, opts, &declcode);
if (opts.one_file) {
one_file_code += declcode;
}