52 template <
typename Container>
53 using IsCharContainer = std::enable_if_t<
54 (
sizeof(std::remove_cv_t<typename Container::value_type>) == 1)
55 && !std::is_same_v<std::remove_cv_t<typename Container::value_type>,
bool>>;
61 std::optional<double> scoreDurationSeconds;
63 std::optional<std::filesystem::path> sourcePath;
74 using EmbeddedGraphicFiles = std::vector<EmbeddedGraphicFile>;
79 std::vector<char> notationMetadata,
80 EmbeddedGraphicFiles embeddedGraphicFiles,
82 : partVoicingPolicy(policy),
83 m_notationMetadata(std::move(notationMetadata)),
84 m_embeddedGraphics(parseEmbeddedGraphicFiles(std::move(embeddedGraphicFiles))),
85 m_sourcePath(std::move(inputFilepath))
90 [[nodiscard]]
const std::vector<char>& getNotationMetadata()
const {
return m_notationMetadata; }
92 [[nodiscard]]
const std::optional<std::filesystem::path>& getSourcePath()
const {
return m_sourcePath; }
99 for (
auto& file : files) {
100 const auto dot = file.filename.find(
'.');
101 if (dot == std::string::npos || dot == 0 || dot + 1 >= file.filename.size())
continue;
102 const std::string_view idText(file.filename.data(), dot);
103 if (!std::all_of(idText.begin(), idText.end(),
104 [](
unsigned char ch) { return std::isdigit(ch) != 0; }))
continue;
105 unsigned long long id = 0;
107 id = std::stoull(std::string(idText));
111 if (
id >
static_cast<unsigned long long>((std::numeric_limits<dom::Cmper>::max)()))
continue;
112 auto& entry = result[
static_cast<dom::Cmper>(id)];
113 entry.extension = file.filename.substr(dot + 1);
114 entry.bytes = std::move(file.bytes);
119 std::vector<char> m_notationMetadata;
121 std::optional<std::filesystem::path> m_sourcePath;
133 [[nodiscard]]
const DocumentPtr& getDocument()
const;
135 [[nodiscard]]
const ConstructionContext& getConstructionContext()
const {
return m_context; }
136 [[nodiscard]] DocumentPtr finish() &&;
139 enum class State { Active, Consumed };
142 DocumentPtr m_document;
144 State m_state = State::Active;
152 template <
typename XmlDocumentType>
153 [[nodiscard]]
static DocumentPtr create(
const char* data,
size_t size)
158 template <
typename XmlDocumentType,
typename Container,
typename = IsCharContainer<Container>>
159 [[nodiscard]]
static DocumentPtr create(
const Container& xmlBuffer)
161 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(), CreateOptions{});
164 template <
typename XmlDocumentType>
165 [[nodiscard]]
static DocumentPtr create(
const char* data,
size_t size, CreateOptions&& createOptions)
167 static_assert(std::is_base_of_v<xml::IXmlDocument, XmlDocumentType>,
168 "XmlDocumentType must derive from IXmlDocument.");
169 auto xmlDocument = std::make_unique<XmlDocumentType>();
170 xmlDocument->loadFromBuffer(data, size);
171 auto root = xmlDocument->getRootElement();
172 if (!root || root->getTagName() !=
"finale") {
173 throw std::invalid_argument(
"Missing <finale> element.");
176 ConstructionOptions options;
177 options.partVoicingPolicy = createOptions.partVoicingPolicy;
178 options.scoreDurationSeconds = parseScoreDurationSeconds<XmlDocumentType>(
179 createOptions.getNotationMetadata());
180 options.embeddedGraphics = createOptions.takeEmbeddedGraphics();
181 options.sourcePath = createOptions.getSourcePath();
182 return createFromXmlRoot(root, std::move(options));
185 template <
typename XmlDocumentType,
typename Container,
typename = IsCharContainer<Container>>
186 [[nodiscard]]
static DocumentPtr create(
const Container& xmlBuffer, CreateOptions&& createOptions)
188 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(), std::move(createOptions));
192 static DocumentPtr createFromXmlRoot(
194 static void finalize(
const DocumentPtr& document, ConstructionContext& context);
196 template <
typename Container>
197 static const char* asCharData(
const Container& buffer)
199 return reinterpret_cast<const char*
>(buffer.data());
202 template <
typename XmlDocumentType>
203 static std::optional<double> parseScoreDurationSeconds(
const std::vector<char>& metadata)
205 if (metadata.empty())
return std::nullopt;
206 auto document = std::make_unique<XmlDocumentType>();
208 document->loadFromBuffer(metadata.data(), metadata.size());
212 auto root = document->getRootElement();
213 if (!root || root->getTagName() !=
"metadata")
return std::nullopt;
214 auto fileInfo = root->getFirstChildElement(
"fileInfo");
215 if (!fileInfo)
return std::nullopt;
216 auto creator = fileInfo->getFirstChildElement(
"creatorString");
217 if (!creator || creator->getTextTrimmed().rfind(
"Finale", 0) != 0)
return std::nullopt;
218 auto duration = fileInfo->getFirstChildElement(
"scoreDuration");
219 if (!duration)
return std::nullopt;
221 return duration->template getTextAs<double>();
State shared by all factories during one document construction.
Definition ConstructionContext.h:12