mercredi 24 mai 2017

shared_ptr member with polymorphism

I've such codes for B+ Set

template<class _Key>
class BPlusTreeSetNode
{
   public:
.............................
        void setParent(std::shared_ptr<BPlusTreeSetNode>& pNode, int32 nFoundIndex = -1)
        {
            m_pParent =  pNode;
            m_nFoundIndex = nFoundIndex;
            if (pNode.get())
                m_nParent = pNode->addr();
            else
                m_nParent = -1;
        }

        int64 parentAddr() const { return m_nParent; }
        int32 foundIndex() const  { return m_nFoundIndex; }
        void setFoundIndex(int32 nFoundIndex) { m_nFoundIndex = nFoundIndex; }
        std::shared_ptr<BPlusTreeSetNode> parentNodePtr() { return m_pParent.lock(); }
............................
  protected:

    typedef std::weak_ptr<BPlusTreeSetNode> TParentNodePtr;
    TParentNodePtr m_pParent;
    int32 m_nFoundIndex;
    int64 m_nParentAddr;
};

.......................................
template< class _TKey,  class _TNode = BPlusTreeSetNode<_TKey> >
class TBPlusSet
{
................................................
typedef std::shared_ptr<_TNode> TreeNodePtr;
TreeNodePtr m_pRoot;
................................................
TIterator find(const TKey& key)
{
..........................................................................
    int32 nIndex = -1;
    int64 nNextAddr = m_pRoot->lower_bound(comp, key, nIndex);
    TreeNodePtr pParent = m_pRoot;
    for (;;)
    {
        if (nNextAddr == -1)
        {
            break;
        }
        TreeNodePtr pNode = getNode(nNextAddr);
        if (!pNode.get())
        {
            return TIterator(this, TreeNodePtr(), -1);
            break;
        }
        pNode->setParent(pParent, nIndex); // <-- There is error

     .......................................................
    } 
}
};

In B+ Set is all ok. But if i extend B+ Set to B+ Map:

template<class _Key,class _Value,  class _Transaction>
class BPlusTreeMapNode : public BPlusTreeSetNode<_Key, _Transaction>
{
   public:
..........................................
};

template< class _Key, class _Value, class _TNode = BPlusTreeMapNode<_TKey, _Value> >
class TBPlusMap : public TBPlusSet<_Key, _TNode >
{
..................................
}

i have get errors

Error C2664 'void embDB::BPlusTreeSetNode<_Key>::setParent(std::shared_ptr

<_Key> > &,int32)':
 cannot convert argument 1 from 'std::shared_ptr<embDB::BPlusTreeMapNode<int64,int64>>' to 'std::shared_ptr<embDB::BPlusTreeSetNode<_TKey> &

I think that in this architecture, the only solution to use std :: enable_shared_from_this, maybe there are other solutions?

Aucun commentaire:

Enregistrer un commentaire