use one styles vector instead of multiple tables

Change-Id: I276f2fae8083f3131fa3ff92fc7633c0f88a3ed6
This commit is contained in:
Bjoern Michaelsen
2015-11-27 01:34:18 +01:00
parent d7ceaae4cd
commit c50e1c442b

View File

@@ -90,16 +90,27 @@
#include <memory> #include <memory>
#include <set> #include <set>
#define STYLE_FAMILY_COUNT 5 // we have 5 style families namespace
const sal_uInt16 aStyleByIndex[] =
{ {
SFX_STYLE_FAMILY_CHAR, struct StyleFamilyEntry
SFX_STYLE_FAMILY_PARA, {
SFX_STYLE_FAMILY_PAGE, const SfxStyleFamily m_eFamily;
SFX_STYLE_FAMILY_FRAME, const SwGetPoolIdFromName m_aPoolId;
SFX_STYLE_FAMILY_PSEUDO const OUString m_sName;
}; StyleFamilyEntry(SfxStyleFamily eFamily, SwGetPoolIdFromName aPoolId, OUString sName)
: m_eFamily(eFamily)
, m_aPoolId(aPoolId)
, m_sName(sName)
{}
};
static const std::vector<StyleFamilyEntry> our_vStyleFamilyEntries {
{ SFX_STYLE_FAMILY_CHAR, nsSwGetPoolIdFromName::GET_POOLID_CHRFMT, "CharacterStyles" },
{ SFX_STYLE_FAMILY_PARA, nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL, "ParagraphStyles" },
{ SFX_STYLE_FAMILY_PAGE, nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC, "PageStyles" },
{ SFX_STYLE_FAMILY_FRAME, nsSwGetPoolIdFromName::GET_POOLID_FRMFMT, "FrameStyles" },
{ SFX_STYLE_FAMILY_PSEUDO, nsSwGetPoolIdFromName::GET_POOLID_NUMRULE, "NumberingStyles" }
};
}
// Already implemented autostyle families: 3 // Already implemented autostyle families: 3
#define AUTOSTYLE_FAMILY_COUNT 3 #define AUTOSTYLE_FAMILY_COUNT 3
@@ -112,24 +123,14 @@ const IStyleAccess::SwAutoStyleFamily aAutoStyleByIndex[] =
using namespace ::com::sun::star; using namespace ::com::sun::star;
static SwGetPoolIdFromName lcl_GetSwEnumFromSfxEnum ( SfxStyleFamily eFamily ) static SwGetPoolIdFromName lcl_GetSwEnumFromSfxEnum(SfxStyleFamily eFamily)
{ {
switch ( eFamily ) const auto pEntry = std::find_if(our_vStyleFamilyEntries.begin(), our_vStyleFamilyEntries.end(),
{ [eFamily] (const StyleFamilyEntry& e) { return e.m_eFamily == eFamily; });
case SFX_STYLE_FAMILY_CHAR: if(pEntry != our_vStyleFamilyEntries.end())
return nsSwGetPoolIdFromName::GET_POOLID_CHRFMT; return pEntry->m_aPoolId;
case SFX_STYLE_FAMILY_PARA: SAL_WARN("sw.uno", "someone asking for all styles in unostyle.cxx!" );
return nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL; return nsSwGetPoolIdFromName::GET_POOLID_CHRFMT;
case SFX_STYLE_FAMILY_FRAME:
return nsSwGetPoolIdFromName::GET_POOLID_FRMFMT;
case SFX_STYLE_FAMILY_PAGE:
return nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC;
case SFX_STYLE_FAMILY_PSEUDO:
return nsSwGetPoolIdFromName::GET_POOLID_NUMRULE;
default:
OSL_ENSURE(false, "someone asking for all styles in unostyle.cxx!" );
return nsSwGetPoolIdFromName::GET_POOLID_CHRFMT;
}
} }
class SwAutoStylesEnumImpl class SwAutoStylesEnumImpl
@@ -185,52 +186,33 @@ uno::Any SAL_CALL SwXStyleFamilies::getByName(const OUString& Name)
uno::RuntimeException, std::exception ) uno::RuntimeException, std::exception )
{ {
SolarMutexGuard aGuard; SolarMutexGuard aGuard;
// the index comes from const unsigned short aStyleByIndex[] = ...
uno::Any aRet;
if(!IsValid()) if(!IsValid())
throw uno::RuntimeException(); throw uno::RuntimeException();
if(Name=="CharacterStyles" ) const auto pEntry = std::find_if(our_vStyleFamilyEntries.begin(), our_vStyleFamilyEntries.end(),
aRet = getByIndex(0); [&Name] (const StyleFamilyEntry& e) { return e.m_sName == Name; });
else if(Name=="ParagraphStyles" ) if(pEntry == our_vStyleFamilyEntries.end())
aRet = getByIndex(1);
else if(Name=="FrameStyles" )
aRet = getByIndex(3);
else if(Name=="PageStyles" )
aRet = getByIndex(2);
else if(Name=="NumberingStyles" )
aRet = getByIndex(4);
else
throw container::NoSuchElementException(); throw container::NoSuchElementException();
return aRet; return getByIndex(pEntry-our_vStyleFamilyEntries.begin());
} }
uno::Sequence< OUString > SwXStyleFamilies::getElementNames() throw( uno::RuntimeException, std::exception ) uno::Sequence< OUString > SwXStyleFamilies::getElementNames() throw( uno::RuntimeException, std::exception )
{ {
uno::Sequence< OUString > aNames(STYLE_FAMILY_COUNT); uno::Sequence< OUString > aNames(our_vStyleFamilyEntries.size());
OUString* pNames = aNames.getArray(); std::transform(our_vStyleFamilyEntries.begin(), our_vStyleFamilyEntries.end(),
pNames[0] = "CharacterStyles"; aNames.begin(), [] (const StyleFamilyEntry& e) { return e.m_sName; });
pNames[1] = "ParagraphStyles";
pNames[2] = "FrameStyles";
pNames[3] = "PageStyles";
pNames[4] = "NumberingStyles";
return aNames; return aNames;
} }
sal_Bool SwXStyleFamilies::hasByName(const OUString& Name) throw( uno::RuntimeException, std::exception ) sal_Bool SwXStyleFamilies::hasByName(const OUString& Name) throw( uno::RuntimeException, std::exception )
{ {
if( Name=="CharacterStyles" || const auto pEntry = std::find_if(our_vStyleFamilyEntries.begin(), our_vStyleFamilyEntries.end(),
Name=="ParagraphStyles" || [&Name] (const StyleFamilyEntry& e) { return e.m_sName == Name; });
Name=="FrameStyles" || return pEntry != our_vStyleFamilyEntries.end();
Name=="PageStyles" ||
Name=="NumberingStyles" )
return sal_True;
else
return sal_False;
} }
sal_Int32 SwXStyleFamilies::getCount() throw( uno::RuntimeException, std::exception ) sal_Int32 SwXStyleFamilies::getCount() throw( uno::RuntimeException, std::exception )
{ {
return STYLE_FAMILY_COUNT; return our_vStyleFamilyEntries.size();
} }
uno::Any SwXStyleFamilies::getByIndex(sal_Int32 nIndex) uno::Any SwXStyleFamilies::getByIndex(sal_Int32 nIndex)
@@ -238,19 +220,19 @@ uno::Any SwXStyleFamilies::getByIndex(sal_Int32 nIndex)
{ {
SolarMutexGuard aGuard; SolarMutexGuard aGuard;
uno::Any aRet; uno::Any aRet;
if(nIndex < 0 || nIndex >= STYLE_FAMILY_COUNT) if(nIndex < 0 || nIndex >= static_cast<sal_Int32>(our_vStyleFamilyEntries.size()))
throw lang::IndexOutOfBoundsException(); throw lang::IndexOutOfBoundsException();
if(IsValid()) if(IsValid())
{ {
uno::Reference< container::XNameContainer > aRef; uno::Reference< container::XNameContainer > aRef;
const sal_uInt16 nType = aStyleByIndex[nIndex]; auto eFamily = our_vStyleFamilyEntries[nIndex].m_eFamily;
switch( nType ) switch( eFamily )
{ {
case SFX_STYLE_FAMILY_CHAR: case SFX_STYLE_FAMILY_CHAR:
{ {
if(!m_xCharStyles.is()) if(!m_xCharStyles.is())
{ {
m_xCharStyles = new SwXStyleFamily(m_pDocShell, nType); m_xCharStyles = new SwXStyleFamily(m_pDocShell, eFamily);
} }
aRef = m_xCharStyles; aRef = m_xCharStyles;
} }
@@ -259,25 +241,25 @@ uno::Any SwXStyleFamilies::getByIndex(sal_Int32 nIndex)
{ {
if(!m_xParaStyles.is()) if(!m_xParaStyles.is())
{ {
m_xParaStyles = new SwXStyleFamily(m_pDocShell, nType); m_xParaStyles = new SwXStyleFamily(m_pDocShell, eFamily);
} }
aRef = m_xParaStyles; aRef = m_xParaStyles;
} }
break; break;
case SFX_STYLE_FAMILY_PAGE : case SFX_STYLE_FAMILY_PAGE:
{ {
if(!m_xPageStyles.is()) if(!m_xPageStyles.is())
{ {
m_xPageStyles = new SwXStyleFamily(m_pDocShell, nType); m_xPageStyles = new SwXStyleFamily(m_pDocShell, eFamily);
} }
aRef = m_xPageStyles; aRef = m_xPageStyles;
} }
break; break;
case SFX_STYLE_FAMILY_FRAME : case SFX_STYLE_FAMILY_FRAME:
{ {
if(!m_xFrameStyles.is()) if(!m_xFrameStyles.is())
{ {
m_xFrameStyles = new SwXStyleFamily(m_pDocShell, nType); m_xFrameStyles = new SwXStyleFamily(m_pDocShell, eFamily);
} }
aRef = m_xFrameStyles; aRef = m_xFrameStyles;
} }
@@ -286,11 +268,13 @@ uno::Any SwXStyleFamilies::getByIndex(sal_Int32 nIndex)
{ {
if(!m_xNumberingStyles.is()) if(!m_xNumberingStyles.is())
{ {
m_xNumberingStyles = new SwXStyleFamily(m_pDocShell, nType); m_xNumberingStyles = new SwXStyleFamily(m_pDocShell, eFamily);
} }
aRef = m_xNumberingStyles; aRef = m_xNumberingStyles;
} }
break; break;
case SFX_STYLE_FAMILY_ALL:
assert(false);
} }
aRet.setValue(&aRef, cppu::UnoType<container::XNameContainer>::get()); aRet.setValue(&aRef, cppu::UnoType<container::XNameContainer>::get());
} }