AccessibleOrProxy.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*- Mode: C++; tab-width: 2; 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. #ifndef mozilla_a11y_AccessibleOrProxy_h
  6. #define mozilla_a11y_AccessibleOrProxy_h
  7. #include "mozilla/a11y/Accessible.h"
  8. #include "mozilla/a11y/ProxyAccessible.h"
  9. #include "mozilla/a11y/Role.h"
  10. #include <stdint.h>
  11. namespace mozilla {
  12. namespace a11y {
  13. /**
  14. * This class stores an Accessible* or a ProxyAccessible* in a safe manner
  15. * with size sizeof(void*).
  16. */
  17. class AccessibleOrProxy
  18. {
  19. public:
  20. MOZ_IMPLICIT AccessibleOrProxy(Accessible* aAcc) :
  21. mBits(reinterpret_cast<uintptr_t>(aAcc)) {}
  22. MOZ_IMPLICIT AccessibleOrProxy(ProxyAccessible* aProxy) :
  23. mBits(aProxy ? (reinterpret_cast<uintptr_t>(aProxy) | IS_PROXY) : 0) {}
  24. MOZ_IMPLICIT AccessibleOrProxy(decltype(nullptr)) : mBits(0) {}
  25. bool IsProxy() const { return mBits & IS_PROXY; }
  26. ProxyAccessible* AsProxy() const
  27. {
  28. if (IsProxy()) {
  29. return reinterpret_cast<ProxyAccessible*>(mBits & ~IS_PROXY);
  30. }
  31. return nullptr;
  32. }
  33. bool IsAccessible() const { return !IsProxy(); }
  34. Accessible* AsAccessible() const
  35. {
  36. if (IsAccessible()) {
  37. return reinterpret_cast<Accessible*>(mBits);
  38. }
  39. return nullptr;
  40. }
  41. bool IsNull() const { return mBits == 0; }
  42. uint32_t ChildCount() const
  43. {
  44. if (IsProxy()) {
  45. return AsProxy()->ChildrenCount();
  46. }
  47. return AsAccessible()->ChildCount();
  48. }
  49. /**
  50. * Return the child object either an accessible or a proxied accessible at
  51. * the given index.
  52. */
  53. AccessibleOrProxy ChildAt(uint32_t aIdx)
  54. {
  55. if (IsProxy()) {
  56. return AsProxy()->ChildAt(aIdx);
  57. }
  58. return AsAccessible()->GetChildAt(aIdx);
  59. }
  60. /**
  61. * Return the first child object.
  62. */
  63. AccessibleOrProxy FirstChild()
  64. {
  65. if (IsProxy()) {
  66. return AsProxy()->FirstChild();
  67. }
  68. return AsAccessible()->FirstChild();
  69. }
  70. /**
  71. * Return the first child object.
  72. */
  73. AccessibleOrProxy LastChild()
  74. {
  75. if (IsProxy()) {
  76. return AsProxy()->LastChild();
  77. }
  78. return AsAccessible()->LastChild();
  79. }
  80. role Role() const
  81. {
  82. if (IsProxy()) {
  83. return AsProxy()->Role();
  84. }
  85. return AsAccessible()->Role();
  86. }
  87. AccessibleOrProxy Parent() const;
  88. // XXX these are implementation details that ideally would not be exposed.
  89. uintptr_t Bits() const { return mBits; }
  90. void SetBits(uintptr_t aBits) { mBits = aBits; }
  91. private:
  92. uintptr_t mBits;
  93. static const uintptr_t IS_PROXY = 0x1;
  94. };
  95. }
  96. }
  97. #endif