MUSX Document Model
Loading...
Searching...
No Matches
BaseClasses.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 <cassert>
25#include <filesystem>
26#include <set>
27#include <string_view>
28#include <unordered_set>
29#include <memory>
30
31#include "musx/dom/Fundamentals.h"
32#include "musx/xml/XmlInterface.h"
33#include "musx/util/EnigmaString.h"
34#include "musx/util/Logger.h"
35#include "DocumentElement.h"
36#include "MusxInstance.h"
37
38namespace musx {
39
44namespace dom {
45
49class integrity_error : public std::runtime_error
50{
51public:
52 using std::runtime_error::runtime_error;
53};
54
55#ifndef DOXYGEN_SHOULD_IGNORE_THIS
56template <typename T>
57struct PartContextRebinder
58{
59 static void rebind(const std::shared_ptr<T>&) {}
60};
61
62class PartContextCloner {
63public:
64 template <typename T>
65 static void setRequestedPartId(const std::shared_ptr<T>& obj, Cmper partId)
66 {
67 obj->setRequestedPartId(partId);
68 }
69
70 template <typename T>
71 static std::shared_ptr<T> copyWithPartId(const std::shared_ptr<const T>& obj, Cmper partId)
72 {
73 auto result = std::make_shared<T>(*obj);
74 setRequestedPartId(result, partId);
75 PartContextRebinder<T>::rebind(result);
76 return result;
77 }
78};
79#endif
80
88{
89 Cmper getPartId() const = delete;
90
91public:
93 using SharedNodes = std::set<std::string>;
94
97 enum class ShareMode
98 {
99 All,
100 Partial,
101 None
102 };
103
112
116 ShareMode getShareMode() const { return m_shareMode; }
117
121 const SharedNodes& getUnlinkedNodes() const { return m_unlinkedNodes; }
122
126 void addUnlinkedNode(const std::string& nodeName)
127 {
128 m_unlinkedNodes.insert(nodeName);
129 }
130
132 virtual void integrityCheck([[maybe_unused]] const std::shared_ptr<EnigmaBase>& ptrToThis) { }
133
135 virtual bool requireAllFields() const { return true; }
136
137protected:
145 EnigmaBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode)
146 : DocumentElement(document, partId), m_shareMode(shareMode) {}
147
148 EnigmaBase(const EnigmaBase&) = default;
149 EnigmaBase(EnigmaBase&&) noexcept = default;
150
152 EnigmaBase& operator=(const EnigmaBase&) { return *this; }
153
155 EnigmaBase& operator=(EnigmaBase&&) noexcept { return *this; }
156
157private:
158 const ShareMode m_shareMode;
159 SharedNodes m_unlinkedNodes;
160};
161
168{
169 Cmper getSourcePartId() const = delete;
170
171public:
178 : EnigmaBase(document, SCORE_PARTID, ShareMode::All) {}
179
180protected:
181 // Because the part ID and share mode are hard-coded for this category of classes,
182 // these EnigmaBase functions do not return useful results.
185};
186
192{
193 Cmper getSourcePartId() const = delete;
194
195protected:
196 // A shallow copy of an owning OthersBase or DetailsBase leaves contained objects parented to
197 // the source instance. If that distinction matters, the owner must use PartContextRebinder.
198 // PartContextRebinder<details::CenterShape> is a simple example.
199
202 {
203 if (this != &other) {
204 this->EnigmaBase::operator=(other);
205 }
206 return *this;
207 }
208
211 {
212 if (this != &other) {
213 this->EnigmaBase::operator=(std::move(other));
214 }
215 return *this;
216 }
217
218public:
225 : EnigmaBase(parent->getDocument(), SCORE_PARTID, ShareMode::All), m_parent(parent)
226 {}
227
229 template <typename ParentClass = EnigmaBase>
231 {
232 auto result = m_parent.lock();
233 MUSX_ASSERT_IF (!result) {
234 throw std::logic_error("Attempt to get parent of contained class, but the parent is no longer allocated.");
235 }
236 if constexpr (std::is_same_v<EnigmaBase, ParentClass>) {
237 return result;
238 } else {
239 return std::dynamic_pointer_cast<const ParentClass>(result);
240 }
241 }
242
243private:
244 const MusxInstanceWeak<EnigmaBase> m_parent;
245};
246
252class OptionsBase : public EnigmaBase {
253 Cmper getSourcePartId() const = delete;
254
255protected:
263 OptionsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode)
264 : EnigmaBase(document, partId, shareMode) {}
265};
266
273class OthersBase : public EnigmaBase
274{
276 void setRequestedPartId(Cmper newValue) { m_requestedPartId = newValue; }
277
278 friend class PartContextCloner; // gives access to private setter
279
280protected:
290 OthersBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper, std::optional<Inci> inci = std::nullopt)
291 : EnigmaBase(document, partId, shareMode), m_requestedPartId(partId), m_cmper(cmper), m_inci(inci) {}
292
295 {
296 if (this != &other) {
297 this->EnigmaBase::operator=(other);
298 }
299 return *this;
300 }
302 OthersBase& operator=(OthersBase&& other) noexcept
303 {
304 if (this != &other) {
305 this->EnigmaBase::operator=(other);
306 }
307 return *this;
308 }
309
310public:
311 OthersBase(const OthersBase&) = default;
312 OthersBase(OthersBase&&) noexcept = default;
313
319 Cmper getCmper() const { return m_cmper; }
320
326 std::optional<Inci> getInci() const { return m_inci; }
327
331 Cmper getRequestedPartId() const { return m_requestedPartId; }
332
333private:
334 Cmper m_requestedPartId{};
335 Cmper m_cmper;
336 std::optional<Inci> m_inci;
337};
338
342template <typename ElementType, size_t REQUIRED_SIZE = 0>
344{
345private:
346 virtual std::string_view xmlTag() const = 0;
347
348public:
350 explicit OthersArray(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
351 : OthersBase(document, partId, shareMode, cmper)
352 {
353 }
354
355 std::vector<ElementType> values;
357
360 bool containsValue(const ElementType& value) const noexcept
361 {
362 const auto& v = this->values;
363 return std::find(v.begin(), v.end(), value) != v.end();
364 }
365
367 void integrityCheck(const std::shared_ptr<EnigmaBase>& ptrToThis) override
368 {
370 if constexpr (REQUIRED_SIZE > 0) {
371 const size_t originalSize = values.size();
372 values.resize(REQUIRED_SIZE); // resize first, in case MUSX_INTEGRITY_ERROR throws. (Avoid unreachable code warning.)
373 if (originalSize < REQUIRED_SIZE) {
374 MUSX_INTEGRITY_ERROR("Array with xml tag " + std::string(xmlTag()) + " and cmper " + std::to_string(getCmper())
375 + " has fewer than " + std::to_string(REQUIRED_SIZE) + " elements.");
376 }
377 }
378 }
379};
380
385class OthersName : public OthersBase
386{
387public:
389 explicit OthersName(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
390 : OthersBase(document, partId, shareMode, cmper)
391 {
392 }
393
394 std::string name;
395
397};
398
406{
408 void setRequestedPartId(Cmper newValue) { m_requestedPartId = newValue; }
409
410 friend class PartContextCloner; // gives access to private setter
411
412protected:
423 DetailsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2, std::optional<Inci> inci = std::nullopt)
424 : EnigmaBase(document, partId, shareMode), m_requestedPartId(partId), m_cmper1(cmper1), m_cmper2(cmper2), m_inci(inci) {}
425
428 {
429 if (this != &other) {
430 this->EnigmaBase::operator=(other);
431 }
432 return *this;
433 }
436 {
437 if (this != &other) {
438 this->EnigmaBase::operator=(other);
439 }
440 return *this;
441 }
442
443public:
444 DetailsBase(const DetailsBase&) = default;
445 DetailsBase(DetailsBase&&) noexcept = default;
446
450 Cmper getCmper1() const { return m_cmper1; }
451
455 Cmper getCmper2() const { return m_cmper2; }
456
460 std::optional<Inci> getInci() const { return m_inci; }
461
465 Cmper getRequestedPartId() const { return m_requestedPartId; }
466
467private:
468 Cmper m_requestedPartId{};
469 Cmper m_cmper1;
470 Cmper m_cmper2;
471 std::optional<Inci> m_inci;
472};
473
478{
479public:
482 enum class StemSelection
483 {
484 MatchEntry,
485 UpStem,
486 DownStem,
487 Any
488 };
489
490protected:
500 EntryDetailsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, EntryNumber entnum, std::optional<Inci> inci = std::nullopt)
501 : DetailsBase(document, partId, shareMode, Cmper(entnum >> 16), Cmper(entnum & 0xffff), inci) {}
502
510 template <typename EDUP, typename EDDOWN, typename EDBASE>
511 static MusxInstance<EDBASE> getStemDependentDetail(const EntryInfoPtr& entryInfo, StemSelection stemSelection);
512
513public:
517 EntryNumber getEntryNumber() const { return EntryNumber(getCmper1()) << 16 | EntryNumber(getCmper2()); }
518
519private:
522};
523
527template <typename ElementType, size_t REQUIRED_SIZE = 0>
529{
530private:
531 virtual std::string_view xmlTag() const = 0;
532
533public:
535 explicit DetailsArray(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2)
536 : DetailsBase(document, partId, shareMode, cmper1, cmper2)
537 {
538 }
539
540 std::vector<ElementType> values;
542
544 void integrityCheck(const std::shared_ptr<EnigmaBase>& ptrToThis) override
545 {
547 if constexpr (REQUIRED_SIZE > 0) {
548 const size_t originalSize = values.size();
549 values.resize(REQUIRED_SIZE); // resize first, in case MUSX_INTEGRITY_ERROR throws. (Avoid unreachable code warning.)
550 if (originalSize < REQUIRED_SIZE) {
551 MUSX_INTEGRITY_ERROR("Array with xml tag " + std::string(xmlTag()) + " and cmpers [" + std::to_string(getCmper1()) + ", " + std::to_string(getCmper2())
552 + "] has fewer than " + std::to_string(REQUIRED_SIZE) + " elements.");
553 }
554 }
555 }
556};
557
560{
561public:
563 virtual NoteNumber getNoteId() const = 0;
564
565protected:
568};
569
570class FontInfo;
576class TextsBase : public EnigmaBase
577{
578 Cmper getSourcePartId() const = delete;
579
580public:
589 TextsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper textNumber)
590 : EnigmaBase(document, partId, shareMode), m_textNumber(textNumber) {}
591
592 std::string text;
593
597 Cmper getTextNumber() const { return m_textNumber; }
598
602 void setTextNumber(Cmper textNumber) { m_textNumber = textNumber; }
603
610 util::EnigmaParsingContext getRawTextCtx(const MusxInstance<TextsBase>& ptrToThis, Cmper forPartId, std::optional<Cmper> forPageId = std::nullopt,
612
613private:
614 Cmper m_textNumber;
615};
616
617} // namespace dom
618} // namespace musx
EnigmaBase class for classes that are commonly used among others, details, entries,...
Definition BaseClasses.h:168
CommonClassBase(const DocumentWeakPtr &document)
Constructs a CommonClassBase object.
Definition BaseClasses.h:177
EnigmaBase class for classes that are contained by other classes.
Definition BaseClasses.h:192
ContainedClassBase & operator=(ContainedClassBase &&other) noexcept
Move assignment preserves the destination's parent while allowing subclasses to move their values.
Definition BaseClasses.h:210
ContainedClassBase(const MusxInstance< EnigmaBase > &parent)
Constructs a ContainedClassBase object.
Definition BaseClasses.h:224
ContainedClassBase & operator=(const ContainedClassBase &other)
Assignment preserves the destination's parent while allowing subclasses to copy their values.
Definition BaseClasses.h:201
MusxInstance< ParentClass > getParent() const
Get the parent.
Definition BaseClasses.h:230
Template pattern for DetailsBase items consisting of an array of a single item.
Definition BaseClasses.h:529
DetailsArray(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2)
Constructor function.
Definition BaseClasses.h:535
std::vector< ElementType > values
Definition BaseClasses.h:540
void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis) override
Override of EnigmaBase::integrityCheck.
Definition BaseClasses.h:544
EnigmaBase class for all "details" types.
Definition BaseClasses.h:406
DetailsBase & operator=(const DetailsBase &other)
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:427
DetailsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2, std::optional< Inci > inci=std::nullopt)
Constructs a DetailsBase object.
Definition BaseClasses.h:423
DetailsBase(DetailsBase &&) noexcept=default
explicit default move constructor
Cmper getCmper1() const
Gets the cmper1 key value.
Definition BaseClasses.h:450
DetailsBase(const DetailsBase &)=default
explicit default copy constructor
DetailsBase & operator=(DetailsBase &&other) noexcept
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:435
Cmper getRequestedPartId() const
If this instance was retrieved from an object pool, it contains the part ID that was used to retrieve...
Definition BaseClasses.h:465
Cmper getCmper2() const
Gets the cmper2 key value.
Definition BaseClasses.h:455
std::optional< Inci > getInci() const
Gets the optional array index (inci).
Definition BaseClasses.h:460
Base for DOM classes that belong to a Document.
Definition DocumentElement.h:46
DocumentPtr getDocument() const
Gets a reference to the Document.
Definition DocumentElement.h:58
Cmper getPartId() const
Gets the part id associated with this instance.
Definition DocumentElement.h:70
Base for DOM classes that are represented in EnigmaData.
Definition BaseClasses.h:88
virtual bool requireAllFields() const
Returns true if all fields are required for valid input.
Definition BaseClasses.h:135
EnigmaBase & operator=(const EnigmaBase &)
no-op copy assignment operator allows subclasses to copy their values.
Definition BaseClasses.h:152
ShareMode getShareMode() const
Gets the sharing mode for this instance.
Definition BaseClasses.h:116
std::set< std::string > SharedNodes
The container type for shared nodes.
Definition BaseClasses.h:93
const SharedNodes & getUnlinkedNodes() const
Gets the unlinked nodes for this instance. (Only populated for ShareMode::Partial)
Definition BaseClasses.h:121
EnigmaBase(const EnigmaBase &)=default
explicit default copy constructor
void addUnlinkedNode(const std::string &nodeName)
Adds a shared node for this instance.
Definition BaseClasses.h:126
EnigmaBase & operator=(EnigmaBase &&) noexcept
no-op move assignment operator allows subclasses to move their values.
Definition BaseClasses.h:155
EnigmaBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode)
Constructs the base class.
Definition BaseClasses.h:145
ShareMode
Describes how this instance is shared between part and score.
Definition BaseClasses.h:98
EnigmaBase(EnigmaBase &&) noexcept=default
explicit default move constructor
Cmper getSourcePartId() const
Gets the source partId for this instance. If an instance is fully shared with the score,...
Definition BaseClasses.h:111
virtual void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis)
Performs a final consistency check after population.
Definition BaseClasses.h:132
EnigmaBase class for all "details" types that use entnum rather than cmper and cmper.
Definition BaseClasses.h:478
EntryDetailsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, EntryNumber entnum, std::optional< Inci > inci=std::nullopt)
Constructs a EntryDetailsBase object.
Definition BaseClasses.h:500
EntryNumber getEntryNumber() const
Gets the entnum key value.
Definition BaseClasses.h:517
StemSelection
The options for choosing which version to retrieve for stem-specific details.
Definition BaseClasses.h:483
@ DownStem
retrieve the downstem version
@ MatchEntry
match entry's stem direction
@ UpStem
retrieve the upstem version
@ Any
retrieve the first one encountered (starting with upstem)
static MusxInstance< EDBASE > getStemDependentDetail(const EntryInfoPtr &entryInfo, StemSelection stemSelection)
Implement retrieval of stem-specific details. (Actual functions are defined per stem-specific class)
Definition Details.cpp:48
Wraps a frame of shared_ptr<const EntryInfo> and an index for per entry access. This class manages ow...
Definition Entries.h:534
Represents the default font settings for a particular element type.
Definition CommonClasses.h:104
EnigmaBase class note details. Note details are entry details associated with a note ID.
Definition BaseClasses.h:560
virtual NoteNumber getNoteId() const =0
Required virtual function that returns the note id.
EnigmaBase class for all "options" types.
Definition BaseClasses.h:252
OptionsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode)
Constructs the OptionsBase and validates XmlNodeName in the derived class.
Definition BaseClasses.h:263
Template pattern for OthersBase items consisting of an array of a single item.
Definition BaseClasses.h:344
void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis) override
Override of EnigmaBase::integrityCheck.
Definition BaseClasses.h:367
std::vector< ElementType > values
Definition BaseClasses.h:355
OthersArray(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper)
Constructor function.
Definition BaseClasses.h:350
bool containsValue(const ElementType &value) const noexcept
Returns true if the array contains the specified value.
Definition BaseClasses.h:360
EnigmaBase class for all "others" types.
Definition BaseClasses.h:274
OthersBase & operator=(OthersBase &&other) noexcept
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:302
OthersBase & operator=(const OthersBase &other)
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:294
Cmper getRequestedPartId() const
If this instance was retrieved from an object pool, it contains the part ID that was used to retrieve...
Definition BaseClasses.h:331
std::optional< Inci > getInci() const
Gets the optional array index (inci).
Definition BaseClasses.h:326
OthersBase(OthersBase &&) noexcept=default
explicit default move constructor
OthersBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper, std::optional< Inci > inci=std::nullopt)
Constructs an OthersBase object.
Definition BaseClasses.h:290
Cmper getCmper() const
Gets the cmper key value.
Definition BaseClasses.h:319
OthersBase(const OthersBase &)=default
explicit default copy constructor
Many element names are embedded directly in top-level xml tags. This encapsulates that pattern.
Definition BaseClasses.h:386
static const xml::XmlElementArray< OthersName > & xmlMappingArray()
Required for musx::factory::FieldPopulator.
OthersName(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper)
Constructor function.
Definition BaseClasses.h:389
std::string name
the name
Definition BaseClasses.h:394
EnigmaBase class for all text blocks.
Definition BaseClasses.h:577
Cmper getTextNumber() const
Returns the raw text number.
Definition BaseClasses.h:597
util::EnigmaParsingContext getRawTextCtx(const MusxInstance< TextsBase > &ptrToThis, Cmper forPartId, std::optional< Cmper > forPageId=std::nullopt, util::EnigmaString::TextInsertCallback defaultInsertFunc=util::EnigmaString::defaultInsertsCallback) const
Gets the raw text block.
Definition Texts.cpp:36
std::string text
Raw Enigma string (with Enigma string tags), encoded UTF-8.
Definition BaseClasses.h:592
TextsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper textNumber)
Constructs a TextsBase object.
Definition BaseClasses.h:589
void setTextNumber(Cmper textNumber)
Sets the raw text number.
Definition BaseClasses.h:602
Exception for integrity errors. (Used when MUSX_THROW_ON_INTEGRITY_CHECK_FAIL is defined....
Definition BaseClasses.h:50
Wrapper class for interpreting and rendering Enigma-style strings with insert handling.
Definition EnigmaString.h:430
static TextInsertCallback defaultInsertsCallback
Inserts callback to take all default insert subsitutions determined by parseEnigmaText.
Definition EnigmaString.h:351
std::function< std::optional< std::string >(const std::vector< std::string > &parsedCommand)> TextInsertCallback
Iteration function type that the parser calls back when it encounters an Enigma text insert that requ...
Definition EnigmaString.h:348
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:81
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:57
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition DocumentElement.h:37
int32_t EntryNumber
Entry identifier.
Definition Fundamentals.h:71
uint16_t NoteNumber
Note identifier.
Definition Fundamentals.h:72
std::weak_ptr< const T > MusxInstanceWeak
Defines a weak ptr to the type of a musx instance stored in a pool.
Definition MusxInstance.h:45
std::vector< XmlElementDescriptor< T > > XmlElementArray
an array type for XmlElementDescriptor instances.
Definition XmlInterface.h:127
object model for musx file (enigmaxml)
Definition BaseClasses.h:38