1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2011
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Trevor Saunders <trev.saunders@gmail.com> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef RELATION_H_
41 : #define RELATION_H_
42 :
43 : #include "AccIterator.h"
44 :
45 : /**
46 : * This class is used to return Relation objects from functions. A copy
47 : * constructor doesn't work here because we need to mutate the old relation to
48 : * have its nsAutoPtr forget what it points to.
49 : */
50 : struct RelationCopyHelper
51 : {
52 0 : RelationCopyHelper(AccIterable* aFirstIter, AccIterable* aLastIter) :
53 0 : mFirstIter(aFirstIter), mLastIter(aLastIter) { }
54 :
55 : AccIterable* mFirstIter;
56 : AccIterable* mLastIter;
57 : };
58 :
59 : /**
60 : * A collection of relation targets of a certain type. Targets are computed
61 : * lazily while enumerating.
62 : */
63 : class Relation
64 0 : {
65 : public:
66 0 : Relation() : mFirstIter(nsnull), mLastIter(nsnull) { }
67 :
68 0 : Relation(const RelationCopyHelper aRelation) :
69 0 : mFirstIter(aRelation.mFirstIter), mLastIter(aRelation.mLastIter) { }
70 :
71 0 : Relation(AccIterable* aIter) : mFirstIter(aIter), mLastIter(aIter) { }
72 :
73 0 : Relation(nsAccessible* aAcc) :
74 0 : mFirstIter(nsnull), mLastIter(nsnull)
75 0 : { AppendTarget(aAcc); }
76 :
77 0 : Relation(nsIContent* aContent) :
78 0 : mFirstIter(nsnull), mLastIter(nsnull)
79 0 : { AppendTarget(aContent); }
80 :
81 : Relation& operator = (const RelationCopyHelper& aRH)
82 : {
83 : mFirstIter = aRH.mFirstIter;
84 : mLastIter = aRH.mLastIter;
85 : return *this;
86 : }
87 :
88 : Relation& operator = (Relation& aRelation)
89 : {
90 : mFirstIter = aRelation.mFirstIter;
91 : mLastIter = aRelation.mLastIter;
92 : return *this;
93 : }
94 :
95 0 : operator RelationCopyHelper()
96 : {
97 0 : return RelationCopyHelper(mFirstIter.forget(), mLastIter);
98 : }
99 :
100 0 : inline void AppendIter(AccIterable* aIter)
101 : {
102 0 : if (mLastIter)
103 0 : mLastIter->mNextIter = aIter;
104 : else
105 0 : mFirstIter = aIter;
106 :
107 0 : mLastIter = aIter;
108 0 : }
109 :
110 : /**
111 : * Append the given accessible to the set of related accessibles.
112 : */
113 0 : inline void AppendTarget(nsAccessible* aAcc)
114 : {
115 0 : if (aAcc)
116 0 : AppendIter(new SingleAccIterator(aAcc));
117 0 : }
118 :
119 : /**
120 : * Append the one accessible for this content node to the set of related
121 : * accessibles.
122 : */
123 0 : inline void AppendTarget(nsIContent* aContent)
124 : {
125 0 : if (aContent)
126 0 : AppendTarget(GetAccService()->GetAccessible(aContent, nsnull));
127 0 : }
128 :
129 : /**
130 : * compute and return the next related accessible.
131 : */
132 0 : inline nsAccessible* Next()
133 : {
134 0 : nsAccessible* target = nsnull;
135 :
136 : // a trick nsAutoPtr deletes what it used to point to when assigned to
137 0 : while (mFirstIter && !(target = mFirstIter->Next()))
138 0 : mFirstIter = mFirstIter->mNextIter;
139 :
140 0 : if (!mFirstIter)
141 0 : mLastIter = nsnull;
142 :
143 0 : return target;
144 : }
145 :
146 : private:
147 : Relation& operator = (const Relation&);
148 :
149 : nsAutoPtr<AccIterable> mFirstIter;
150 : AccIterable* mLastIter;
151 : };
152 :
153 : #endif
154 :
|