comphelper/source/misc/anycompare.cxx         |   39 ++-----------------
 comphelper/source/misc/anytostring.cxx        |   52 ++++++++++++--------------
 comphelper/source/misc/typedescriptionref.hxx |   51 +++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 63 deletions(-)

New commits:
commit afda77cd6565e53a356c0b0be93d708808079e49
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Thu Jan 6 14:11:30 2022 +0100
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Sun Jan 9 15:43:07 2022 +0100

    use C++ class handling ownership for anyToString()
    
    This matches changes I did for anyLess() in my previous commit.
    
    Change-Id: Id38c49c10223340b7359765e1071d8a9f72ba92e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128046
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/comphelper/source/misc/anycompare.cxx 
b/comphelper/source/misc/anycompare.cxx
index 68cd6dc0f9d2..8a23877239be 100644
--- a/comphelper/source/misc/anycompare.cxx
+++ b/comphelper/source/misc/anycompare.cxx
@@ -26,10 +26,13 @@
 #include <com/sun/star/util/Time.hpp>
 #include <com/sun/star/util/DateTime.hpp>
 
+#include "typedescriptionref.hxx"
+
 namespace comphelper
 {
     using ::com::sun::star::uno::Reference;
     using ::com::sun::star::uno::Type;
+    using ::com::sun::star::uno::TypeDescription;
     using ::com::sun::star::uno::TypeClass_CHAR;
     using ::com::sun::star::uno::TypeClass_BOOLEAN;
     using ::com::sun::star::uno::TypeClass_BYTE;
@@ -50,6 +53,7 @@ namespace comphelper
     using ::com::sun::star::util::Date;
     using ::com::sun::star::util::Time;
     using ::com::sun::star::util::DateTime;
+    using ::comphelper::detail::TypeDescriptionRef;
 
     namespace {
 
@@ -178,41 +182,6 @@ namespace comphelper
         return std::nullopt; // equal, so can't yet tell if anyLess() should 
return
     }
 
-    using com::sun::star::uno::TypeDescription;
-
-    // This is like com::sun::star::uno::TypeDescription, but it uses 
TYPELIB_DANGER_GET
-    // (which the code used originally, but it's easier to have a class to 
handle ownership).
-    class TypeDescriptionRef
-    {
-    public:
-        TypeDescriptionRef( typelib_TypeDescriptionReference* typeDef )
-        {
-            TYPELIB_DANGER_GET( &typeDescr, typeDef );
-        }
-        ~TypeDescriptionRef()
-        {
-            TYPELIB_DANGER_RELEASE( typeDescr );
-        }
-        typelib_TypeDescription* get()
-        {
-            return typeDescr;
-        }
-        typelib_TypeDescription* operator->()
-        {
-            return typeDescr;
-        }
-        bool is()
-        {
-            return typeDescr != nullptr;
-        }
-        bool equals( const TypeDescriptionRef& other ) const
-        {
-            return typeDescr && other.typeDescr && 
typelib_typedescription_equals( typeDescr, other.typeDescr );
-        }
-    private:
-        typelib_TypeDescription* typeDescr = nullptr;
-    };
-
     // This is typelib_typedescription_equals(), but returns -1/0/1 values 
like strcmp().
     int compareTypes( const typelib_TypeDescription * lhsType,
                       const typelib_TypeDescription * rhsType )
diff --git a/comphelper/source/misc/anytostring.cxx 
b/comphelper/source/misc/anytostring.cxx
index 4e068cec9acd..ebc338b0b4be 100644
--- a/comphelper/source/misc/anytostring.cxx
+++ b/comphelper/source/misc/anytostring.cxx
@@ -20,10 +20,14 @@
 
 #include <comphelper/anytostring.hxx>
 #include <rtl/ustrbuf.hxx>
-#include <typelib/typedescription.h>
+#include <typelib/typedescription.hxx>
 #include <com/sun/star/lang/XServiceInfo.hpp>
 
+#include "typedescriptionref.hxx"
+
 using namespace ::com::sun::star;
+using ::com::sun::star::uno::TypeDescription;
+using ::comphelper::detail::TypeDescriptionRef;
 
 namespace comphelper {
 namespace {
@@ -90,15 +94,16 @@ void appendValue( OUStringBuffer & buf,
     case typelib_TypeClass_STRUCT:
     case typelib_TypeClass_EXCEPTION: {
         buf.append( "{ " );
-        typelib_TypeDescription * typeDescr = nullptr;
-        typelib_typedescriptionreference_getDescription( &typeDescr, typeRef );
-        if (typeDescr == nullptr || !typelib_typedescription_complete( 
&typeDescr )) {
+        TypeDescription typeDescr( typeRef );
+        if (!typeDescr.is())
+            typeDescr.makeComplete();
+        if (!typeDescr.is()) {
             appendTypeError( buf, typeRef );
         }
         else {
             typelib_CompoundTypeDescription * compType =
                 reinterpret_cast< typelib_CompoundTypeDescription * >(
-                    typeDescr );
+                    typeDescr.get() );
             sal_Int32 nDescr = compType->nMembers;
 
             if (compType->pBaseTypeDescription) {
@@ -119,9 +124,8 @@ void appendValue( OUStringBuffer & buf,
             {
                 buf.append( ppMemberNames[ nPos ] );
                 buf.append( " = " );
-                typelib_TypeDescription * memberType = nullptr;
-                TYPELIB_DANGER_GET( &memberType, ppTypeRefs[ nPos ] );
-                if (memberType == nullptr) {
+                TypeDescriptionRef memberType( ppTypeRefs[ nPos ] );
+                if (!memberType.is()) {
                     appendTypeError( buf, ppTypeRefs[ nPos ] );
                 }
                 else {
@@ -129,30 +133,25 @@ void appendValue( OUStringBuffer & buf,
                                  static_cast< char const * >(
                                      val ) + memberOffsets[ nPos ],
                                  memberType->pWeakRef, true );
-                    TYPELIB_DANGER_RELEASE( memberType );
                 }
                 if (nPos < (nDescr - 1))
                     buf.append( ", " );
             }
         }
         buf.append( " }" );
-        if (typeDescr != nullptr)
-            typelib_typedescription_release( typeDescr );
         break;
     }
     case typelib_TypeClass_SEQUENCE: {
-        typelib_TypeDescription * typeDescr = nullptr;
-        TYPELIB_DANGER_GET( &typeDescr, typeRef );
-        if (typeDescr == nullptr) {
+        TypeDescriptionRef typeDescr( typeRef );
+        if (!typeDescr.is()) {
             appendTypeError( buf,typeRef );
         }
         else {
             typelib_TypeDescriptionReference * elementTypeRef =
                 reinterpret_cast<
-                typelib_IndirectTypeDescription * >(typeDescr)->pType;
-            typelib_TypeDescription * elementTypeDescr = nullptr;
-            TYPELIB_DANGER_GET( &elementTypeDescr, elementTypeRef );
-            if (elementTypeDescr == nullptr)
+                typelib_IndirectTypeDescription * >(typeDescr.get())->pType;
+            TypeDescriptionRef elementTypeDescr( elementTypeRef );
+            if (!elementTypeDescr.is())
             {
                 appendTypeError( buf, elementTypeRef );
             }
@@ -181,9 +180,7 @@ void appendValue( OUStringBuffer & buf,
                 {
                     buf.append( "{}" );
                 }
-                TYPELIB_DANGER_RELEASE( elementTypeDescr );
             }
-            TYPELIB_DANGER_RELEASE( typeDescr );
         }
         break;
     }
@@ -218,18 +215,19 @@ void appendValue( OUStringBuffer & buf,
         break;
     }
     case typelib_TypeClass_ENUM: {
-        typelib_TypeDescription * typeDescr = nullptr;
-        typelib_typedescriptionreference_getDescription( &typeDescr, typeRef );
-        if (typeDescr == nullptr || !typelib_typedescription_complete( 
&typeDescr )) {
+        TypeDescription typeDescr( typeRef );
+        if (!typeDescr.is())
+            typeDescr.makeComplete();
+        if (!typeDescr.is()) {
             appendTypeError( buf, typeRef );
         }
         else
         {
             sal_Int32 * pValues =
                 reinterpret_cast< typelib_EnumTypeDescription * >(
-                    typeDescr )->pEnumValues;
+                    typeDescr.get() )->pEnumValues;
             sal_Int32 nPos = reinterpret_cast< typelib_EnumTypeDescription * >(
-                typeDescr )->nEnumValues;
+                typeDescr.get() )->nEnumValues;
             while (nPos--)
             {
                 if (pValues[ nPos ] == *static_cast< int const * >(val))
@@ -238,15 +236,13 @@ void appendValue( OUStringBuffer & buf,
             if (nPos >= 0)
             {
                 buf.append( reinterpret_cast< typelib_EnumTypeDescription * >(
-                                typeDescr )->ppEnumNames[ nPos ] );
+                                typeDescr.get() )->ppEnumNames[ nPos ] );
             }
             else
             {
                 buf.append( "?unknown enum value?" );
             }
         }
-        if (typeDescr != nullptr)
-            typelib_typedescription_release( typeDescr );
         break;
     }
     case typelib_TypeClass_BOOLEAN:
diff --git a/comphelper/source/misc/typedescriptionref.hxx 
b/comphelper/source/misc/typedescriptionref.hxx
new file mode 100644
index 000000000000..f4580cb2e4cb
--- /dev/null
+++ b/comphelper/source/misc/typedescriptionref.hxx
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#pragma once
+
+#include <typelib/typedescription.h>
+
+namespace comphelper::detail
+{
+// This is like com::sun::star::uno::TypeDescription, but it uses 
TYPELIB_DANGER_GET
+// (which the code used originally, but it's easier to have a class to handle 
ownership).
+class TypeDescriptionRef
+{
+public:
+    TypeDescriptionRef(typelib_TypeDescriptionReference* typeDef)
+    {
+        TYPELIB_DANGER_GET(&typeDescr, typeDef);
+    }
+    ~TypeDescriptionRef() { TYPELIB_DANGER_RELEASE(typeDescr); }
+    typelib_TypeDescription* get() { return typeDescr; }
+    typelib_TypeDescription* operator->() { return typeDescr; }
+    bool is() { return typeDescr != nullptr; }
+    bool equals(const TypeDescriptionRef& other) const
+    {
+        return typeDescr && other.typeDescr
+               && typelib_typedescription_equals(typeDescr, other.typeDescr);
+    }
+
+private:
+    typelib_TypeDescription* typeDescr = nullptr;
+};
+
+} // namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to