accessible/src/base/Relation.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/accessible/src/base/Relation.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_a11y_relation_h_
    1.11 +#define mozilla_a11y_relation_h_
    1.12 +
    1.13 +#include "AccIterator.h"
    1.14 +
    1.15 +#include "mozilla/Move.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +namespace a11y {
    1.19 +
    1.20 +/**
    1.21 + * A collection of relation targets of a certain type.  Targets are computed
    1.22 + * lazily while enumerating.
    1.23 + */
    1.24 +class Relation
    1.25 +{
    1.26 +public:
    1.27 +  Relation() : mFirstIter(nullptr), mLastIter(nullptr) { }
    1.28 +
    1.29 +  Relation(AccIterable* aIter) :
    1.30 +    mFirstIter(aIter), mLastIter(aIter) { }
    1.31 +
    1.32 +  Relation(Accessible* aAcc) :
    1.33 +    mFirstIter(nullptr), mLastIter(nullptr)
    1.34 +    { AppendTarget(aAcc); }
    1.35 +
    1.36 +  Relation(DocAccessible* aDocument, nsIContent* aContent) :
    1.37 +    mFirstIter(nullptr), mLastIter(nullptr)
    1.38 +    { AppendTarget(aDocument, aContent); }
    1.39 +
    1.40 +  Relation(Relation&& aOther) :
    1.41 +    mFirstIter(Move(aOther.mFirstIter)), mLastIter(aOther.mLastIter)
    1.42 +  {
    1.43 +    aOther.mLastIter = nullptr;
    1.44 +  }
    1.45 +
    1.46 +  Relation& operator = (Relation&& aRH)
    1.47 +  {
    1.48 +    mFirstIter = Move(aRH.mFirstIter);
    1.49 +    mLastIter = aRH.mLastIter;
    1.50 +    aRH.mLastIter = nullptr;
    1.51 +    return *this;
    1.52 +  }
    1.53 +
    1.54 +  inline void AppendIter(AccIterable* aIter)
    1.55 +  {
    1.56 +    if (mLastIter)
    1.57 +      mLastIter->mNextIter = aIter;
    1.58 +    else
    1.59 +      mFirstIter = aIter;
    1.60 +
    1.61 +    mLastIter = aIter;
    1.62 +  }
    1.63 +
    1.64 +  /**
    1.65 +   * Append the given accessible to the set of related accessibles.
    1.66 +   */
    1.67 +  inline void AppendTarget(Accessible* aAcc)
    1.68 +  {
    1.69 +    if (aAcc)
    1.70 +      AppendIter(new SingleAccIterator(aAcc));
    1.71 +  }
    1.72 +
    1.73 +  /**
    1.74 +   * Append the one accessible for this content node to the set of related
    1.75 +   * accessibles.
    1.76 +   */
    1.77 +  void AppendTarget(DocAccessible* aDocument, nsIContent* aContent)
    1.78 +  {
    1.79 +    if (aContent)
    1.80 +      AppendTarget(aDocument->GetAccessible(aContent));
    1.81 +  }
    1.82 +
    1.83 +  /**
    1.84 +   * compute and return the next related accessible.
    1.85 +   */
    1.86 +  inline Accessible* Next()
    1.87 +  {
    1.88 +    Accessible* target = nullptr;
    1.89 +
    1.90 +    // a trick nsAutoPtr deletes what it used to point to when assigned to
    1.91 +    while (mFirstIter && !(target = mFirstIter->Next()))
    1.92 +      mFirstIter = mFirstIter->mNextIter;
    1.93 +
    1.94 +    if (!mFirstIter)
    1.95 +      mLastIter = nullptr;
    1.96 +
    1.97 +    return target;
    1.98 +  }
    1.99 +
   1.100 +private:
   1.101 +  Relation& operator = (const Relation&) MOZ_DELETE;
   1.102 +  Relation(const Relation&) MOZ_DELETE;
   1.103 +
   1.104 +  nsAutoPtr<AccIterable> mFirstIter;
   1.105 +  AccIterable* mLastIter;
   1.106 +};
   1.107 +
   1.108 +} // namespace a11y
   1.109 +} // namespace mozilla
   1.110 +
   1.111 +#endif
   1.112 +

mercurial