<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def on_selection_changed(self):
 """ Callback invoked one the selection has changed. """

        d = self.declaration
        selection = self.scene.selectedItems()
        self._guards |= 0x01
        try:
            d.selected_items = [item.ref().declaration for item in selection
                                if item.ref()]
        finally:
            self._guards &= ~0x01