From: Hwankyu Jhun Date: Tue, 8 Aug 2023 01:42:15 +0000 (+0900) Subject: Modify Map & Set implementation of C generator X-Git-Tag: accepted/tizen/unified/20230914.164942~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=89e5ccb9643e454bbbaf9b218a0124fafe9bd268;p=platform%2Fcore%2Fappfw%2Ftidl.git Modify Map & Set implementation of C generator The key type of map and set containers should be 'char', 'int', 'short', 'string', 'bool', float' and 'double'. Change-Id: I92c3ac2f36d476733d1a8a05677c8e90c4576a2a Signed-off-by: Hwankyu Jhun --- diff --git a/idlc/gen/version2/c_body_generator_base.cc b/idlc/gen/version2/c_body_generator_base.cc index 0869bb2d..a19a0f30 100644 --- a/idlc/gen/version2/c_body_generator_base.cc +++ b/idlc/gen/version2/c_body_generator_base.cc @@ -712,6 +712,18 @@ std::string CBodyGeneratorBase::GenMapCompareKeyVarsDefinition( return RemoveLine(code); } +std::string CBodyGeneratorBase::GenMapCompareKeyVarsImpl(const BaseType& type) { + std::string code; + if (type.ToString() == "string") + code = CB_MAP_STRING_COMPARISION_IMPL; + else if (type.ToString() == "bool") + code = CB_MAP_BOOL_COMPARISION_IMPL; + else + code = CB_MAP_BASE_COMPARISION_IMPL; + + return RemoveLine(code); +} + std::string CBodyGeneratorBase::GenMapUnitMapValueRead( const BaseType& type) { std::string code; @@ -807,6 +819,8 @@ void CBodyGeneratorBase::GenStructureMapBase(std::ofstream& stream, .Change("", GenMapUnitMapValueRead(value_type)) .Change("", GenMapCompareKeyVarsDefinition(key_type)) + .Change("", + GenMapCompareKeyVarsImpl(key_type)) .Change("", GenMapFreeFunc(key_type)) .Change("", GenMapFreeFunc(value_type)) .Change("", @@ -937,6 +951,18 @@ std::string CBodyGeneratorBase::GenSetCompareKeyVarsDefinition( return RemoveLine(code); } +std::string CBodyGeneratorBase::GenSetCompareKeyVarsImpl(const BaseType& type) { + std::string code; + if (type.ToString() == "string") + code = CB_SET_STRING_COMPARISION_IMPL; + else if (type.ToString() == "bool") + code = CB_SET_BOOL_COMPARISION_IMPL; + else + code = CB_SET_BASE_COMPARISION_IMPL; + + return RemoveLine(code); +} + std::string CBodyGeneratorBase::GenSetUnitMapKeyRead(const BaseType& type) { std::string code; @@ -981,6 +1007,8 @@ void CBodyGeneratorBase::GenStructureSetBase(std::ofstream& stream, .Change("", GenSetUnitMapKeyRead(key_type)) .Change("", GenSetCompareKeyVarsDefinition(key_type)) + .Change("", + GenSetCompareKeyVarsImpl(key_type)) .Change("", GenSetKeyFreeFunc(key_type)) .Change("", GenSetKeyNullCheck(key_type)) .Change("", GenSetInsert(key_type)) @@ -1116,8 +1144,10 @@ std::string CBodyGeneratorBase::GenBaseSetGet(const std::string& name, std::string code; for (const auto& elm : elms) { auto& type = elm->GetType(); - auto param_type_in = GetParamTypeString(ParameterType::Direction::IN, type, name); - auto param_type_out = GetParamTypeString(ParameterType::Direction::OUT, type, name); + auto param_type_in = + GetParamTypeString(ParameterType::Direction::IN, type, name); + auto param_type_out = + GetParamTypeString(ParameterType::Direction::OUT, type, name); code += ReplaceAll(CB_STRUCTURE_BASE_SET_GET) .Change("", GetHandlePrefix()) diff --git a/idlc/gen/version2/c_body_generator_base.hh b/idlc/gen/version2/c_body_generator_base.hh index dfdca332..b2f4559a 100644 --- a/idlc/gen/version2/c_body_generator_base.hh +++ b/idlc/gen/version2/c_body_generator_base.hh @@ -84,6 +84,7 @@ class CBodyGeneratorBase : public tidl::CBodyGeneratorBase { const BaseType& value_type); std::string GenMapFreeFunc(const BaseType& type); std::string GenMapCompareKeyVarsDefinition(const BaseType& type); + std::string GenMapCompareKeyVarsImpl(const BaseType& type); std::string GenMapUnitMapValueRead(const BaseType& type); std::string GenMapUnitMapKeyRead(const BaseType& type); std::string GenMapUnitMapValueWrite(const BaseType& type); @@ -97,6 +98,7 @@ class CBodyGeneratorBase : public tidl::CBodyGeneratorBase { std::string GenSetKeyNullCheck(const BaseType& type); std::string GenSetKeyFreeFunc(const BaseType& type); std::string GenSetCompareKeyVarsDefinition(const BaseType& type); + std::string GenSetCompareKeyVarsImpl(const BaseType& type); std::string GenSetUnitMapKeyRead(const BaseType& type); std::string GenSetUnitMapKeyWrite(const BaseType& type); void GenStructureSetBase(std::ofstream& stream, const Structure& st); diff --git a/idlc/gen/version2/c_body_generator_map_base_cb.hh b/idlc/gen/version2/c_body_generator_map_base_cb.hh index 5eeb236a..af727f6a 100644 --- a/idlc/gen/version2/c_body_generator_map_base_cb.hh +++ b/idlc/gen/version2/c_body_generator_map_base_cb.hh @@ -29,13 +29,9 @@ R"__c_cb( typedef struct __s { rpc_port_parcelable_t parcelable; GTree *node; - __compare_cb compare_cb; void *user_data; } __t; -static __compare_cb ____compare_cb; -static void *____user_data; - typedef struct __traverse_info_s { rpc_port_unit_map_h map; int number; @@ -60,6 +56,7 @@ typedef struct __foreach_info_s { * The implementation to read the key from the unit map. * The implementation to read the value from the unit map. * The definition of the key variables of the compare callback function. + * The implemenation to compare key variables. * The implementation to free the key. * The implementation to free the value. * The implementation to check whether the arg is nullptr or not. @@ -176,12 +173,7 @@ static gint ____comparison_cb(gconstpointer a, gconstpointer b, gp { - if (____compare_cb == nullptr) { - _E("__set_compare_cb() MUST be called"); - return -1; - } - - return ____compare_cb(key_a, key_b, ____user_data); + } int __create(__h *h) @@ -193,11 +185,6 @@ int __create(__h *h) return RPC_PORT_ERROR_INVALID_PARAMETER; } - if (____compare_cb == nullptr) { - _E("__set_compare_cb() MUST be called"); - return RPC_PORT_ERROR_IO_ERROR; - } - handle = calloc(1, sizeof(__t)); if (handle == nullptr) { _E("Out of memory"); @@ -266,19 +253,6 @@ int __clone(__h h, __h *clone) return ret; } -int __set_compare_cb(__compare_cb callback, void *user_data) -{ - if (callback == nullptr) { - _E("Invalid parameter"); - return RPC_PORT_ERROR_INVALID_PARAMETER; - } - - ____compare_cb = callback; - ____user_data = user_data; - - return RPC_PORT_ERROR_NONE; -} - int __insert(__h h, key, value) { __t *handle = h; @@ -406,6 +380,30 @@ int __clear(__h h) } )__c_cb"; +constexpr const char CB_MAP_STRING_COMPARISION_IMPL[] = +R"__c_cb( +return strcmp(key_a, key_b); +)__c_cb"; + +constexpr const char CB_MAP_BASE_COMPARISION_IMPL[] = +R"__c_cb( +if (key_a == key_b) + return 0; + +if (key_a > key_b) + return 1; + +return -1; +)__c_cb"; + +constexpr const char CB_MAP_BOOL_COMPARISION_IMPL[] = +R"__c_cb( +if (key_a == key_b) + return 0; + +return -1; +)__c_cb"; + /** * The name of the key. * The type of the key. diff --git a/idlc/gen/version2/c_body_generator_set_base_cb.hh b/idlc/gen/version2/c_body_generator_set_base_cb.hh index 660bdfb3..fad02c5e 100644 --- a/idlc/gen/version2/c_body_generator_set_base_cb.hh +++ b/idlc/gen/version2/c_body_generator_set_base_cb.hh @@ -31,9 +31,6 @@ typedef struct __s { GTree *node; } __t; -static __compare_cb ____compare_cb; -static void *____user_data; - typedef struct __traverse_info_s { rpc_port_unit_map_h map; int number; @@ -53,6 +50,7 @@ typedef struct __foreach_info_s { * The implementation to write the key to the unit map. * The implementation to read the key from the unit map. * The definition of the key variables of the compare callback function. + * The implementation to compare key variables. * The implementation to free the key. * The implementation to check whether the arg is nullptr or not. * The implementation to insert the key and the value to the set. @@ -158,25 +156,7 @@ static gint ____comparison_cb(gconstpointer a, gconstpointer b, gp { - if (____compare_cb == nullptr) { - _E("__set_compare_cb() MUST be called"); - return -1; - } - - return ____compare_cb(key_a, key_b, ____user_data); -} - -int __set_compare_cb(__compare_cb callback, void *user_data) -{ - if (callback == nullptr) { - _E("Invalid parameter"); - return RPC_PORT_ERROR_INVALID_PARAMETER; - } - - ____compare_cb = callback; - ____user_data = user_data; - - return RPC_PORT_ERROR_NONE; + } int __create(__h *h) @@ -188,11 +168,6 @@ int __create(__h *h) return RPC_PORT_ERROR_INVALID_PARAMETER; } - if (____compare_cb == nullptr) { - _E("__set_compare_cb() MUST be called"); - return RPC_PORT_ERROR_IO_ERROR; - } - handle = calloc(1, sizeof(__t)); if (handle == nullptr) { _E("Out of memory"); @@ -359,6 +334,30 @@ int __clear(__h h) } )__c_cb"; +constexpr const char CB_SET_STRING_COMPARISION_IMPL[] = +R"__c_cb( +return strcmp(key_a, key_b); +)__c_cb"; + +constexpr const char CB_SET_BASE_COMPARISION_IMPL[] = +R"__c_cb( +if (key_a == key_b) + return 0; + +if (key_a > key_b) + return 1; + +return -1; +)__c_cb"; + +constexpr const char CB_SET_BOOL_COMPARISION_IMPL[] = +R"__c_cb( +if (key_a == key_b) + return 0; + +return -1; +)__c_cb"; + /** * The name of the key. * The type of the key. diff --git a/idlc/gen/version2/c_header_generator_map_base_cb.hh b/idlc/gen/version2/c_header_generator_map_base_cb.hh index 32a9697b..2281fae1 100644 --- a/idlc/gen/version2/c_header_generator_map_base_cb.hh +++ b/idlc/gen/version2/c_header_generator_map_base_cb.hh @@ -30,19 +30,6 @@ namespace version2 { constexpr const char CB_STRUCTURE_MAP_BASE[] = R"__c_cb( -/** - * @brief Called to compare the keys in the _ handle. - * - * @param[in] a A key - * @param[in] b B key - * @param[in] user_data The user data passed from the registration function - * @return Nagative value if a < b, - * zero if a = b, - * positive value if a > b. - * @see __set_compare_cb() - */ -typedef int (*__compare_cb)(a, b, void *user_data); - /** * @brief Called to retrieve the key/value contained in the _ handle. * @@ -56,21 +43,6 @@ typedef int (*__compare_cb)(a, b, */ typedef bool (*__foreach_cb)(key, value, void *user_data); -/** - * @brief Sets the comparision callback function for the _ handle. - * @details The _ uses the registered callback function. - * If it's not set, calling the __create() returns a negative error value. - * - * @param[in] callback The comparison callback function - * @param[in] user_data The user data to be passed to the comparision callback function - * @return @c 0 on success, - * otherwise a negative error value - * @retval #RPC_PORT_ERROR_NONE Successful - * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter - * @see __create() - */ -int __set_compare_cb(__compare_cb callback, void *user_data); - /** * @brief Creates a _ handle. * diff --git a/idlc/gen/version2/c_header_generator_set_base_cb.hh b/idlc/gen/version2/c_header_generator_set_base_cb.hh index 8def3871..90608279 100644 --- a/idlc/gen/version2/c_header_generator_set_base_cb.hh +++ b/idlc/gen/version2/c_header_generator_set_base_cb.hh @@ -28,19 +28,6 @@ namespace version2 { constexpr const char CB_STRUCTURE_SET_BASE[] = R"__c_cb( -/** - * @brief Called to compare the keys in the _ handle. - * - * @param[in] a A key - * @param[in] b B key - * @param[in] user_data The user data passed from the registration function - * @return Nagative value if a < b, - * zero if a = b, - * positive value if a > b. - * @see __set_compare_cb() - */ -typedef int (*__compare_cb)(a, b, void *user_data); - /** * @brief Called to retrieve the key contained in the _ handle. * @@ -53,21 +40,6 @@ typedef int (*__compare_cb)(a, b, */ typedef bool (*__foreach_cb)(key, void *user_data); -/** - * @brief Sets the comparision callback function for the _ handle. - * @details The _ uses the registered callback function. - * If it's not set, calling the __create() returns a negative error value. - * - * @param[in] callback The comparison callback function - * @param[in] user_data The user data to be passed to the comparison callback function - * @return @c 0 on success, - * otherwise a negative error value - * @retval #RPC_PORT_ERROR_NONE Successful - * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter - * @see __create() - */ -int __set_compare_cb(__compare_cb callback, void *user_data); - /** * @brief Creates a _ handle. *