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
56class PartContextCloner {
57public:
58 template <typename T>
59 static void setRequestedPartId(const std::shared_ptr<T>& obj, Cmper partId)
60 {
61 obj->setRequestedPartId(partId);
62 }
63
64 template <typename T>
65 static std::shared_ptr<T> copyWithPartId(const std::shared_ptr<const T>& obj, Cmper partId)
66 {
67 auto result = std::make_shared<T>(*obj);
68 setRequestedPartId(result, partId);
69 return result;
70 }
71};
72#endif
73
81{
82 Cmper getPartId() const = delete;
83
84public:
86 using SharedNodes = std::set<std::string>;
87
90 enum class ShareMode
91 {
92 All,
93 Partial,
94 None
95 };
96
105
109 ShareMode getShareMode() const { return m_shareMode; }
110
114 const SharedNodes& getUnlinkedNodes() const { return m_unlinkedNodes; }
115
119 void addUnlinkedNode(const std::string& nodeName)
120 {
121 m_unlinkedNodes.insert(nodeName);
122 }
123
125 virtual void integrityCheck([[maybe_unused]] const std::shared_ptr<EnigmaBase>& ptrToThis) { }
126
128 virtual bool requireAllFields() const { return true; }
129
130protected:
138 EnigmaBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode)
139 : DocumentElement(document, partId), m_shareMode(shareMode) {}
140
141 EnigmaBase(const EnigmaBase&) = default;
142 EnigmaBase(EnigmaBase&&) noexcept = default;
143
145 EnigmaBase& operator=(const EnigmaBase&) { return *this; }
146
148 EnigmaBase& operator=(EnigmaBase&&) noexcept { return *this; }
149
150private:
151 const ShareMode m_shareMode;
152 SharedNodes m_unlinkedNodes;
153};
154
161{
162 Cmper getSourcePartId() const = delete;
163
164public:
171 : EnigmaBase(document, SCORE_PARTID, ShareMode::All) {}
172
173protected:
174 // Because the part ID and share mode are hard-coded for this category of classes,
175 // these EnigmaBase functions do not return useful results.
178};
179
185{
186 Cmper getSourcePartId() const = delete;
187
188public:
195 : EnigmaBase(parent->getDocument(), SCORE_PARTID, ShareMode::All), m_parent(parent)
196 {}
197
199 template <typename ParentClass = EnigmaBase>
201 {
202 auto result = m_parent.lock();
203 MUSX_ASSERT_IF (!result) {
204 throw std::logic_error("Attempt to get parent of contained class, but the parent is no longer allocated.");
205 }
206 if constexpr (std::is_same_v<EnigmaBase, ParentClass>) {
207 return result;
208 } else {
209 return std::dynamic_pointer_cast<const ParentClass>(result);
210 }
211 }
212
213private:
214 const MusxInstanceWeak<EnigmaBase> m_parent;
215};
216
222class OptionsBase : public EnigmaBase {
223 Cmper getSourcePartId() const = delete;
224
225protected:
233 OptionsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode)
234 : EnigmaBase(document, partId, shareMode) {}
235};
236
243class OthersBase : public EnigmaBase
244{
246 void setRequestedPartId(Cmper newValue) { m_requestedPartId = newValue; }
247
248 friend class PartContextCloner; // gives access to private setter
249
250protected:
260 OthersBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper, std::optional<Inci> inci = std::nullopt)
261 : EnigmaBase(document, partId, shareMode), m_requestedPartId(partId), m_cmper(cmper), m_inci(inci) {}
262
265 {
266 if (this != &other) {
267 this->EnigmaBase::operator=(other);
268 }
269 return *this;
270 }
272 OthersBase& operator=(OthersBase&& other) noexcept
273 {
274 if (this != &other) {
275 this->EnigmaBase::operator=(other);
276 }
277 return *this;
278 }
279
280public:
281 OthersBase(const OthersBase&) = default;
282 OthersBase(OthersBase&&) noexcept = default;
283
289 Cmper getCmper() const { return m_cmper; }
290
296 std::optional<Inci> getInci() const { return m_inci; }
297
301 Cmper getRequestedPartId() const { return m_requestedPartId; }
302
303private:
304 Cmper m_requestedPartId{};
305 Cmper m_cmper;
306 std::optional<Inci> m_inci;
307};
308
312template <typename ElementType, size_t REQUIRED_SIZE = 0>
314{
315private:
316 virtual std::string_view xmlTag() const = 0;
317
318public:
320 explicit OthersArray(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
321 : OthersBase(document, partId, shareMode, cmper)
322 {
323 }
324
325 std::vector<ElementType> values;
327
330 bool containsValue(const ElementType& value) const noexcept
331 {
332 const auto& v = this->values;
333 return std::find(v.begin(), v.end(), value) != v.end();
334 }
335
337 void integrityCheck(const std::shared_ptr<EnigmaBase>& ptrToThis) override
338 {
340 if constexpr (REQUIRED_SIZE > 0) {
341 const size_t originalSize = values.size();
342 values.resize(REQUIRED_SIZE); // resize first, in case MUSX_INTEGRITY_ERROR throws. (Avoid unreachable code warning.)
343 if (originalSize < REQUIRED_SIZE) {
344 MUSX_INTEGRITY_ERROR("Array with xml tag " + std::string(xmlTag()) + " and cmper " + std::to_string(getCmper())
345 + " has fewer than " + std::to_string(REQUIRED_SIZE) + " elements.");
346 }
347 }
348 }
349};
350
355class OthersName : public OthersBase
356{
357public:
359 explicit OthersName(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
360 : OthersBase(document, partId, shareMode, cmper)
361 {
362 }
363
364 std::string name;
365
367};
368
376{
378 void setRequestedPartId(Cmper newValue) { m_requestedPartId = newValue; }
379
380 friend class PartContextCloner; // gives access to private setter
381
382protected:
393 DetailsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2, std::optional<Inci> inci = std::nullopt)
394 : EnigmaBase(document, partId, shareMode), m_requestedPartId(partId), m_cmper1(cmper1), m_cmper2(cmper2), m_inci(inci) {}
395
398 {
399 if (this != &other) {
400 this->EnigmaBase::operator=(other);
401 }
402 return *this;
403 }
406 {
407 if (this != &other) {
408 this->EnigmaBase::operator=(other);
409 }
410 return *this;
411 }
412
413public:
414 DetailsBase(const DetailsBase&) = default;
415 DetailsBase(DetailsBase&&) noexcept = default;
416
420 Cmper getCmper1() const { return m_cmper1; }
421
425 Cmper getCmper2() const { return m_cmper2; }
426
430 std::optional<Inci> getInci() const { return m_inci; }
431
435 Cmper getRequestedPartId() const { return m_requestedPartId; }
436
437private:
438 Cmper m_requestedPartId{};
439 Cmper m_cmper1;
440 Cmper m_cmper2;
441 std::optional<Inci> m_inci;
442};
443
448{
449public:
452 enum class StemSelection
453 {
454 MatchEntry,
455 UpStem,
456 DownStem,
457 Any
458 };
459
460protected:
470 EntryDetailsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, EntryNumber entnum, std::optional<Inci> inci = std::nullopt)
471 : DetailsBase(document, partId, shareMode, Cmper(entnum >> 16), Cmper(entnum & 0xffff), inci) {}
472
480 template <typename EDUP, typename EDDOWN, typename EDBASE>
481 static MusxInstance<EDBASE> getStemDependentDetail(const EntryInfoPtr& entryInfo, StemSelection stemSelection);
482
483public:
487 EntryNumber getEntryNumber() const { return EntryNumber(getCmper1()) << 16 | EntryNumber(getCmper2()); }
488
489private:
492};
493
497template <typename ElementType, size_t REQUIRED_SIZE = 0>
499{
500private:
501 virtual std::string_view xmlTag() const = 0;
502
503public:
505 explicit DetailsArray(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2)
506 : DetailsBase(document, partId, shareMode, cmper1, cmper2)
507 {
508 }
509
510 std::vector<ElementType> values;
512
514 void integrityCheck(const std::shared_ptr<EnigmaBase>& ptrToThis) override
515 {
517 if constexpr (REQUIRED_SIZE > 0) {
518 const size_t originalSize = values.size();
519 values.resize(REQUIRED_SIZE); // resize first, in case MUSX_INTEGRITY_ERROR throws. (Avoid unreachable code warning.)
520 if (originalSize < REQUIRED_SIZE) {
521 MUSX_INTEGRITY_ERROR("Array with xml tag " + std::string(xmlTag()) + " and cmpers [" + std::to_string(getCmper1()) + ", " + std::to_string(getCmper2())
522 + "] has fewer than " + std::to_string(REQUIRED_SIZE) + " elements.");
523 }
524 }
525 }
526};
527
530{
531public:
533 virtual NoteNumber getNoteId() const = 0;
534
535protected:
538};
539
540class FontInfo;
546class TextsBase : public EnigmaBase
547{
548 Cmper getSourcePartId() const = delete;
549
550public:
559 TextsBase(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper textNumber)
560 : EnigmaBase(document, partId, shareMode), m_textNumber(textNumber) {}
561
562 std::string text;
563
567 Cmper getTextNumber() const { return m_textNumber; }
568
572 void setTextNumber(Cmper textNumber) { m_textNumber = textNumber; }
573
580 util::EnigmaParsingContext getRawTextCtx(const MusxInstance<TextsBase>& ptrToThis, Cmper forPartId, std::optional<Cmper> forPageId = std::nullopt,
582
583private:
584 Cmper m_textNumber;
585};
586
587} // namespace dom
588} // namespace musx
EnigmaBase class for classes that are commonly used among others, details, entries,...
Definition BaseClasses.h:161
CommonClassBase(const DocumentWeakPtr &document)
Constructs a CommonClassBase object.
Definition BaseClasses.h:170
EnigmaBase class for classes that are contained by other classes.
Definition BaseClasses.h:185
ContainedClassBase(const MusxInstance< EnigmaBase > &parent)
Constructs a ContainedClassBase object.
Definition BaseClasses.h:194
MusxInstance< ParentClass > getParent() const
Get the parent.
Definition BaseClasses.h:200
Template pattern for DetailsBase items consisting of an array of a single item.
Definition BaseClasses.h:499
DetailsArray(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper1, Cmper cmper2)
Constructor function.
Definition BaseClasses.h:505
std::vector< ElementType > values
Definition BaseClasses.h:510
void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis) override
Override of EnigmaBase::integrityCheck.
Definition BaseClasses.h:514
EnigmaBase class for all "details" types.
Definition BaseClasses.h:376
DetailsBase & operator=(const DetailsBase &other)
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:397
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:393
DetailsBase(DetailsBase &&) noexcept=default
explicit default move constructor
Cmper getCmper1() const
Gets the cmper1 key value.
Definition BaseClasses.h:420
DetailsBase(const DetailsBase &)=default
explicit default copy constructor
DetailsBase & operator=(DetailsBase &&other) noexcept
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:405
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:435
Cmper getCmper2() const
Gets the cmper2 key value.
Definition BaseClasses.h:425
std::optional< Inci > getInci() const
Gets the optional array index (inci).
Definition BaseClasses.h:430
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:81
virtual bool requireAllFields() const
Returns true if all fields are required for valid input.
Definition BaseClasses.h:128
EnigmaBase & operator=(const EnigmaBase &)
no-op copy assignment operator allows subclasses to copy their values.
Definition BaseClasses.h:145
ShareMode getShareMode() const
Gets the sharing mode for this instance.
Definition BaseClasses.h:109
std::set< std::string > SharedNodes
The container type for shared nodes.
Definition BaseClasses.h:86
const SharedNodes & getUnlinkedNodes() const
Gets the unlinked nodes for this instance. (Only populated for ShareMode::Partial)
Definition BaseClasses.h:114
EnigmaBase(const EnigmaBase &)=default
explicit default copy constructor
void addUnlinkedNode(const std::string &nodeName)
Adds a shared node for this instance.
Definition BaseClasses.h:119
EnigmaBase & operator=(EnigmaBase &&) noexcept
no-op move assignment operator allows subclasses to move their values.
Definition BaseClasses.h:148
EnigmaBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode)
Constructs the base class.
Definition BaseClasses.h:138
ShareMode
Describes how this instance is shared between part and score.
Definition BaseClasses.h:91
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:104
virtual void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis)
Performs a final consistency check after population.
Definition BaseClasses.h:125
EnigmaBase class for all "details" types that use entnum rather than cmper and cmper.
Definition BaseClasses.h:448
EntryDetailsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, EntryNumber entnum, std::optional< Inci > inci=std::nullopt)
Constructs a EntryDetailsBase object.
Definition BaseClasses.h:470
EntryNumber getEntryNumber() const
Gets the entnum key value.
Definition BaseClasses.h:487
StemSelection
The options for choosing which version to retrieve for stem-specific details.
Definition BaseClasses.h:453
@ 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:39
Wraps a frame of shared_ptr<const EntryInfo> and an index for per entry access. This class manages ow...
Definition Entries.h:553
Represents the default font settings for a particular element type.
Definition CommonClasses.h:67
EnigmaBase class note details. Note details are entry details associated with a note ID.
Definition BaseClasses.h:530
virtual NoteNumber getNoteId() const =0
Required virtual function that returns the note id.
EnigmaBase class for all "options" types.
Definition BaseClasses.h:222
OptionsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode)
Constructs the OptionsBase and validates XmlNodeName in the derived class.
Definition BaseClasses.h:233
Template pattern for OthersBase items consisting of an array of a single item.
Definition BaseClasses.h:314
void integrityCheck(const std::shared_ptr< EnigmaBase > &ptrToThis) override
Override of EnigmaBase::integrityCheck.
Definition BaseClasses.h:337
std::vector< ElementType > values
Definition BaseClasses.h:325
OthersArray(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper)
Constructor function.
Definition BaseClasses.h:320
bool containsValue(const ElementType &value) const noexcept
Returns true if the array contains the specified value.
Definition BaseClasses.h:330
EnigmaBase class for all "others" types.
Definition BaseClasses.h:244
OthersBase & operator=(OthersBase &&other) noexcept
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:272
OthersBase & operator=(const OthersBase &other)
Assignment operator delegates to base, preserving OthersBase state.
Definition BaseClasses.h:264
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:301
std::optional< Inci > getInci() const
Gets the optional array index (inci).
Definition BaseClasses.h:296
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:260
Cmper getCmper() const
Gets the cmper key value.
Definition BaseClasses.h:289
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:356
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:359
std::string name
the name
Definition BaseClasses.h:364
EnigmaBase class for all text blocks.
Definition BaseClasses.h:547
Cmper getTextNumber() const
Returns the raw text number.
Definition BaseClasses.h:567
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:562
TextsBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper textNumber)
Constructs a TextsBase object.
Definition BaseClasses.h:559
void setTextNumber(Cmper textNumber)
Sets the raw text number.
Definition BaseClasses.h:572
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:416
static TextInsertCallback defaultInsertsCallback
Inserts callback to take all default insert subsitutions determined by parseEnigmaText.
Definition EnigmaString.h:340
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:337
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
int32_t EntryNumber
Entry identifier.
Definition Fundamentals.h:69
uint16_t NoteNumber
Note identifier.
Definition Fundamentals.h:70
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