// Copyright (c) 2023 CINN Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once namespace cinn { namespace utils { #include #include #include #include template constexpr auto PrettyName() { std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}; name.remove_prefix(name.find_last_of(" ") + 1); if (name.front() == '(') name.remove_prefix(name.size()); return name; } template constexpr bool IsValidEnum() { return !PrettyName().empty(); } template constexpr auto MakeIntegerSequence(std::integer_sequence) { return std::integer_sequence(); } constexpr auto NormalIntegerSequence = MakeIntegerSequence(std::make_integer_sequence()); template constexpr size_t GetEnumSize(std::integer_sequence) { constexpr std::array valid{ IsValidEnum(Seq)>()...}; constexpr std::size_t count = [](decltype((valid)) v) constexpr noexcept->std::size_t { auto cnt = std::size_t{0}; for (auto b : v) { if (b) { ++cnt; } } return cnt; } (valid); return count; } template constexpr auto GetAllValidValues(std::integer_sequence) { constexpr std::size_t count = sizeof...(Seq); constexpr std::array valid{ IsValidEnum(Seq)>()...}; constexpr std::array seq{Seq...}; std::array(NormalIntegerSequence)> values{}; for (std::size_t i = 0, v = 0; i < count; ++i) { if (valid[i]) { values[v++] = seq[i]; } } return values; } template constexpr auto GetAllValidNames(std::integer_sequence) { constexpr std::array names{ PrettyName(Seq)>()...}; std::array(NormalIntegerSequence)> valid_names{}; for (std::size_t i = 0, v = 0; i < names.size(); ++i) { if (!names[i].empty()) { valid_names[v++] = names[i]; } } return valid_names; } template constexpr std::string_view Enum2String(E V) { constexpr auto names = GetAllValidNames(NormalIntegerSequence); constexpr auto values = GetAllValidValues(NormalIntegerSequence); constexpr auto size = GetEnumSize(NormalIntegerSequence); for (size_t i = 0; i < size; ++i) { if (static_cast(V) == values[i]) { return names[i]; } } return std::to_string(static_cast(V)); } } // namespace utils } // namespace cinn