MUSX Document Model
Loading...
Searching...
No Matches
Instrument.h
1/*
2 * Copyright (C) 2026, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <cstddef>
25#include <map>
26#include <unordered_map>
27#include <vector>
28
29#include "CommonClasses.h"
30
31namespace musx {
32namespace dom {
33
34namespace others {
35class StaffComposite;
36}
37
45class InstrumentMap;
46
48{
49public:
52 std::string instUuid;
53
55 bool operator==(const InstrumentIdentity& other) const
56 { return instUuid == other.instUuid; }
57
59 bool operator<(const InstrumentIdentity& other) const
60 { return instUuid < other.instUuid; }
61 };
62
68
70 using InstrumentChangeEvents = std::map<MusicPoint, InstrumentChange>;
71
75 explicit InstrumentInfo(const DocumentWeakPtr& document, Cmper partId = SCORE_PARTID)
76 : DocumentElement(document, partId) {}
77
78 std::unordered_map<StaffCmper, size_t> staves;
81
83 std::vector<StaffCmper> getSequentialStaves() const;
84
93
95 std::vector<InstrumentIdentity> getInstrumentIdentities() const;
96
99 InstrumentIdentity getInstrumentIdentityAt(MusicPoint point) const;
100};
101
106class InstrumentMap : public DocumentElement, private std::unordered_map<StaffCmper, InstrumentInfo>
107{
108 // Private inheritance keeps the map implementation detail out of the public API and avoids any
109 // accidental polymorphic use of a standard container with a non-virtual destructor.
110public:
111 using typename std::unordered_map<StaffCmper, InstrumentInfo>::key_type;
112 using typename std::unordered_map<StaffCmper, InstrumentInfo>::mapped_type;
113 using typename std::unordered_map<StaffCmper, InstrumentInfo>::value_type;
114 using typename std::unordered_map<StaffCmper, InstrumentInfo>::hasher;
115 using typename std::unordered_map<StaffCmper, InstrumentInfo>::key_equal;
116 using typename std::unordered_map<StaffCmper, InstrumentInfo>::allocator_type;
117 using typename std::unordered_map<StaffCmper, InstrumentInfo>::size_type;
118 using typename std::unordered_map<StaffCmper, InstrumentInfo>::difference_type;
119 using typename std::unordered_map<StaffCmper, InstrumentInfo>::reference;
120 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_reference;
121 using typename std::unordered_map<StaffCmper, InstrumentInfo>::pointer;
122 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_pointer;
123 using typename std::unordered_map<StaffCmper, InstrumentInfo>::iterator;
124 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_iterator;
125 using typename std::unordered_map<StaffCmper, InstrumentInfo>::local_iterator;
126 using typename std::unordered_map<StaffCmper, InstrumentInfo>::const_local_iterator;
127
131 explicit InstrumentMap(const DocumentWeakPtr& document, Cmper partId = SCORE_PARTID)
132 : DocumentElement(document, partId) {}
133
134 using std::unordered_map<StaffCmper, InstrumentInfo>::begin;
135 using std::unordered_map<StaffCmper, InstrumentInfo>::end;
136 using std::unordered_map<StaffCmper, InstrumentInfo>::empty;
137 using std::unordered_map<StaffCmper, InstrumentInfo>::size;
138 using std::unordered_map<StaffCmper, InstrumentInfo>::clear;
139 using std::unordered_map<StaffCmper, InstrumentInfo>::find;
140 using std::unordered_map<StaffCmper, InstrumentInfo>::count;
141 using std::unordered_map<StaffCmper, InstrumentInfo>::emplace;
142 using std::unordered_map<StaffCmper, InstrumentInfo>::insert;
143 using std::unordered_map<StaffCmper, InstrumentInfo>::erase;
144 using std::unordered_map<StaffCmper, InstrumentInfo>::operator[];
145 using std::unordered_map<StaffCmper, InstrumentInfo>::at;
146
150 const InstrumentInfo* getInstrumentForStaff(StaffCmper staffId) const;
151};
152
153} // namespace dom
154} // namespace musx
Base for DOM classes that belong to a Document.
Definition DocumentElement.h:46
Derived description of a logical instrument in a document.
Definition Instrument.h:48
std::vector< StaffCmper > getSequentialStaves() const
Returns the staffIds in sequence as they appear in Scroll View in the score.
Definition Instrument.cpp:73
Cmper multistaffGroupId
The others::MultiStaffInstrumentGroup that defines the instrument. (May be zero.)
Definition Instrument.h:80
std::vector< InstrumentIdentity > getInstrumentIdentities() const
Returns the unique instrument identities in order of first appearance.
Definition Instrument.cpp:150
std::map< MusicPoint, InstrumentChange > InstrumentChangeEvents
Effective top-staff states keyed by the musical location where each state begins.
Definition Instrument.h:70
InstrumentInfo(const DocumentWeakPtr &document, Cmper partId=SCORE_PARTID)
Constructs an empty instrument info for a score or linked part.
Definition Instrument.h:75
InstrumentChangeEvents getChanges() const
Returns effective instrument states for this logical instrument.
Definition Instrument.cpp:87
Cmper staffGroupId
The details::StaffGroup that visually represents the instrument. (May be zero.)
Definition Instrument.h:79
std::unordered_map< StaffCmper, size_t > staves
List of each staffId with its sequence index from top to bottom.
Definition Instrument.h:78
InstrumentIdentity getInstrumentIdentityAt(MusicPoint point) const
Returns the instrument identity in effect at the specified music point.
Definition Instrument.cpp:162
A list of instruments, which may be single- or multi-staff.
Definition Instrument.h:107
const InstrumentInfo * getInstrumentForStaff(StaffCmper staffId) const
Get the instrument info for the given staffId in the given map.
Definition Instrument.cpp:172
InstrumentMap(const DocumentWeakPtr &document, Cmper partId=SCORE_PARTID)
Constructs an empty instrument map for a score or linked part.
Definition Instrument.h:131
Utility class that represents a single location in musical time.
Definition CommonClasses.h:423
std::shared_ptr< const T > MusxInstance
Defines the type of a musx instance stored in a pool.
Definition MusxInstance.h:40
constexpr Cmper SCORE_PARTID
The part id of the score.
Definition Fundamentals.h:79
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition DocumentElement.h:37
int16_t StaffCmper
Enigma staff (staffId) Cmper (may be negative when not applicable)
Definition Fundamentals.h:65
object model for musx file (enigmaxml)
Definition BaseClasses.h:38
Effective instrument state beginning at a musical location.
Definition Instrument.h:64
InstrumentIdentity identity
Stable identity for the effective instrument.
Definition Instrument.h:65
MusxInstance< others::StaffComposite > topStaffComposite
Effective state of the top staff.
Definition Instrument.h:66
Stable identity for a distinct instrument used by this logical instrument.
Definition Instrument.h:51
std::string instUuid
The effective instrument UUID.
Definition Instrument.h:52
bool operator==(const InstrumentIdentity &other) const
Equality comparison operator.
Definition Instrument.h:55
bool operator<(const InstrumentIdentity &other) const
Ordering operator for use in ordered containers.
Definition Instrument.h:59