1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "precompiled_sd.hxx" 29 30 #include "model/SlideSorterModel.hxx" 31 #include "model/SlsPageDescriptor.hxx" 32 33 using namespace ::sd::slidesorter; 34 using namespace ::sd::slidesorter::model; 35 36 namespace { 37 38 class PageEnumerationImpl 39 : public Enumeration<SharedPageDescriptor> 40 { 41 public: 42 inline PageEnumerationImpl ( 43 const SlideSorterModel& rModel, 44 const PageEnumeration::PagePredicate& rPredicate); 45 virtual ~PageEnumerationImpl (void); 46 /** Create a copy of the called enumeration object. 47 */ 48 virtual inline ::std::auto_ptr<Enumeration<SharedPageDescriptor> > Clone (void); 49 50 virtual inline bool HasMoreElements (void) const; 51 virtual inline SharedPageDescriptor GetNextElement (void); 52 virtual inline void Rewind (void); 53 54 private: 55 const SlideSorterModel& mrModel; 56 const PageEnumeration::PagePredicate maPredicate; 57 int mnIndex; 58 59 /** This constructor sets the internal page index to the given value. 60 It does not call AdvanceToNextValidElement() to skip elements that 61 do not fullfill Predicate. 62 */ 63 inline PageEnumerationImpl ( 64 const SlideSorterModel& rModel, 65 const PageEnumeration::PagePredicate& rPredicate, 66 int nIndex); 67 68 /** Skip all elements that do not fullfill Predicate starting with the 69 one pointed to by mnIndex. 70 */ 71 inline void AdvanceToNextValidElement (void); 72 73 // Default constructor not implemented. 74 PageEnumerationImpl (void); 75 // Assignment operator not implemented. 76 PageEnumerationImpl& operator= (const PageEnumerationImpl&); 77 }; 78 79 } // end of anonymouse namespace 80 81 82 83 84 namespace sd { namespace slidesorter { namespace model { 85 86 87 PageEnumeration PageEnumeration::Create ( 88 const SlideSorterModel& rModel, 89 const PagePredicate& rPredicate) 90 { 91 return PageEnumeration(::std::auto_ptr<Enumeration<SharedPageDescriptor> >( 92 new PageEnumerationImpl(rModel, rPredicate))); 93 } 94 95 96 97 98 PageEnumeration::PageEnumeration ( 99 ::std::auto_ptr<Enumeration<SharedPageDescriptor> > pImpl) 100 : mpImpl(pImpl) 101 { 102 } 103 104 105 106 107 PageEnumeration::PageEnumeration ( 108 PageEnumeration& rEnumeration, 109 bool bCloneImpl) 110 { 111 112 if( bCloneImpl ) 113 { 114 mpImpl = rEnumeration.mpImpl->Clone(); 115 } 116 else 117 { 118 mpImpl = rEnumeration.mpImpl; 119 } 120 } 121 122 123 124 125 PageEnumeration::PageEnumeration (const PageEnumeration& rEnumeration ) 126 : sd::slidesorter::model::Enumeration<sd::slidesorter::model::SharedPageDescriptor>() 127 { 128 mpImpl = rEnumeration.mpImpl->Clone(); 129 } 130 131 132 133 134 PageEnumeration::~PageEnumeration (void) 135 { 136 } 137 138 139 140 141 PageEnumeration& PageEnumeration::operator= ( 142 const PageEnumeration& rEnumeration) 143 { 144 mpImpl = rEnumeration.mpImpl->Clone(); 145 return *this; 146 } 147 148 149 150 151 ::std::auto_ptr<Enumeration<SharedPageDescriptor> > PageEnumeration::Clone (void) 152 { 153 return ::std::auto_ptr<Enumeration<SharedPageDescriptor> >( 154 new PageEnumeration (*this, true)); 155 } 156 157 158 159 160 bool PageEnumeration::HasMoreElements (void) const 161 { 162 return mpImpl->HasMoreElements(); 163 } 164 165 166 167 SharedPageDescriptor PageEnumeration::GetNextElement (void) 168 { 169 return mpImpl->GetNextElement(); 170 } 171 172 173 174 175 void PageEnumeration::Rewind (void) 176 { 177 return mpImpl->Rewind(); 178 } 179 180 } } } // end of namespace ::sd::slidesorter::model 181 182 183 184 185 namespace { 186 187 PageEnumerationImpl::PageEnumerationImpl ( 188 const SlideSorterModel& rModel, 189 const PageEnumeration::PagePredicate& rPredicate) 190 : mrModel(rModel), 191 maPredicate(rPredicate), 192 mnIndex(0) 193 { 194 Rewind(); 195 } 196 197 198 199 200 PageEnumerationImpl::PageEnumerationImpl ( 201 const SlideSorterModel& rModel, 202 const PageEnumeration::PagePredicate& rPredicate, 203 int nIndex) 204 : mrModel(rModel), 205 maPredicate(rPredicate), 206 mnIndex(nIndex) 207 { 208 } 209 210 211 212 213 PageEnumerationImpl::~PageEnumerationImpl (void) 214 { 215 } 216 217 218 219 220 ::std::auto_ptr<Enumeration<SharedPageDescriptor> > 221 PageEnumerationImpl::Clone (void) 222 { 223 return ::std::auto_ptr<Enumeration<SharedPageDescriptor> >( 224 new PageEnumerationImpl(mrModel,maPredicate,mnIndex)); 225 } 226 227 228 229 230 bool PageEnumerationImpl::HasMoreElements (void) const 231 { 232 return (mnIndex < mrModel.GetPageCount()); 233 } 234 235 236 237 238 SharedPageDescriptor PageEnumerationImpl::GetNextElement (void) 239 { 240 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(mnIndex)); 241 242 // Go to the following valid element. 243 mnIndex += 1; 244 AdvanceToNextValidElement(); 245 246 return pDescriptor; 247 } 248 249 250 251 252 void PageEnumerationImpl::Rewind (void) 253 { 254 // Go to first valid element. 255 mnIndex = 0; 256 AdvanceToNextValidElement(); 257 } 258 259 260 261 262 263 void PageEnumerationImpl::AdvanceToNextValidElement (void) 264 { 265 while (mnIndex < mrModel.GetPageCount()) 266 { 267 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(mnIndex)); 268 269 // Test for the predicate being fullfilled. 270 if (pDescriptor.get()!=NULL && maPredicate(pDescriptor)) 271 { 272 // This predicate is valid. 273 break; 274 } 275 else 276 { 277 // Advance to next predicate. 278 mnIndex += 1; 279 } 280 } 281 } 282 283 } // end of anonymous namespace 284 285