/* * Copyright (c) 2023 Huawei Device Co., Ltd. * 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 MAPLE_UTIL_INCLUDE_SAFE_CAST_H #define MAPLE_UTIL_INCLUDE_SAFE_CAST_H #include "utils/meta.h" namespace maple { template struct SafeCastCondition : public std::false_type { }; #define REGISTER_SAFE_CAST(type, condition) \ template <> \ struct SafeCastCondition : public std::true_type { \ template \ static inline bool DoIt(const FromT &from) \ { \ return (condition); \ } \ } namespace impl { template ::value>> struct InstanceOfImpl { static inline bool DoIt(const FromT &from) { return (SafeCastCondition::DoIt(from)); } }; template struct InstanceOfImpl::value>> { static inline bool DoIt(const FromT &) { return true; } }; template struct EnabledSafeCast : public utils::meta_or, SafeCastCondition>::type { }; } // namespace impl template ::value>> inline bool instance_of(FromT &from) { return impl::InstanceOfImpl::DoIt(from); } template ::value>> inline bool instance_of(FromT *from) { return (from != nullptr && instance_of(*from)); } template ::value || std::is_const>::value, std::add_pointer_t>>, std::add_pointer_t>>, typename = std::enable_if_t::value>> inline RetT safe_cast(FromT &from) { return (instance_of(from) ? static_cast(&from) : nullptr); } template ::value || std::is_const>::value, std::add_pointer_t>>, std::add_pointer_t>>, typename = std::enable_if_t::value>> inline RetT safe_cast(FromT *from) { return (instance_of(from) ? static_cast(from) : nullptr); } } // namespace maple #endif // MAPLE_UTIL_INCLUDE_SAFE_CAST_H