I have a directed graph model represented by `Node` and `Edge` classes. Each `Node` object holds pointers to all its outgoing AND incoming edges and each `Edge` object holds pointers to its origin and end (`Node` objects).

```
class Model
{
public:
    ~Model() 
    {
        for(Node *node : nodes)
            delete node;
    }

    QVector<Node*> nodes;
};

class Node
{
    public:
    ~Node
    {
            for(Edge *edge : from)
            {
                if(edge)
                {
                    edge->to->to.replace(edge, nullptr);      
                    delete edge;
                }
            }

            for(Edge *edge : to)
            {
                if(edge)
                {
                    edge->from->from.replace(edge, nullptr);
                    delete edge;
                }
            }
    }

    QVector<Edge*> from;
    QVector<Edge*> to;
};

class Edge
{
public:
    Node *from;
    Node *to;
};

```

So far I have it implemented using raw pointers like this. It is rather complicated and involves manually removing the pointer from the other side of the edge (for every edge) when the node is deleted.I would rather like using smart pointers to make it all a bit safer and cleaner.

However the fact that I need to delete the edge (and invalidate the pointer on the other side) for both sides makes it problematic. If I used shared pointers the edges would never get deleted (and the remaining reference would remain valid even when the other side is gone). If I used weak pointers I would in turn need to store the shared pointer somewhere else which would again become more complicated because I would need to look it up and get rid of it whenever either side gets deleted.

What I need is a "smart" pointer that will keep track of the pointee (like shared pointer) but delete its pointee when its internal counter drops to 1 (instead of 0) so that it invalidates itself (like weak pointer) without going out of scope.

Should I write it myself or is there another solution?