1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 #ifndef RG_MAPPEDINSERTERSORT_H
19 #define RG_MAPPEDINSERTERSORT_H
20 
21 #include "sound/MappedInserterBase.h"
22 #include "sound/MappedEvent.h"
23 #include <list>
24 
25 namespace Rosegarden
26 {
27 
28 /// Sorts MappedEvent objects.
29 /**
30  * SortingInserter is a pseudo-inserter that accepts events,
31  * sorts them (as fetchEvents does not wholly do) and (via insertSorted())
32  * re-inserts them somewhere, sorted.
33  *
34  * This is used when generating a standard MIDI file.  See
35  * MidiFile::convertToMidi().
36  *
37  * @author Tom Breton (Tehom)
38  */
39 class SortingInserter : public MappedInserterBase
40 {
41     /// Comparison functor for std::list::sort()
42     struct MappedEventCmp
43     {
44         bool operator()(const MappedEvent &a, const MappedEvent &b) const {
45             // ??? This functor may not be needed given that operator<
46             //     is defined for MappedEvent.  std::list::sort() without
47             //     arguments will use MappedEvent::operator<().
48             return a < b;
49         }
50     };
51 
52 public:
53     /// Sorts the events and copies them to an inserter.
54     /**
55      * Call this after inserting events via insertCopy() to get the
56      * sorted events out.
57      *
58      * rename: exportSorted()?  extractSorted()?  Something a little more
59      *         "output-oriented" seems like it might be easier to understand.
60      */
61     void insertSorted(MappedInserterBase &exporter);
62 
63 private:
64     /// Inserts an event into a list in preparation for sorting.
65     /**
66      * See insertSorted() which sorts the list and extracts the events
67      * from the list in sorted order.
68      */
69     void insertCopy(const MappedEvent &evt) override;
70 
71     // NB, this is not the same as MappedEventList which is actually a
72     // std::multiset.
73     std::list<MappedEvent> m_list;
74 };
75 
76 }
77 
78 #endif /* ifndef RG_MAPPEDINSERTERSORT_H */
79