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/. */
6 
7 /* some utilities for stylo */
8 
9 #ifndef mozilla_ServoUtils_h
10 #define mozilla_ServoUtils_h
11 
12 #include "mozilla/TypeTraits.h"
13 
14 #ifdef MOZ_STYLO
15 # define MOZ_DECL_STYLO_CHECK_METHODS \
16   bool IsGecko() const { return !IsServo(); } \
17   bool IsServo() const { return mType == StyleBackendType::Servo; }
18 #else
19 # define MOZ_DECL_STYLO_CHECK_METHODS \
20   bool IsGecko() const { return true; } \
21   bool IsServo() const { return false; }
22 #endif
23 
24 /**
25  * Macro used in a base class of |geckotype_| and |servotype_|.
26  * The class should define |StyleBackendType mType;| itself.
27  */
28 #define MOZ_DECL_STYLO_METHODS(geckotype_, servotype_)  \
29   MOZ_DECL_STYLO_CHECK_METHODS                          \
30   inline geckotype_* AsGecko();                         \
31   inline servotype_* AsServo();                         \
32   inline const geckotype_* AsGecko() const;             \
33   inline const servotype_* AsServo() const;
34 
35 /**
36  * Macro used in inline header of class |type_| with its Gecko and Servo
37  * subclasses named |geckotype_| and |servotype_| correspondingly for
38  * implementing the inline methods defined by MOZ_DECL_STYLO_METHODS.
39  */
40 #define MOZ_DEFINE_STYLO_METHODS(type_, geckotype_, servotype_) \
41   geckotype_* type_::AsGecko() {                                \
42     MOZ_ASSERT(IsGecko());                                      \
43     return static_cast<geckotype_*>(this);                      \
44   }                                                             \
45   servotype_* type_::AsServo() {                                \
46     MOZ_ASSERT(IsServo());                                      \
47     return static_cast<servotype_*>(this);                      \
48   }                                                             \
49   const geckotype_* type_::AsGecko() const {                    \
50     MOZ_ASSERT(IsGecko());                                      \
51     return static_cast<const geckotype_*>(this);                \
52   }                                                             \
53   const servotype_* type_::AsServo() const {                    \
54     MOZ_ASSERT(IsServo());                                      \
55     return static_cast<const servotype_*>(this);                \
56   }
57 
58 #define MOZ_STYLO_THIS_TYPE  mozilla::RemovePointer<decltype(this)>::Type
59 #define MOZ_STYLO_GECKO_TYPE mozilla::RemovePointer<decltype(AsGecko())>::Type
60 #define MOZ_STYLO_SERVO_TYPE mozilla::RemovePointer<decltype(AsServo())>::Type
61 
62 /**
63  * Macro used to forward a method call to the concrete method defined by
64  * the Servo or Gecko implementation. The class of the method using it
65  * should use MOZ_DECL_STYLO_METHODS to define basic stylo methods.
66  */
67 #define MOZ_STYLO_FORWARD_CONCRETE(method_, geckoargs_, servoargs_)         \
68   static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_),   \
69                                  decltype(&MOZ_STYLO_GECKO_TYPE::method_)>  \
70                 ::value, "Gecko subclass should define its own " #method_); \
71   static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_),   \
72                                  decltype(&MOZ_STYLO_SERVO_TYPE::method_)>  \
73                 ::value, "Servo subclass should define its own " #method_); \
74   if (IsServo()) {                                                          \
75     return AsServo()->method_ servoargs_;                                   \
76   }                                                                         \
77   return AsGecko()->method_ geckoargs_;
78 
79 #define MOZ_STYLO_FORWARD(method_, args_) \
80   MOZ_STYLO_FORWARD_CONCRETE(method_, args_, args_)
81 
82 #endif // mozilla_ServoUtils_h
83