MUSX Document Model
Loading...
Searching...
No Matches
MusxInstance.h
1/*
2 * Copyright (C) 2025, 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 <vector>
25#include <memory>
26
27#include "Fundamentals.h"
28
29namespace musx {
30namespace dom {
31
34template <typename T>
35using MusxInstance = std::shared_ptr<const T>;
36
39template <typename T>
40using MusxInstanceWeak = std::weak_ptr<const T>;
41
42class Document;
43
44namespace others {
45class StaffUsed;
46class Staff;
47}
48
57template <typename T>
58class MusxInstanceListBase : public std::vector<MusxInstance<T>>
59{
60 using VectorType = std::vector<MusxInstance<T>>;
61
62public:
64 explicit MusxInstanceListBase(const std::weak_ptr<Document>& document, Cmper partId)
65 : m_document(document), m_partId(partId) {}
66
68 Cmper getRequestedPartId() const { return m_partId; }
69
71 std::shared_ptr<Document> getDocument() const
72 {
73 auto document = m_document.lock();
74 MUSX_ASSERT_IF(!document) {
75 throw std::logic_error("Document pointer is no longer valid.");
76 }
77 return document;
78 }
79
80private:
81 std::weak_ptr<Document> m_document;
82 Cmper m_partId;
83};
84
94template <typename T>
96{
97public:
98 using MusxInstanceListBase<T>::MusxInstanceListBase;
99
100 // No helpers by default
101};
102
107template<>
108class MusxInstanceList<others::StaffUsed> : public MusxInstanceListBase<others::StaffUsed>
109{
110public:
111 using MusxInstanceListBase<others::StaffUsed>::MusxInstanceListBase;
112
115 MusxInstance<others::Staff> getStaffInstanceAtIndex(Cmper index) const;
116
119 std::optional<size_t> getIndexForStaff(StaffCmper staffId) const;
120};
121
122} // namespace dom
123} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:67
A container of pooled shared object instances from an ObjectPool.
Definition MusxInstance.h:59
MusxInstanceListBase(const std::weak_ptr< Document > &document, Cmper partId)
Default constructor.
Definition MusxInstance.h:64
Cmper getRequestedPartId() const
Gets the part id that was used to create this list.
Definition MusxInstance.h:68
std::shared_ptr< Document > getDocument() const
Gets the document that was used to create this list.
Definition MusxInstance.h:71
Provides optional per-type extension methods for MusxInstanceList.
Definition MusxInstance.h:96
An array of StaffUsed defines a set of staves in a staff system or in Scroll View.
Definition Others.h:631
std::shared_ptr< const T > MusxInstance
Defines the type of a musx instance stored in a pool.
Definition MusxInstance.h:35
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
int16_t StaffCmper
Enigma staff (staffId) Cmper (may be negative when not applicable)
Definition Fundamentals.h:65
std::weak_ptr< const T > MusxInstanceWeak
Defines a weak ptr to the type of a musx instance stored in a pool.
Definition MusxInstance.h:40
object model for musx file (enigmaxml)
Definition BaseClasses.h:36