template<typename To, typename From, typename SimpleFrom> structisa_impl_wrap { // When From != SimplifiedType, we can simplify the type some more by using // the simplify_type template. staticbooldoit(const From &Val){ return isa_impl_wrap<To, SimpleFrom, typename simplify_type<SimpleFrom>::SimpleType>::doit( simplify_type<const From>::getSimplifiedValue(Val)); } };
template<typename To, typename FromTy> structisa_impl_wrap<To, FromTy, FromTy> { // When From == SimpleType, we are as simple as we are going to get. staticbooldoit(const FromTy &Val){ return isa_impl_cl<To,FromTy>::doit(Val); } };
这里 isa_impl_wrap 只是为了特殊处理 const From,最终都会走到 is_impl_cl 上,is_impl_cl 分别处理第二个模板参数为 From, const From, std::unique<From>, From*, From *const, const From *, const From *const 的情况
template <typename To, typename From> structisa_impl_cl { staticinlinebooldoit(const From &Val){ return isa_impl<To, From>::doit(Val); } };
template <typename To, typename From> structisa_impl_cl<To, const From> { staticinlinebooldoit(const From &Val){ return isa_impl<To, From>::doit(Val); } };
template <typename To, typename From> structisa_impl_cl<To, const std::unique_ptr<From>> { staticinlinebooldoit(const std::unique_ptr<From> &Val){ assert(Val && "isa<> used on a null pointer"); return isa_impl_cl<To, From>::doit(*Val); } };
template <typename To, typename From> structisa_impl_cl<To, From*> { staticinlinebooldoit(const From *Val){ assert(Val && "isa<> used on a null pointer"); return isa_impl<To, From>::doit(*Val); } };
// From *const, const From *, const From *const
最后就是具体的实现部分,如果 To 是 From 的基类或 To::classof(&Val),则为 true
template <typename To, typename From, typename Enabler = void> struct isa_impl { staticinlinebooldoit(const From &Val) { return To::classof(&Val); } };
/// Always allow upcasts, and perform no dynamic check for them. template <typename To, typename From> structisa_impl<To, From, std::enable_if_t<std::is_base_of<To, From>::value>> { staticinlinebooldoit(const From &){ returntrue; } };