/* * Copyright (C) 2018 The Android Open Source Project * * 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. */ #ifndef TNT_ENUMMANAGER_H #define TNT_ENUMMANAGER_H #include #include #include #include namespace filamat { using Property = MaterialBuilder::Property; using UniformType = MaterialBuilder::UniformType; using SamplerType = MaterialBuilder::SamplerType; using SubpassType = MaterialBuilder::SubpassType; using SamplerFormat = MaterialBuilder::SamplerFormat; using SamplerPrecision = MaterialBuilder::SamplerPrecision; using OutputTarget = MaterialBuilder::OutputTarget; using OutputQualifier = MaterialBuilder::VariableQualifier; using OutputType = MaterialBuilder::OutputType; // Convenience methods to convert std::string to Enum and also iterate over Enum values. class Enums { public: // Returns true if string "s" is a valid string representation of an element of enum T. template static bool isValid(const std::string& s) noexcept { std::unordered_map& map = getMap(); return map.find(s) != map.end(); } // Return enum matching its string representation. Returns undefined if s is not a valid enum T // value. You should always call isValid() first to validate a string before calling toEnum(). template static T toEnum(const std::string& s) noexcept { std::unordered_map& map = getMap(); return map.at(s); } template static std::string toString(T t) noexcept; // Return a map of all values in an enum with their string representation. template static std::unordered_map& map() noexcept { std::unordered_map& map = getMap(); return map; }; private: template static std::unordered_map& getMap() noexcept; static std::unordered_map mStringToProperty; static std::unordered_map mStringToUniformType; static std::unordered_map mStringToSamplerType; static std::unordered_map mStringToSubpassType; static std::unordered_map mStringToSamplerFormat; static std::unordered_map mStringToSamplerPrecision; static std::unordered_map mStringToOutputTarget; static std::unordered_map mStringToOutputQualifier; static std::unordered_map mStringToOutputType; }; template std::string Enums::toString(T t) noexcept { std::unordered_map& map = getMap(); auto result = std::find_if(map.begin(), map.end(), [t](auto& pair) { return pair.second == t; }); if (result != map.end()) { return result->first; } return ""; } } // namespace filamat #endif //TNT_ENUMMANAGER_H