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#include <functional>
27#include <optional>
28#include <type_traits>
29#include <utility>
30
31#include "Fundamentals.h"
32
33namespace musx {
34namespace dom {
35
38template <typename T>
39using MusxInstance = std::shared_ptr<const T>;
40
43template <typename T>
44using MusxInstanceWeak = std::weak_ptr<const T>;
45
46class Document;
47class MusicRange;
48class EntryInfoPtr;
49
50namespace others {
51class StaffUsed;
52class Staff;
53class StaffComposite;
54}
55
64template <typename T>
65class MusxInstanceListBase : public std::vector<MusxInstance<T>>
66{
67 using VectorType = std::vector<MusxInstance<T>>;
68
69public:
71 explicit MusxInstanceListBase(const std::weak_ptr<Document>& document, Cmper partId)
72 : m_document(document), m_partId(partId) {}
73
75 Cmper getRequestedPartId() const { return m_partId; }
76
78 std::shared_ptr<Document> getDocument() const
79 {
80 auto document = m_document.lock();
81 MUSX_ASSERT_IF(!document) {
82 throw std::logic_error("Document pointer is no longer valid.");
83 }
84 return document;
85 }
86
87private:
88 std::weak_ptr<Document> m_document;
89 Cmper m_partId;
90};
91
101template <typename T>
103{
104public:
105 using MusxInstanceListBase<T>::MusxInstanceListBase;
106
107 // No helpers by default
108};
109
114template<>
115class MusxInstanceList<others::StaffUsed> : public MusxInstanceListBase<others::StaffUsed>
116{
117public:
118 using MusxInstanceListBase<others::StaffUsed>::MusxInstanceListBase;
119
122 MusxInstance<others::Staff> getStaffInstanceAtIndex(size_t index) const;
123
128 MusxInstance<others::StaffComposite> getStaffInstanceAtIndex(size_t index, MeasCmper measureId, Edu eduPosition = 0) const;
129
132 std::optional<size_t> getIndexForStaff(StaffCmper staffId) const;
133
135 StaffCmper getTopStaffId() const;
136
138 StaffCmper getBottomStaffId() const;
139
149 bool iterateEntries(size_t startIndex, size_t endIndex, const MusicRange& range, std::function<bool(const EntryInfoPtr&)> iterator) const;
150};
151
178template<class T>
180 static_assert(!std::is_reference_v<T>, "T must not be a reference");
181 using OwnedT = std::remove_const_t<T>;
182
183public:
185 DeferredReference() noexcept = default;
186
189 DeferredReference(const T& ref) noexcept : m_ref(&ref) {}
190
193 explicit DeferredReference(const T* ptr) noexcept : m_ref(ptr) {}
194
202 const T& emplace(OwnedT&& value) noexcept(std::is_nothrow_move_constructible_v<OwnedT>) {
203 m_owned.emplace(std::move(value));
204 m_ref = std::addressof(*m_owned);
205 return *m_ref;
206 }
207
209 explicit operator bool() const noexcept { return m_ref != nullptr; }
210
213 const T& get() const noexcept { return *m_ref; }
214
216 const T* operator->() const noexcept { return m_ref; }
217
219 const T& operator*() const noexcept { return *m_ref; }
220
221private:
222 std::optional<OwnedT> m_owned{};
223 const T* m_ref = nullptr;
224};
225
226#ifndef DOXYGEN_SHOULD_IGNORE_THIS
235template<class T> DeferredReference(const T&) -> DeferredReference<T>;
236template<class T> DeferredReference(const T*) -> DeferredReference<T>;
238#endif // DOXYGEN_SHOULD_IGNORE_THIS
239
240} // namespace dom
241} // namespace musx
Wraps a reference to an existing object or owns a temporary value if needed.
Definition MusxInstance.h:179
DeferredReference(const T *ptr) noexcept
Constructs a non-owning DeferredReference bound to a pointer.
Definition MusxInstance.h:193
DeferredReference() noexcept=default
Constructs an empty DeferredReference with no bound reference.
const T & emplace(OwnedT &&value) noexcept(std::is_nothrow_move_constructible_v< OwnedT >)
Moves a value into owned storage and binds to it.
Definition MusxInstance.h:202
const T * operator->() const noexcept
Provides pointer-like access to the referenced or owned object.
Definition MusxInstance.h:216
const T & operator*() const noexcept
Dereferences to the referenced or owned object.
Definition MusxInstance.h:219
const T & get() const noexcept
Gets a const reference to the referenced or owned object.
Definition MusxInstance.h:213
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:68
Wraps a frame of shared_ptr<const EntryInfo> and an index for per entry access. This class manages ow...
Definition Entries.h:420
Utility class that represents of a range of musical time.
Definition CommonClasses.h:474
A container of pooled shared object instances from an ObjectPool.
Definition MusxInstance.h:66
MusxInstanceListBase(const std::weak_ptr< Document > &document, Cmper partId)
Default constructor.
Definition MusxInstance.h:71
Cmper getRequestedPartId() const
Gets the part id that was used to create this list.
Definition MusxInstance.h:75
std::shared_ptr< Document > getDocument() const
Gets the document that was used to create this list.
Definition MusxInstance.h:78
Provides optional per-type extension methods for MusxInstanceList.
Definition MusxInstance.h:103
An array of StaffUsed defines a set of staves in a staff system or in Scroll View.
Definition Others.h:2712
int16_t MeasCmper
Enigma meas Cmper (may be negative when not applicable)
Definition Fundamentals.h:64
std::shared_ptr< const T > MusxInstance
Defines the type of a musx instance stored in a pool.
Definition MusxInstance.h:39
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
int32_t Edu
"Enigma Durational Units" value (1024 per quarter note)
Definition Fundamentals.h:61
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:44
object model for musx file (enigmaxml)
Definition BaseClasses.h:36