diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2021-08-02 19:10:58 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-08-03 13:20:48 +0200 |
commit | a172ad38f39acfef38f8f84398ec231c2f498e64 (patch) | |
tree | c3621860e25b4ec99bd6dc4730b728c50dd693a4 /svx/source | |
parent | 16b98ec93e2c04f511ea7fb25777331e48aa580c (diff) |
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@collabora.co.uk>
Diffstat (limited to 'svx/source')
-rw-r--r-- | svx/source/customshapes/EnhancedCustomShapeTypeNames.cxx | 97 |
1 files changed, 40 insertions, 57 deletions
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: */ |