// Copyright Advanced Micro Devices, Inc., or its affiliates. // SPDX-License-Identifier: MIT #pragma once #include #include #include #include namespace rocRoller { namespace Serialization { template struct KeyConversion { static std::string toString(Key const& value) { return concatenate(value); } static Key fromString(std::string const& value) { std::istringstream stream(value); Key rv; stream >> rv; return rv; } }; template <> struct KeyConversion { static std::string const& toString(std::string const& value) { return value; } static std::string const& fromString(std::string const& value) { return value; } }; template struct DefaultCustomMappingTraits { using iot = IOTraits; using key_type = typename Map::key_type; using mapped_type = typename Map::mapped_type; static void inputOne(IO& io, std::string const& keyStr, Map& value) { iot::mapRequired( io, keyStr.c_str(), value[KeyConversion::fromString(keyStr)]); } static void output(IO& io, Map& value) { if(Sort) { std::vector keys; keys.reserve(value.size()); std::ranges::copy(value | std::views::keys, std::back_inserter(keys)); std::sort(keys.begin(), keys.end()); for(auto const& key : keys) { auto keyStr = KeyConversion::toString(key); iot::mapRequired(io, keyStr.c_str(), value.find(key)->second); } } else { for(auto& pair : value) { auto keyStr = KeyConversion::toString(pair.first); iot::mapRequired(io, keyStr.c_str(), pair.second); } } } template static void output(IO& io, Map& value, t ignore) { output(io, value); } static const bool flow = Flow; }; template struct CustomMappingTraits, IO> : public DefaultCustomMappingTraits, IO, false, true> { }; template struct CustomMappingTraits, IO> : public DefaultCustomMappingTraits, IO, false, true> { }; template struct CustomMappingTraits, IO> : public DefaultCustomMappingTraits, IO, false, true> { }; template struct DefaultSequenceTraits { using Value = typename Seq::value_type; static size_t size(IO& io, Seq& s) { return s.size(); } static Value& element(IO& io, Seq& s, size_t index) { if(index >= s.size()) { size_t n = index - s.size() + 1; s.insert(s.end(), n, Value()); } return s[index]; } const static bool flow = Flow; }; #define ROCROLLER_SERIALIZE_VECTOR(flow, ...) \ template \ struct SequenceTraits, IO> \ : public DefaultSequenceTraits, IO, flow> \ { \ } template struct SequenceTraits, IO> { static const bool flow = true; using Value = T; static size_t size(IO& io, std::array& v) { return N; } static T& element(IO& io, std::array& v, size_t index) { if(index >= N) { IOTraits::setError(io, concatenate("invalid array index ", index)); } return v[index]; } }; template struct SequenceTraits, IO> { static const bool flow = true; using Value = T; static size_t size(IO& io, std::pair& v) { return 2; } static T& element(IO& io, std::pair& v, size_t index) { AssertFatal(index == 0 || index == 1); if(index == 0) return v.first; return v.second; } }; ROCROLLER_SERIALIZE_VECTOR(true, int); ROCROLLER_SERIALIZE_VECTOR(true, unsigned int); } // namespace Serialization } // namespace rocRoller