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#ifndef TypeCasts_h
27#define TypeCasts_h
28
29#include <type_traits>
30
31namespace WTF {
32
33template <typename ExpectedType, typename ArgType, bool isBaseType = std::is_base_of<ExpectedType, ArgType>::value>
34struct TypeCastTraits {
35 static bool isOfType(ArgType&)
36 {
37 // If you're hitting this assertion, it is likely because you used
38 // is<>() or downcast<>() with a type that doesn't have the needed
39 // TypeCastTraits specialization. Please use the following macro
40 // to add that specialization:
41 // SPECIALIZE_TYPE_TRAITS_BEGIN() / SPECIALIZE_TYPE_TRAITS_END()
42 static_assert(std::is_void<ExpectedType>::value, "Missing TypeCastTraits specialization");
43 return false;
44 }
45};
46
47// Template specialization for the case where ExpectedType is a base of ArgType,
48// so we can return return true unconditionally.
49template <typename ExpectedType, typename ArgType>
50struct TypeCastTraits<ExpectedType, ArgType, true /* isBaseType */> {
51 static bool isOfType(ArgType&) { return true; }
52};
53
54// Type checking function, to use before casting with downcast<>().
55template <typename ExpectedType, typename ArgType>
56inline bool is(ArgType& source)
57{
58 static_assert(!std::is_base_of<ExpectedType, ArgType>::value, "Unnecessary type check");
59 return TypeCastTraits<const ExpectedType, const ArgType>::isOfType(source);
60}
61
62template <typename ExpectedType, typename ArgType>
63inline bool is(ArgType* source)
64{
65 static_assert(!std::is_base_of<ExpectedType, ArgType>::value, "Unnecessary type check");
66 return source && TypeCastTraits<const ExpectedType, const ArgType>::isOfType(*source);
67}
68
69// Update T's constness to match Reference's.
70template <typename Reference, typename T>
71struct match_constness {
72 typedef typename std::conditional<std::is_const<Reference>::value, typename std::add_const<T>::type, typename std::remove_const<T>::type>::type type;
73};
74
75// Safe downcasting functions.
76template<typename Target, typename Source>
77inline typename match_constness<Source, Target>::type& downcast(Source& source)
78{
79 static_assert(!std::is_base_of<Target, Source>::value, "Unnecessary cast");
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_base_of<Target, Source>::value, "Unnecessary cast");
87 ASSERT_WITH_SECURITY_IMPLICATION(!source || is<Target>(*source));
88 return static_cast<typename match_constness<Source, Target>::type*>(source);
89}
90
91// Add support for type checking / casting using is<>() / downcast<>() helpers for a specific class.
92#define SPECIALIZE_TYPE_TRAITS_BEGIN(ClassName) \
93namespace WTF { \
94template <typename ArgType> \
95class TypeCastTraits<const ClassName, ArgType, false /* isBaseType */> { \
96public: \
97 static bool isOfType(ArgType& source) { return isType(source); } \
98private:
99
100#define SPECIALIZE_TYPE_TRAITS_END() \
101}; \
102}
103
104} // namespace WTF
105
106using WTF::TypeCastTraits;
107using WTF::is;
108using WTF::downcast;
109
110#endif // TypeCasts_h
111