MUSX Document Model
Loading...
Searching...
No Matches
TypeRegistry.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 <array>
25#include <memory>
26#include <string>
27#include <tuple>
28
29#include "musx/dom/BaseClasses.h"
30#include "musx/dom/CommonClasses.h"
31#include "musx/dom/Options.h"
32#include "musx/dom/Others.h"
33#include "musx/dom/Details.h"
34#include "musx/dom/SmartShape.h"
35#include "musx/dom/Entries.h"
36#include "musx/dom/Texts.h"
37#include "musx/xml/XmlInterface.h"
38#include "FieldPopulatorsOptions.h"
39#include "FieldPopulatorsOthers.h"
40#include "FieldPopulatorsDetails.h"
41#include "FieldPopulatorsTexts.h"
42
43#ifdef _MSC_VER
44# pragma warning(push)
45# pragma warning(disable : 4244) // disable spurious warnings due to overzealous MSC checking paths that are protected by constexpr
46#endif
47
48namespace musx {
49namespace factory {
50
59template <typename... Types>
61{
62private:
63 using VariantType = std::variant<Types*...>;
64 using Base = dom::Base;
65
72 static inline const auto registry = []() {
73 return std::unordered_map<std::string_view, VariantType>{
74 {Types::XmlNodeName, VariantType(static_cast<Types*>(nullptr))}...
75 };
76 }();
77
86 static std::optional<VariantType> findRegisteredType(std::string_view nodeName)
87 {
88 const auto it = registry.find(nodeName);
89 if (it == registry.end()) {
90 return std::nullopt;
91 }
92 return it->second;
93 }
94
96 template <typename T, typename PoolPtr, typename... Args>
97 static auto getScoreValue(const PoolPtr& pool, Args&&... args)
98 {
99 if constexpr (std::is_same_v<PoolPtr, ::musx::dom::OthersPoolPtr> || std::is_same_v<PoolPtr, ::musx::dom::DetailsPoolPtr>) {
100 return pool->template get<T>(SCORE_PARTID, std::forward<Args>(args)...);
101 } else {
102 return pool->template get<T>(std::forward<Args>(args)...);
103 }
104 }
105
106public:
121 template <typename PoolPtr, typename... Args>
122 static std::shared_ptr<Base> createInstance([[maybe_unused]]const PoolPtr& pool, const XmlElementPtr& node, ElementLinker& elementLinker, const DocumentPtr& document, Args&&... args)
123 {
124 auto typePtr = TypeRegistry::findRegisteredType(node->getTagName());
125 if (!typePtr.has_value()) {
126 return nullptr; // Type not yet implemented
127 }
128
129 return std::visit(
130 [&](auto const& ptr) -> std::shared_ptr<Base> {
131 using T = std::remove_pointer_t<std::remove_reference_t<decltype(ptr)>>;
132 // Only enable this part if T is constructible with Args...
133 if constexpr (std::is_constructible_v<T, const DocumentPtr&, Cmper, Base::ShareMode, Args...>) {
134 auto partAttr = node->findAttribute("part");
135 Cmper partId = partAttr ? partAttr->getValueAs<Cmper>() : SCORE_PARTID; // zero is the score ID
136 auto shareMode = Base::ShareMode::All;
137 if (auto shareAttr = node->findAttribute("shared")) {
138 shareMode = shareAttr->getValueAs<bool>() ? Base::ShareMode::Partial : Base::ShareMode::None;
139 }
140 auto instance = std::make_shared<T>(document, partId, shareMode, std::forward<Args>(args)...);
141 if constexpr (!std::is_same_v<PoolPtr, ::musx::dom::EntryPoolPtr>) {
142 if (instance->getShareMode() == Base::ShareMode::Partial) {
143 for (auto child = node->getFirstChildElement(); child; child = child->getNextSibling()) {
144 instance->addUnlinkedNode(child->getTagName());
145 }
146 auto scoreValue = getScoreValue<T>(pool, std::forward<Args>(args)...);
147 if (scoreValue) {
148 *instance = *scoreValue;
149 } else {
150 throw std::invalid_argument("Score instance not found for partially linked part instance");
151 }
152 }
153 }
154 factory::FieldPopulator<T>::populate(instance, node, elementLinker);
155 return instance;
156 } else {
157 assert(false); // This path should never actually be taken, but it is required for std::visit.
158 throw std::logic_error("Type for " + node->getTagName() + " is not constructible with given arguments");
159 }
160 },
161 typePtr.value()
162 );
163 }
164};
165
198>;
199
221 dom::others::SystemLock, // xml node is "lockMeas", which is still sequenced non-alphabetically
246 dom::others::SmartShapeCustomLine, // node name is `ssLineStyle`
268>;
269
284 dom::details::StemAlterationsUnderBeam, // "beamStemAdjust" is the xml key
286 dom::details::BeamAlterationsDownStem, // Finale serializes the beam alts out of alpha sequence
294 dom::details::IndependentStaffDetails, // "floats" is the xml key
300 dom::details::NoteAlterations, // this is out of alpha sequence, but that's how Finale serializes it
304 dom::details::CustomDownStem, // "stemDefDown" is the xml key
305 dom::details::CustomUpStem, // "stemDefUp" is the xml key
313>;
314
322>;
323
338>;
339
340} // namespace factory
341} // namespace musx
342
343#ifdef _MSC_VER
344# pragma warning(pop)
345#endif
Base class to enforce polymorphism across all DOM classes.
Definition BaseClasses.h:60
ShareMode
Describes how this instance is shared between part and score.
Definition BaseClasses.h:68
Represents an entry containing metadata and notes.
Definition Entries.h:166
Represents display alterations to an accidental for a specific note.
Definition Details.h:70
Assigns an articulation to an entry.
Definition Details.h:100
Contains the baseline offsets for lyrics chorus records.
Definition Details.h:159
Contains the baseline offsets for lyrics chorus records.
Definition Details.h:173
Contains the baseline offsets for lyrics verse records.
Definition Details.h:186
Beam alteration for downstem primary beams.
Definition Details.h:258
Beam alteration for upstem primary beams.
Definition Details.h:280
Beam extension for downstem beams.
Definition Details.h:333
Beam extension for upstem beams.
Definition Details.h:347
Specifies the direction for beam stubs (if they are manually overridden.)
Definition Details.h:363
Represents a center shape for a others::SmartShape that spans three or more measures.
Definition SmartShape.h:388
Represents chord symbol assignment for a staff and measure.
Definition Details.h:390
Represents a cross-staff assignment for the note, if any.
Definition Details.h:456
Custom stem for downstem context.
Definition Details.h:516
Custom stem for upstem context.
Definition Details.h:530
Represents display offsets and spacing adjustments for augmentation dots on a specific note.
Definition Details.h:546
Specifies a custom size for an entry. It scales the entire entry, including the stem and all notehead...
Definition Details.h:574
Represents the attributes of a Finale frame holder.
Definition Details.h:600
Represents independent time and key signature overrides for a staff.
Definition Details.h:733
Contains the syllable assignments for lyrics chorus blocks.
Definition Details.h:821
Contains the syllable assignments for lyrics section blocks.
Definition Details.h:836
Contains the syllable assignments for lyrics verse blocks.
Definition Details.h:851
Specifies lyric alignment and justification for a single entry. This affects all lyric assignments on...
Definition Details.h:867
Represents a text block assignment for a staff and measure.
Definition Details.h:906
Represents graphical and notational alterations applied to a note.
Definition Details.h:942
Represents a percussion note code override for a single note.
Definition Details.h:976
Beam alteration for downstem secondary beams.
Definition Details.h:1000
Beam alteration for downstem secondary beams.
Definition Details.h:1033
Specifies which secondary beams break and restart on the associated entry.
Definition Details.h:1068
Assigns a smart shape to an entry.
Definition SmartShape.h:428
Represents the attributes of a Finale staff group that brackets staves.
Definition Details.h:1122
Represents a per-staff-size override for a specific staff in a system.
Definition Details.h:1298
Specifies horizontal and vertical adjustments for stems under beam.
Definition Details.h:1354
Specifies horizontal and vertical stem adjustments for upstem and downstem contexts.
Definition Details.h:1328
Alterations for tie ends.
Definition Details.h:1417
Alterations for tie starts. (Tie starts are normal ties.)
Definition Details.h:1434
Options controlling the appearance of tuplets.
Definition Details.h:1451
Options controlling the appearance and positioning of accidentals.
Definition Options.h:49
Options controlling the appearance of alternate notation.
Definition Options.h:72
Options controlling the appearance of augmentation dots.
Definition Options.h:96
Options controlling the appearance of barlines.
Definition Options.h:129
Options controlling the appearance of beams.
Definition Options.h:160
Options controlling chord symbol and fretboard rendering.
Definition Options.h:200
Options for configuring clefs in a musical context.
Definition Options.h:251
Options controlling the appearance and adjustments of flags.
Definition Options.h:315
An unordered map of default font settings for the document.
Definition Options.h:353
Options controlling the appearance and behavior of grace notes.
Definition Options.h:458
Options controlling the appearance and behavior of key signatures.
Definition Options.h:481
Options controlling the appearance of line and curve elements.
Definition Options.h:510
Options controlling lyric rendering in the musx file.
Definition Options.h:540
Options controlling miscellaneous settings.
Definition Options.h:656
Options controlling the appearance and behavior of multimeasure rests.
Definition Options.h:680
Options controlling music spacing.
Definition Options.h:708
Options specifying the charactes for musical symbols.
Definition Options.h:782
Options for page formatting in the document.
Definition Options.h:867
Options controlling the appearance of piano braces and brackets.
Definition Options.h:966
Options controlling the appearance of repeats.
Definition Options.h:995
Options controlling the appearance of smart shapes in the musx file.
Definition Options.h:1054
Options controlling the appearance and layout of staves.
Definition Options.h:1249
Options controlling the appearance of stems.
Definition Options.h:1276
Options controlling the appearance of ties.
Definition Options.h:1301
Options controlling the appearance and behavior of time signatures.
Definition Options.h:1434
Options controlling the appearance of tuplets.
Definition Options.h:1465
Lists the aleration values of each nth flat in a custom key signature. Normally these values are all ...
Definition Others.h:75
Lists the aleration values of each nth sharp in a custom key signature. Normally these values are all...
Definition Others.h:107
Lists the order of flats by pitch class index (0..6 = C..B) in a custom key signature....
Definition Others.h:133
Lists the order of sharps by pitch class index (0..6 = C..B) in a custom key signature....
Definition Others.h:164
Stores the properties and behaviors of articulation definitions.
Definition Others.h:184
A single beat chart element from Finale's music spacing system.
Definition Others.h:315
Represents a single element in a chord suffix (e.g., a symbol like "1" or "+").
Definition Others.h:377
Represents a list of repeat ending numbers for a RepeatEndingStart instance.
Definition Others.h:424
Represents an element in multimeasure clef list with its positioning and percentage values.
Definition Others.h:444
Identifies the percussion map ("drum library") for a staff style with percussion notations.
Definition Others.h:493
Identifies the percussion map ("drum library") for a staff with percussion notations.
Definition Others.h:473
The name and font characteristics of fonts contained.
Definition Others.h:509
Represents the attributes of a TGF entry frame.
Definition Others.h:577
An array of InstrumentUsed defines a set of staves in a staff system or in Scroll View.
Definition Others.h:623
The key format for a custom key signature.
Definition Others.h:656
An array of step elements from which one can create a key map.
Definition Others.h:675
Represents the attributes of a Finale "layer".
Definition Others.h:715
Represents the name associated with a MarkingCategory.
Definition Others.h:913
Represents a category of markings used.
Definition Others.h:838
Assigns a TextExpressionDef or ShapeExpressionDef to a measure.
Definition Others.h:1074
Represents the Measure Number Region with detailed font and enclosure settings for score and part dat...
Definition Others.h:1125
Represents the attributes of a measure.
Definition Others.h:931
Represents a group ID for a multi-staff setup.
Definition Others.h:1294
Represents a group of instruments spanning multiple staves.
Definition Others.h:1312
Represents the attributes of a multimeasure rest in the page layout.
Definition Others.h:1248
Overrides abbreviated name positioning for Staff.
Definition Others.h:1361
Overrides full name positioning for Staff.
Definition Others.h:1389
Overrides abbreviated name positioning for StaffStyle.
Definition Others.h:1375
Overrides full name positioning for StaffStyle.
Definition Others.h:1403
Represents a page text assignment with positioning and page range properties.
Definition Others.h:1459
Represents the attributes of a page in the page layout.
Definition Others.h:1416
Represents the attributes of a Finale "partDef".
Definition Others.h:1513
Represents global values that can vary by part.
Definition Others.h:1564
Represents percussion notehead and staff position info for a given percussion note type.
Definition Others.h:1595
Represents a repeat-backward marker with positioning and behavior properties.
Definition Others.h:1653
Represents a repeat ending start marker in the document.
Definition Others.h:1688
Represents the text associated with a RepeatEndingStart.
Definition Others.h:1745
Represents a list of repeat ending numbers for a RepeatEndingStart instance.
Definition Others.h:1766
Represents the data for instruction associated with a ShapeDef.
Definition Others.h:1783
Represents a shape created in Finale's Shape Designer.
Definition Others.h:1801
Stores the properties and behaviors of shape expressions.
Definition Others.h:1880
Represents the data for instruction associated with a ShapeDef.
Definition Others.h:1924
Represents a Finale smart shape custom line style.
Definition SmartShape.h:223
Assigns a smart shape or center shape to a measure.
Definition SmartShape.h:352
Represents a Finale smart shape.
Definition SmartShape.h:45
Represents an assignment.
Definition Others.h:2365
Represents a Finale staff style.
Definition Others.h:2256
Represents the attributes of a staff system in the page layout.
Definition Others.h:2438
Represents the definition of a Finale staff.
Definition Others.h:1957
Locks a span of one or more measures so that they always appear in a StaffSystem together.
Definition Others.h:2497
A single tempo change value entered with Finale's Tempo tool.
Definition Others.h:2526
Represents the attributes of a Finale "textBlock".
Definition Others.h:2564
Stores the properties and behaviors of text expressions.
Definition Others.h:2614
The enclosure for a text expression (if it exists)
Definition Others.h:2665
Represents a text repeat assignment with positioning and behavior properties.
Definition Others.h:2682
Defines text repeat elements with font styling and justification.
Definition Others.h:2722
The enclosure for a text expression (if it exists)
Definition Others.h:2761
Represents the text associated with a TextRepeatDef.
Definition Others.h:2778
Represents the lower composite time signature array.
Definition Others.h:2801
Represents the upper composite time signature structure.
Definition Others.h:2836
Maps the number of flats to a tonal center for a linear custom key. If there are zero flats or sharps...
Definition Others.h:2880
Maps number of sharps (0..7) to a tonal center for a linear custom key. Also maps 0 sharps or flats....
Definition Others.h:2902
Contains block text (Finale Text Tool, both page- and measure-attached)
Definition Texts.h:178
Contains bookmark description text.
Definition Texts.h:223
Contains text blocks associated with text expressions.
Definition Texts.h:208
Contains File Info text (from Finale's Score Manager)
Definition Texts.h:48
Contains chorus text for lyrics.
Definition Texts.h:148
Contains section text for lyrics.
Definition Texts.h:163
Contains verse text for lyrics.
Definition Texts.h:133
Contains text blocks associated with custom line smart shapes.
Definition Texts.h:193
A utility class for managing deferred relationships between elements during document construction.
Definition FactoryBase.h:71
A registry of types for mapping XML node names to types.
Definition TypeRegistry.h:61
static std::shared_ptr< Base > createInstance(const PoolPtr &pool, const XmlElementPtr &node, ElementLinker &elementLinker, const DocumentPtr &document, Args &&... args)
Creates an instance of the registered type corresponding to the provided node name.
Definition TypeRegistry.h:122
constexpr Cmper SCORE_PARTID
The part id of the score.
Definition Fundamentals.h:80
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
std::shared_ptr< Document > DocumentPtr
Shared Document pointer.
Definition BaseClasses.h:51
std::shared_ptr< IXmlElement > XmlElementPtr
shared pointer to IXmlElement
Definition XmlInterface.h:121
object model for musx file (enigmaxml)
Definition BaseClasses.h:32