ServoUtils.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* some utilities for stylo */
  6. #ifndef mozilla_ServoUtils_h
  7. #define mozilla_ServoUtils_h
  8. #include "mozilla/TypeTraits.h"
  9. #ifdef MOZ_STYLO
  10. # define MOZ_DECL_STYLO_CHECK_METHODS \
  11. bool IsGecko() const { return !IsServo(); } \
  12. bool IsServo() const { return mType == StyleBackendType::Servo; }
  13. #else
  14. # define MOZ_DECL_STYLO_CHECK_METHODS \
  15. bool IsGecko() const { return true; } \
  16. bool IsServo() const { return false; }
  17. #endif
  18. /**
  19. * Macro used in a base class of |geckotype_| and |servotype_|.
  20. * The class should define |StyleBackendType mType;| itself.
  21. */
  22. #define MOZ_DECL_STYLO_METHODS(geckotype_, servotype_) \
  23. MOZ_DECL_STYLO_CHECK_METHODS \
  24. inline geckotype_* AsGecko(); \
  25. inline servotype_* AsServo(); \
  26. inline const geckotype_* AsGecko() const; \
  27. inline const servotype_* AsServo() const;
  28. /**
  29. * Macro used in inline header of class |type_| with its Gecko and Servo
  30. * subclasses named |geckotype_| and |servotype_| correspondingly for
  31. * implementing the inline methods defined by MOZ_DECL_STYLO_METHODS.
  32. */
  33. #define MOZ_DEFINE_STYLO_METHODS(type_, geckotype_, servotype_) \
  34. geckotype_* type_::AsGecko() { \
  35. MOZ_ASSERT(IsGecko()); \
  36. return static_cast<geckotype_*>(this); \
  37. } \
  38. servotype_* type_::AsServo() { \
  39. MOZ_ASSERT(IsServo()); \
  40. return static_cast<servotype_*>(this); \
  41. } \
  42. const geckotype_* type_::AsGecko() const { \
  43. MOZ_ASSERT(IsGecko()); \
  44. return static_cast<const geckotype_*>(this); \
  45. } \
  46. const servotype_* type_::AsServo() const { \
  47. MOZ_ASSERT(IsServo()); \
  48. return static_cast<const servotype_*>(this); \
  49. }
  50. #define MOZ_STYLO_THIS_TYPE mozilla::RemovePointer<decltype(this)>::Type
  51. #define MOZ_STYLO_GECKO_TYPE mozilla::RemovePointer<decltype(AsGecko())>::Type
  52. #define MOZ_STYLO_SERVO_TYPE mozilla::RemovePointer<decltype(AsServo())>::Type
  53. /**
  54. * Macro used to forward a method call to the concrete method defined by
  55. * the Servo or Gecko implementation. The class of the method using it
  56. * should use MOZ_DECL_STYLO_METHODS to define basic stylo methods.
  57. */
  58. #define MOZ_STYLO_FORWARD_CONCRETE(method_, geckoargs_, servoargs_) \
  59. static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_), \
  60. decltype(&MOZ_STYLO_GECKO_TYPE::method_)> \
  61. ::value, "Gecko subclass should define its own " #method_); \
  62. static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_), \
  63. decltype(&MOZ_STYLO_SERVO_TYPE::method_)> \
  64. ::value, "Servo subclass should define its own " #method_); \
  65. if (IsServo()) { \
  66. return AsServo()->method_ servoargs_; \
  67. } \
  68. return AsGecko()->method_ geckoargs_;
  69. #define MOZ_STYLO_FORWARD(method_, args_) \
  70. MOZ_STYLO_FORWARD_CONCRETE(method_, args_, args_)
  71. #endif // mozilla_ServoUtils_h