1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <type_traits>
29
30namespace WTF {
31
32template <typename ExpectedType, typename ArgType, bool isBaseType = std::is_base_of<ExpectedType, ArgType>::value>
33struct TypeCastTraits {
34 static bool isOfType(ArgType&)
35 {
36 // If you're hitting this assertion, it is likely because you used
37 // is<>() or downcast<>() with a type that doesn't have the needed
38 // TypeCastTraits specialization. Please use the following macro
39 // to add that specialization:
40 // SPECIALIZE_TYPE_TRAITS_BEGIN() / SPECIALIZE_TYPE_TRAITS_END()
41 static_assert(std::is_void<ExpectedType>::value, "Missing TypeCastTraits specialization");
42 return false;
43 }
44};
45
46// Template specialization for the case where ExpectedType is a base of ArgType,
47// so we can return return true unconditionally.
48template <typename ExpectedType, typename ArgType>
49struct TypeCastTraits<ExpectedType, ArgType, true /* isBaseType */> {
50 static bool isOfType(ArgType&) { return true; }
51};
52
53// Type checking function, to use before casting with downcast<>().
54template <typename ExpectedType, typename ArgType>
55inline bool is(ArgType& source)
56{
57 static_assert(std::is_base_of<ArgType, ExpectedType>::value, "Unnecessary type check");
58 return TypeCastTraits<const ExpectedType, const ArgType>::isOfType(source);
59}
60
61template <typename ExpectedType, typename ArgType>
62inline bool is(ArgType* source)
63{
64 static_assert(std::is_base_of<ArgType, ExpectedType>::value, "Unnecessary type check");
65 return source && TypeCastTraits<const ExpectedType, const ArgType>::isOfType(*source);
66}
67
68// Update T's constness to match Reference's.
69template <typename Reference, typename T>
70struct match_constness {
71 typedef typename std::conditional<std::is_const<Reference>::value, typename std::add_const<T>::type, typename std::remove_const<T>::type>::type type;
72};
73
74// Safe downcasting functions.
75template<typename Target, typename Source>
76inline typename match_constness<Source, Target>::type& downcast(Source& source)
77{
78 static_assert(!std::is_same<Source, Target>::value, "Unnecessary cast to same type");
79 static_assert(std::is_base_of<Source, Target>::value, "Should be a downcast");
80 ASSERT_WITH_SECURITY_IMPLICATION(is<Target>(source));
81 return static_cast<typename match_constness<Source, Target>::type&>(source);
82}
83template<typename Target, typename Source>
84inline typename match_constness<Source, Target>::type* downcast(Source* source)
85{
86 static_assert(!std::is_same<Source, Target>::value, "Unnecessary cast to same type");
87 static_assert(std::is_base_of<Source, Target>::value, "Should be a downcast");
88 ASSERT_WITH_SECURITY_IMPLICATION(!source || is<Target>(*source));
89 return static_cast<typename match_constness<Source, Target>::type*>(source);
90}
91
92// Add support for type checking / casting using is<>() / downcast<>() helpers for a specific class.
93#define SPECIALIZE_TYPE_TRAITS_BEGIN(ClassName) \
94namespace WTF { \
95template <typename ArgType> \
96class TypeCastTraits<const ClassName, ArgType, false /* isBaseType */> { \
97public: \
98 static bool isOfType(ArgType& source) { return isType(source); } \
99private:
100
101#define SPECIALIZE_TYPE_TRAITS_END() \
102}; \
103}
104
105} // namespace WTF
106
107using WTF::TypeCastTraits;
108using WTF::is;
109using WTF::downcast;
110