diff options
author | Chris Laplante <mostthingsweb@gmail.com> | 2014-04-09 15:15:14 -0400 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2014-04-18 09:48:36 +0000 |
commit | bfefe114e11d155abf0785a3b2c887fcf4077199 (patch) | |
tree | d8542c1b73ec94419317f3b68ff5b11b7374ee98 | |
parent | f38ccdcc5bf5792a242bf20b282748007160d6e5 (diff) |
Rewrite getHash to reduce code duplication & improve maintainability.
Instead of each case block repeating the same operations, we build
an intermediate vector of entries to process.
Change-Id: Id111d7938a7802f6f016c2a0d1be0cb062a32110
Reviewed-on: https://gerrit.libreoffice.org/8912
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
-rw-r--r-- | sw/source/core/doc/SwStyleNameMapper.cxx | 155 |
1 files changed, 60 insertions, 95 deletions
diff --git a/sw/source/core/doc/SwStyleNameMapper.cxx b/sw/source/core/doc/SwStyleNameMapper.cxx index d85a6de2ce82..6bff57590f8c 100644 --- a/sw/source/core/doc/SwStyleNameMapper.cxx +++ b/sw/source/core/doc/SwStyleNameMapper.cxx @@ -17,6 +17,9 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <numeric> +#include <boost/tuple/tuple.hpp> + #include <SwStyleNameMapper.hxx> #include <tools/resmgr.hxx> #include <poolfmt.hxx> @@ -388,6 +391,13 @@ static void lcl_CheckSuffixAndDelete(OUString & rString) rString = rString.copy(0, rString.getLength() - 7); } } + +typedef boost::tuple<sal_uInt16, sal_uInt16, const ::std::vector<OUString>& (*)() > NameArrayIndexTuple_t; + +static sal_uInt16 lcl_AccumulateIndexCount( sal_uInt16 nSum, NameArrayIndexTuple_t const tuple ){ + // Return running sum + (index end) - (index start) + return nSum + boost::get<1>( tuple ) - boost::get<0>( tuple ); +} } #ifdef _NEED_TO_DEBUG_MAPPING @@ -413,124 +423,79 @@ void SwStyleNameMapper::testNameTable( SwGetPoolIdFromName const nFamily, sal_uI const NameToIdHash & SwStyleNameMapper::getHashTable ( SwGetPoolIdFromName eFlags, bool bProgName ) { - NameToIdHash *pHash = 0; - const ::std::vector<OUString> *pStrings = 0; + // pHashPointer is a pointer to a pointer which stores the UI/prog name array + NameToIdHash **pHashPointer = 0; + // Stores tuples representing (index start, index end, pointer to function which returns ref to name array) + ::std::vector<NameArrayIndexTuple_t> vIndexes; switch ( eFlags ) { case nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL: { - sal_uInt16 nIndex; - sal_uInt16 nId; - - pHash = bProgName ? pParaProgMap : pParaUIMap; - if ( !pHash ) - { - pHash = new NameToIdHash ( RES_POOLCOLL_TEXT_END - RES_POOLCOLL_TEXT_BEGIN + - RES_POOLCOLL_LISTS_END - RES_POOLCOLL_LISTS_BEGIN + - RES_POOLCOLL_EXTRA_END - RES_POOLCOLL_EXTRA_BEGIN + - RES_POOLCOLL_REGISTER_END - RES_POOLCOLL_REGISTER_BEGIN + - RES_POOLCOLL_DOC_END - RES_POOLCOLL_DOC_BEGIN + - RES_POOLCOLL_HTML_END - RES_POOLCOLL_HTML_BEGIN ); - pStrings = bProgName ? &GetTextProgNameArray() : &GetTextUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_TEXT_BEGIN ; nId < RES_POOLCOLL_TEXT_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetListsProgNameArray() : &GetListsUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_LISTS_BEGIN ; nId < RES_POOLCOLL_LISTS_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetExtraProgNameArray() : &GetExtraUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_EXTRA_BEGIN ; nId < RES_POOLCOLL_EXTRA_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetRegisterProgNameArray() : &GetRegisterUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_REGISTER_BEGIN ; nId < RES_POOLCOLL_REGISTER_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetDocProgNameArray() : &GetDocUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_DOC_BEGIN ; nId < RES_POOLCOLL_DOC_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetHTMLProgNameArray() : &GetHTMLUINameArray(); - for ( nIndex = 0, nId = RES_POOLCOLL_HTML_BEGIN ; nId < RES_POOLCOLL_HTML_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - - if ( bProgName ) - pParaProgMap = pHash; - else - pParaUIMap = pHash; - } + pHashPointer = bProgName ? &pParaProgMap : &pParaUIMap; + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_TEXT_BEGIN, RES_POOLCOLL_TEXT_END, bProgName ? &GetTextProgNameArray : &GetTextUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_LISTS_BEGIN, RES_POOLCOLL_LISTS_END, bProgName ? &GetListsProgNameArray : &GetListsUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_EXTRA_BEGIN, RES_POOLCOLL_EXTRA_END, bProgName ? &GetExtraProgNameArray : &GetExtraUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_REGISTER_BEGIN, RES_POOLCOLL_REGISTER_END, bProgName ? &GetRegisterProgNameArray : &GetRegisterUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_DOC_BEGIN, RES_POOLCOLL_DOC_END, bProgName ? &GetDocProgNameArray : &GetDocUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCOLL_HTML_BEGIN, RES_POOLCOLL_HTML_END, bProgName ? &GetHTMLProgNameArray : &GetHTMLUINameArray) ); } break; case nsSwGetPoolIdFromName::GET_POOLID_CHRFMT: { - pHash = bProgName ? pCharProgMap : pCharUIMap; - if ( !pHash ) - { - sal_uInt16 nIndex; - sal_uInt16 nId; - - pHash = new NameToIdHash ( RES_POOLCHR_NORMAL_END - RES_POOLCHR_NORMAL_BEGIN + - RES_POOLCHR_HTML_END - RES_POOLCHR_HTML_BEGIN ); - pStrings = bProgName ? &GetChrFmtProgNameArray() : &GetChrFmtUINameArray(); - for ( nIndex = 0, nId = RES_POOLCHR_NORMAL_BEGIN ; nId < RES_POOLCHR_NORMAL_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - pStrings = bProgName ? &GetHTMLChrFmtProgNameArray() : &GetHTMLChrFmtUINameArray(); - for ( nIndex = 0, nId = RES_POOLCHR_HTML_BEGIN ; nId < RES_POOLCHR_HTML_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - if (bProgName ) - pCharProgMap = pHash; - else - pCharUIMap = pHash; - } + pHashPointer = bProgName ? &pCharProgMap : &pCharUIMap; + vIndexes.push_back( boost::make_tuple(RES_POOLCHR_NORMAL_BEGIN, RES_POOLCHR_NORMAL_END, bProgName ? &GetChrFmtProgNameArray : &GetChrFmtUINameArray) ); + vIndexes.push_back( boost::make_tuple(RES_POOLCHR_HTML_BEGIN, RES_POOLCHR_HTML_END, bProgName ? &GetHTMLChrFmtProgNameArray : &GetHTMLChrFmtUINameArray) ); } break; case nsSwGetPoolIdFromName::GET_POOLID_FRMFMT: { - pHash = bProgName ? pFrameProgMap : pFrameUIMap; - if ( !pHash ) - { - pHash = new NameToIdHash ( RES_POOLFRM_END - RES_POOLFRM_BEGIN ); - pStrings = bProgName ? &GetFrmFmtProgNameArray() : &GetFrmFmtUINameArray(); - for ( sal_uInt16 nIndex=0,nId = RES_POOLFRM_BEGIN ; nId < RES_POOLFRM_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - if ( bProgName ) - pFrameProgMap = pHash; - else - pFrameUIMap = pHash; - } + pHashPointer = bProgName ? &pFrameProgMap : &pFrameUIMap; + vIndexes.push_back( boost::make_tuple(RES_POOLFRM_BEGIN, RES_POOLFRM_END, bProgName ? &GetFrmFmtProgNameArray : &GetFrmFmtUINameArray) ); } break; case nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC: { - pHash = bProgName ? pPageProgMap : pPageUIMap; - if ( !pHash ) - { - pHash = new NameToIdHash ( RES_POOLPAGE_END - RES_POOLPAGE_BEGIN ); - pStrings = bProgName ? &GetPageDescProgNameArray() : &GetPageDescUINameArray(); - for ( sal_uInt16 nIndex=0,nId = RES_POOLPAGE_BEGIN ; nId < RES_POOLPAGE_END ; nId++,nIndex++ ) - (*pHash)[((*pStrings)[nIndex])] = nId; - if ( bProgName ) - pPageProgMap = pHash; - else - pPageUIMap = pHash; - } + pHashPointer = bProgName ? &pPageProgMap : &pPageUIMap; + vIndexes.push_back( boost::make_tuple(RES_POOLPAGE_BEGIN, RES_POOLPAGE_END, bProgName ? &GetPageDescProgNameArray : &GetPageDescUINameArray) ); } break; case nsSwGetPoolIdFromName::GET_POOLID_NUMRULE: { - pHash = bProgName ? pNumRuleProgMap : pNumRuleUIMap; - if ( !pHash ) + pHashPointer = bProgName ? &pNumRuleProgMap : &pNumRuleUIMap; + vIndexes.push_back( boost::make_tuple(RES_POOLNUMRULE_BEGIN, RES_POOLNUMRULE_END, bProgName ? &GetNumRuleProgNameArray : &GetNumRuleUINameArray) ); + } + break; + default: + { + // TODO: Is there a better way of failing here? + *pHashPointer = new NameToIdHash( 0 ); + return **pHashPointer; + } + break; + } + + // Proceed if we have a pointer to a hash, and the hash hasn't already been populated + if ( pHashPointer && !*pHashPointer ) + { + // Compute the size of the hash we need to build + sal_uInt16 nSize = std::accumulate( vIndexes.begin(), vIndexes.end(), 0, lcl_AccumulateIndexCount ); + + NameToIdHash *pHash = new NameToIdHash( nSize ); + for ( ::std::vector<NameArrayIndexTuple_t>::iterator entry = vIndexes.begin(); entry != vIndexes.end(); ++entry ) + { + // Get a pointer to the function which will populate pStrings + const ::std::vector<OUString>& (*pStringsFetchFunc)() = boost::get<2>( *entry ); + if ( pStringsFetchFunc ) { - pHash = new NameToIdHash ( RES_POOLNUMRULE_END - RES_POOLNUMRULE_BEGIN ); - pStrings = bProgName ? &GetNumRuleProgNameArray() : &GetNumRuleUINameArray(); - for ( sal_uInt16 nIndex=0,nId = RES_POOLNUMRULE_BEGIN ; nId < RES_POOLNUMRULE_END ; nId++,nIndex++ ) - { - (*pHash)[((*pStrings)[nIndex])] = nId; - } - if ( bProgName ) - pNumRuleProgMap = pHash; - else - pNumRuleUIMap = pHash; + const ::std::vector<OUString>& pStrings = pStringsFetchFunc(); + sal_uInt16 nIndex, nId; + for ( nIndex = 0, nId = boost::get<0>( *entry ) ; nId < boost::get<1>( *entry ) ; nId++, nIndex++ ) + (*pHash)[pStrings[nIndex]] = nId; } } - break; + + *pHashPointer = pHash; } #ifdef _NEED_TO_DEBUG_MAPPING @@ -552,7 +517,7 @@ const NameToIdHash & SwStyleNameMapper::getHashTable ( SwGetPoolIdFromName eFlag testNameTable( nsSwGetPoolIdFromName::GET_POOLID_NUMRULE, RES_POOLNUMRULE_BEGIN, RES_POOLNUMRULE_END ); } #endif - return *pHash; + return **pHashPointer; } // This gets the UI name from the programmatic name |