Hi David,
I cannot speak for how it works in C++, but I am surprised it does not work as you describe.
The flatcc C code generator has an option for force values as you describe, for exactly the reasons you mention, if I understand you correctly:
e.g.
ns(Monster_hp_force_add(B, 100));
Any value that is not set at all will appear absent.
Force only applies to scalars.
Vectors, strings, and tables fields can be absent, or present with zero length, or have actual content. It is possible to detect if they are present or not. Struct member fields also can only be absent or present, but unlike C++ I think, flatcc struct members can have members that a not set which then become 0.
Float and double are a bit tricky because testing for the default value is numerically unstable when converting from ascii text in the schema to the internal representation, so it may be a bit of a hit or miss depending on the value. These are better used as getting a default if none were set, which is also the real motivation behind defaults I guess, rather than a compression concept.
Here is a snippet from the monster test case dealing with defaults and force add:
// ns is macro to wrap the long name space prefix
int test_add_set_defaults(flatcc_builder_t *B)
{
void *buffer;
size_t size;
ns(Monster_table_t) mon;
flatcc_builder_reset(B);
ns(Monster_start_as_root(B));
ns(Monster_name_create_str(B, "MyMonster"));
ns(Monster_hp_add(B, 100));
ns(Monster_mana_add(B, 100));
ns(Monster_color_add(B, ns(Color_Blue)));
ns(Monster_end_as_root(B));
buffer = flatcc_builder_get_direct_buffer(B, &size);
mon = ns(Monster_as_root(buffer));
if (ns(Monster_hp_is_present(mon))) {
printf("default should not be present for hp field\n");
return -1;
}
if (!ns(Monster_mana_is_present(mon))) {
printf("non-default should be present for mana field\n");
return -1;
}
if (ns(Monster_color_is_present(mon))) {
printf("default should not be present for color field\n");
return -1;
}
flatcc_builder_reset(B);
ns(Monster_start_as_root(B));
ns(Monster_name_create_str(B, "MyMonster"));
ns(Monster_hp_force_add(B, 100));
ns(Monster_mana_force_add(B, 100));
ns(Monster_color_force_add(B, ns(Color_Blue)));
ns(Monster_end_as_root(B));
buffer = flatcc_builder_get_direct_buffer(B, &size);
mon = ns(Monster_as_root(buffer));
if (!ns(Monster_hp_is_present(mon))) {
printf("default should be present for hp field when forced\n");
return -1;
}
if (!ns(Monster_mana_is_present(mon))) {
printf("non-default should be present for mana field, also when forced\n");
return -1;
}
if (!ns(Monster_color_is_present(mon))) {
printf("default should be present for color field when forced\n");
return -1;
}
return 0;
}