Kaydet (Commit) 6c2f575f authored tarafından Armin Le Grand's avatar Armin Le Grand

WIP: Use ItemControlBlock as TypeInfo

Removed last big change to have a global static
ItemControlBlock registry. Thought about things
and what is needed/wanted is RTTI for the Items.
Instead of using typeid(T).hash_code() it is
equally possible to use the ItemControlBlock of
each Item - it exists once as global static
instance. So use it's address. That way e.g. the
ItemSet holding it *will* hjave a direct reference
to the ItemControlBlock, so no need for a global
registration.
Checked, tested, adapted, works.

Change-Id: I4c9b6bfe4ba6858b234238d6125793378574af87
üst 203d0f02
...@@ -32,7 +32,6 @@ namespace Item ...@@ -32,7 +32,6 @@ namespace Item
static ::Item::ItemControlBlock aItemControlBlock( static ::Item::ItemControlBlock aItemControlBlock(
[](){ return new Sbx(ScriptDocument::getApplicationScriptDocument()); }, [](){ return new Sbx(ScriptDocument::getApplicationScriptDocument()); },
[](const ItemBase& rRef){ return new Sbx(static_cast<const Sbx&>(rRef)); }, [](const ItemBase& rRef){ return new Sbx(static_cast<const Sbx&>(rRef)); },
typeid(Sbx).hash_code(),
"Sbx"); "Sbx");
return aItemControlBlock; return aItemControlBlock;
......
...@@ -101,7 +101,12 @@ namespace Item ...@@ -101,7 +101,12 @@ namespace Item
// this way accessible for all incarnations - at the cost of // this way accessible for all incarnations - at the cost of
// one local reference // one local reference
friend bool isDefault(const ItemBase& rCandidate); friend bool isDefault(const ItemBase& rCandidate);
friend class ItemSet;
ItemControlBlock& m_rItemControlBlock; ItemControlBlock& m_rItemControlBlock;
const ItemControlBlock& getItemControlBlock() const
{
return m_rItemControlBlock;
}
protected: protected:
// PutValue/Any interface for automated instance creation from SfxType // PutValue/Any interface for automated instance creation from SfxType
......
...@@ -38,19 +38,14 @@ namespace Item ...@@ -38,19 +38,14 @@ namespace Item
std::unique_ptr<const ItemBase> m_aDefaultItem; std::unique_ptr<const ItemBase> m_aDefaultItem;
std::function<ItemBase*()> m_aConstructDefaultItem; std::function<ItemBase*()> m_aConstructDefaultItem;
std::function<ItemBase*(const ItemBase&)> m_aCloneItem; std::function<ItemBase*(const ItemBase&)> m_aCloneItem;
size_t m_aHashCode;
OUString m_aName; OUString m_aName;
// EmptyItemControlBlock: default constructor *only* for internal use
ItemControlBlock();
public: public:
ItemControlBlock( ItemControlBlock(
std::function<ItemBase*()>aConstructDefaultItem, std::function<ItemBase*()>aConstructDefaultItem,
std::function<ItemBase*(const ItemBase&)>aCloneItem, std::function<ItemBase*(const ItemBase&)>aCloneItem,
size_t aHashCode,
const OUString& rName); const OUString& rName);
~ItemControlBlock(); ~ItemControlBlock() = default;
const ItemBase& getDefault() const; const ItemBase& getDefault() const;
bool isDefault(const ItemBase& rItem) const; bool isDefault(const ItemBase& rItem) const;
...@@ -60,23 +55,11 @@ namespace Item ...@@ -60,23 +55,11 @@ namespace Item
return m_aName; return m_aName;
} }
size_t getHashCode() const
{
return m_aHashCode;
}
// clone-op, secured by returning a std::unique_ptr to make // clone-op, secured by returning a std::unique_ptr to make
// explicit the ownership you get when calling this // explicit the ownership you get when calling this
std::unique_ptr<ItemBase> clone(const ItemBase&) const; std::unique_ptr<ItemBase> clone(const ItemBase&) const;
std::unique_ptr<const ItemBase> createFromAny(const ItemBase::AnyIDArgs& rArgs); std::unique_ptr<const ItemBase> createFromAny(const ItemBase::AnyIDArgs& rArgs);
// static access to registered ItemControlBlocks
static ItemControlBlock* getItemControlBlock(size_t HashCode);
template< typename TItem > ItemControlBlock* getItemControlBlock()
{
return getItemControlBlock(typeid(TItem).HashCode());
}
}; };
} // end of namespace Item } // end of namespace Item
......
...@@ -216,14 +216,14 @@ namespace Item ...@@ -216,14 +216,14 @@ namespace Item
ModelSpecificItemValues::SharedPtr m_aModelSpecificIValues; ModelSpecificItemValues::SharedPtr m_aModelSpecificIValues;
// the items as content // the items as content
std::unordered_map<size_t, const ItemBase*> m_aItems; std::unordered_map<const ItemControlBlock*, const ItemBase*> m_aItems;
// helpers for reduction of template member implementations, // helpers for reduction of template member implementations,
// all based on typeid(<type>).hash_code() // all based on ItemControlBlock as unique identifier
const ItemBase* implGetStateAndItem(size_t hash_code, IState& rIState, bool bSearchParent) const; const ItemBase* implGetStateAndItem(const ItemControlBlock& rICB, IState& rIState, bool bSearchParent) const;
void implInvalidateItem(size_t hash_code); void implInvalidateItem(const ItemControlBlock& rICB);
void implDisableItem(size_t hash_code); void implDisableItem(const ItemControlBlock& rICB);
bool implClearItem(size_t hash_code); bool implClearItem(const ItemControlBlock& rICB);
// ...or a given default // ...or a given default
const ItemBase& implGetDefault(const ItemBase& rCurrent) const; const ItemBase& implGetDefault(const ItemBase& rCurrent) const;
...@@ -262,49 +262,37 @@ namespace Item ...@@ -262,49 +262,37 @@ namespace Item
// the compiler for each class. // the compiler for each class.
// reduction uses local immp methods wherever possible, based // reduction uses local immp methods wherever possible, based
// on the fetched TypeID // on the fetched TypeID
template< typename TItem > void invalidateItem() template< typename T > void invalidateItem()
{ {
implInvalidateItem(typeid(TItem).hash_code()); implInvalidateItem(T::GetStaticItemControlBlock());
} }
template< typename TItem > void disableItem() template< typename T > void disableItem()
{ {
implDisableItem(typeid(TItem).hash_code()); implDisableItem(T::GetStaticItemControlBlock());
} }
template< typename TItem > const TItem& getDefault() const template< typename T > const T& getDefault() const
{ {
return static_cast<const TItem&>( return static_cast<const T&>(
implGetDefault( implGetDefault(
Item::getDefault<TItem>())); Item::getDefault<T>()));
} }
template< typename TItem > StateAndItem<TItem> getStateAndItem(bool bSearchParent = true) const template< typename T > StateAndItem<T> getStateAndItem(bool bSearchParent = true) const
{ {
IState aIState(IState::DEFAULT); IState aIState(IState::DEFAULT);
const ItemBase* pItem(implGetStateAndItem(typeid(TItem).hash_code(), aIState, bSearchParent)); const ItemBase* pItem(implGetStateAndItem(T::GetStaticItemControlBlock(), aIState, bSearchParent));
// SfxItemState::DEFAULT
// SfxItemState::DONTCARE || SfxItemState::DISABLED -> should already be
// solved from ImplInvalidateItem/ImplDisableItem, but to have the
// fallback here additionally is never wrong
// in short: no specific ItemBase -> use default
if(nullptr == pItem)
{
return StateAndItem<TItem>(
aIState,
Item::getDefault<TItem>());
}
// SfxItemState::SET // SfxItemState::SET
return StateAndItem<TItem>( return StateAndItem<T>(
aIState, aIState,
*static_cast<const TItem*>(pItem)); *static_cast<const T*>(pItem));
} }
template< typename TItem > bool clearItem() template< typename T > bool clearItem()
{ {
return implClearItem(typeid(TItem).hash_code()); return implClearItem(T::GetStaticItemControlBlock());
} }
}; };
} // end of namespace Item } // end of namespace Item
......
...@@ -15,57 +15,17 @@ ...@@ -15,57 +15,17 @@
namespace Item namespace Item
{ {
std::unordered_map<size_t, ItemControlBlock*>& getRegisteredItemControlBlocks()
{
// all globally registered ItemControlBlocks
static std::unordered_map<size_t, ItemControlBlock*> aItemControlBlocks;
return aItemControlBlocks;
}
ItemControlBlock::ItemControlBlock()
: m_aDefaultItem(),
m_aConstructDefaultItem(),
m_aCloneItem(),
m_aHashCode(0),
m_aName()
{
// EmptyItemControlBlock: *only* for internal use, fallback for
// extra-Items like ImplInvalidateItem/ImplDisableItem
// Do *not* register this instance at aItemControlBlocks (!)
}
ItemControlBlock::ItemControlBlock( ItemControlBlock::ItemControlBlock(
std::function<ItemBase*()>aConstructDefaultItem, std::function<ItemBase*()>aConstructDefaultItem,
std::function<ItemBase*(const ItemBase&)>aCloneItem, std::function<ItemBase*(const ItemBase&)>aCloneItem,
size_t aHashCode,
const OUString& rName) const OUString& rName)
: m_aDefaultItem(), : m_aDefaultItem(),
m_aConstructDefaultItem(aConstructDefaultItem), m_aConstructDefaultItem(aConstructDefaultItem),
m_aCloneItem(aCloneItem), m_aCloneItem(aCloneItem),
m_aHashCode(aHashCode),
m_aName(rName) m_aName(rName)
{ {
assert(nullptr != m_aConstructDefaultItem && "nullptr not allowed, a Item-Constructor *is* required (!)"); assert(nullptr != m_aConstructDefaultItem && "nullptr not allowed, a Item-Constructor *is* required (!)");
assert(nullptr != aCloneItem && "nullptr not allowed, a Item-Clone lambda *is* required (!)"); assert(nullptr != aCloneItem && "nullptr not allowed, a Item-Clone lambda *is* required (!)");
assert(size_t(0) != m_aHashCode && "NULL hash_code not allowed, a Item-identifier (usually typeid(T).hash_code()) *is* required (!)");
assert(getRegisteredItemControlBlocks().find(m_aHashCode) == getRegisteredItemControlBlocks().end()
&& "Constructed ItemControlBlock already globally registered - this hints to an error (!)");
// globally register new ItemControlBlock
getRegisteredItemControlBlocks()[m_aHashCode] = this;
}
ItemControlBlock::~ItemControlBlock()
{
assert((0 == m_aHashCode || // is the EmptyItemControlBlock
getRegisteredItemControlBlocks().find(m_aHashCode) != getRegisteredItemControlBlocks().end()) // or has to exist
&& "Destructed ItemControlBlock was not globally registered - this hints to an error (!)");
// since ItemControlBlocks themselves are static this can only happen when
// handling libs, e.g. lib shutdown and of course app shutdown. Nonetheless
// do this to avoid errors
if(0 != m_aHashCode) // do not forget default constructor -> EmptyItemControlBlock
{
getRegisteredItemControlBlocks().erase(getRegisteredItemControlBlocks().find(m_aHashCode));
}
} }
const ItemBase& ItemControlBlock::getDefault() const const ItemBase& ItemControlBlock::getDefault() const
...@@ -97,23 +57,6 @@ namespace Item ...@@ -97,23 +57,6 @@ namespace Item
pNewInstance->putAnyValues(rArgs); pNewInstance->putAnyValues(rArgs);
return std::unique_ptr<const ItemBase>(pNewInstance); return std::unique_ptr<const ItemBase>(pNewInstance);
} }
ItemControlBlock* ItemControlBlock::getItemControlBlock(size_t hash_code)
{
if(size_t(0) != hash_code)
{
std::unordered_map<size_t, ItemControlBlock*>& rBlocks(getRegisteredItemControlBlocks());
const auto aCandidate(rBlocks.find(hash_code));
if(aCandidate != rBlocks.end())
{
return aCandidate->second;
}
}
static ItemControlBlock aEmptyItemControlBlock;
return &aEmptyItemControlBlock;
}
} // end of namespace Item } // end of namespace Item
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
This diff is collapsed.
...@@ -31,7 +31,6 @@ namespace Item ...@@ -31,7 +31,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueSimple(); }, [](){ return new MultiValueSimple(); },
[](const ItemBase& rRef){ return new MultiValueSimple(static_cast<const MultiValueSimple&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueSimple(static_cast<const MultiValueSimple&>(rRef)); },
typeid(MultiValueSimple).hash_code(),
"MultiValueSimple"); "MultiValueSimple");
return aItemControlBlock; return aItemControlBlock;
...@@ -90,7 +89,6 @@ namespace Item ...@@ -90,7 +89,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueSimple_derivedClass(); }, [](){ return new MultiValueSimple_derivedClass(); },
[](const ItemBase& rRef){ return new MultiValueSimple_derivedClass(static_cast<const MultiValueSimple_derivedClass&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueSimple_derivedClass(static_cast<const MultiValueSimple_derivedClass&>(rRef)); },
typeid(MultiValueSimple_derivedClass).hash_code(),
"MultiValueSimple_derivedClass"); "MultiValueSimple_derivedClass");
return aItemControlBlock; return aItemControlBlock;
...@@ -117,7 +115,6 @@ namespace Item ...@@ -117,7 +115,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueSimple_plus(); }, [](){ return new MultiValueSimple_plus(); },
[](const ItemBase& rRef){ return new MultiValueSimple_plus(static_cast<const MultiValueSimple_plus&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueSimple_plus(static_cast<const MultiValueSimple_plus&>(rRef)); },
typeid(MultiValueSimple_plus).hash_code(),
"MultiValueSimple_plus"); "MultiValueSimple_plus");
return aItemControlBlock; return aItemControlBlock;
...@@ -167,7 +164,6 @@ namespace Item ...@@ -167,7 +164,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueSimple_plus_derivedClass(); }, [](){ return new MultiValueSimple_plus_derivedClass(); },
[](const ItemBase& rRef){ return new MultiValueSimple_plus_derivedClass(static_cast<const MultiValueSimple_plus_derivedClass&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueSimple_plus_derivedClass(static_cast<const MultiValueSimple_plus_derivedClass&>(rRef)); },
typeid(MultiValueSimple_plus_derivedClass).hash_code(),
"MultiValueSimple_plus_derivedClass"); "MultiValueSimple_plus_derivedClass");
return aItemControlBlock; return aItemControlBlock;
...@@ -194,7 +190,6 @@ namespace Item ...@@ -194,7 +190,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueBuffered(); }, [](){ return new MultiValueBuffered(); },
[](const ItemBase& rRef){ return new MultiValueBuffered(static_cast<const MultiValueBuffered&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueBuffered(static_cast<const MultiValueBuffered&>(rRef)); },
typeid(MultiValueBuffered).hash_code(),
"MultiValueBuffered"); "MultiValueBuffered");
return aItemControlBlock; return aItemControlBlock;
...@@ -346,7 +341,6 @@ namespace Item ...@@ -346,7 +341,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueBuffered_derivedClass(); }, [](){ return new MultiValueBuffered_derivedClass(); },
[](const ItemBase& rRef){ return new MultiValueBuffered_derivedClass(static_cast<const MultiValueBuffered_derivedClass&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueBuffered_derivedClass(static_cast<const MultiValueBuffered_derivedClass&>(rRef)); },
typeid(MultiValueBuffered_derivedClass).hash_code(),
"MultiValueBuffered_derivedClass"); "MultiValueBuffered_derivedClass");
return aItemControlBlock; return aItemControlBlock;
...@@ -373,7 +367,6 @@ namespace Item ...@@ -373,7 +367,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueBuffered_plus(); }, [](){ return new MultiValueBuffered_plus(); },
[](const ItemBase& rRef){ return new MultiValueBuffered_plus(static_cast<const MultiValueBuffered_plus&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueBuffered_plus(static_cast<const MultiValueBuffered_plus&>(rRef)); },
typeid(MultiValueBuffered_plus).hash_code(),
"MultiValueBuffered_plus"); "MultiValueBuffered_plus");
return aItemControlBlock; return aItemControlBlock;
...@@ -489,7 +482,6 @@ namespace Item ...@@ -489,7 +482,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new MultiValueBuffered_plus_derivedClass(); }, [](){ return new MultiValueBuffered_plus_derivedClass(); },
[](const ItemBase& rRef){ return new MultiValueBuffered_plus_derivedClass(static_cast<const MultiValueBuffered_plus_derivedClass&>(rRef)); }, [](const ItemBase& rRef){ return new MultiValueBuffered_plus_derivedClass(static_cast<const MultiValueBuffered_plus_derivedClass&>(rRef)); },
typeid(MultiValueBuffered_plus_derivedClass).hash_code(),
"MultiValueBuffered_plus_derivedClass"); "MultiValueBuffered_plus_derivedClass");
return aItemControlBlock; return aItemControlBlock;
...@@ -516,7 +508,6 @@ namespace Item ...@@ -516,7 +508,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new CntInt16_derived(); }, [](){ return new CntInt16_derived(); },
[](const ItemBase& rRef){ return new CntInt16_derived(static_cast<const CntInt16_derived&>(rRef)); }, [](const ItemBase& rRef){ return new CntInt16_derived(static_cast<const CntInt16_derived&>(rRef)); },
typeid(CntInt16_derived).hash_code(),
"CntInt16_derived"); "CntInt16_derived");
return aItemControlBlock; return aItemControlBlock;
...@@ -543,7 +534,6 @@ namespace Item ...@@ -543,7 +534,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new CntOUString_derived(); }, [](){ return new CntOUString_derived(); },
[](const ItemBase& rRef){ return new CntOUString_derived(static_cast<const CntOUString_derived&>(rRef)); }, [](const ItemBase& rRef){ return new CntOUString_derived(static_cast<const CntOUString_derived&>(rRef)); },
typeid(CntOUString_derived).hash_code(),
"CntOUString_derived"); "CntOUString_derived");
return aItemControlBlock; return aItemControlBlock;
......
...@@ -21,7 +21,6 @@ namespace Item ...@@ -21,7 +21,6 @@ namespace Item
static ItemControlBlock aItemControlBlock( static ItemControlBlock aItemControlBlock(
[](){ return new TransformAnchor(); }, [](){ return new TransformAnchor(); },
[](const ItemBase& rRef){ return new TransformAnchor(static_cast<const TransformAnchor&>(rRef)); }, [](const ItemBase& rRef){ return new TransformAnchor(static_cast<const TransformAnchor&>(rRef)); },
typeid(TransformAnchor).hash_code(),
"TransformAnchor"); "TransformAnchor");
return aItemControlBlock; return aItemControlBlock;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment