/******************************************************************************* * * MIT License * * Copyright (C) 2022-2025 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace TensileLite { namespace Serialization { template struct MappingTraits>, IO> : public BaseClassMappingTraits, IO, false> { }; template struct SubclassMappingTraits, IO> : public DefaultSubclassMappingTraits< SubclassMappingTraits, IO>, SolutionLibrary, IO> { using Self = SubclassMappingTraits, IO>; using Base = DefaultSubclassMappingTraits, IO>; using SubclassMap = typename Base::SubclassMap; const static SubclassMap subclasses; static typename Base::SubclassMap GetSubclasses() { return typename Base::SubclassMap( {Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>(), Base::template Pair>()}); } }; template using dsmt = SubclassMappingTraits, IO>; template const typename dsmt::SubclassMap SubclassMappingTraits, IO>::subclasses = dsmt::GetSubclasses(); template struct MappingTraits, IO> { using Library = SingleSolutionLibrary; using iot = IOTraits; static void mapping(IO& io, Library& lib) { auto ctx = static_cast*>(iot::getContext(io)); if(ctx == nullptr || ctx->solutions == nullptr) { iot::setError(io, "SingleSolutionLibrary requires that context be set to " "a SolutionMap."); } int index; if(iot::outputting(io)) index = lib.solution->index; iot::mapRequired(io, "index", index); if(!iot::outputting(io)) { auto iter = ctx->solutions->find(index); if(iter == ctx->solutions->end()) { std::ostringstream msg; msg << "[SolutionLibrary] Invalid solution index: " << index; iot::setError(io, msg.str()); } else { lib.solution = iter->second; } } } const static bool flow = true; }; template struct MappingTraits, IO, EmptyContext> { using Library = MasterSolutionLibrary; using iot = IOTraits; static void mapping(IO& io, Library& lib) { iot::mapOptional(io, "version", lib.version); std::vector> solutions; if(iot::outputting(io)) { solutions.reserve(lib.solutions.size()); for(auto const& pair : lib.solutions) solutions.push_back(pair.second); } iot::mapRequired(io, "solutions", solutions); if(!iot::outputting(io)) { for(auto const& s : solutions) lib.solutions[s->index] = s; auto ctx = static_cast*>(iot::getContext(io)); ctx->solutions = &lib.solutions; ctx->solutionsGuard = &lib.solutionsGuard; ctx->loadedFiles = &lib.loadedFiles; ctx->indexLoadedLibraries = (void*)&lib.indexLoadedLibraries; } std::shared_ptr> innerLibrary; if(iot::outputting(io)) { auto cache = std::dynamic_pointer_cast>( lib.library); if(cache) { innerLibrary = cache->library(); } else { innerLibrary = lib.library; } } iot::mapRequired(io, "library", innerLibrary); if(!iot::outputting(io)) { auto cache = std::make_shared>(innerLibrary); lib.library = cache; } } const static bool flow = false; }; } // namespace Serialization } // namespace TensileLite