[Libreoffice-commits] core.git: include/svx svx/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Tue Aug 3 11:21:22 UTC 2021


 include/svx/EnhancedCustomShapeTypeNames.hxx             |    2 
 svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx |   97 ++++++---------
 2 files changed, 41 insertions(+), 58 deletions(-)

New commits:
commit a172ad38f39acfef38f8f84398ec231c2f498e64
Author:     Noel Grandin <noelgrandin at gmail.com>
AuthorDate: Mon Aug 2 19:10:58 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Tue Aug 3 13:20:48 2021 +0200

    simplify lookup maps in EnhancedCustomShapeTypeNames
    
    simplify initialisation, and use OUString as the key to avoid
    conversions during lookup
    
    Change-Id: I0b1f1f461a4d2375cd92d13bb3333c8e0b887ee7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119897
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/include/svx/EnhancedCustomShapeTypeNames.hxx b/include/svx/EnhancedCustomShapeTypeNames.hxx
index fb6efdd731df..b369c8c42c9d 100644
--- a/include/svx/EnhancedCustomShapeTypeNames.hxx
+++ b/include/svx/EnhancedCustomShapeTypeNames.hxx
@@ -27,7 +27,7 @@ namespace EnhancedCustomShapeTypeNames
 {
 SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString&);
 SVXCORE_DLLPUBLIC OUString Get(const MSO_SPT);
-SVXCORE_DLLPUBLIC OUString GetAccName(const OUString&);
+SVXCORE_DLLPUBLIC const OUString& GetAccName(const OUString&);
 }
 
 #endif
diff --git a/svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx b/svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx
index a7423c580f5b..0a1ae13c61e9 100644
--- a/svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx
+++ b/svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx
@@ -22,15 +22,6 @@
 #include <unordered_map>
 #include <memory>
 
-typedef std::unordered_map< const char*, MSO_SPT, rtl::CStringHash, rtl::CStringEqual> TypeNameHashMap;
-
-static TypeNameHashMap* pHashMap = nullptr;
-static ::osl::Mutex& getHashMapMutex()
-{
-    static osl::Mutex s_aHashMapProtection;
-    return s_aHashMapProtection;
-}
-
 namespace {
 
 struct NameTypeTable
@@ -277,29 +268,28 @@ const NameTypeTable pNameTypeTableArray[] =
     // gallery: fontwork-arch-left-pour
     // gallery: fontwork-arch-right-pour
 
+
+typedef std::unordered_map< OUString, MSO_SPT> TypeNameHashMap;
+
+static const TypeNameHashMap& GetTypeNameHashMap()
+{
+    static TypeNameHashMap aMap = []()
+    {
+        TypeNameHashMap map;
+        for (auto const & i : pNameTypeTableArray)
+            map[OUString::createFromAscii(i.pS)] = i.pE;
+        return map;
+    }();
+    return aMap;
+}
+
+
 MSO_SPT EnhancedCustomShapeTypeNames::Get( const OUString& rShapeType )
 {
-    if ( !pHashMap )
-    {   // init hash map
-        ::osl::MutexGuard aGuard( getHashMapMutex() );
-        if ( !pHashMap )
-        {
-            TypeNameHashMap* pH = new TypeNameHashMap;
-            const NameTypeTable* pPtr = pNameTypeTableArray;
-            const NameTypeTable* pEnd = pPtr + SAL_N_ELEMENTS( pNameTypeTableArray );
-            for ( ; pPtr < pEnd; pPtr++ )
-                (*pH)[ pPtr->pS ] = pPtr->pE;
-            pHashMap = pH;
-        }
-    }
+    const TypeNameHashMap & rTypeMap = GetTypeNameHashMap();
     MSO_SPT eRetValue = mso_sptNil;
-    int i, nLen = rShapeType.getLength();
-    std::unique_ptr<char[]> pBuf(new char[ nLen + 1 ]);
-    for ( i = 0; i < nLen; i++ )
-        pBuf[ i ] = static_cast<char>(rShapeType[ i ]);
-    pBuf[ i ] = 0;
-    TypeNameHashMap::const_iterator aHashIter( pHashMap->find( pBuf.get() ) );
-    if ( aHashIter != pHashMap->end() )
+    auto aHashIter = rTypeMap.find( rShapeType );
+    if ( aHashIter != rTypeMap.end() )
         eRetValue = (*aHashIter).second;
     return eRetValue;
 }
@@ -311,10 +301,6 @@ OUString EnhancedCustomShapeTypeNames::Get( const MSO_SPT eShapeType )
         : OUString();
 }
 
-typedef std::unordered_map< const char*, const char*, rtl::CStringHash, rtl::CStringEqual> TypeACCNameHashMap;
-
-static TypeACCNameHashMap* pACCHashMap = nullptr;
-
 namespace {
 
 struct ACCNameTypeTable
@@ -537,31 +523,28 @@ const ACCNameTypeTable pACCNameTypeTableArray[] =
     { "col-502ad400", "Diamond Bevel" }
 };
 
-OUString EnhancedCustomShapeTypeNames::GetAccName( const OUString& rShapeType )
+typedef std::unordered_map<OUString, OUString> TypeACCNameHashMap;
+
+static const TypeACCNameHashMap& GetACCHashMap()
+{
+    static TypeACCNameHashMap aMap = []()
+    {
+        TypeACCNameHashMap map;
+        for (auto const & i : pACCNameTypeTableArray)
+            map[OUString::createFromAscii(i.pS)] = OUString::createFromAscii(i.pE);
+        return map;
+    }();
+    return aMap;
+}
+
+const OUString & EnhancedCustomShapeTypeNames::GetAccName( const OUString& rShapeType )
 {
-    if ( !pACCHashMap )
-    {   // init hash map
-        ::osl::MutexGuard aGuard( getHashMapMutex() );
-        if ( !pACCHashMap )
-        {
-            TypeACCNameHashMap* pH = new TypeACCNameHashMap;
-            const ACCNameTypeTable* pPtr = pACCNameTypeTableArray;
-            const ACCNameTypeTable* pEnd = pPtr + SAL_N_ELEMENTS( pACCNameTypeTableArray );
-            for ( ; pPtr < pEnd; pPtr++ )
-                (*pH)[ pPtr->pS ] = pPtr->pE;
-            pACCHashMap = pH;
-        }
-    }
-    OUString sRetValue;
-    int i, nLen = rShapeType.getLength();
-    std::unique_ptr<char[]> pBuf(new char[ nLen + 1 ]);
-    for ( i = 0; i < nLen; i++ )
-        pBuf[ i ] = static_cast<char>(rShapeType[ i ]);
-    pBuf[ i ] = 0;
-    TypeACCNameHashMap::const_iterator aHashIter( pACCHashMap->find( pBuf.get() ) );
-    if ( aHashIter != pACCHashMap->end() )
-        sRetValue = OUString::createFromAscii( (*aHashIter).second );
-    return sRetValue;
+    static const OUString EMPTY;
+    const TypeACCNameHashMap& rACCMap = GetACCHashMap();
+    auto aHashIter = rACCMap.find( rShapeType );
+    if ( aHashIter != rACCMap.end() )
+        return aHashIter->second;
+    return EMPTY;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list