MUSX Document Model
Loading...
Searching...
No Matches
PoolFactory.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 <functional>
25#include <memory>
26#include <stdexcept>
27#include <string>
28#include <string_view>
29#include <type_traits>
30
31#include "musx/dom/Document.h"
32#include "musx/dom/ObjectPool.h"
33#include "musx/xml/XmlInterface.h"
34#include "musx/factory/ConstructionContext.h"
35
36namespace musx {
37namespace factory {
38
50using NodeFilter = std::function<bool(const xml::XmlElementPtr&)>;
51
54{
55public:
61 template <typename T, typename NodeNames>
62 static void initializePartial(const std::shared_ptr<T>& partInstance,
63 const std::shared_ptr<const T>& scoreInstance,
64 const NodeNames& unlinkedNodeNames)
65 {
66 static_assert(std::is_base_of_v<dom::EnigmaBase, T>, "T must derive from EnigmaBase");
67 if (!partInstance || !scoreInstance) {
68 throw std::invalid_argument("Partial sharing requires both score and part instances.");
69 }
70 if (partInstance->getShareMode() != dom::EnigmaBase::ShareMode::Partial) {
71 throw std::invalid_argument("Invalid score/part instances for partial sharing.");
72 }
73 *partInstance = *scoreInstance;
74 dom::PartContextRebinder<T>::rebind(partInstance);
75 for (const auto& nodeName : unlinkedNodeNames) {
76 partInstance->addUnlinkedNode(std::string(nodeName));
77 }
78 }
79};
80
83{
84public:
88 [[nodiscard]] static dom::OptionsPoolPtr create(
89 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
90 const NodeFilter& filter = {});
91};
92
95{
96public:
100 [[nodiscard]] static dom::OthersPoolPtr create(
101 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
102 const NodeFilter& filter = {});
103};
104
107{
108public:
112 [[nodiscard]] static dom::DetailsPoolPtr create(
113 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
114 const NodeFilter& filter = {});
115};
116
119{
120public:
124 [[nodiscard]] static dom::EntryPoolPtr create(
125 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
126 const NodeFilter& filter = {});
127};
128
131{
132public:
136 [[nodiscard]] static dom::TextsPoolPtr create(
137 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
138 const NodeFilter& filter = {});
139};
140
141} // namespace factory
142} // namespace musx
State shared by all factories during one document construction.
Definition ConstructionContext.h:12
Creates a details pool from an XML <details> element.
Definition PoolFactory.h:107
static dom::DetailsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:455
Creates an entry pool from an XML <entries> element.
Definition PoolFactory.h:119
static dom::EntryPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:490
Creates an options pool from an XML <options> element.
Definition PoolFactory.h:83
static dom::OptionsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:425
Creates an others pool from an XML <others> element.
Definition PoolFactory.h:95
static dom::OthersPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:435
Initializes a partially shared part object from its score object.
Definition PoolFactory.h:54
static void initializePartial(const std::shared_ptr< T > &partInstance, const std::shared_ptr< const T > &scoreInstance, const NodeNames &unlinkedNodeNames)
Copies score values into a partial part object and records its unlinked fields.
Definition PoolFactory.h:62
Creates a texts pool from an XML <texts> element.
Definition PoolFactory.h:131
static dom::TextsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:509
std::shared_ptr< OthersPool > OthersPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:622
std::shared_ptr< DetailsPool > DetailsPoolPtr
Shared DetailsPool pointer.
Definition ObjectPool.h:722
std::shared_ptr< TextsPool > TextsPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:839
std::shared_ptr< OptionsPool > OptionsPoolPtr
Shared OptionsPool pointer.
Definition ObjectPool.h:540
std::shared_ptr< Document > DocumentPtr
Shared Document pointer.
Definition DocumentElement.h:35
std::shared_ptr< EntryPool > EntryPoolPtr
Shared EntryPool pointer.
Definition ObjectPool.h:776
std::function< bool(const xml::XmlElementPtr &)> NodeFilter
Selects which child elements a pool factory creates objects from.
Definition PoolFactory.h:50
std::shared_ptr< IXmlElement > XmlElementPtr
shared pointer to IXmlElement
Definition XmlInterface.h:127
object model for musx file (enigmaxml)
Definition BaseClasses.h:39