2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-01-12 22:20:59 +03:00
|
|
|
|
|
|
|
#ifndef nsQueryFrame_h
|
|
|
|
#define nsQueryFrame_h
|
|
|
|
|
|
|
|
#include "nscore.h"
|
2013-04-04 03:35:07 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
|
|
|
// NOTE: the long lines in this file are intentional to make compiler error
|
|
|
|
// messages more readable.
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_DECL_QUERYFRAME_TARGET(classname) \
|
2013-04-04 03:35:07 +04:00
|
|
|
static const nsQueryFrame::FrameIID kFrameIID = nsQueryFrame::classname##_id; \
|
|
|
|
typedef classname Has_NS_DECL_QUERYFRAME_TARGET;
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_DECL_QUERYFRAME \
|
2017-01-18 03:27:02 +03:00
|
|
|
void* QueryFrame(FrameIID id) override;
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_QUERYFRAME_HEAD(class) \
|
|
|
|
void* class::QueryFrame(FrameIID id) { switch (id) {
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_QUERYFRAME_ENTRY(class) \
|
2013-04-04 03:35:07 +04:00
|
|
|
case class::kFrameIID: { \
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(mozilla::IsSame<class, class::Has_NS_DECL_QUERYFRAME_TARGET>::value, \
|
|
|
|
#class " must declare itself as a queryframe target"); \
|
2013-04-04 03:35:07 +04:00
|
|
|
return static_cast<class*>(this); \
|
|
|
|
}
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_QUERYFRAME_ENTRY_CONDITIONAL(class, condition) \
|
|
|
|
case class::kFrameIID: \
|
2013-04-04 03:35:07 +04:00
|
|
|
if (condition) { \
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(mozilla::IsSame<class, class::Has_NS_DECL_QUERYFRAME_TARGET>::value, \
|
|
|
|
#class " must declare itself as a queryframe target"); \
|
2013-04-04 03:35:07 +04:00
|
|
|
return static_cast<class*>(this); \
|
|
|
|
} \
|
2009-09-12 20:49:24 +04:00
|
|
|
break;
|
2009-01-12 22:20:59 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
#define NS_QUERYFRAME_TAIL_INHERITING(class) \
|
|
|
|
default: break; \
|
|
|
|
} \
|
|
|
|
return class::QueryFrame(id); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_QUERYFRAME_TAIL_INHERITANCE_ROOT \
|
|
|
|
default: break; \
|
|
|
|
} \
|
2013-04-04 03:35:07 +04:00
|
|
|
MOZ_ASSERT(id != GetFrameId(), \
|
|
|
|
"A frame failed to QueryFrame to its *own type*. " \
|
|
|
|
"It may be missing NS_DECL_QUERYFRAME, or a " \
|
|
|
|
"NS_QUERYFRAME_ENTRY() line with its own type name"); \
|
|
|
|
return nullptr; \
|
2009-09-12 20:49:24 +04:00
|
|
|
}
|
2009-01-12 22:20:59 +03:00
|
|
|
|
|
|
|
class nsQueryFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum FrameIID {
|
2017-05-26 13:11:11 +03:00
|
|
|
#define FRAME_ID(classname, ...) classname##_id,
|
|
|
|
#define ABSTRACT_FRAME_ID(classname) classname##_id,
|
2012-06-05 19:10:28 +04:00
|
|
|
#include "nsFrameIdList.h"
|
|
|
|
#undef FRAME_ID
|
2017-05-26 13:11:11 +03:00
|
|
|
#undef ABSTRACT_FRAME_ID
|
2009-09-16 02:00:04 +04:00
|
|
|
|
2017-05-24 16:41:41 +03:00
|
|
|
// This marker allows mozilla::ArenaObjectID to "extend" this enum
|
|
|
|
// with additional sequential values for use in nsPresArena and
|
|
|
|
// nsIPresShell::{Allocate,Free}ByObjectId
|
|
|
|
NON_FRAME_MARKER
|
2009-01-12 22:20:59 +03:00
|
|
|
};
|
|
|
|
|
2017-05-26 13:11:11 +03:00
|
|
|
// A strict subset of FrameIID above for frame classes that we instantiate.
|
|
|
|
enum class ClassID : uint8_t {
|
|
|
|
#define FRAME_ID(classname, ...) classname##_id,
|
|
|
|
#define ABSTRACT_FRAME_ID(classname)
|
|
|
|
#include "nsFrameIdList.h"
|
|
|
|
#undef FRAME_ID
|
|
|
|
#undef ABSTRACT_FRAME_ID
|
|
|
|
};
|
|
|
|
|
2009-01-12 22:20:59 +03:00
|
|
|
virtual void* QueryFrame(FrameIID id) = 0;
|
|
|
|
};
|
|
|
|
|
2017-09-10 01:50:16 +03:00
|
|
|
class nsIFrame;
|
|
|
|
|
|
|
|
template<class Source>
|
|
|
|
class do_QueryFrameHelper
|
2009-01-12 22:20:59 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-09-10 01:50:16 +03:00
|
|
|
explicit do_QueryFrameHelper(Source* s) : mRawPtr(s) { }
|
2014-08-08 03:48:38 +04:00
|
|
|
|
|
|
|
// The return and argument types here are arbitrarily selected so no
|
|
|
|
// corresponding member function exists.
|
2017-09-10 01:50:16 +03:00
|
|
|
typedef void (do_QueryFrameHelper::* MatchNullptr)(double, float);
|
2014-08-08 03:48:38 +04:00
|
|
|
// Implicit constructor for nullptr, trick borrowed from already_AddRefed.
|
2017-09-10 01:50:16 +03:00
|
|
|
MOZ_IMPLICIT do_QueryFrameHelper(MatchNullptr aRawPtr) : mRawPtr(nullptr) {}
|
2009-01-12 22:20:59 +03:00
|
|
|
|
|
|
|
template<class Dest>
|
|
|
|
operator Dest*() {
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(mozilla::IsSame<Dest, typename Dest::Has_NS_DECL_QUERYFRAME_TARGET>::value,
|
|
|
|
"Dest must declare itself as a queryframe target");
|
2017-09-10 01:50:16 +03:00
|
|
|
if (!mRawPtr) {
|
2013-04-01 21:10:37 +04:00
|
|
|
return nullptr;
|
2017-09-10 01:50:16 +03:00
|
|
|
}
|
|
|
|
if (Dest* f = FastQueryFrame<Source, Dest>::QueryFrame(mRawPtr)) {
|
|
|
|
MOZ_ASSERT(f ==
|
|
|
|
reinterpret_cast<Dest*>(mRawPtr->QueryFrame(Dest::kFrameIID)),
|
|
|
|
"fast and slow paths should give the same result");
|
|
|
|
return f;
|
|
|
|
}
|
2009-01-12 22:20:59 +03:00
|
|
|
return reinterpret_cast<Dest*>(mRawPtr->QueryFrame(Dest::kFrameIID));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-10 01:50:16 +03:00
|
|
|
// For non-nsIFrame types there is no fast-path.
|
|
|
|
template<class Src, class Dst, typename = void, typename = void>
|
|
|
|
struct FastQueryFrame
|
|
|
|
{
|
|
|
|
static Dst* QueryFrame(Src* aFrame) { return nullptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for any nsIFrame type to any nsIFrame type -- if the source
|
|
|
|
// instance's mClass matches kFrameIID of the destination type then
|
|
|
|
// downcasting is safe.
|
|
|
|
template<class Src, class Dst>
|
|
|
|
struct FastQueryFrame<Src, Dst,
|
2017-09-10 03:10:03 +03:00
|
|
|
typename mozilla::EnableIf<mozilla::IsBaseOf<nsIFrame, Src>::value>::Type,
|
2017-09-10 01:50:16 +03:00
|
|
|
typename mozilla::EnableIf<mozilla::IsBaseOf<nsIFrame, Dst>::value>::Type>
|
|
|
|
{
|
|
|
|
static Dst* QueryFrame(Src* aFrame) {
|
|
|
|
return nsQueryFrame::FrameIID(aFrame->mClass) == Dst::kFrameIID ?
|
|
|
|
reinterpret_cast<Dst*>(aFrame) : nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Source* mRawPtr;
|
2009-01-12 22:20:59 +03:00
|
|
|
};
|
|
|
|
|
2017-09-10 01:50:16 +03:00
|
|
|
template<class T>
|
|
|
|
inline do_QueryFrameHelper<T>
|
|
|
|
do_QueryFrame(T* s)
|
|
|
|
{
|
|
|
|
return do_QueryFrameHelper<T>(s);
|
|
|
|
}
|
|
|
|
|
2009-01-12 22:20:59 +03:00
|
|
|
#endif // nsQueryFrame_h
|