1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_sd.hxx"
25 
26 #include "model/SlideSorterModel.hxx"
27 #include "model/SlsPageDescriptor.hxx"
28 
29 using namespace ::sd::slidesorter;
30 using namespace ::sd::slidesorter::model;
31 
32 namespace {
33 
34 class PageEnumerationImpl
35     : public Enumeration<SharedPageDescriptor>
36 {
37 public:
38     inline PageEnumerationImpl (
39         const SlideSorterModel& rModel,
40         const PageEnumeration::PagePredicate& rPredicate);
41     virtual ~PageEnumerationImpl (void);
42     /** Create a copy of the called enumeration object.
43     */
44     virtual inline ::std::auto_ptr<Enumeration<SharedPageDescriptor> > Clone (void);
45 
46     virtual inline bool HasMoreElements (void) const;
47     virtual inline SharedPageDescriptor GetNextElement (void);
48     virtual inline void Rewind (void);
49 
50 private:
51     const SlideSorterModel& mrModel;
52     const PageEnumeration::PagePredicate maPredicate;
53     int mnIndex;
54 
55     /** This constructor sets the internal page index to the given value.
56         It does not call AdvanceToNextValidElement() to skip elements that
57         do not fullfill Predicate.
58     */
59     inline PageEnumerationImpl (
60         const SlideSorterModel& rModel,
61         const PageEnumeration::PagePredicate& rPredicate,
62         int nIndex);
63 
64     /** Skip all elements that do not fullfill Predicate starting with the
65         one pointed to by mnIndex.
66     */
67     inline void AdvanceToNextValidElement (void);
68 
69     // Default constructor not implemented.
70     PageEnumerationImpl (void);
71     // Assignment operator not implemented.
72     PageEnumerationImpl& operator= (const PageEnumerationImpl&);
73 };
74 
75 } // end of anonymouse namespace
76 
77 
78 
79 
80 namespace sd { namespace slidesorter { namespace model {
81 
82 
Create(const SlideSorterModel & rModel,const PagePredicate & rPredicate)83 PageEnumeration PageEnumeration::Create (
84     const SlideSorterModel& rModel,
85     const PagePredicate& rPredicate)
86 {
87     return PageEnumeration(::std::auto_ptr<Enumeration<SharedPageDescriptor> >(
88         new PageEnumerationImpl(rModel, rPredicate)));
89 }
90 
91 
92 
93 
PageEnumeration(::std::auto_ptr<Enumeration<SharedPageDescriptor>> pImpl)94 PageEnumeration::PageEnumeration (
95     ::std::auto_ptr<Enumeration<SharedPageDescriptor> > pImpl)
96     : mpImpl(pImpl)
97 {
98 }
99 
100 
101 
102 
PageEnumeration(PageEnumeration & rEnumeration,bool bCloneImpl)103 PageEnumeration::PageEnumeration (
104     PageEnumeration& rEnumeration,
105     bool bCloneImpl)
106 {
107 
108 	if( bCloneImpl )
109 	{
110 		mpImpl = rEnumeration.mpImpl->Clone();
111 	}
112 	else
113 	{
114 		mpImpl = rEnumeration.mpImpl;
115 	}
116 }
117 
118 
119 
120 
PageEnumeration(const PageEnumeration & rEnumeration)121 PageEnumeration::PageEnumeration (const PageEnumeration& rEnumeration )
122 : sd::slidesorter::model::Enumeration<sd::slidesorter::model::SharedPageDescriptor>()
123 {
124 	mpImpl = rEnumeration.mpImpl->Clone();
125 }
126 
127 
128 
129 
~PageEnumeration(void)130 PageEnumeration::~PageEnumeration (void)
131 {
132 }
133 
134 
135 
136 
operator =(const PageEnumeration & rEnumeration)137 PageEnumeration& PageEnumeration::operator= (
138     const PageEnumeration& rEnumeration)
139 {
140     mpImpl = rEnumeration.mpImpl->Clone();
141     return *this;
142 }
143 
144 
145 
146 
Clone(void)147 ::std::auto_ptr<Enumeration<SharedPageDescriptor> > PageEnumeration::Clone (void)
148 {
149     return ::std::auto_ptr<Enumeration<SharedPageDescriptor> >(
150         new PageEnumeration (*this, true));
151 }
152 
153 
154 
155 
HasMoreElements(void) const156 bool PageEnumeration::HasMoreElements (void) const
157 {
158     return mpImpl->HasMoreElements();
159 }
160 
161 
162 
GetNextElement(void)163 SharedPageDescriptor PageEnumeration::GetNextElement (void)
164 {
165     return mpImpl->GetNextElement();
166 }
167 
168 
169 
170 
Rewind(void)171 void PageEnumeration::Rewind (void)
172 {
173     return mpImpl->Rewind();
174 }
175 
176 } } } // end of namespace ::sd::slidesorter::model
177 
178 
179 
180 
181 namespace {
182 
PageEnumerationImpl(const SlideSorterModel & rModel,const PageEnumeration::PagePredicate & rPredicate)183 PageEnumerationImpl::PageEnumerationImpl (
184     const SlideSorterModel& rModel,
185     const PageEnumeration::PagePredicate& rPredicate)
186     : mrModel(rModel),
187       maPredicate(rPredicate),
188       mnIndex(0)
189 {
190     Rewind();
191 }
192 
193 
194 
195 
PageEnumerationImpl(const SlideSorterModel & rModel,const PageEnumeration::PagePredicate & rPredicate,int nIndex)196 PageEnumerationImpl::PageEnumerationImpl (
197     const SlideSorterModel& rModel,
198     const PageEnumeration::PagePredicate& rPredicate,
199     int nIndex)
200     : mrModel(rModel),
201       maPredicate(rPredicate),
202       mnIndex(nIndex)
203 {
204 }
205 
206 
207 
208 
~PageEnumerationImpl(void)209 PageEnumerationImpl::~PageEnumerationImpl (void)
210 {
211 }
212 
213 
214 
215 
216 ::std::auto_ptr<Enumeration<SharedPageDescriptor> >
Clone(void)217     PageEnumerationImpl::Clone (void)
218 {
219     return ::std::auto_ptr<Enumeration<SharedPageDescriptor> >(
220         new PageEnumerationImpl(mrModel,maPredicate,mnIndex));
221 }
222 
223 
224 
225 
HasMoreElements(void) const226 bool PageEnumerationImpl::HasMoreElements (void) const
227 {
228     return (mnIndex < mrModel.GetPageCount());
229 }
230 
231 
232 
233 
GetNextElement(void)234 SharedPageDescriptor PageEnumerationImpl::GetNextElement (void)
235 {
236     SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(mnIndex));
237 
238     // Go to the following valid element.
239     mnIndex += 1;
240     AdvanceToNextValidElement();
241 
242     return pDescriptor;
243 }
244 
245 
246 
247 
Rewind(void)248 void PageEnumerationImpl::Rewind (void)
249 {
250     // Go to first valid element.
251     mnIndex = 0;
252     AdvanceToNextValidElement();
253 }
254 
255 
256 
257 
258 
AdvanceToNextValidElement(void)259 void PageEnumerationImpl::AdvanceToNextValidElement (void)
260 {
261     while (mnIndex < mrModel.GetPageCount())
262     {
263         SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(mnIndex));
264 
265         // Test for the predicate being fullfilled.
266         if (pDescriptor.get()!=NULL && maPredicate(pDescriptor))
267         {
268             // This predicate is valid.
269             break;
270         }
271         else
272         {
273             // Advance to next predicate.
274             mnIndex += 1;
275         }
276     }
277 }
278 
279 } // end of anonymous namespace
280 
281