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:
62 template <typename T>
63 static void initializePartial(const std::shared_ptr<T>& partInstance,
64 const std::shared_ptr<const T>& scoreInstance)
65 {
66 static_assert(std::is_base_of_v<dom::EnigmaBase, T>, "T must derive from EnigmaBase");
67 MUSX_ASSERT_IF(!partInstance || !scoreInstance) {
68 throw std::invalid_argument("Partial sharing requires both score and part instances.");
69 }
70 MUSX_ASSERT_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 }
76};
77
80{
81public:
85 [[nodiscard]] static dom::OptionsPoolPtr create(
86 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
87 const NodeFilter& filter = {});
88};
89
92{
93public:
97 [[nodiscard]] static dom::OthersPoolPtr create(
98 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
99 const NodeFilter& filter = {});
100};
101
104{
105public:
109 [[nodiscard]] static dom::DetailsPoolPtr create(
110 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
111 const NodeFilter& filter = {});
112};
113
116{
117public:
121 [[nodiscard]] static dom::EntryPoolPtr create(
122 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
123 const NodeFilter& filter = {});
124};
125
128{
129public:
133 [[nodiscard]] static dom::TextsPoolPtr create(
134 ConstructionContext& context, const xml::XmlElementPtr& element, const dom::DocumentPtr& document,
135 const NodeFilter& filter = {});
136};
137
138} // namespace factory
139} // 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:104
static dom::DetailsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:451
Creates an entry pool from an XML <entries> element.
Definition PoolFactory.h:116
static dom::EntryPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:486
Creates an options pool from an XML <options> element.
Definition PoolFactory.h:80
static dom::OptionsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:421
Creates an others pool from an XML <others> element.
Definition PoolFactory.h:92
static dom::OthersPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:431
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)
Copies score values into a partial part object.
Definition PoolFactory.h:63
Creates a texts pool from an XML <texts> element.
Definition PoolFactory.h:128
static dom::TextsPoolPtr create(ConstructionContext &context, const xml::XmlElementPtr &element, const dom::DocumentPtr &document, const NodeFilter &filter={})
Definition PoolFactory.cpp:505
std::shared_ptr< OthersPool > OthersPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:674
std::shared_ptr< DetailsPool > DetailsPoolPtr
Shared DetailsPool pointer.
Definition ObjectPool.h:797
std::shared_ptr< TextsPool > TextsPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:914
std::shared_ptr< OptionsPool > OptionsPoolPtr
Shared OptionsPool pointer.
Definition ObjectPool.h:570
std::shared_ptr< Document > DocumentPtr
Shared Document pointer.
Definition DocumentElement.h:35
std::shared_ptr< EntryPool > EntryPoolPtr
Shared EntryPool pointer.
Definition ObjectPool.h:851
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:38