case nsIDataType::VTYPE_EMPTY_ARRAY: if(inVarType != nsIDataType::VTYPE_EMPTY_ARRAY) return NS_ERROR_CANNOT_CONVERT_DATA; rv = outVar->SetAsEmptyArray(); NS_ENSURE_SUCCESS(rv,rv); break;
static nsresult ConvertAndCopyVariant(nsIVariant *inVar, PRUint16 type, nsIVariant **_retval){    nsresult rv;        nsCOMPtr<nsIWritableVariant> outVar;    outVar = do_CreateInstance("@mozilla.org/variant;1");    if(!outVar)        return NS_ERROR_FAILURE;    PRUint16 inVarType;    rv = inVar->GetDataType(&inVarType);    if(NS_FAILED(rv))        return rv;    nsDiscriminatedUnion du;    nsVariant::Initialize(&du);    switch(type)    {    case nsIDataType::VTYPE_INT8:        MEMBER_COPY_CAST(Int8, PRUint8)        break;    case nsIDataType::VTYPE_INT16:        MEMBER_COPY(Int16)        break;    case nsIDataType::VTYPE_INT32:                MEMBER_COPY(Int32)        break;    case nsIDataType::VTYPE_INT64:                MEMBER_COPY(Int64)        break;    case nsIDataType::VTYPE_UINT8:                MEMBER_COPY(Uint8)        break;    case nsIDataType::VTYPE_UINT16:                MEMBER_COPY(Uint16)        break;    case nsIDataType::VTYPE_UINT32:                MEMBER_COPY(Uint32)        break;    case nsIDataType::VTYPE_UINT64:                MEMBER_COPY(Uint64)        break;    case nsIDataType::VTYPE_FLOAT:                MEMBER_COPY(Float)        break;    case nsIDataType::VTYPE_DOUBLE:                MEMBER_COPY(Double)        break;    case nsIDataType::VTYPE_BOOL:                MEMBER_COPY(Bool)        break;    case nsIDataType::VTYPE_CHAR:                MEMBER_COPY(Char)        break;    case nsIDataType::VTYPE_WCHAR:                MEMBER_COPY(WChar)        break;    case nsIDataType::VTYPE_VOID:                if(inVarType != nsIDataType::VTYPE_VOID)            return NS_ERROR_CANNOT_CONVERT_DATA;        rv = outVar->SetAsVoid();        NS_ENSURE_SUCCESS(rv,rv);        break;    case nsIDataType::VTYPE_ID:                MEMBER_COPY(ID)        break;    case nsIDataType::VTYPE_ASTRING:            case nsIDataType::VTYPE_DOMSTRING:    {        nsAutoString str;        rv = inVar->GetAsAString(str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsAString(str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_UTF8STRING:    {        nsUTF8String str;        rv = inVar->GetAsAUTF8String(str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsAUTF8String(str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_CSTRING:    {        nsCAutoString str;        rv = inVar->GetAsACString(str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsACString(str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }        case nsIDataType::VTYPE_CHAR_STR:            {        char* str;        rv = inVar->GetAsString(&str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsString(str);        if(str) nsMemory::Free(str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_STRING_SIZE_IS:            {        char* str;        PRUint32 size;        rv = inVar->GetAsStringWithSize(&size, &str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsStringWithSize(size, str);        if(str) nsMemory::Free(str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_WCHAR_STR:            {        PRUnichar* str;        rv = inVar->GetAsWString(&str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsWString(str);        if(str) nsMemory::Free((char*)str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_WSTRING_SIZE_IS:            {        PRUnichar* str;        PRUint32 size;        rv = inVar->GetAsWStringWithSize(&size, &str);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsWStringWithSize(size, str);        if(str) nsMemory::Free((char*)str);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_INTERFACE:            {        nsISupports* ptr;        rv = inVar->GetAsISupports(&ptr);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsISupports(ptr);        NS_IF_RELEASE(ptr);        NS_ENSURE_SUCCESS(rv,rv);        break;    }    case nsIDataType::VTYPE_INTERFACE_IS:            {        nsISupports* ptr;        nsIID* iid;        rv = inVar->GetAsInterface(&iid, (void**)&ptr);        if(NS_FAILED(rv)) return rv;        rv = outVar->SetAsInterface(*iid, ptr);        NS_IF_RELEASE(ptr);        if(iid) nsMemory::Free((char*)iid);        NS_ENSURE_SUCCESS(rv,rv);        break;    }        break;    case nsIDataType::VTYPE_ARRAY:        rv = inVar->GetAsArray(&du.u.array.mArrayType,                               &du.u.array.mArrayInterfaceID,                               &du.u.array.mArrayCount,                               &du.u.array.mArrayValue);        if(NS_FAILED(rv)) return rv;        du.mType = type;        rv = outVar->SetAsArray(du.u.array.mArrayType,                                &du.u.array.mArrayInterfaceID,                                du.u.array.mArrayCount,                                du.u.array.mArrayValue);        NS_ENSURE_SUCCESS(rv,rv);        break;    case nsIDataType::VTYPE_EMPTY:        if(inVarType != nsIDataType::VTYPE_EMPTY)            return NS_ERROR_CANNOT_CONVERT_DATA;        rv = outVar->SetAsEmpty();        NS_ENSURE_SUCCESS(rv,rv);        break;    default:        NS_ERROR("bad type in variant!");        break;    }    nsVariant::Cleanup(&du);    *_retval = outVar;    NS_IF_ADDREF(*_retval);    return NS_OK;}        