Modify Map & Set implementation of C generator 50/296950/6
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 8 Aug 2023 01:42:15 +0000 (10:42 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 8 Aug 2023 04:31:47 +0000 (04:31 +0000)
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 <h.jhun@samsung.com>
idlc/gen/version2/c_body_generator_base.cc
idlc/gen/version2/c_body_generator_base.hh
idlc/gen/version2/c_body_generator_map_base_cb.hh
idlc/gen/version2/c_body_generator_set_base_cb.hh
idlc/gen/version2/c_header_generator_map_base_cb.hh
idlc/gen/version2/c_header_generator_set_base_cb.hh

index 0869bb2d5e19183ae18d908896cf68f5bfef4f3c..a19a0f3058bbd3f0c124df6cdad27c4ec06184cc 100644 (file)
@@ -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("<MAP_UNIT_MAP_VALUE_READ>", GenMapUnitMapValueRead(value_type))
       .Change("<MAP_COMPARE_KEY_VARS_DEF>",
           GenMapCompareKeyVarsDefinition(key_type))
+      .Change("<MAP_COMPARE_KEY_VARS_IMPL>",
+          GenMapCompareKeyVarsImpl(key_type))
       .Change("<MAP_KEY_FREE_FUNC>", GenMapFreeFunc(key_type))
       .Change("<MAP_VALUE_FREE_FUNC>", GenMapFreeFunc(value_type))
       .Change("<MAP_INSERT_ARGS_NULL_CHECK>",
@@ -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("<SET_UNIT_MAP_KEY_READ>", GenSetUnitMapKeyRead(key_type))
       .Change("<SET_COMPARE_KEY_VARS_DEF>",
           GenSetCompareKeyVarsDefinition(key_type))
+      .Change("<SET_COMPARE_KEY_VARS_IMPL>",
+          GenSetCompareKeyVarsImpl(key_type))
       .Change("<SET_KEY_FREE_FUNC>", GenSetKeyFreeFunc(key_type))
       .Change("<SET_INSERT_ARGS_NULL_CHECK>", GenSetKeyNullCheck(key_type))
       .Change("<SET_INSERT>", 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("<PREFIX>", GetHandlePrefix())
index dfdca332d9e10dac979cc5174b018d6acd778f3c..b2f4559a14e65f155d04559e0d008e76caea72d6 100644 (file)
@@ -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);
index 5eeb236acd4297822de1d1e1eff4a26f5a25a626..af727f6ae928b623b0cb03349d632c0c11d2edf3 100644 (file)
@@ -29,13 +29,9 @@ R"__c_cb(
 typedef struct <PREFIX>_<NAME>_s {
   rpc_port_parcelable_t parcelable;
   GTree *node;
-  <PREFIX>_<NAME>_compare_cb compare_cb;
   void *user_data;
 } <PREFIX>_<NAME>_t;
 
-static <PREFIX>_<NAME>_compare_cb __<PREFIX>_<NAME>_compare_cb;
-static void *__<PREFIX>_<NAME>_user_data;
-
 typedef struct <PREFIX>_<NAME>_traverse_info_s {
   rpc_port_unit_map_h map;
   int number;
@@ -60,6 +56,7 @@ typedef struct <PREFIX>_<NAME>_foreach_info_s {
  * <MAP_UNIT_MAP_KEY_READ> The implementation to read the key from the unit map.
  * <MAP_UNIT_MAP_VALUE_READ> The implementation to read the value from the unit map.
  * <MAP_COMPARE_KEY_VARS_DEF> The definition of the key variables of the compare callback function.
+ * <MAP_COMPARE_KEY_VARS_IMPL> The implemenation to compare key variables.
  * <MAP_KEY_FREE_FUNC> The implementation to free the key.
  * <MAP_VALUE_FREE_FUNC> The implementation to free the value.
  * <MAP_INSERT_ARGS_NULL_CHECK> The implementation to check whether the arg is nullptr or not.
@@ -176,12 +173,7 @@ static gint __<PREFIX>_<NAME>_comparison_cb(gconstpointer a, gconstpointer b, gp
 {
   <MAP_COMPARE_KEY_VARS_DEF>
 
-  if (__<PREFIX>_<NAME>_compare_cb == nullptr) {
-    _E("<PREFIX>_<NAME>_set_compare_cb() MUST be called");
-    return -1;
-  }
-
-  return __<PREFIX>_<NAME>_compare_cb(key_a, key_b, __<PREFIX>_<NAME>_user_data);
+  <MAP_COMPARE_KEY_VARS_IMPL>
 }
 
 int <PREFIX>_<NAME>_create(<PREFIX>_<NAME>_h *h)
@@ -193,11 +185,6 @@ int <PREFIX>_<NAME>_create(<PREFIX>_<NAME>_h *h)
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
 
-  if (__<PREFIX>_<NAME>_compare_cb == nullptr) {
-    _E("<PREFIX>_<NAME>_set_compare_cb() MUST be called");
-    return RPC_PORT_ERROR_IO_ERROR;
-  }
-
   handle = calloc(1, sizeof(<PREFIX>_<NAME>_t));
   if (handle == nullptr) {
     _E("Out of memory");
@@ -266,19 +253,6 @@ int <PREFIX>_<NAME>_clone(<PREFIX>_<NAME>_h h, <PREFIX>_<NAME>_h *clone)
   return ret;
 }
 
-int <PREFIX>_<NAME>_set_compare_cb(<PREFIX>_<NAME>_compare_cb callback, void *user_data)
-{
-  if (callback == nullptr) {
-    _E("Invalid parameter");
-    return RPC_PORT_ERROR_INVALID_PARAMETER;
-  }
-
-  __<PREFIX>_<NAME>_compare_cb = callback;
-  __<PREFIX>_<NAME>_user_data = user_data;
-
-  return RPC_PORT_ERROR_NONE;
-}
-
 int <PREFIX>_<NAME>_insert(<PREFIX>_<NAME>_h h, <KEY_PARAM_TYPE>key, <VALUE_PARAM_TYPE>value)
 {
   <PREFIX>_<NAME>_t *handle = h;
@@ -406,6 +380,30 @@ int <PREFIX>_<NAME>_clear(<PREFIX>_<NAME>_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";
+
 /**
  * <KEY> The name of the key.
  * <KEY_TYPE> The type of the key.
index 660bdfb3e24dd75bfac167978f510d05154f0cf8..fad02c5e62c19af6a4045d9a72cb145aa2f4e8cd 100644 (file)
@@ -31,9 +31,6 @@ typedef struct <PREFIX>_<NAME>_s {
   GTree *node;
 } <PREFIX>_<NAME>_t;
 
-static <PREFIX>_<NAME>_compare_cb __<PREFIX>_<NAME>_compare_cb;
-static void *__<PREFIX>_<NAME>_user_data;
-
 typedef struct <PREFIX>_<NAME>_traverse_info_s {
   rpc_port_unit_map_h map;
   int number;
@@ -53,6 +50,7 @@ typedef struct <PREFIX>_<NAME>_foreach_info_s {
  * <SET_UNIT_MAP_KEY_WRITE> The implementation to write the key  to the unit map.
  * <SET_UNIT_MAP_KEY_READ> The implementation to read the key from the unit map.
  * <SET_COMPARE_KEY_VARS_DEF> The definition of the key variables of the compare callback function.
+ * <SET_COMPARE_KEY_VARS_IMPL> The implementation to compare key variables.
  * <SET_KEY_FREE_FUNC> The implementation to free the key.
  * <SET_INSERT_ARGS_NULL_CHECK> The implementation to check whether the arg is nullptr or not.
  * <SET_INSERT> The implementation to insert the key and the value to the set.
@@ -158,25 +156,7 @@ static gint __<PREFIX>_<NAME>_comparison_cb(gconstpointer a, gconstpointer b, gp
 {
   <SET_COMPARE_KEY_VARS_DEF>
 
-  if (__<PREFIX>_<NAME>_compare_cb == nullptr) {
-    _E("<PREFIX>_<NAME>_set_compare_cb() MUST be called");
-    return -1;
-  }
-
-  return __<PREFIX>_<NAME>_compare_cb(key_a, key_b, __<PREFIX>_<NAME>_user_data);
-}
-
-int <PREFIX>_<NAME>_set_compare_cb(<PREFIX>_<NAME>_compare_cb callback, void *user_data)
-{
-  if (callback == nullptr) {
-    _E("Invalid parameter");
-    return RPC_PORT_ERROR_INVALID_PARAMETER;
-  }
-
-  __<PREFIX>_<NAME>_compare_cb = callback;
-  __<PREFIX>_<NAME>_user_data = user_data;
-
-  return RPC_PORT_ERROR_NONE;
+  <SET_COMPARE_KEY_VARS_IMPL>
 }
 
 int <PREFIX>_<NAME>_create(<PREFIX>_<NAME>_h *h)
@@ -188,11 +168,6 @@ int <PREFIX>_<NAME>_create(<PREFIX>_<NAME>_h *h)
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
 
-  if (__<PREFIX>_<NAME>_compare_cb == nullptr) {
-    _E("<PREFIX>_<NAME>_set_compare_cb() MUST be called");
-    return RPC_PORT_ERROR_IO_ERROR;
-  }
-
   handle = calloc(1, sizeof(<PREFIX>_<NAME>_t));
   if (handle == nullptr) {
     _E("Out of memory");
@@ -359,6 +334,30 @@ int <PREFIX>_<NAME>_clear(<PREFIX>_<NAME>_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";
+
 /**
  * <KEY> The name of the key.
  * <KEY_TYPE> The type of the key.
index 32a9697ba4ec2b638a933f8367a11c6e466b1c49..2281fae1f86a0049eedf7af7d29ff6dbcf23c82a 100644 (file)
@@ -30,19 +30,6 @@ namespace version2 {
 constexpr const char CB_STRUCTURE_MAP_BASE[] =
 R"__c_cb(
 
-/**
- * @brief Called to compare the keys in the <PREFIX>_<NAME> 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 <PREFIX>_<NAME>_set_compare_cb()
- */
-typedef int (*<PREFIX>_<NAME>_compare_cb)(<KEY_PARAM_TYPE>a, <KEY_PARAM_TYPE>b, void *user_data);
-
 /**
  * @brief Called to retrieve the key/value contained in the <PREFIX>_<NAME> handle.
  *
@@ -56,21 +43,6 @@ typedef int (*<PREFIX>_<NAME>_compare_cb)(<KEY_PARAM_TYPE>a, <KEY_PARAM_TYPE>b,
  */
 typedef bool (*<PREFIX>_<NAME>_foreach_cb)(<KEY_PARAM_TYPE>key, <VALUE_PARAM_TYPE>value, void *user_data);
 
-/**
- * @brief Sets the comparision callback function for the <PREFIX>_<NAME> handle.
- * @details The <PREFIX>_<NAME> uses the registered callback function.
- *          If it's not set, calling the <PREFIX>_<NAME>_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 <PREFIX>_<NAME>_create()
- */
-int <PREFIX>_<NAME>_set_compare_cb(<PREFIX>_<NAME>_compare_cb callback, void *user_data);
-
 /**
  * @brief Creates a <PREFIX>_<NAME> handle.
  *
index 8def3871b21aca49ce887f14257ef27e95d5a6e9..90608279334066a19d430363e85f38decbfc4a77 100644 (file)
@@ -28,19 +28,6 @@ namespace version2 {
 constexpr const char CB_STRUCTURE_SET_BASE[] =
 R"__c_cb(
 
-/**
- * @brief Called to compare the keys in the <PREFIX>_<NAME> 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 <PREFIX>_<NAME>_set_compare_cb()
- */
-typedef int (*<PREFIX>_<NAME>_compare_cb)(<KEY_PARAM_TYPE>a, <KEY_PARAM_TYPE>b, void *user_data);
-
 /**
  * @brief Called to retrieve the key contained in the <PREFIX>_<NAME> handle.
  *
@@ -53,21 +40,6 @@ typedef int (*<PREFIX>_<NAME>_compare_cb)(<KEY_PARAM_TYPE>a, <KEY_PARAM_TYPE>b,
  */
 typedef bool (*<PREFIX>_<NAME>_foreach_cb)(<KEY_PARAM_TYPE>key, void *user_data);
 
-/**
- * @brief Sets the comparision callback function for the <PREFIX>_<NAME> handle.
- * @details The <PREFIX>_<NAME> uses the registered callback function.
- *          If it's not set, calling the <PREFIX>_<NAME>_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 <PREFIX>_<NAME>_create()
- */
-int <PREFIX>_<NAME>_set_compare_cb(<PREFIX>_<NAME>_compare_cb callback, void *user_data);
-
 /**
  * @brief Creates a <PREFIX>_<NAME> handle.
  *