string t_c_glib_generator::generate_free_func_from_type()

in compiler/cpp/src/thrift/generate/t_c_glib_generator.cc [4342:4406]


string t_c_glib_generator::generate_free_func_from_type(t_type* ttype) {
  if (ttype == nullptr)
    return "NULL";

  if (ttype->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_VOID:
      throw "compiler error: cannot determine hash type";
      break;
    case t_base_type::TYPE_BOOL:
    case t_base_type::TYPE_I8:
    case t_base_type::TYPE_I16:
    case t_base_type::TYPE_I32:
    case t_base_type::TYPE_I64:
    case t_base_type::TYPE_DOUBLE:
      return "g_free";
    case t_base_type::TYPE_STRING:
      if (((t_base_type*)ttype)->is_binary()) {
        return "thrift_string_free";
      }
      return "g_free";
    default:
      throw "compiler error: no hash table info for type";
    }
  } else if (ttype->is_enum()) {
    return "NULL";
  } else if (ttype->is_map() || ttype->is_set()) {
    return "(GDestroyNotify) thrift_safe_hash_table_destroy";
  } else if (ttype->is_struct()) {
    return "g_object_unref";
  } else if (ttype->is_list()) {
    t_type* etype = ((t_list*)ttype)->get_elem_type();
    if (etype->is_base_type()) {
      t_base_type::t_base tbase = ((t_base_type*)etype)->get_base();
      switch (tbase) {
      case t_base_type::TYPE_VOID:
        throw "compiler error: cannot determine array type";
        break;
      case t_base_type::TYPE_BOOL:
      case t_base_type::TYPE_I8:
      case t_base_type::TYPE_I16:
      case t_base_type::TYPE_I32:
      case t_base_type::TYPE_I64:
      case t_base_type::TYPE_DOUBLE:
        return "(GDestroyNotify) g_array_unref";
      case t_base_type::TYPE_STRING:
        return "(GDestroyNotify) g_ptr_array_unref";
      default:
        throw "compiler error: no array info for type";
      }
    } else if (etype->is_container() || etype->is_struct()) {
      return "(GDestroyNotify) g_ptr_array_unref";
      ;
    } else if (etype->is_enum()) {
      return "(GDestroyNotify) g_array_unref";
    }
    printf("Type not expected inside the array: %s\n", etype->get_name().c_str());
    throw "Type not expected inside array";
  } else if (ttype->is_typedef()) {
    return generate_free_func_from_type(((t_typedef*)ttype)->get_type());
  }
  printf("Type not expected: %s\n", ttype->get_name().c_str());
  throw "Type not expected";
}