Repository: microsoft/terminal
File: src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp (C++)

Code before:
    size{ other.size },
    isMinimumSize{ other.isMinimumSize },
    firstChild{ other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr },
    secondChild{ other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr },
    nextFirstChild{ other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr },
    nextSecondChild{ other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr }
{
}

// Method Description:
// - Makes sure that this node and all its descendants equal the supplied node.
//   This may be more efficient that copy construction since it will reuse its
//   allocated children.
// Arguments:
// - other: Node to take the values from.
// Return Value:
// - itself
Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& other)
{
    size = other.size;
    isMinimumSize = other.isMinimumSize;

    _AssignChildNode(firstChild, other.firstChild.get());
    _AssignChildNode(secondChild, other.secondChild.get());
    _AssignChildNode(nextFirstChild, other.nextFirstChild.get());
    _AssignChildNode(nextSecondChild, other.nextSecondChild.get());

    return *this;
}

// Method Description:
// - Performs assignment operation on a single child node reusing
// - current one if present.
// Arguments:
// - nodeField: Reference to our field holding concerned node.
// - other: Node to take the values from.
// Return Value:
// - <none>
void Pane::LayoutSizeNode::_AssignChildNode(std::unique_ptr<LayoutSizeNode>& nodeField, const LayoutSizeNode* const newNode)
{
    if (newNode)
    {
        if (nodeField)
        {
            *nodeField = *newNode;
        }
        else
        {
            nodeField = std::make_unique<LayoutSizeNode>(*newNode);
        }
    }

Review comment (performance):
This is definitely a lot shorter, but it's not more efficient by any means. It's actually significantly less efficient. Although I bet it doesn't really matter, since panes aren't really that widely used in the first place.

Code after:
    size{ other.size },
    isMinimumSize{ other.isMinimumSize },
    firstChild{ other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr },
    secondChild{ other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr },
    nextFirstChild{ other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr },
    nextSecondChild{ other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr }
{
}

// Method Description:
// - Makes sure that this node and all its descendants equal the supplied node.
//   This may be more efficient that copy construction since it will reuse its
//   allocated children.
// Arguments:
// - other: Node to take the values from.
// Return Value:
// - itself
Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& other)
{
    size = other.size;
    isMinimumSize = other.isMinimumSize;

    firstChild = other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr;
    secondChild = other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr;
    nextFirstChild = other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr;
    nextSecondChild = other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr;

    return *this;
}