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/ObjectPool.h"
30#include "musx/dom/BaseClasses.h"
31#include "musx/dom/CommonClasses.h"
32#include "musx/dom/Options.h"
33#include "musx/dom/Others.h"
34#include "musx/dom/Details.h"
35#include "musx/dom/Graphics.h"
36#include "musx/dom/Ossia.h"
37#include "musx/dom/ShapeDesigner.h"
38#include "musx/dom/SmartShape.h"
39#include "musx/dom/Staff.h"
40#include "musx/dom/Entries.h"
41#include "musx/dom/Texts.h"
42#include "musx/xml/XmlInterface.h"
43#include "FieldPopulatorsOptions.h"
44#include "FieldPopulatorsOthers.h"
45#include "FieldPopulatorsDetails.h"
46#include "FieldPopulatorsTexts.h"
47
48#ifdef _MSC_VER
49# pragma warning(push)
50# pragma warning(disable : 4244) // disable spurious warnings due to overzealous MSC checking paths that are protected by constexpr
51#endif
52
53namespace musx {
54namespace factory {
55
64template <typename... Types>
66{
67private:
68 using VariantType = std::variant<Types*...>;
69 using Base = dom::Base;
70
77 static inline const auto registry = []() {
78 return std::unordered_map<std::string_view, VariantType>{
79 {Types::XmlNodeName, VariantType(static_cast<Types*>(nullptr))}...
80 };
81 }();
82
91 static std::optional<VariantType> findRegisteredType(std::string_view nodeName)
92 {
93 const auto it = registry.find(nodeName);
94 if (it == registry.end()) {
95 return std::nullopt;
96 }
97 return it->second;
98 }
99
101 template <typename T, typename PoolPtr, typename... Args>
102 static auto getScoreValue(const PoolPtr& pool, Args&&... args)
103 {
104 if constexpr (std::is_same_v<PoolPtr, ::musx::dom::OthersPoolPtr> || std::is_same_v<PoolPtr, ::musx::dom::DetailsPoolPtr>) {
105 return pool->template get<T>(SCORE_PARTID, std::forward<Args>(args)...);
106 } else {
107 return pool->template get<T>(std::forward<Args>(args)...);
108 }
109 }
110
111public:
114 template <typename T>
115 struct is_registered_type : std::disjunction<std::is_same<T, Types>...> {};
116
119 template <typename T>
121
125 {
127 CreatedInstanceInfo(std::shared_ptr<Base> inst, std::string_view nodeName)
128 : instance(inst), xmlNodeName(nodeName) {}
129
130 std::shared_ptr<Base> instance;
131 std::string_view xmlNodeName;
132 };
133
148 template <typename PoolPtr, typename... Args>
149 static std::optional<CreatedInstanceInfo> createInstance([[maybe_unused]]const PoolPtr& pool, const XmlElementPtr& node, ElementLinker& elementLinker, const DocumentPtr& document, Args&&... args)
150 {
151 auto typePtr = TypeRegistry::findRegisteredType(node->getTagName());
152 if (!typePtr.has_value()) {
153 return std::nullopt; // Type not yet implemented
154 }
155
156 return std::visit(
157 [&](auto const& ptr) -> CreatedInstanceInfo {
158 using T = std::remove_pointer_t<std::remove_reference_t<decltype(ptr)>>;
159 // Only enable this part if T is constructible with Args...
160 if constexpr (std::is_constructible_v<T, const DocumentPtr&, Cmper, Base::ShareMode, Args...>) {
161 auto partAttr = node->findAttribute("part");
162 Cmper partId = partAttr ? partAttr->getValueAs<Cmper>() : SCORE_PARTID; // zero is the score ID
163 auto shareMode = Base::ShareMode::All;
164 if (auto shareAttr = node->findAttribute("shared")) {
165 shareMode = shareAttr->getValueAs<bool>() ? Base::ShareMode::Partial : Base::ShareMode::None;
166 }
167 auto instance = std::make_shared<T>(document, partId, shareMode, std::forward<Args>(args)...);
168 if constexpr (!std::is_same_v<PoolPtr, ::musx::dom::EntryPoolPtr>) {
169 if (instance->getShareMode() == Base::ShareMode::Partial) {
170 for (auto child = node->getFirstChildElement(); child; child = child->getNextSibling()) {
171 instance->addUnlinkedNode(child->getTagName());
172 }
173 auto scoreValue = getScoreValue<T>(pool, std::forward<Args>(args)...);
174 if (scoreValue) {
175 *instance = *scoreValue;
176 } else {
177 throw std::invalid_argument("Score instance not found for partially linked part instance");
178 }
179 }
180 }
181 factory::FieldPopulator<T>::populate(instance, node, elementLinker);
182 return CreatedInstanceInfo(instance,T::XmlNodeName);
183 } else {
184 assert(false); // This path should never actually be taken, but it is required for std::visit.
185 throw std::logic_error("Type for " + node->getTagName() + " is not constructible with given arguments");
186 }
187 },
188 typePtr.value()
189 );
190 }
191};
192
227>;
228
239 dom::others::OssiaBounds, // this was possibly internally "arbitBounds" at one time, which may be why it serializes here
240 dom::others::OssiaHeader, // this was possibly internally "arbitHeader" at one time, which may be why it serializes here
241 dom::others::OssiaMusic, // this was possibly internally "arbitMusic" at one time, which may be why it serializes here
256 dom::others::SystemLock, // xml node is "lockMeas", which is still sequenced non-alphabetically
260 dom::others::StaffUsed, // xml node is "instUsed"
289 dom::others::SmartShapeCustomLine, // node name is `ssLineStyle`
321>;
322
337 dom::details::StemAlterationsUnderBeam, // "beamStemAdjust" is the xml key
339 dom::details::BeamAlterationsDownStem, // Finale serializes the beam alts out of alpha sequence
350 dom::details::IndependentStaffDetails, // "floats" is the xml key
361 dom::details::NoteAlterations, // this is out of alpha sequence, but that's how Finale serializes it
367 dom::details::CustomDownStem, // "stemDefDown" is the xml key
368 dom::details::CustomUpStem, // "stemDefUp" is the xml key
377>;
378
386>;
387
402>;
403
404} // namespace factory
405
406namespace dom {
407
409template <typename T>
410struct is_pool_type<OptionsPool, T> : musx::factory::RegisteredOptions::template is_registered_type<T> {};
411
413template <typename T>
414struct is_pool_type<OthersPool, T> : musx::factory::RegisteredOthers::template is_registered_type<T> {};
415
417template <typename T>
418struct is_pool_type<DetailsPool, T> : musx::factory::RegisteredDetails::template is_registered_type<T> {};
419
421template <typename T>
422struct is_pool_type<EntryPool, T> : musx::factory::RegisteredEntries::template is_registered_type<T> {};
423
425template <typename T>
426struct is_pool_type<TextsPool, T> : musx::factory::RegisteredTexts::template is_registered_type<T> {};
427
428} // namespace dom
429} // namespace musx
430
431#ifdef _MSC_VER
432# pragma warning(pop)
433#endif
Base class to enforce polymorphism across all DOM classes.
Definition BaseClasses.h:83
ShareMode
Describes how this instance is shared between part and score.
Definition BaseClasses.h:91
A pool that manages collections of DetailsBase objects, organized by XML node names and Cmper values.
Definition ObjectPool.h:456
Entry pool.
Definition ObjectPool.h:538
Represents an entry containing metadata and notes.
Definition Entries.h:261
A pool that manages collections of OptionsBase objects that have no Cmper value.
Definition ObjectPool.h:358
A pool that manages collections of OthersBase objects.
Definition ObjectPool.h:400
Text pool.
Definition ObjectPool.h:571
Represents display alterations to an accidental for a specific note.
Definition Details.h:79
Assigns an articulation to an entry.
Definition Details.h:109
Contains the baseline offsets for lyrics chorus records.
Definition Details.h:168
Contains the baseline offsets for lyrics chorus records.
Definition Details.h:182
Contains the baseline offsets for lyrics verse records.
Definition Details.h:195
Beam alteration for downstem primary beams.
Definition Details.h:267
Beam alteration for upstem primary beams.
Definition Details.h:289
Beam extension for downstem beams.
Definition Details.h:342
Beam extension for upstem beams.
Definition Details.h:354
Specifies the direction for beam stubs (if they are manually overridden.)
Definition Details.h:370
Represents a bracket, used in two different contexts:
Definition Details.h:402
Represents a center shape for a others::SmartShape that spans three or more measures.
Definition SmartShape.h:587
Represents chord symbol assignment for a staff and measure.
Definition Details.h:451
Defines the octaves in which each clef should display flats in key signatures. Only linear key signat...
Definition Details.h:545
Defines the octaves in which each clef should display sharps in key signatures.
Definition Details.h:574
Represents a cross-staff assignment for the note, if any.
Definition Details.h:593
Custom stem for downstem context.
Definition Details.h:653
Custom stem for upstem context.
Definition Details.h:667
Represents display offsets and spacing adjustments for augmentation dots on a specific note.
Definition Details.h:683
Specifies a custom size for an entry. It scales the entire entry, including the stem and all notehead...
Definition Details.h:711
FretboardDiagram diagram for chord symbols.
Definition Details.h:742
Represents the attributes of a Finale frame holder.
Definition Details.h:835
Represents independent time and key signature overrides for a staff.
Definition Details.h:898
Represents a single element in a Finale accidental symbol list.
Definition Details.h:957
Contains the syllable assignments for lyrics chorus blocks.
Definition Details.h:1029
Contains the syllable assignments for lyrics section blocks.
Definition Details.h:1050
Contains the syllable assignments for lyrics verse blocks.
Definition Details.h:1071
Specifies lyric alignment and justification for a single entry. This affects all lyric assignments on...
Definition Details.h:1093
Represents a graphic assignment anchored to a specific staff and measure.
Definition Graphics.h:335
Per-staff/per-measure overrides for the position and appearance of a measure number.
Definition Details.h:1124
Assigns an ossia passage to a specific staff/measure location.
Definition Ossia.h:179
Represents a text block assignment for a staff and measure.
Definition Details.h:1179
Represents graphical and notational alterations applied to a note.
Definition Details.h:1221
Represents a percussion note code override for a single note.
Definition Details.h:1255
Beam alteration for downstem secondary beams.
Definition Details.h:1279
Beam alteration for downstem secondary beams.
Definition Details.h:1312
Specifies which secondary beams break and restart on the associated entry.
Definition Details.h:1347
Shape Note settings for staff styles.
Definition Details.h:1470
Shape Note settings for staves.
Definition Details.h:1455
Assigns a smart shape to an entry.
Definition SmartShape.h:630
Represents the attributes of a Finale staff group that brackets staves.
Definition Details.h:1483
Represents a per-staff-size override for a specific staff in a system.
Definition Details.h:1651
Specifies horizontal and vertical adjustments for stems under beam.
Definition Details.h:1711
Specifies horizontal and vertical stem adjustments for upstem and downstem contexts.
Definition Details.h:1685
Specifies the TAB string a note appears on. Finale automatically figures out the fret number from the...
Definition Details.h:1728
Alterations for tie ends.
Definition Details.h:1801
Alterations for tie starts. (Tie starts are normal ties.)
Definition Details.h:1818
Options controlling the appearance of tuplets.
Definition Details.h:1835
Options controlling the appearance and positioning of accidentals.
Definition Options.h:57
Options controlling the appearance of alternate notation.
Definition Options.h:80
Options controlling the appearance of augmentation dots.
Definition Options.h:104
Options controlling the appearance of barlines.
Definition Options.h:137
Options controlling the appearance of beams.
Definition Options.h:168
Options controlling chord symbol and fretboard rendering.
Definition Options.h:209
Options for configuring clefs in a musical context.
Definition Options.h:262
Options controlling the appearance and adjustments of flags.
Definition Options.h:357
An unordered map of default font settings for the document.
Definition Options.h:395
Options controlling the appearance and behavior of grace notes.
Definition Options.h:500
Options controlling the appearance and behavior of key signatures.
Definition Options.h:523
Options controlling the appearance of line and curve elements.
Definition Options.h:552
Options controlling lyric rendering in the musx file.
Definition Options.h:582
Options controlling miscellaneous settings.
Definition Options.h:705
Options controlling the appearance and behavior of multimeasure rests.
Definition Options.h:733
Options controlling music spacing.
Definition Options.h:761
Options specifying the charactes for musical symbols.
Definition Options.h:838
Options controlling note/rest display and positioning.
Definition Options.h:920
Options for page formatting in the document.
Definition Options.h:983
Options controlling the appearance of piano braces and brackets.
Definition Options.h:1092
Options controlling the appearance of repeats.
Definition Options.h:1121
Options controlling the appearance of smart shapes in the musx file.
Definition Options.h:1182
Options controlling the appearance and layout of staves.
Definition Options.h:1384
Options controlling the appearance of stems.
Definition Options.h:1411
Options controlling text rendering in the musx file. Many of these options are default values that ar...
Definition Options.h:1482
Options controlling the appearance of ties.
Definition Options.h:1582
Options controlling the appearance and behavior of time signatures.
Definition Options.h:1742
Options controlling the appearance of tuplets.
Definition Options.h:1773
Lists the aleration values of each nth flat in a custom key signature. Normally these values are all ...
Definition Others.h:80
Lists the aleration values of each nth sharp in a custom key signature. Normally these values are all...
Definition Others.h:113
Lists the order of flats by pitch class index (0..6 = C..B) in a custom key signature....
Definition Others.h:139
Lists the order of sharps by pitch class index (0..6 = C..B) in a custom key signature....
Definition Others.h:170
Stores the properties and behaviors of articulation definitions.
Definition Others.h:190
A single beat chart element from Finale's music spacing system.
Definition Others.h:326
Represents a single element in a chord suffix (e.g., a symbol like "1" or "+").
Definition Others.h:388
Represents a list of repeat ending numbers for a RepeatEndingStart instance.
Definition Others.h:435
Represents an element in multimeasure clef list with its positioning and percentage values.
Definition Others.h:455
Identifies the percussion map ("drum library") for a staff style with percussion notation.
Definition Others.h:506
Identifies the percussion map ("drum library") for a staff with percussion notation.
Definition Others.h:486
Opaque storage for a platform-specific alias handle associated with a graphic.
Definition Graphics.h:41
Describes the locator flavor Finale used for the associated file/graphic.
Definition Graphics.h:69
Stores a human-readable path/leaf name recorded by Finale.
Definition Graphics.h:105
Opaque storage for a platform-specific URL/bookmark blob (e.g., macOS CFURL bookmark).
Definition Graphics.h:124
The name and font characteristics of fonts contained.
Definition Others.h:522
Represents the attributes of a TGF entry frame.
Definition Others.h:592
Describes a fretted instrument (strings, frets, name, clef). It is used for both TAB notation and Fre...
Definition Others.h:732
A named group of fretboard diagrams associated with a specific fretboard instrument.
Definition Others.h:644
Fretboard diagram style: shapes, spacing, fonts, and offsets.
Definition Others.h:674
Represents the attributes associated with a Finale key signature.
Definition Others.h:844
The key format for a custom key signature.
Definition Others.h:787
An array of step elements from which one can create a key map.
Definition Others.h:806
Represents the attributes of a Finale "layer".
Definition Others.h:876
Represents the name associated with a MarkingCategory.
Definition Others.h:1077
Represents a category of markings used.
Definition Others.h:999
Assigns a TextExpressionDef or ShapeExpressionDef to a measure.
Definition Others.h:1241
Represents the Measure Number Region with detailed font and enclosure settings for score and part dat...
Definition Others.h:1329
Represents the attributes of a measure.
Definition Others.h:1090
Represents a group ID for a multi-staff setup.
Definition Others.h:1500
Represents a group of instruments spanning multiple staves.
Definition Others.h:1518
Represents the attributes of a multimeasure rest in the page layout.
Definition Others.h:1454
Overrides abbreviated name positioning for Staff.
Definition Others.h:1571
Overrides full name positioning for Staff.
Definition Others.h:1599
Overrides abbreviated name positioning for StaffStyle.
Definition Others.h:1585
Overrides full name positioning for StaffStyle.
Definition Others.h:1613
Stores additional positioning offsets for ossia passages.
Definition Ossia.h:46
Header properties for an ossia passage (clef, key, time, grouping).
Definition Ossia.h:71
Stores display and source options for an ossia passage.
Definition Ossia.h:115
Represents a page graphic assignment with positioning and page-range properties.
Definition Graphics.h:160
Represents the assignment of an ossia passage to a specific page.
Definition Ossia.h:149
Represents a page text assignment with positioning and page range properties.
Definition Others.h:1683
Represents the attributes of a page in the page layout.
Definition Others.h:1626
Represents the attributes of a Finale "partDef".
Definition Others.h:1796
Represents global values that can vary by part.
Definition Others.h:1864
Defines voicing options for linked parts, either by selection rules or by assigning a layer.
Definition Others.h:1897
Represents percussion notehead and staff position info for a given percussion note type.
Definition Others.h:1950
Represents a repeat-backward marker with positioning and behavior properties.
Definition Others.h:2022
Represents a repeat ending start marker in the document.
Definition Others.h:2057
Represents the text associated with a RepeatEndingStart.
Definition Others.h:2114
Represents a list of repeat ending numbers for a RepeatEndingStart instance.
Definition Others.h:2135
Represents the data for instruction associated with a ShapeDef.
Definition ShapeDesigner.h:54
Represents a shape created in Finale's Shape Designer.
Definition ShapeDesigner.h:72
Stores the properties and behaviors of shape expressions.
Definition Others.h:2151
Represents a graphic assignment anchored to a specific staff and measure.
Definition Graphics.h:282
Represents the data for instruction associated with a ShapeDef.
Definition ShapeDesigner.h:297
Represents a Finale smart shape custom line style.
Definition SmartShape.h:389
Assigns a smart shape or center shape to a measure.
Definition SmartShape.h:551
Represents a Finale smart shape.
Definition SmartShape.h:180
Defines the split point(s) where a measure may be divided between two systems. A measure can only spl...
Definition Others.h:2212
This class specified the name of a staff list used by @ ref MarkingCategory.
Definition Others.h:2251
This class is used by MarkingCategory to define the staves for parts in a staff list.
Definition Others.h:2265
This class is used by MarkingCategory to define the staves for parts in a staff list.
Definition Others.h:2281
This class specified the name of a staff list used by RepeatEndingStart, RepeatBack,...
Definition Others.h:2297
This class is used by repeat classes to define the forced staves for parts in a staff list....
Definition Others.h:2328
This class is used by repeat classes to define the staves for parts in a staff list.
Definition Others.h:2311
This class is used by repeat classes to define the forced staves for the score in a staff list....
Definition Others.h:2361
This class is used by repeat classes to define the staves for the score in a staff list.
Definition Others.h:2344
Represents an assignment.
Definition Staff.h:551
Represents a Finale staff style.
Definition Staff.h:441
Represents the attributes of a staff system in the page layout.
Definition Others.h:2377
An array of StaffUsed defines a set of staves in a staff system or in Scroll View.
Definition Others.h:2454
Represents the definition of a Finale staff.
Definition Staff.h:48
Locks a span of one or more measures so that they always appear in a StaffSystem together.
Definition Others.h:2487
A single tempo change value entered with Finale's Tempo tool.
Definition Others.h:2518
Represents the attributes of a Finale "textBlock".
Definition Others.h:2554
Stores the properties and behaviors of text expressions.
Definition Others.h:2615
The enclosure for a text expression (if it exists)
Definition Others.h:2673
Represents a text repeat assignment with positioning and behavior properties.
Definition Others.h:2690
Defines text repeat elements with font styling and justification.
Definition Others.h:2730
The enclosure for a text expression (if it exists)
Definition Others.h:2772
Represents the text associated with a TextRepeatDef.
Definition Others.h:2789
Represents the lower composite time signature array.
Definition Others.h:2812
Represents the upper composite time signature structure.
Definition Others.h:2847
Maps the number of flats to a tonal center for a linear custom key. If there are zero flats or sharps...
Definition Others.h:2891
Maps number of sharps (0..7) to a tonal center for a linear custom key. Also maps 0 sharps or flats....
Definition Others.h:2913
Contains block text (Finale Text Tool, both page- and measure-attached)
Definition Texts.h:182
Contains bookmark description text.
Definition Texts.h:229
Contains text blocks associated with text expressions.
Definition Texts.h:212
Contains File Info text (from Finale's Score Manager)
Definition Texts.h:50
Contains chorus text for lyrics.
Definition Texts.h:152
Contains section text for lyrics.
Definition Texts.h:167
Contains verse text for lyrics.
Definition Texts.h:137
Contains text blocks associated with custom line smart shapes.
Definition Texts.h:197
A utility class for managing deferred relationships between elements during document construction.
Definition FactoryBase.h:72
A registry of types for mapping XML node names to types.
Definition TypeRegistry.h:66
static std::optional< CreatedInstanceInfo > 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:149
static constexpr bool is_registered_type_v
Shorthand to get the value of is_registered_type.
Definition TypeRegistry.h:120
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::shared_ptr< Document > DocumentPtr
Shared Document pointer.
Definition BaseClasses.h:55
std::shared_ptr< IXmlElement > XmlElementPtr
shared pointer to IXmlElement
Definition XmlInterface.h:121
object model for musx file (enigmaxml)
Definition BaseClasses.h:36
Type trait to determine if a class in a given pool.
Definition ObjectPool.h:60
Information about a created instance returned by createInstance.
Definition TypeRegistry.h:125
std::shared_ptr< Base > instance
The newly created instance.
Definition TypeRegistry.h:130
std::string_view xmlNodeName
The static std::string_view containing the instance's node name.
Definition TypeRegistry.h:131
CreatedInstanceInfo(std::shared_ptr< Base > inst, std::string_view nodeName)
Constructor.
Definition TypeRegistry.h:127
Determine at compile-time if type T is a class in this registry.
Definition TypeRegistry.h:115