From 2a61931f1ca2b4d05f5cfee9e5b8c85150af7dd8 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Thu, 26 Aug 2021 14:07:22 +0200 Subject: [PATCH 1/3] c/c++ - add enum-based fields to the comparison test --- .../fbs/typeful/c/objectbox-model.h.expected | 115 +++++++------- .../fbs/typeful/c/schema.obx.h.expected | 150 ++++++++++++++++++ .../typeful/cpp/objectbox-model.h.expected | 115 +++++++------- .../fbs/typeful/cpp/schema.obx.cpp.expected | 36 +++++ .../fbs/typeful/cpp/schema.obx.hpp.expected | 35 ++++ .../fbs/typeful/objectbox-model.json.expected | 131 ++++++++------- .../testdata/fbs/typeful/schema.fbs | 20 ++- 7 files changed, 442 insertions(+), 160 deletions(-) diff --git a/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected b/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected index f379d04..1cdb61f 100644 --- a/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected +++ b/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected @@ -25,87 +25,96 @@ static inline OBX_model* create_obx_model() { obx_model_entity(model, "Typeful", 1, 8717895732742165505); obx_model_entity_flags(model, (OBXEntityFlags) (OBXEntityFlags_SHARED_GLOBAL_IDS | OBXEntityFlags_SYNC_ENABLED)); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 3390393562759376202); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 2669985732393126063); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "int", OBXPropertyType_Int, 2, 2669985732393126063); - obx_model_property(model, "int8", OBXPropertyType_Byte, 3, 1774932891286980153); - obx_model_property(model, "int16", OBXPropertyType_Short, 4, 6044372234677422456); - obx_model_property(model, "int32", OBXPropertyType_Int, 5, 8274930044578894929); - obx_model_property(model, "int64", OBXPropertyType_Long, 6, 1543572285742637646); - obx_model_property(model, "uint", OBXPropertyType_Int, 7, 2661732831099943416); + obx_model_property(model, "int", OBXPropertyType_Int, 2, 1774932891286980153); + obx_model_property(model, "int8", OBXPropertyType_Byte, 3, 6044372234677422456); + obx_model_property(model, "int16", OBXPropertyType_Short, 4, 8274930044578894929); + obx_model_property(model, "int32", OBXPropertyType_Int, 5, 1543572285742637646); + obx_model_property(model, "int64", OBXPropertyType_Long, 6, 2661732831099943416); + obx_model_property(model, "uint", OBXPropertyType_Int, 7, 8325060299420976708); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint8", OBXPropertyType_Byte, 8, 8325060299420976708); + obx_model_property(model, "uint8", OBXPropertyType_Byte, 8, 7837839688282259259); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint16", OBXPropertyType_Short, 9, 7837839688282259259); + obx_model_property(model, "uint16", OBXPropertyType_Short, 9, 2518412263346885298); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint32", OBXPropertyType_Int, 10, 2518412263346885298); + obx_model_property(model, "uint32", OBXPropertyType_Int, 10, 5617773211005988520); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint64", OBXPropertyType_Long, 11, 5617773211005988520); + obx_model_property(model, "uint64", OBXPropertyType_Long, 11, 2339563716805116249); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "bool", OBXPropertyType_Bool, 12, 2339563716805116249); - obx_model_property(model, "string", OBXPropertyType_String, 13, 7144924247938981575); - obx_model_property(model, "stringvector", OBXPropertyType_StringVector, 14, 161231572858529631); - obx_model_property(model, "byte", OBXPropertyType_Byte, 15, 7259475919510918339); - obx_model_property(model, "ubyte", OBXPropertyType_Byte, 16, 7373105480197164748); + obx_model_property(model, "bool", OBXPropertyType_Bool, 12, 7144924247938981575); + obx_model_property(model, "string", OBXPropertyType_String, 13, 161231572858529631); + obx_model_property(model, "stringvector", OBXPropertyType_StringVector, 14, 7259475919510918339); + obx_model_property(model, "byte", OBXPropertyType_Byte, 15, 7373105480197164748); + obx_model_property(model, "ubyte", OBXPropertyType_Byte, 16, 3287288577352441706); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "bytevector", OBXPropertyType_ByteVector, 17, 3287288577352441706); - obx_model_property(model, "ubytevector", OBXPropertyType_ByteVector, 18, 3930927879439176946); - obx_model_property(model, "float32", OBXPropertyType_Float, 19, 4706154865122290029); - obx_model_property(model, "float64", OBXPropertyType_Double, 20, 2217592893536642650); - obx_model_property(model, "float", OBXPropertyType_Float, 21, 1929546706668609706); - obx_model_property(model, "double", OBXPropertyType_Double, 22, 6392442863481646880); - obx_model_property(model, "relId", OBXPropertyType_Relation, 23, 3706853784096366226); + obx_model_property(model, "bytevector", OBXPropertyType_ByteVector, 17, 3930927879439176946); + obx_model_property(model, "ubytevector", OBXPropertyType_ByteVector, 18, 4706154865122290029); + obx_model_property(model, "float32", OBXPropertyType_Float, 19, 2217592893536642650); + obx_model_property(model, "float64", OBXPropertyType_Double, 20, 1929546706668609706); + obx_model_property(model, "float", OBXPropertyType_Float, 21, 6392442863481646880); + obx_model_property(model, "double", OBXPropertyType_Double, 22, 3706853784096366226); + obx_model_property(model, "relId", OBXPropertyType_Relation, 23, 2627038740284806767); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_INDEX_PARTIAL_SKIP_ZERO)); - obx_model_property_relation(model, "AnnotatedEntity", 1, 2627038740284806767); - obx_model_entity_last_property_id(model, 23, 3706853784096366226); + obx_model_property_relation(model, "AnnotatedEntity", 1, 6303220950515014660); + obx_model_entity_last_property_id(model, 23, 2627038740284806767); obx_model_entity(model, "AnnotatedEntity", 2, 2259404117704393152); obx_model_entity_flags(model, OBXEntityFlags_SYNC_ENABLED); - obx_model_property(model, "identifier", OBXPropertyType_Long, 1, 6303220950515014660); + obx_model_property(model, "identifier", OBXPropertyType_Long, 1, 4035568504096476779); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_ID | OBXPropertyFlags_ID_SELF_ASSIGNABLE)); - obx_model_property(model, "name", OBXPropertyType_String, 2, 4035568504096476779); + obx_model_property(model, "name", OBXPropertyType_String, 2, 959367522974354090); obx_model_property_flags(model, OBXPropertyFlags_INDEX_HASH); - obx_model_property_index_id(model, 2, 959367522974354090); - obx_model_property(model, "time", OBXPropertyType_Date, 3, 2914295034816259174); - obx_model_property(model, "relId", OBXPropertyType_Relation, 4, 1395437218309923052); + obx_model_property_index_id(model, 2, 2914295034816259174); + obx_model_property(model, "time", OBXPropertyType_Date, 3, 1395437218309923052); + obx_model_property(model, "relId", OBXPropertyType_Relation, 4, 6745438398739480977); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_INDEX_PARTIAL_SKIP_ZERO)); - obx_model_property_relation(model, "Typeful", 3, 6745438398739480977); - obx_model_property(model, "unique", OBXPropertyType_String, 5, 2897681629866238117); + obx_model_property_relation(model, "Typeful", 3, 2897681629866238117); + obx_model_property(model, "unique", OBXPropertyType_String, 5, 3398579248012586914); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 4, 3398579248012586914); - obx_model_property(model, "uniqueValue", OBXPropertyType_String, 6, 5974317550424871033); + obx_model_property_index_id(model, 4, 5974317550424871033); + obx_model_property(model, "uniqueValue", OBXPropertyType_String, 6, 3317123977833389635); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 5, 3317123977833389635); - obx_model_property(model, "uniqueHash", OBXPropertyType_String, 7, 5001958211167890979); + obx_model_property_index_id(model, 5, 5001958211167890979); + obx_model_property(model, "uniqueHash", OBXPropertyType_String, 7, 167566062957544642); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 6, 167566062957544642); - obx_model_property(model, "uniqueHash64", OBXPropertyType_String, 8, 4778690082005258714); + obx_model_property_index_id(model, 6, 4778690082005258714); + obx_model_property(model, "uniqueHash64", OBXPropertyType_String, 8, 1059542851699319360); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH64 | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 7, 1059542851699319360); - obx_model_property(model, "uid", OBXPropertyType_Int, 9, 6972732843819909978); + obx_model_property_index_id(model, 7, 6972732843819909978); + obx_model_property(model, "uid", OBXPropertyType_Int, 9, 5558237345453186302); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 8, 5558237345453186302); - obx_model_relation(model, 1, 7845762441295307478, 1, 8717895732742165505); - obx_model_relation(model, 2, 771642788862502430, 1, 8717895732742165505); - obx_model_entity_last_property_id(model, 9, 6972732843819909978); + obx_model_property_index_id(model, 8, 7845762441295307478); + obx_model_relation(model, 1, 771642788862502430, 1, 8717895732742165505); + obx_model_relation(model, 2, 8514850266767180993, 1, 8717895732742165505); + obx_model_entity_last_property_id(model, 9, 5558237345453186302); obx_model_entity(model, "TSDate", 3, 6050128673802995827); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 8514850266767180993); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 8683452355129068124); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "timestamp", OBXPropertyType_Date, 2, 8683452355129068124); + obx_model_property(model, "timestamp", OBXPropertyType_Date, 2, 4345851588384648695); obx_model_property_flags(model, OBXPropertyFlags_ID_COMPANION); - obx_model_entity_last_property_id(model, 2, 8683452355129068124); + obx_model_entity_last_property_id(model, 2, 4345851588384648695); obx_model_entity(model, "TSDateNano", 4, 501233450539197794); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 4345851588384648695); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 7699391924090763411); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "timestamp", OBXPropertyType_DateNano, 2, 7699391924090763411); + obx_model_property(model, "timestamp", OBXPropertyType_DateNano, 2, 388440063886460141); obx_model_property_flags(model, OBXPropertyFlags_ID_COMPANION); - obx_model_entity_last_property_id(model, 2, 7699391924090763411); + obx_model_entity_last_property_id(model, 2, 388440063886460141); - obx_model_last_entity_id(model, 4, 501233450539197794); - obx_model_last_index_id(model, 8, 5558237345453186302); - obx_model_last_relation_id(model, 2, 771642788862502430); + obx_model_entity(model, "WithEnums", 5, 3390393562759376202); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 7561811714888168464); + obx_model_property_flags(model, OBXPropertyFlags_ID); + obx_model_property(model, "color", OBXPropertyType_Byte, 2, 3959279844101328186); + obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); + obx_model_property(model, "flags", OBXPropertyType_Int, 3, 8902041070398994519); + obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); + obx_model_entity_last_property_id(model, 3, 8902041070398994519); + + obx_model_last_entity_id(model, 5, 3390393562759376202); + obx_model_last_index_id(model, 8, 7845762441295307478); + obx_model_last_relation_id(model, 2, 8514850266767180993); return model; // NOTE: the returned model will contain error information if an error occurred. } diff --git a/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected b/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected index 66f80f8..0bf39bc 100644 --- a/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected +++ b/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected @@ -221,6 +221,41 @@ static void ns_TSDateNano_free_pointers(ns_TSDateNano* object); /// Equivalent to calling ns_TSDateNano_free_pointers() followed by free(); static void ns_TSDateNano_free(ns_TSDateNano* object); +typedef struct ns_WithEnums { + obx_id id; + uint8_t color; + uint32_t flags; + +} ns_WithEnums; + +enum ns_WithEnums_ { + ns_WithEnums_ENTITY_ID = 5, + ns_WithEnums_PROP_ID_id = 1, + ns_WithEnums_PROP_ID_color = 2, + ns_WithEnums_PROP_ID_flags = 3, +}; + +/// Write given object to the FlatBufferBuilder +static bool ns_WithEnums_to_flatbuffer(flatcc_builder_t* B, const ns_WithEnums* object, void** out_buffer, size_t* out_size); + +/// Read an object from a valid FlatBuffer. +/// If the read object contains vectors or strings, those are allocated on heap and must be freed after use by calling ns_WithEnums_free_pointers(). +/// Thus, when calling this function multiple times on the same object, ensure to call ns_WithEnums_free_pointers() before subsequent calls to avoid leaks. +/// @returns true if the object was deserialized successfully or false on (allocation) error in which case any memory +/// allocated by this function will also be freed before returning, allowing you to retry. +static bool ns_WithEnums_from_flatbuffer(const void* data, size_t size, ns_WithEnums* out_object); + +/// Read an object from a valid FlatBuffer, allocating the object on heap. +/// The object must be freed after use by calling ns_WithEnums_free(); +static ns_WithEnums* ns_WithEnums_new_from_flatbuffer(const void* data, size_t size); + +/// Free memory allocated for vector and string properties, setting the freed pointers to NULL. +static void ns_WithEnums_free_pointers(ns_WithEnums* object); + +/// Free ns_WithEnums* object pointer and all its property pointers (vectors and strings). +/// Equivalent to calling ns_WithEnums_free_pointers() followed by free(); +static void ns_WithEnums_free(ns_WithEnums* object); + static bool Typeful_to_flatbuffer(flatcc_builder_t* B, const Typeful* object, void** out_buffer, size_t* out_size) { assert(B); assert(object); @@ -1042,6 +1077,121 @@ static ns_TSDateNano* ns_TSDateNano_get(OBX_box* box, obx_id id) { return (ns_TSDateNano*) schema_obx_h_get_object(box, id, (void* (*) (const void*, size_t)) ns_TSDateNano_new_from_flatbuffer); } +static bool ns_WithEnums_to_flatbuffer(flatcc_builder_t* B, const ns_WithEnums* object, void** out_buffer, size_t* out_size) { + assert(B); + assert(object); + assert(out_buffer); + assert(out_size); + + flatcc_builder_reset(B); + flatcc_builder_start_buffer(B, 0, 0, 0); + + + if (flatcc_builder_start_table(B, 3) != 0) return false; + + void* p; + flatcc_builder_ref_t* _p; + + { + if (!(p = flatcc_builder_table_add(B, 0, 8, 8))) return false; + flatbuffers_uint64_write_to_pe(p, object->id); + } + + { + if (!(p = flatcc_builder_table_add(B, 1, 1, 1))) return false; + flatbuffers_uint8_write_to_pe(p, object->color); + } + + { + if (!(p = flatcc_builder_table_add(B, 2, 4, 4))) return false; + flatbuffers_uint32_write_to_pe(p, object->flags); + } + + flatcc_builder_ref_t ref; + if (!(ref = flatcc_builder_end_table(B))) return false; + if (!flatcc_builder_end_buffer(B, ref)) return false; + return (*out_buffer = flatcc_builder_finalize_aligned_buffer(B, out_size)) != NULL; +} + +static bool ns_WithEnums_from_flatbuffer(const void* data, size_t size, ns_WithEnums* out_object) { + assert(data); + assert(size > 0); + assert(out_object); + + const uint8_t* table = (const uint8_t*) data + __flatbuffers_uoffset_read_from_pe(data); + assert(table); + const flatbuffers_voffset_t* vt = (const flatbuffers_voffset_t*) (table - __flatbuffers_soffset_read_from_pe(table)); + flatbuffers_voffset_t vs = __flatbuffers_voffset_read_from_pe(vt); + + // variables reused when reading strings and vectors + flatbuffers_voffset_t offset; + const flatbuffers_uoffset_t* val; + size_t len; + + // reset so that dangling pointers are freed properly on malloc() failures +#ifdef __cplusplus + *out_object = {}; +#else + *out_object = (ns_WithEnums){0}; +#endif + if ((offset = schema_obx_h_fb_field_offset(vs, vt, 0))) { + out_object->id = flatbuffers_uint64_read_from_pe(table + offset); + } + if ((offset = schema_obx_h_fb_field_offset(vs, vt, 1))) { + out_object->color = flatbuffers_uint8_read_from_pe(table + offset); + } + if ((offset = schema_obx_h_fb_field_offset(vs, vt, 2))) { + out_object->flags = flatbuffers_uint32_read_from_pe(table + offset); + } + return true; +} + +static ns_WithEnums* ns_WithEnums_new_from_flatbuffer(const void* data, size_t size) { + ns_WithEnums* object = (ns_WithEnums*) malloc(sizeof(ns_WithEnums)); + if (object) { + if (!ns_WithEnums_from_flatbuffer(data, size, object)) { + free(object); + object = NULL; + } + } + return object; +} + +static void ns_WithEnums_free_pointers(ns_WithEnums* object) { + if (object == NULL) return; + +} + +static void ns_WithEnums_free(ns_WithEnums* object) { + ns_WithEnums_free_pointers(object); + free(object); +} + +/// Insert or update the given object in the database. +/// @param object (in & out) will be updated with a newly inserted ID if the one specified previously was zero. If an ID +/// was already specified (non-zero), it will remain unchanged. +/// @return object ID from the object param (see object param docs) or a zero on error. If a zero was returned, you can +/// check obx_last_error_*() to get the error details. In an unlikely event that those functions return no error +/// code/message, the error occurred in FlatBuffers serialization, e.g. due to memory allocation issues. +static obx_id ns_WithEnums_put(OBX_box* box, ns_WithEnums* object) { + obx_id id = schema_obx_h_put_object(box, object, + (bool (*)(flatcc_builder_t*, const void*, void**, size_t*)) ns_WithEnums_to_flatbuffer, + OBXPutMode_PUT); + if (id != 0) { + object->id = id; // update the ID property on new objects for convenience + } + return id; +} + +/// Read an object from the database, returning a pointer. +/// @return an object pointer or NULL if an object with the given ID doesn't exist or any other error occurred. You can +/// check obx_last_error_*() if NULL is returned to get the error details. In an unlikely event that those functions +/// return no error code/message, the error occurred in FlatBuffers serialization, e.g. due to memory allocation issues. +/// @note: The returned object must be freed after use by calling ns_WithEnums_free(); +static ns_WithEnums* ns_WithEnums_get(OBX_box* box, obx_id id) { + return (ns_WithEnums*) schema_obx_h_get_object(box, id, (void* (*) (const void*, size_t)) ns_WithEnums_new_from_flatbuffer); +} + static obx_id schema_obx_h_put_object(OBX_box* box, void* object, bool (*to_flatbuffer)(flatcc_builder_t*, const void*, void**, size_t*), OBXPutMode mode) { flatcc_builder_t builder; diff --git a/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected b/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected index f379d04..1cdb61f 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected @@ -25,87 +25,96 @@ static inline OBX_model* create_obx_model() { obx_model_entity(model, "Typeful", 1, 8717895732742165505); obx_model_entity_flags(model, (OBXEntityFlags) (OBXEntityFlags_SHARED_GLOBAL_IDS | OBXEntityFlags_SYNC_ENABLED)); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 3390393562759376202); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 2669985732393126063); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "int", OBXPropertyType_Int, 2, 2669985732393126063); - obx_model_property(model, "int8", OBXPropertyType_Byte, 3, 1774932891286980153); - obx_model_property(model, "int16", OBXPropertyType_Short, 4, 6044372234677422456); - obx_model_property(model, "int32", OBXPropertyType_Int, 5, 8274930044578894929); - obx_model_property(model, "int64", OBXPropertyType_Long, 6, 1543572285742637646); - obx_model_property(model, "uint", OBXPropertyType_Int, 7, 2661732831099943416); + obx_model_property(model, "int", OBXPropertyType_Int, 2, 1774932891286980153); + obx_model_property(model, "int8", OBXPropertyType_Byte, 3, 6044372234677422456); + obx_model_property(model, "int16", OBXPropertyType_Short, 4, 8274930044578894929); + obx_model_property(model, "int32", OBXPropertyType_Int, 5, 1543572285742637646); + obx_model_property(model, "int64", OBXPropertyType_Long, 6, 2661732831099943416); + obx_model_property(model, "uint", OBXPropertyType_Int, 7, 8325060299420976708); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint8", OBXPropertyType_Byte, 8, 8325060299420976708); + obx_model_property(model, "uint8", OBXPropertyType_Byte, 8, 7837839688282259259); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint16", OBXPropertyType_Short, 9, 7837839688282259259); + obx_model_property(model, "uint16", OBXPropertyType_Short, 9, 2518412263346885298); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint32", OBXPropertyType_Int, 10, 2518412263346885298); + obx_model_property(model, "uint32", OBXPropertyType_Int, 10, 5617773211005988520); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "uint64", OBXPropertyType_Long, 11, 5617773211005988520); + obx_model_property(model, "uint64", OBXPropertyType_Long, 11, 2339563716805116249); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "bool", OBXPropertyType_Bool, 12, 2339563716805116249); - obx_model_property(model, "string", OBXPropertyType_String, 13, 7144924247938981575); - obx_model_property(model, "stringvector", OBXPropertyType_StringVector, 14, 161231572858529631); - obx_model_property(model, "byte", OBXPropertyType_Byte, 15, 7259475919510918339); - obx_model_property(model, "ubyte", OBXPropertyType_Byte, 16, 7373105480197164748); + obx_model_property(model, "bool", OBXPropertyType_Bool, 12, 7144924247938981575); + obx_model_property(model, "string", OBXPropertyType_String, 13, 161231572858529631); + obx_model_property(model, "stringvector", OBXPropertyType_StringVector, 14, 7259475919510918339); + obx_model_property(model, "byte", OBXPropertyType_Byte, 15, 7373105480197164748); + obx_model_property(model, "ubyte", OBXPropertyType_Byte, 16, 3287288577352441706); obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); - obx_model_property(model, "bytevector", OBXPropertyType_ByteVector, 17, 3287288577352441706); - obx_model_property(model, "ubytevector", OBXPropertyType_ByteVector, 18, 3930927879439176946); - obx_model_property(model, "float32", OBXPropertyType_Float, 19, 4706154865122290029); - obx_model_property(model, "float64", OBXPropertyType_Double, 20, 2217592893536642650); - obx_model_property(model, "float", OBXPropertyType_Float, 21, 1929546706668609706); - obx_model_property(model, "double", OBXPropertyType_Double, 22, 6392442863481646880); - obx_model_property(model, "relId", OBXPropertyType_Relation, 23, 3706853784096366226); + obx_model_property(model, "bytevector", OBXPropertyType_ByteVector, 17, 3930927879439176946); + obx_model_property(model, "ubytevector", OBXPropertyType_ByteVector, 18, 4706154865122290029); + obx_model_property(model, "float32", OBXPropertyType_Float, 19, 2217592893536642650); + obx_model_property(model, "float64", OBXPropertyType_Double, 20, 1929546706668609706); + obx_model_property(model, "float", OBXPropertyType_Float, 21, 6392442863481646880); + obx_model_property(model, "double", OBXPropertyType_Double, 22, 3706853784096366226); + obx_model_property(model, "relId", OBXPropertyType_Relation, 23, 2627038740284806767); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_INDEX_PARTIAL_SKIP_ZERO)); - obx_model_property_relation(model, "AnnotatedEntity", 1, 2627038740284806767); - obx_model_entity_last_property_id(model, 23, 3706853784096366226); + obx_model_property_relation(model, "AnnotatedEntity", 1, 6303220950515014660); + obx_model_entity_last_property_id(model, 23, 2627038740284806767); obx_model_entity(model, "AnnotatedEntity", 2, 2259404117704393152); obx_model_entity_flags(model, OBXEntityFlags_SYNC_ENABLED); - obx_model_property(model, "identifier", OBXPropertyType_Long, 1, 6303220950515014660); + obx_model_property(model, "identifier", OBXPropertyType_Long, 1, 4035568504096476779); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_ID | OBXPropertyFlags_ID_SELF_ASSIGNABLE)); - obx_model_property(model, "name", OBXPropertyType_String, 2, 4035568504096476779); + obx_model_property(model, "name", OBXPropertyType_String, 2, 959367522974354090); obx_model_property_flags(model, OBXPropertyFlags_INDEX_HASH); - obx_model_property_index_id(model, 2, 959367522974354090); - obx_model_property(model, "time", OBXPropertyType_Date, 3, 2914295034816259174); - obx_model_property(model, "relId", OBXPropertyType_Relation, 4, 1395437218309923052); + obx_model_property_index_id(model, 2, 2914295034816259174); + obx_model_property(model, "time", OBXPropertyType_Date, 3, 1395437218309923052); + obx_model_property(model, "relId", OBXPropertyType_Relation, 4, 6745438398739480977); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_INDEX_PARTIAL_SKIP_ZERO)); - obx_model_property_relation(model, "Typeful", 3, 6745438398739480977); - obx_model_property(model, "unique", OBXPropertyType_String, 5, 2897681629866238117); + obx_model_property_relation(model, "Typeful", 3, 2897681629866238117); + obx_model_property(model, "unique", OBXPropertyType_String, 5, 3398579248012586914); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 4, 3398579248012586914); - obx_model_property(model, "uniqueValue", OBXPropertyType_String, 6, 5974317550424871033); + obx_model_property_index_id(model, 4, 5974317550424871033); + obx_model_property(model, "uniqueValue", OBXPropertyType_String, 6, 3317123977833389635); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 5, 3317123977833389635); - obx_model_property(model, "uniqueHash", OBXPropertyType_String, 7, 5001958211167890979); + obx_model_property_index_id(model, 5, 5001958211167890979); + obx_model_property(model, "uniqueHash", OBXPropertyType_String, 7, 167566062957544642); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 6, 167566062957544642); - obx_model_property(model, "uniqueHash64", OBXPropertyType_String, 8, 4778690082005258714); + obx_model_property_index_id(model, 6, 4778690082005258714); + obx_model_property(model, "uniqueHash64", OBXPropertyType_String, 8, 1059542851699319360); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEX_HASH64 | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 7, 1059542851699319360); - obx_model_property(model, "uid", OBXPropertyType_Int, 9, 6972732843819909978); + obx_model_property_index_id(model, 7, 6972732843819909978); + obx_model_property(model, "uid", OBXPropertyType_Int, 9, 5558237345453186302); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); - obx_model_property_index_id(model, 8, 5558237345453186302); - obx_model_relation(model, 1, 7845762441295307478, 1, 8717895732742165505); - obx_model_relation(model, 2, 771642788862502430, 1, 8717895732742165505); - obx_model_entity_last_property_id(model, 9, 6972732843819909978); + obx_model_property_index_id(model, 8, 7845762441295307478); + obx_model_relation(model, 1, 771642788862502430, 1, 8717895732742165505); + obx_model_relation(model, 2, 8514850266767180993, 1, 8717895732742165505); + obx_model_entity_last_property_id(model, 9, 5558237345453186302); obx_model_entity(model, "TSDate", 3, 6050128673802995827); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 8514850266767180993); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 8683452355129068124); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "timestamp", OBXPropertyType_Date, 2, 8683452355129068124); + obx_model_property(model, "timestamp", OBXPropertyType_Date, 2, 4345851588384648695); obx_model_property_flags(model, OBXPropertyFlags_ID_COMPANION); - obx_model_entity_last_property_id(model, 2, 8683452355129068124); + obx_model_entity_last_property_id(model, 2, 4345851588384648695); obx_model_entity(model, "TSDateNano", 4, 501233450539197794); - obx_model_property(model, "id", OBXPropertyType_Long, 1, 4345851588384648695); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 7699391924090763411); obx_model_property_flags(model, OBXPropertyFlags_ID); - obx_model_property(model, "timestamp", OBXPropertyType_DateNano, 2, 7699391924090763411); + obx_model_property(model, "timestamp", OBXPropertyType_DateNano, 2, 388440063886460141); obx_model_property_flags(model, OBXPropertyFlags_ID_COMPANION); - obx_model_entity_last_property_id(model, 2, 7699391924090763411); + obx_model_entity_last_property_id(model, 2, 388440063886460141); - obx_model_last_entity_id(model, 4, 501233450539197794); - obx_model_last_index_id(model, 8, 5558237345453186302); - obx_model_last_relation_id(model, 2, 771642788862502430); + obx_model_entity(model, "WithEnums", 5, 3390393562759376202); + obx_model_property(model, "id", OBXPropertyType_Long, 1, 7561811714888168464); + obx_model_property_flags(model, OBXPropertyFlags_ID); + obx_model_property(model, "color", OBXPropertyType_Byte, 2, 3959279844101328186); + obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); + obx_model_property(model, "flags", OBXPropertyType_Int, 3, 8902041070398994519); + obx_model_property_flags(model, OBXPropertyFlags_UNSIGNED); + obx_model_entity_last_property_id(model, 3, 8902041070398994519); + + obx_model_last_entity_id(model, 5, 3390393562759376202); + obx_model_last_index_id(model, 8, 7845762441295307478); + obx_model_last_relation_id(model, 2, 8514850266767180993); return model; // NOTE: the returned model will contain error information if an error occurred. } diff --git a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected index 4eba4f9..0acf72a 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected @@ -262,3 +262,39 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns: } +const obx::Property ns::WithEnums_::id(1); +const obx::Property ns::WithEnums_::color(2); +const obx::Property ns::WithEnums_::flags(3); + +void ns::WithEnums::_OBX_MetaInfo::toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb, const ns::WithEnums& object) { + fbb.Clear(); + flatbuffers::uoffset_t fbStart = fbb.StartTable(); + fbb.TrackField(4, fbb.PushElement(object.id)); + fbb.TrackField(6, fbb.PushElement(object.color)); + fbb.TrackField(8, fbb.PushElement(object.flags)); + flatbuffers::Offset offset; + offset.o = fbb.EndTable(fbStart); + fbb.Finish(offset); +} + +ns::WithEnums ns::WithEnums::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t size) { + ns::WithEnums object; + fromFlatBuffer(data, size, object); + return object; +} + +std::unique_ptr ns::WithEnums::_OBX_MetaInfo::newFromFlatBuffer(const void* data, size_t size) { + auto object = std::unique_ptr(new ns::WithEnums()); + fromFlatBuffer(data, size, *object); + return object; +} + +void ns::WithEnums::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::WithEnums& outObject) { + const auto* table = flatbuffers::GetRoot(data); + assert(table); + outObject.id = table->GetField(4, 0); + outObject.color = table->GetField(6, 0); + outObject.flags = table->GetField(8, 0); + +} + diff --git a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected index 830d7d0..6ca3e0d 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected @@ -206,3 +206,38 @@ struct TSDateNano_ { }; } // namespace ns + +namespace ns { +struct WithEnums_; + +struct WithEnums { + obx_id id; + uint8_t color; + uint32_t flags; + + struct _OBX_MetaInfo { + static constexpr obx_schema_id entityId() { return 5; } + + static void setObjectId(WithEnums& object, obx_id newId) { object.id = newId; } + + /// Write given object to the FlatBufferBuilder + static void toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb, const WithEnums& object); + + /// Read an object from a valid FlatBuffer + static WithEnums fromFlatBuffer(const void* data, size_t size); + + /// Read an object from a valid FlatBuffer + static std::unique_ptr newFromFlatBuffer(const void* data, size_t size); + + /// Read an object from a valid FlatBuffer + static void fromFlatBuffer(const void* data, size_t size, WithEnums& outObject); + }; +}; + +struct WithEnums_ { + static const obx::Property id; + static const obx::Property color; + static const obx::Property flags; +}; +} // namespace ns + diff --git a/test/comparison/testdata/fbs/typeful/objectbox-model.json.expected b/test/comparison/testdata/fbs/typeful/objectbox-model.json.expected index dd8a206..066cb5f 100644 --- a/test/comparison/testdata/fbs/typeful/objectbox-model.json.expected +++ b/test/comparison/testdata/fbs/typeful/objectbox-model.json.expected @@ -5,131 +5,131 @@ "entities": [ { "id": "1:8717895732742165505", - "lastPropertyId": "23:3706853784096366226", + "lastPropertyId": "23:2627038740284806767", "name": "Typeful", "flags": 6, "properties": [ { - "id": "1:3390393562759376202", + "id": "1:2669985732393126063", "name": "id", "type": 6, "flags": 1 }, { - "id": "2:2669985732393126063", + "id": "2:1774932891286980153", "name": "int", "type": 5 }, { - "id": "3:1774932891286980153", + "id": "3:6044372234677422456", "name": "int8", "type": 2 }, { - "id": "4:6044372234677422456", + "id": "4:8274930044578894929", "name": "int16", "type": 3 }, { - "id": "5:8274930044578894929", + "id": "5:1543572285742637646", "name": "int32", "type": 5 }, { - "id": "6:1543572285742637646", + "id": "6:2661732831099943416", "name": "int64", "type": 6 }, { - "id": "7:2661732831099943416", + "id": "7:8325060299420976708", "name": "uint", "type": 5, "flags": 8192 }, { - "id": "8:8325060299420976708", + "id": "8:7837839688282259259", "name": "uint8", "type": 2, "flags": 8192 }, { - "id": "9:7837839688282259259", + "id": "9:2518412263346885298", "name": "uint16", "type": 3, "flags": 8192 }, { - "id": "10:2518412263346885298", + "id": "10:5617773211005988520", "name": "uint32", "type": 5, "flags": 8192 }, { - "id": "11:5617773211005988520", + "id": "11:2339563716805116249", "name": "uint64", "type": 6, "flags": 8192 }, { - "id": "12:2339563716805116249", + "id": "12:7144924247938981575", "name": "bool", "type": 1 }, { - "id": "13:7144924247938981575", + "id": "13:161231572858529631", "name": "string", "type": 9 }, { - "id": "14:161231572858529631", + "id": "14:7259475919510918339", "name": "stringvector", "type": 30 }, { - "id": "15:7259475919510918339", + "id": "15:7373105480197164748", "name": "byte", "type": 2 }, { - "id": "16:7373105480197164748", + "id": "16:3287288577352441706", "name": "ubyte", "type": 2, "flags": 8192 }, { - "id": "17:3287288577352441706", + "id": "17:3930927879439176946", "name": "bytevector", "type": 23 }, { - "id": "18:3930927879439176946", + "id": "18:4706154865122290029", "name": "ubytevector", "type": 23 }, { - "id": "19:4706154865122290029", + "id": "19:2217592893536642650", "name": "float32", "type": 7 }, { - "id": "20:2217592893536642650", + "id": "20:1929546706668609706", "name": "float64", "type": 8 }, { - "id": "21:1929546706668609706", + "id": "21:6392442863481646880", "name": "float", "type": 7 }, { - "id": "22:6392442863481646880", + "id": "22:3706853784096366226", "name": "double", "type": 8 }, { - "id": "23:3706853784096366226", + "id": "23:2627038740284806767", "name": "relId", - "indexId": "1:2627038740284806767", + "indexId": "1:6303220950515014660", "type": 11, "flags": 520, "relationTarget": "AnnotatedEntity" @@ -138,80 +138,80 @@ }, { "id": "2:2259404117704393152", - "lastPropertyId": "9:6972732843819909978", + "lastPropertyId": "9:5558237345453186302", "name": "AnnotatedEntity", "flags": 2, "properties": [ { - "id": "1:6303220950515014660", + "id": "1:4035568504096476779", "name": "identifier", "type": 6, "flags": 129 }, { - "id": "2:4035568504096476779", + "id": "2:959367522974354090", "name": "name", - "indexId": "2:959367522974354090", + "indexId": "2:2914295034816259174", "type": 9, "flags": 2048 }, { - "id": "3:2914295034816259174", + "id": "3:1395437218309923052", "name": "time", "type": 10 }, { - "id": "4:1395437218309923052", + "id": "4:6745438398739480977", "name": "relId", - "indexId": "3:6745438398739480977", + "indexId": "3:2897681629866238117", "type": 11, "flags": 520, "relationTarget": "Typeful" }, { - "id": "5:2897681629866238117", + "id": "5:3398579248012586914", "name": "unique", - "indexId": "4:3398579248012586914", + "indexId": "4:5974317550424871033", "type": 9, "flags": 2080 }, { - "id": "6:5974317550424871033", + "id": "6:3317123977833389635", "name": "uniqueValue", - "indexId": "5:3317123977833389635", + "indexId": "5:5001958211167890979", "type": 9, "flags": 40 }, { - "id": "7:5001958211167890979", + "id": "7:167566062957544642", "name": "uniqueHash", - "indexId": "6:167566062957544642", + "indexId": "6:4778690082005258714", "type": 9, "flags": 2080 }, { - "id": "8:4778690082005258714", + "id": "8:1059542851699319360", "name": "uniqueHash64", - "indexId": "7:1059542851699319360", + "indexId": "7:6972732843819909978", "type": 9, "flags": 4128 }, { - "id": "9:6972732843819909978", + "id": "9:5558237345453186302", "name": "uid", - "indexId": "8:5558237345453186302", + "indexId": "8:7845762441295307478", "type": 5, "flags": 40 } ], "relations": [ { - "id": "1:7845762441295307478", + "id": "1:771642788862502430", "name": "typefuls", "targetId": "1:8717895732742165505" }, { - "id": "2:771642788862502430", + "id": "2:8514850266767180993", "name": "m2m", "targetId": "1:8717895732742165505" } @@ -219,17 +219,17 @@ }, { "id": "3:6050128673802995827", - "lastPropertyId": "2:8683452355129068124", + "lastPropertyId": "2:4345851588384648695", "name": "TSDate", "properties": [ { - "id": "1:8514850266767180993", + "id": "1:8683452355129068124", "name": "id", "type": 6, "flags": 1 }, { - "id": "2:8683452355129068124", + "id": "2:4345851588384648695", "name": "timestamp", "type": 10, "flags": 16384 @@ -238,27 +238,52 @@ }, { "id": "4:501233450539197794", - "lastPropertyId": "2:7699391924090763411", + "lastPropertyId": "2:388440063886460141", "name": "TSDateNano", "properties": [ { - "id": "1:4345851588384648695", + "id": "1:7699391924090763411", "name": "id", "type": 6, "flags": 1 }, { - "id": "2:7699391924090763411", + "id": "2:388440063886460141", "name": "timestamp", "type": 12, "flags": 16384 } ] + }, + { + "id": "5:3390393562759376202", + "lastPropertyId": "3:8902041070398994519", + "name": "WithEnums", + "properties": [ + { + "id": "1:7561811714888168464", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:3959279844101328186", + "name": "color", + "type": 2, + "flags": 8192 + }, + { + "id": "3:8902041070398994519", + "name": "flags", + "type": 5, + "flags": 8192 + } + ] } ], - "lastEntityId": "4:501233450539197794", - "lastIndexId": "8:5558237345453186302", - "lastRelationId": "2:771642788862502430", + "lastEntityId": "5:3390393562759376202", + "lastIndexId": "8:7845762441295307478", + "lastRelationId": "2:8514850266767180993", "modelVersion": 5, "modelVersionParserMinimum": 5, "retiredEntityUids": [], diff --git a/test/comparison/testdata/fbs/typeful/schema.fbs b/test/comparison/testdata/fbs/typeful/schema.fbs index 35f529c..115cfd3 100644 --- a/test/comparison/testdata/fbs/typeful/schema.fbs +++ b/test/comparison/testdata/fbs/typeful/schema.fbs @@ -84,4 +84,22 @@ table TSDateNano { id : uint64 ; /// objectbox:id-companion,date-nano timestamp : int64 ; -} \ No newline at end of file +} + +enum Flags: uint(bit_flags) { + a, + b, +} + +enum Color : ubyte { + none, + blue, + green, + red +} + +table WithEnums { + id: uint64; + color: Color; + flags: Flags; +} From a89b4565d7e16afe271b3c0580b832a42d39cd69 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 27 Aug 2021 20:12:25 +0200 Subject: [PATCH 2/3] c/c++ - fully support enums as fields --- internal/generator/c/meta.go | 88 +++++++++++++------ internal/generator/c/schema-reader.go | 17 +++- internal/generator/c/templates/binding-c.go | 10 ++- internal/generator/c/templates/binding-cpp.go | 2 +- internal/generator/c/templates/binding-hpp.go | 17 +++- internal/generator/c/templates/functions.go | 29 ++++++ internal/generator/merge.go | 6 ++ internal/generator/model/meta.go | 6 ++ internal/generator/model/model.go | 1 + .../fbs/typeful/c/schema.obx.h.expected | 16 +++- .../fbs/typeful/cpp/schema.obx.cpp.expected | 64 +++++++------- .../fbs/typeful/cpp/schema.obx.hpp.expected | 23 ++++- .../flatbuffers-c-bridge/src/flatbuffersc.cpp | 1 + 13 files changed, 211 insertions(+), 69 deletions(-) diff --git a/internal/generator/c/meta.go b/internal/generator/c/meta.go index 126719c..5ce3029 100644 --- a/internal/generator/c/meta.go +++ b/internal/generator/c/meta.go @@ -29,6 +29,14 @@ import ( "github.com/objectbox/objectbox-generator/internal/generator/model" ) +type fbsModel struct { + Enums []*fbsEnum +} + +func (mm *fbsModel) Merge(model *model.ModelInfo) model.ModelMeta { + return mm +} + type fbsObject struct { *binding.Object fbsObject *reflection.Object @@ -67,33 +75,6 @@ func (mo *fbsObject) CppNamespacePrefix() string { return cppNamespacePrefix(mo.Namespace) } -// CppNamespaceStart returns c++ namespace opening declaration -func (mo *fbsObject) CppNamespaceStart() string { - if len(mo.Namespace) == 0 { - return "" - } - - var nss = strings.Split(mo.Namespace, ".") - for i, ns := range nss { - nss[i] = "namespace " + ns + " {" - } - return strings.Join(nss, "\n") -} - -// CppNamespaceEnd returns c++ namespace closing declaration -func (mo *fbsObject) CppNamespaceEnd() string { - if len(mo.Namespace) == 0 { - return "" - } - var result = "" - var nss = strings.Split(mo.Namespace, ".") - for _, ns := range nss { - // print in reversed order - result = "} // namespace " + ns + "\n" + result - } - return result -} - // PreDeclareCppRelTargets returns C++ struct pre-declarations for related entities. func (mo *fbsObject) PreDeclareCppRelTargets() (string, error) { // first create a map `(ns.entity) => bool`, then sort it to keep the code from changing, and generate the C++ decl. @@ -140,6 +121,7 @@ func (mo *fbsObject) PreDeclareCppRelTargets() (string, error) { type fbsField struct { *binding.Field + fbsModel *fbsModel fbsField *reflection.Field } @@ -159,6 +141,10 @@ func (mp *fbsField) CppNameRelationTarget() string { return cppNamespacePrefix(mp.relTargetNamespace()) + cppName(mp.ModelProperty.RelationTarget) } +func isEnumSupportedType(t reflection.BaseType) bool { + return t >= reflection.BaseTypeByte && t <= reflection.BaseTypeULong +} + // CppType returns C++ type name func (mp *fbsField) CppType() string { var fbsType = mp.fbsField.Type(nil) @@ -168,6 +154,8 @@ func (mp *fbsField) CppType() string { cppType = cppType + "<" + fbsTypeToCppType[fbsType.Element()] + ">" } else if (mp.ModelProperty.IsIdProperty() || mp.ModelProperty.Type == model.PropertyTypeRelation) && cppType == "uint64_t" { cppType = "obx_id" // defined in objectbox.h + } else if enumIndex := fbsType.Index(); isEnumSupportedType(baseType) && enumIndex >= 0 { + cppType = strings.ReplaceAll(string(mp.fbsModel.Enums[enumIndex].enum.Name()), ".", "::") } return cppType } @@ -301,3 +289,49 @@ func (mr *standaloneRel) Merge(rel *model.StandaloneRelation) model.StandaloneRe func (mr *standaloneRel) CppName() string { return cppName(mr.ModelRelation.Name) } + +type fbsEnum struct { + enum *reflection.Enum + Values []*reflection.EnumVal +} + +func (me *fbsEnum) nameParts() (ns string, name string) { + var components = strings.Split(string(me.enum.Name()), ".") + ns = strings.Join(components[0:len(components)-1], ".") + name = components[len(components)-1] + return +} + +func (me *fbsEnum) Namespace() string { + ns, _ := me.nameParts() + return ns +} + +func (me *fbsEnum) Name() string { + _, name := me.nameParts() + return name +} + +func (me *fbsEnum) UnderlyingCppType() string { + var baseType = me.enum.UnderlyingType(nil).BaseType() + return fbsTypeToCppType[baseType] +} + +func (me *fbsEnum) Comments() []string { + var comments []string + for i := 0; i < me.enum.DocumentationLength(); i++ { + comments = append(comments, strings.TrimSpace(string(me.enum.Documentation(i)))) + } + return comments +} + +func (me *fbsEnum) HasAttribute(name string) bool { + var kv = reflection.KeyValue{} + var i = 0 + for me.enum.Attributes(&kv, i) { + if string(kv.Key()) == name { + return true + } + } + return false +} diff --git a/internal/generator/c/schema-reader.go b/internal/generator/c/schema-reader.go index b67cc42..fad81fe 100644 --- a/internal/generator/c/schema-reader.go +++ b/internal/generator/c/schema-reader.go @@ -64,6 +64,21 @@ type fbSchemaReader struct { // const annotationPrefix = "objectbox:" func (r *fbSchemaReader) read(schema *reflection.Schema) error { + var fbsmodel = &fbsModel{Enums: make([]*fbsEnum, schema.EnumsLength())} + r.model.Meta = fbsmodel + for i := range fbsmodel.Enums { + fbsmodel.Enums[i] = &fbsEnum{&reflection.Enum{}, []*reflection.EnumVal{}} + if !schema.Enums(fbsmodel.Enums[i].enum, i) { + return fmt.Errorf("can't access enum %d", i) + } + for j := 0; j < fbsmodel.Enums[i].enum.ValuesLength(); j++ { + fbsmodel.Enums[i].Values = append(fbsmodel.Enums[i].Values, &reflection.EnumVal{}) + if !fbsmodel.Enums[i].enum.Values(fbsmodel.Enums[i].Values[j], j) { + return fmt.Errorf("can't access enum %s value %d", string(fbsmodel.Enums[i].enum.Name()), j) + } + } + } + for i := 0; i < schema.ObjectsLength(); i++ { var object reflection.Object if !schema.Objects(&object, i) { @@ -132,7 +147,7 @@ func (r *fbSchemaReader) readObject(object *reflection.Object) error { func (r *fbSchemaReader) readObjectField(entity *model.Entity, field *reflection.Field) error { var property = model.CreateProperty(entity, 0, 0) - var metaProperty = &fbsField{binding.CreateField(property), field} + var metaProperty = &fbsField{binding.CreateField(property), r.model.Meta.(*fbsModel), field} property.Meta = metaProperty metaProperty.SetName(string(field.Name())) diff --git a/internal/generator/c/templates/binding-c.go b/internal/generator/c/templates/binding-c.go index 768cfee..caede61 100644 --- a/internal/generator/c/templates/binding-c.go +++ b/internal/generator/c/templates/binding-c.go @@ -46,14 +46,20 @@ static void* {{.FileIdentifier}}_get_object(OBX_box* box, obx_id id, void* (*fro /// Internal function used in other generated functions to get a vTable offset for a given field. static flatbuffers_voffset_t {{.FileIdentifier}}_fb_field_offset(flatbuffers_voffset_t vs, const flatbuffers_voffset_t* vt, size_t field); - +{{range $enum := .Model.Meta.Enums}} +typedef enum { + {{- range $value := $enum.Values}} + {{with CNamespace $enum.Namespace}}{{.}}_{{end}}{{$enum.Name}}_{{String $value.Name}} = {{$value.Value}}, + {{- end}} +} {{with CNamespace $enum.Namespace}}{{.}}_{{end}}{{$enum.Name}}; +{{end}} {{range $entity := .Model.EntitiesWithMeta}} {{PrintComments 0 $entity.Comments}}typedef struct {{$entity.Meta.CName}} { {{range $property := $entity.Properties}}{{$propType := PropTypeName $property.Type -}} {{PrintComments 1 $property.Comments}}{{if $property.Meta.FbIsVector}}{{$property.Meta.CElementType}}* {{$property.Meta.CppName}}; {{- if or (eq $propType "StringVector") (eq $propType "ByteVector")}} size_t {{$property.Meta.CppName}}_len;{{end}} - {{else}}{{$property.Meta.CppType}}{{if $property.Meta.Optional}}*{{end}} {{$property.Meta.CppName}}; + {{else}}{{CNamespace $property.Meta.CppType}}{{if $property.Meta.Optional}}*{{end}} {{$property.Meta.CppName}}; {{end}}{{end}} } {{$entity.Meta.CName}}; diff --git a/internal/generator/c/templates/binding-cpp.go b/internal/generator/c/templates/binding-cpp.go index feb9f2e..181514b 100644 --- a/internal/generator/c/templates/binding-cpp.go +++ b/internal/generator/c/templates/binding-cpp.go @@ -103,7 +103,7 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo: {{- end}}; }{{- else}}outObject.{{$property.Meta.CppName}} {{- template "field-value-assign-pre" $property.Meta -}} - table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}} + table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, static_cast<{{$property.Meta.CppFbType}}>({{$property.Meta.FbDefaultValue}})){{if eq "bool" $property.Meta.CppType}} != 0{{end}} {{- template "field-value-assign-post" $property.Meta}}; {{- end}} {{end}} diff --git a/internal/generator/c/templates/binding-hpp.go b/internal/generator/c/templates/binding-hpp.go index 45fd811..b979416 100644 --- a/internal/generator/c/templates/binding-hpp.go +++ b/internal/generator/c/templates/binding-hpp.go @@ -40,9 +40,22 @@ var CppBindingTemplateHeader = template.Must(template.New("binding-hpp").Funcs(f #include "flatbuffers/flatbuffers.h" #include "objectbox.h" #include "objectbox.hpp" +{{- range $enum := .Model.Meta.Enums}} +{{with CppNamespaceStart $enum.Namespace}} +{{.}}{{end}} +enum class {{$enum.Name}} : {{$enum.UnderlyingCppType}} { + {{- range $value := $enum.Values}} + {{String $value.Name}} = {{$value.Value}}, + {{- end}} +}; +{{- if $enum.HasAttribute "bit_flags"}} +FLATBUFFERS_DEFINE_BITMASK_OPERATORS({{$enum.Name}}, {{$enum.UnderlyingCppType}}) +{{- end}} +{{with CppNamespaceEnd $enum.Namespace}}{{.}}{{end -}} +{{end}} {{range $entity := .Model.EntitiesWithMeta}} {{$entity.Meta.PreDeclareCppRelTargets -}} -{{with $entity.Meta.CppNamespaceStart}} +{{with CppNamespaceStart $entity.Meta.Namespace}} {{.}}{{end}} struct {{$entity.Meta.CppName}}_; @@ -81,6 +94,6 @@ struct {{$entity.Meta.CppName}}_ { static const obx::RelationStandalone<{{$entity.Meta.CppName}}, {{$relation.Target.Meta.CppName}}> {{$relation.Meta.CppName}}; {{- end}} }; -{{with $entity.Meta.CppNamespaceEnd}}{{.}}{{end -}} +{{with CppNamespaceEnd $entity.Meta.Namespace}}{{.}}{{end -}} {{end}} `)) diff --git a/internal/generator/c/templates/functions.go b/internal/generator/c/templates/functions.go index 95c0e8f..8e22529 100644 --- a/internal/generator/c/templates/functions.go +++ b/internal/generator/c/templates/functions.go @@ -93,4 +93,33 @@ var funcMap = template.FuncMap{ "IsOptionalPtr": func(optional string) bool { return optional == "std::unique_ptr" || optional == "std::shared_ptr" }, + "CNamespace": func (ns string) string { + return strings.ReplaceAll(strings.ReplaceAll(ns, ".", "_"), "::", "_") + }, + "CppNamespaceStart": func(ns string) string { + if len(ns) == 0 { + return "" + } + + var nss = strings.Split(ns, ".") + for i, ns := range nss { + nss[i] = "namespace " + ns + " {" + } + return strings.Join(nss, "\n") + }, + "CppNamespaceEnd": func(ns string) string { + if len(ns) == 0 { + return "" + } + var result = "" + var nss = strings.Split(ns, ".") + for _, ns := range nss { + // print in reversed order + result = "} // namespace " + ns + "\n" + result + } + return result + }, + "String": func(val []byte) string { + return string(val) + }, } diff --git a/internal/generator/merge.go b/internal/generator/merge.go index c5b5b3a..b90cade 100644 --- a/internal/generator/merge.go +++ b/internal/generator/merge.go @@ -48,6 +48,12 @@ func mergeBindingWithModelInfo(currentModel *model.ModelInfo, storedModel *model currentModel.LastIndexId = storedModel.LastIndexId currentModel.LastRelationId = storedModel.LastRelationId + if currentModel.Meta != nil { + storedModel.Meta = currentModel.Meta.Merge(storedModel) + } else { + storedModel.Meta = nil + } + return nil } diff --git a/internal/generator/model/meta.go b/internal/generator/model/meta.go index 0155503..9e22b36 100644 --- a/internal/generator/model/meta.go +++ b/internal/generator/model/meta.go @@ -19,6 +19,12 @@ package model +// ModelMeta provides a way for bindings to provide additional information to other users of ModelInfo +type ModelMeta interface { + // Merge produces new ModelMeta based on its internal state and given model + Merge(model *ModelInfo) ModelMeta +} + // EntityMeta provides a way for bindings to provide additional information to other users of Entity type EntityMeta interface { // Merge produces new EntityMeta based on its internal state and given entity diff --git a/internal/generator/model/model.go b/internal/generator/model/model.go index fc631e6..b7c0ee8 100644 --- a/internal/generator/model/model.go +++ b/internal/generator/model/model.go @@ -62,6 +62,7 @@ type ModelInfo struct { file *os.File // file handle, locked while the model is open Rand *rand.Rand `json:"-"` // seeded random number generator + Meta ModelMeta `json:"-"` } var defaultModel = ModelInfo{ diff --git a/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected b/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected index 0bf39bc..32a1ba3 100644 --- a/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected +++ b/test/comparison/testdata/fbs/typeful/c/schema.obx.h.expected @@ -20,6 +20,18 @@ static void* schema_obx_h_get_object(OBX_box* box, obx_id id, void* (*from_flatb /// Internal function used in other generated functions to get a vTable offset for a given field. static flatbuffers_voffset_t schema_obx_h_fb_field_offset(flatbuffers_voffset_t vs, const flatbuffers_voffset_t* vt, size_t field); +typedef enum { + ns_Color_none = 0, + ns_Color_blue = 1, + ns_Color_green = 2, + ns_Color_red = 3, +} ns_Color; + +typedef enum { + ns_Flags_a = 1, + ns_Flags_b = 2, +} ns_Flags; + /// Entity documentation is copied /// into the generated output @@ -223,8 +235,8 @@ static void ns_TSDateNano_free(ns_TSDateNano* object); typedef struct ns_WithEnums { obx_id id; - uint8_t color; - uint32_t flags; + ns_Color color; + ns_Flags flags; } ns_WithEnums; diff --git a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected index 0acf72a..4f72d43 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected @@ -76,18 +76,18 @@ std::unique_ptr Typeful::_OBX_MetaInfo::newFromFlatBuffer(const void* d void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, 0); - outObject.int_ = table->GetField(6, 0); - outObject.int8 = table->GetField(8, 0); - outObject.int16 = table->GetField(10, 0); - outObject.int32 = table->GetField(12, 0); - outObject.int64 = table->GetField(14, 0); - outObject.uint = table->GetField(16, 0); - outObject.uint8 = table->GetField(18, 0); - outObject.uint16 = table->GetField(20, 0); - outObject.uint32 = table->GetField(22, 0); - outObject.uint64 = table->GetField(24, 0); - outObject.bool_ = table->GetField(26, 0) != 0; + outObject.id = table->GetField(4, static_cast(0)); + outObject.int_ = table->GetField(6, static_cast(0)); + outObject.int8 = table->GetField(8, static_cast(0)); + outObject.int16 = table->GetField(10, static_cast(0)); + outObject.int32 = table->GetField(12, static_cast(0)); + outObject.int64 = table->GetField(14, static_cast(0)); + outObject.uint = table->GetField(16, static_cast(0)); + outObject.uint8 = table->GetField(18, static_cast(0)); + outObject.uint16 = table->GetField(20, static_cast(0)); + outObject.uint32 = table->GetField(22, static_cast(0)); + outObject.uint64 = table->GetField(24, static_cast(0)); + outObject.bool_ = table->GetField(26, static_cast(0)) != 0; { auto* ptr = table->GetPointer(28); if (ptr) outObject.string.assign(ptr->c_str()); @@ -102,8 +102,8 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o } } } - outObject.byte = table->GetField(32, 0); - outObject.ubyte = table->GetField(34, 0); + outObject.byte = table->GetField(32, static_cast(0)); + outObject.ubyte = table->GetField(34, static_cast(0)); { auto* ptr = table->GetPointer*>(36); if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end()); @@ -112,11 +112,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o auto* ptr = table->GetPointer*>(38); if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end()); } - outObject.float32 = table->GetField(40, 0.0f); - outObject.float64 = table->GetField(42, 0.0); - outObject.float_ = table->GetField(44, 0.0f); - outObject.double_ = table->GetField(46, 0.0); - outObject.relId = table->GetField(48, 0); + outObject.float32 = table->GetField(40, static_cast(0.0f)); + outObject.float64 = table->GetField(42, static_cast(0.0)); + outObject.float_ = table->GetField(44, static_cast(0.0f)); + outObject.double_ = table->GetField(46, static_cast(0.0)); + outObject.relId = table->GetField(48, static_cast(0)); } @@ -169,13 +169,13 @@ std::unique_ptr ns::Annotated::_OBX_MetaInfo::newFromFlatBuffer(c void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::Annotated& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.identifier = table->GetField(4, 0); + outObject.identifier = table->GetField(4, static_cast(0)); { auto* ptr = table->GetPointer(6); if (ptr) outObject.fullName.assign(ptr->c_str()); } - outObject.time = table->GetField(8, 0); - outObject.relId = table->GetField(10, 0); + outObject.time = table->GetField(8, static_cast(0)); + outObject.relId = table->GetField(10, static_cast(0)); { auto* ptr = table->GetPointer(12); if (ptr) outObject.unique.assign(ptr->c_str()); @@ -192,7 +192,7 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:: auto* ptr = table->GetPointer(18); if (ptr) outObject.uniqueHash64.assign(ptr->c_str()); } - outObject.uid = table->GetField(20, 0); + outObject.uid = table->GetField(20, static_cast(0)); } @@ -224,8 +224,8 @@ std::unique_ptr ns::TSDate::_OBX_MetaInfo::newFromFlatBuffer(const v void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSDate& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, 0); - outObject.timestamp = table->GetField(6, 0); + outObject.id = table->GetField(4, static_cast(0)); + outObject.timestamp = table->GetField(6, static_cast(0)); } @@ -257,8 +257,8 @@ std::unique_ptr ns::TSDateNano::_OBX_MetaInfo::newFromFlatBuffer void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSDateNano& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, 0); - outObject.timestamp = table->GetField(6, 0); + outObject.id = table->GetField(4, static_cast(0)); + outObject.timestamp = table->GetField(6, static_cast(0)); } @@ -270,8 +270,8 @@ void ns::WithEnums::_OBX_MetaInfo::toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb.Clear(); flatbuffers::uoffset_t fbStart = fbb.StartTable(); fbb.TrackField(4, fbb.PushElement(object.id)); - fbb.TrackField(6, fbb.PushElement(object.color)); - fbb.TrackField(8, fbb.PushElement(object.flags)); + fbb.TrackField(6, fbb.PushElement(object.color)); + fbb.TrackField(8, fbb.PushElement(object.flags)); flatbuffers::Offset offset; offset.o = fbb.EndTable(fbStart); fbb.Finish(offset); @@ -292,9 +292,9 @@ std::unique_ptr ns::WithEnums::_OBX_MetaInfo::newFromFlatBuffer(c void ns::WithEnums::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::WithEnums& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, 0); - outObject.color = table->GetField(6, 0); - outObject.flags = table->GetField(8, 0); + outObject.id = table->GetField(4, static_cast(0)); + outObject.color = table->GetField(6, static_cast(0)); + outObject.flags = table->GetField(8, static_cast(0)); } diff --git a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected index 6ca3e0d..be1827d 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.hpp.expected @@ -9,6 +9,25 @@ #include "objectbox.h" #include "objectbox.hpp" +namespace ns { +enum class Color : uint8_t { + none = 0, + blue = 1, + green = 2, + red = 3, +}; +} // namespace ns + + +namespace ns { +enum class Flags : uint32_t { + a = 1, + b = 2, +}; +FLATBUFFERS_DEFINE_BITMASK_OPERATORS(Flags, uint32_t) +} // namespace ns + + namespace ns { struct AnnotatedEntity; } struct Typeful_; @@ -212,8 +231,8 @@ struct WithEnums_; struct WithEnums { obx_id id; - uint8_t color; - uint32_t flags; + ns::Color color; + ns::Flags flags; struct _OBX_MetaInfo { static constexpr obx_schema_id entityId() { return 5; } diff --git a/third_party/flatbuffers-c-bridge/src/flatbuffersc.cpp b/third_party/flatbuffers-c-bridge/src/flatbuffersc.cpp index 710d6f0..05ca71f 100644 --- a/third_party/flatbuffers-c-bridge/src/flatbuffersc.cpp +++ b/third_party/flatbuffers-c-bridge/src/flatbuffersc.cpp @@ -42,6 +42,7 @@ FBS_bytes* fbs_schema_parse_file(const char* filename, const char** out_error) { auto options = flatbuffers::IDLOptions(); options.binary_schema_comments = true; // include doc comments in the binary schema + options.binary_schema_builtins = true; // include attributes flatbuffers::Parser parser(options); if (!parser.Parse(contents.c_str(), nullptr, filename)) { From 204cafc0f20662a31470b8d08e6d97f31abc9d3d Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Tue, 31 Aug 2021 19:07:32 +0200 Subject: [PATCH 3/3] c/c++ - don't do static_cast on plain scalars --- internal/generator/c/meta.go | 14 ++++- internal/generator/c/templates/binding-cpp.go | 2 +- .../fbs/typeful/cpp/schema.obx.cpp.expected | 56 +++++++++---------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/internal/generator/c/meta.go b/internal/generator/c/meta.go index 5ce3029..bcc4657 100644 --- a/internal/generator/c/meta.go +++ b/internal/generator/c/meta.go @@ -154,12 +154,21 @@ func (mp *fbsField) CppType() string { cppType = cppType + "<" + fbsTypeToCppType[fbsType.Element()] + ">" } else if (mp.ModelProperty.IsIdProperty() || mp.ModelProperty.Type == model.PropertyTypeRelation) && cppType == "uint64_t" { cppType = "obx_id" // defined in objectbox.h - } else if enumIndex := fbsType.Index(); isEnumSupportedType(baseType) && enumIndex >= 0 { + } else if enumIndex := mp.enumIndex(); enumIndex >= 0 { cppType = strings.ReplaceAll(string(mp.fbsModel.Enums[enumIndex].enum.Name()), ".", "::") } return cppType } +func (mp *fbsField) enumIndex() int { + var fbsType = mp.fbsField.Type(nil) + var baseType = fbsType.BaseType() + if isEnumSupportedType(baseType) { + return int(fbsType.Index()) + } + return -1 +} + // CppFbType returns C++ type name used in flatbuffers templated functions func (mp *fbsField) CppFbType() string { var cppType = mp.CppType() @@ -261,6 +270,9 @@ func (mp *fbsField) FbDefaultValue() string { case model.PropertyTypeDouble: return "0.0" } + if enumIndex := mp.enumIndex(); enumIndex >= 0 { + return fmt.Sprintf("static_cast<%s>(0)", mp.CppFbType()) + } return "0" } diff --git a/internal/generator/c/templates/binding-cpp.go b/internal/generator/c/templates/binding-cpp.go index 181514b..feb9f2e 100644 --- a/internal/generator/c/templates/binding-cpp.go +++ b/internal/generator/c/templates/binding-cpp.go @@ -103,7 +103,7 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo: {{- end}}; }{{- else}}outObject.{{$property.Meta.CppName}} {{- template "field-value-assign-pre" $property.Meta -}} - table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, static_cast<{{$property.Meta.CppFbType}}>({{$property.Meta.FbDefaultValue}})){{if eq "bool" $property.Meta.CppType}} != 0{{end}} + table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}} {{- template "field-value-assign-post" $property.Meta}}; {{- end}} {{end}} diff --git a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected index 4f72d43..3c031cb 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected @@ -76,18 +76,18 @@ std::unique_ptr Typeful::_OBX_MetaInfo::newFromFlatBuffer(const void* d void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, static_cast(0)); - outObject.int_ = table->GetField(6, static_cast(0)); - outObject.int8 = table->GetField(8, static_cast(0)); - outObject.int16 = table->GetField(10, static_cast(0)); - outObject.int32 = table->GetField(12, static_cast(0)); - outObject.int64 = table->GetField(14, static_cast(0)); - outObject.uint = table->GetField(16, static_cast(0)); - outObject.uint8 = table->GetField(18, static_cast(0)); - outObject.uint16 = table->GetField(20, static_cast(0)); - outObject.uint32 = table->GetField(22, static_cast(0)); - outObject.uint64 = table->GetField(24, static_cast(0)); - outObject.bool_ = table->GetField(26, static_cast(0)) != 0; + outObject.id = table->GetField(4, 0); + outObject.int_ = table->GetField(6, 0); + outObject.int8 = table->GetField(8, 0); + outObject.int16 = table->GetField(10, 0); + outObject.int32 = table->GetField(12, 0); + outObject.int64 = table->GetField(14, 0); + outObject.uint = table->GetField(16, 0); + outObject.uint8 = table->GetField(18, 0); + outObject.uint16 = table->GetField(20, 0); + outObject.uint32 = table->GetField(22, 0); + outObject.uint64 = table->GetField(24, 0); + outObject.bool_ = table->GetField(26, 0) != 0; { auto* ptr = table->GetPointer(28); if (ptr) outObject.string.assign(ptr->c_str()); @@ -102,8 +102,8 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o } } } - outObject.byte = table->GetField(32, static_cast(0)); - outObject.ubyte = table->GetField(34, static_cast(0)); + outObject.byte = table->GetField(32, 0); + outObject.ubyte = table->GetField(34, 0); { auto* ptr = table->GetPointer*>(36); if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end()); @@ -112,11 +112,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o auto* ptr = table->GetPointer*>(38); if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end()); } - outObject.float32 = table->GetField(40, static_cast(0.0f)); - outObject.float64 = table->GetField(42, static_cast(0.0)); - outObject.float_ = table->GetField(44, static_cast(0.0f)); - outObject.double_ = table->GetField(46, static_cast(0.0)); - outObject.relId = table->GetField(48, static_cast(0)); + outObject.float32 = table->GetField(40, 0.0f); + outObject.float64 = table->GetField(42, 0.0); + outObject.float_ = table->GetField(44, 0.0f); + outObject.double_ = table->GetField(46, 0.0); + outObject.relId = table->GetField(48, 0); } @@ -169,13 +169,13 @@ std::unique_ptr ns::Annotated::_OBX_MetaInfo::newFromFlatBuffer(c void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::Annotated& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.identifier = table->GetField(4, static_cast(0)); + outObject.identifier = table->GetField(4, 0); { auto* ptr = table->GetPointer(6); if (ptr) outObject.fullName.assign(ptr->c_str()); } - outObject.time = table->GetField(8, static_cast(0)); - outObject.relId = table->GetField(10, static_cast(0)); + outObject.time = table->GetField(8, 0); + outObject.relId = table->GetField(10, 0); { auto* ptr = table->GetPointer(12); if (ptr) outObject.unique.assign(ptr->c_str()); @@ -192,7 +192,7 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:: auto* ptr = table->GetPointer(18); if (ptr) outObject.uniqueHash64.assign(ptr->c_str()); } - outObject.uid = table->GetField(20, static_cast(0)); + outObject.uid = table->GetField(20, 0); } @@ -224,8 +224,8 @@ std::unique_ptr ns::TSDate::_OBX_MetaInfo::newFromFlatBuffer(const v void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSDate& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, static_cast(0)); - outObject.timestamp = table->GetField(6, static_cast(0)); + outObject.id = table->GetField(4, 0); + outObject.timestamp = table->GetField(6, 0); } @@ -257,8 +257,8 @@ std::unique_ptr ns::TSDateNano::_OBX_MetaInfo::newFromFlatBuffer void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSDateNano& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, static_cast(0)); - outObject.timestamp = table->GetField(6, static_cast(0)); + outObject.id = table->GetField(4, 0); + outObject.timestamp = table->GetField(6, 0); } @@ -292,7 +292,7 @@ std::unique_ptr ns::WithEnums::_OBX_MetaInfo::newFromFlatBuffer(c void ns::WithEnums::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::WithEnums& outObject) { const auto* table = flatbuffers::GetRoot(data); assert(table); - outObject.id = table->GetField(4, static_cast(0)); + outObject.id = table->GetField(4, 0); outObject.color = table->GetField(6, static_cast(0)); outObject.flags = table->GetField(8, static_cast(0));