Example #1
0
// ------------------------------------------------------------------------------------------------
void Signal::Tail(Object & env, Function & func)
{
    // Don't attempt to search anything if there's no head
    if (m_Head == nullptr)
    {
        m_Head = new Slot(env, func, nullptr);
        // We're done here
        return;
    }

    const Slot slot{env, func};
    // Don't attempt to search anything if there's only one element
    if (m_Head->mNext == nullptr)
    {
        // Is it already inserted?
        if (*m_Head != slot)
        {
            m_Head->mNext = new Slot(env, func, nullptr);
            // Link with the head
            m_Head->mNext->mPrev = m_Head;
        }
        // We're done here
        return;
    }

    // Grab the head node
    Slot * node = m_Head, * prev = nullptr;
    // Walk down the chain and find a matching node
    for (; node != nullptr; prev = node, node = node->mNext)
    {
        if (*node == slot)
        {
            break; // Found it
        }
    }
    // Have we found anything?
    if (node == nullptr)
    {
        // Create the slot now
        node = new Slot(env, func, nullptr);
    }
    else
    {
        // Walk down the chain until the end
        while (prev->mNext != nullptr)
        {
            prev = prev->mNext;
        }
        // Finally, detach the node from it's current position
        node->Detach();
    }
    // Knowing 'prev' points to last element
    node->AttachPrev(prev);
}