This is used when you want to swap the track at s1 with the track at s2. The complication is that the tracks are stored in a single linked list.
| 686 | // The complication is that the tracks are stored in a single |
| 687 | // linked list. |
| 688 | void TrackList::SwapNodes(TrackNodePointer s1, TrackNodePointer s2) |
| 689 | { |
| 690 | // if a null pointer is passed in, we want to know about it |
| 691 | wxASSERT(!isNull(s1)); |
| 692 | wxASSERT(!isNull(s2)); |
| 693 | |
| 694 | // Safety check... |
| 695 | if (s1 == s2) |
| 696 | return; |
| 697 | |
| 698 | // Be sure s1 is the earlier iterator |
| 699 | { |
| 700 | const auto begin = ListOfTracks::begin(); |
| 701 | auto d1 = std::distance(begin, s1); |
| 702 | auto d2 = std::distance(begin, s2); |
| 703 | if (d1 > d2) |
| 704 | std::swap(s1, s2); |
| 705 | } |
| 706 | |
| 707 | // For saving the removed tracks |
| 708 | using Saved = ListOfTracks::value_type; |
| 709 | Saved saved1, saved2; |
| 710 | |
| 711 | auto doSave = [&](Saved &saved, TrackNodePointer &s) { |
| 712 | saved = *s, s = erase(s); |
| 713 | }; |
| 714 | |
| 715 | doSave(saved1, s1); |
| 716 | // The two ranges are assumed to be disjoint but might abut |
| 717 | const bool same = (s1 == s2); |
| 718 | doSave(saved2, s2); |
| 719 | if (same) |
| 720 | // Careful, we invalidated s1 in the second doSave! |
| 721 | s1 = s2; |
| 722 | |
| 723 | // Reinsert them |
| 724 | auto doInsert = [&](Saved &saved, TrackNodePointer &s) { |
| 725 | const auto pTrack = saved.get(); |
| 726 | // Insert before s, and reassign s to point at the new node before |
| 727 | // old s; which is why we saved pointers in backwards order |
| 728 | pTrack->SetOwner(shared_from_this(), s = insert(s, saved) ); |
| 729 | }; |
| 730 | // This does not invalidate s2 even when it equals s1: |
| 731 | doInsert(saved2, s1); |
| 732 | // Even if s2 was same as s1, this correctly inserts the saved1 range |
| 733 | // after the saved2 range, when done after: |
| 734 | doInsert(saved1, s2); |
| 735 | |
| 736 | // Now correct the Index in the tracks, and other things |
| 737 | RecalcPositions(s1); |
| 738 | PermutationEvent(s1); |
| 739 | } |
| 740 | |
| 741 | bool TrackList::MoveUp(Track &t) |
| 742 | { |