1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include <type_traits>
13 #include "mozilla/Assertions.h"
15 // NOTE: the long lines in this file are intentional to make compiler error
16 // messages more readable.
18 #define NS_DECL_QUERYFRAME_TARGET(classname) \
19 static const nsQueryFrame::FrameIID kFrameIID = \
20 nsQueryFrame::classname##_id; \
21 typedef classname Has_NS_DECL_QUERYFRAME_TARGET;
23 #define NS_DECL_QUERYFRAME void* QueryFrame(FrameIID id) const override;
25 #define NS_QUERYFRAME_HEAD(class) \
26 void* class ::QueryFrame(FrameIID id) const { \
28 #define NS_QUERYFRAME_ENTRY(class) \
29 case class ::kFrameIID: { \
31 std::is_same_v<class, class ::Has_NS_DECL_QUERYFRAME_TARGET>, \
32 #class " must declare itself as a queryframe target"); \
33 return const_cast<class*>(static_cast<const class*>(this)); \
36 #define NS_QUERYFRAME_ENTRY_CONDITIONAL(class, condition) \
37 case class ::kFrameIID: \
40 std::is_same_v<class, class ::Has_NS_DECL_QUERYFRAME_TARGET>, \
41 #class " must declare itself as a queryframe target"); \
42 return const_cast<class*>(static_cast<const class*>(this)); \
46 #define NS_QUERYFRAME_TAIL_INHERITING(class) \
50 return class ::QueryFrame(id); \
53 #define NS_QUERYFRAME_TAIL_INHERITANCE_ROOT \
57 MOZ_ASSERT(id != GetFrameId(), \
58 "A frame failed to QueryFrame to its *own type*. " \
59 "It may be missing NS_DECL_QUERYFRAME, or a " \
60 "NS_QUERYFRAME_ENTRY() line with its own type name"); \
67 #define FRAME_ID(classname, ...) classname##_id,
68 #define ABSTRACT_FRAME_ID(classname) classname##_id,
69 #include "mozilla/FrameIdList.h"
71 #undef ABSTRACT_FRAME_ID
74 // A strict subset of FrameIID above for frame classes that we instantiate.
75 enum class ClassID
: uint8_t {
76 #define FRAME_ID(classname, ...) classname##_id,
77 #define ABSTRACT_FRAME_ID(classname)
78 #include "mozilla/FrameIdList.h"
80 #undef ABSTRACT_FRAME_ID
83 virtual void* QueryFrame(FrameIID id
) const = 0;
88 template <class Source
>
89 class do_QueryFrameHelper
{
91 explicit do_QueryFrameHelper(Source
* s
) : mRawPtr(s
) {}
93 // The return and argument types here are arbitrarily selected so no
94 // corresponding member function exists.
95 typedef void (do_QueryFrameHelper::*MatchNullptr
)(double, float);
96 // Implicit constructor for nullptr, trick borrowed from already_AddRefed.
97 MOZ_IMPLICIT
do_QueryFrameHelper(MatchNullptr aRawPtr
) : mRawPtr(nullptr) {}
101 static_assert(std::is_same_v
<std::remove_const_t
<Dest
>,
102 typename
Dest::Has_NS_DECL_QUERYFRAME_TARGET
>,
103 "Dest must declare itself as a queryframe target");
107 if (Dest
* f
= FastQueryFrame
<Source
, Dest
>::QueryFrame(mRawPtr
)) {
109 f
== reinterpret_cast<Dest
*>(mRawPtr
->QueryFrame(Dest::kFrameIID
)),
110 "fast and slow paths should give the same result");
113 return reinterpret_cast<Dest
*>(mRawPtr
->QueryFrame(Dest::kFrameIID
));
117 // For non-nsIFrame types there is no fast-path.
118 template <class Src
, class Dst
, typename
= void, typename
= void>
119 struct FastQueryFrame
{
120 static Dst
* QueryFrame(Src
* aFrame
) { return nullptr; }
123 // Specialization for any nsIFrame type to any nsIFrame type -- if the source
124 // instance's mClass matches kFrameIID of the destination type then
125 // downcasting is safe.
126 template <class Src
, class Dst
>
127 struct FastQueryFrame
<
128 Src
, Dst
, std::enable_if_t
<std::is_base_of
<nsIFrame
, Src
>::value
>,
129 std::enable_if_t
<std::is_base_of
<nsIFrame
, Dst
>::value
>> {
130 static Dst
* QueryFrame(Src
* aFrame
) {
131 return nsQueryFrame::FrameIID(aFrame
->mClass
) == Dst::kFrameIID
132 ? reinterpret_cast<Dst
*>(aFrame
)
141 inline do_QueryFrameHelper
<T
> do_QueryFrame(T
* s
) {
142 return do_QueryFrameHelper
<T
>(s
);
145 #endif // nsQueryFrame_h