#include "settingsconfigimpl.h" #include #include #include namespace { using SourceType = SettingsConfigBase::variant; template void convertElement(QXmlStreamReader &reader, TVariantTarget &, SourceType &&) { throw SettingsConfigBase::XmlException{reader, QStringLiteral("Unexpected root element in included file")}; } template void convertElement(QXmlStreamReader &reader, TVariantTarget &target, SourceType &&source) { if(nonstd::holds_alternative(source)) target = nonstd::get(std::move(source)); else convertElement(reader, target, std::move(source)); } template struct VariantInfo {}; template struct VariantInfo>> { using TargetType = SettingsConfigBase::variant; static void convert(QXmlStreamReader &reader, TargetType &target, SourceType &&source) { convertElement(reader, target, std::move(source)); } }; } bool SettingsConfigImpl::finish_group_content(QXmlStreamReader &reader, GroupContentGroup &data, bool hasNext) { hasNext = read_GroupContentGroup(reader, data, hasNext); finishContents(reader, data.content); return hasNext; } bool SettingsConfigImpl::finish_section_content(QXmlStreamReader &reader, SectionContentGroup &data, bool hasNext) { hasNext = read_SectionContentGroup(reader, data, hasNext); finishContents(reader, data.content); return hasNext; } bool SettingsConfigImpl::finish_category_content(QXmlStreamReader &reader, CategoryContentGroup &data, bool hasNext) { hasNext = read_CategoryContentGroup(reader, data, hasNext); finishContents(reader, data.content); return hasNext; } bool SettingsConfigImpl::finish_settings_config_content(QXmlStreamReader &reader, SettingsConfigContentGroup &data, bool hasNext) { hasNext = read_SettingsConfigContentGroup(reader, data, hasNext); finishContents(reader, data.content); return hasNext; } template void SettingsConfigImpl::finishContents(QXmlStreamReader &reader, TGroup &group) { optional index; for(auto it = group.begin(); it != group.end();) { // convert includes to the actual data if(nonstd::holds_alternative(*it)) { if(!readGeneralInclude(reader, nonstd::get(*it), it, group)) continue; } // verify that the contents are all of the same type if(index) { if(index.value() != it->index()) throw XmlException{reader, QStringLiteral("Detected mixture of different child elements. Only includes and a single other type are allowed")}; } else index = it->index(); ++it; } } template bool SettingsConfigImpl::readGeneralInclude(QXmlStreamReader &reader, IncludeType include, TIter &it, TList &list) { try { //make the path relative if possbile if(dynamic_cast(reader.device())) { QFileInfo docInfo{static_cast(reader.device())->fileName()}; include.includePath = docInfo.dir().absoluteFilePath(include.includePath); } // read the document VariantInfo::convert(reader, *it, readDocument(include.includePath)); return true; } catch(FileException &e) { if(include.optional) { qWarning() << e.what(); it = list.erase(it); return false; } else throw; } }