1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsRequestQueue.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "precompiled_sd.hxx"
36 #include "SlsRequestQueue.hxx"
44 namespace sd
{ namespace slidesorter
{ namespace cache
{
46 /** This class extends the actual request data with additional information
47 that is used by the priority queues.
53 CacheKey aKey
, sal_Int32 nPriority
, RequestPriorityClass eClass
)
54 : maKey(aKey
), mnPriorityInClass(nPriority
), meClass(eClass
)
56 /** Sort requests according to priority classes and then to priorities.
58 class Comparator
{ public:
59 bool operator() (const Request
& rRequest1
, const Request
& rRequest2
)
61 if (rRequest1
.meClass
== rRequest2
.meClass
)
62 return (rRequest1
.mnPriorityInClass
> rRequest2
.mnPriorityInClass
);
64 return (rRequest1
.meClass
< rRequest2
.meClass
);
67 /** Request data is compared arbitrarily by their addresses in memory.
68 This just establishes an order so that the STL containers are happy.
69 The order is not semantically interpreted.
71 class DataComparator
{ public:
72 DataComparator (const Request
&rRequest
):maKey(rRequest
.maKey
){}
73 DataComparator (const CacheKey aKey
):maKey(aKey
){}
74 bool operator() (const Request
& rRequest
) { return maKey
== rRequest
.maKey
; }
75 private: const CacheKey maKey
;
79 sal_Int32 mnPriorityInClass
;
80 RequestPriorityClass meClass
;
84 class RequestQueue::Container
94 //===== GenericRequestQueue =================================================
97 RequestQueue::RequestQueue (const SharedCacheContext
& rpCacheContext
)
99 mpRequestQueue(new Container()),
100 mpCacheContext(rpCacheContext
),
101 mnMinimumPriority(0),
109 RequestQueue::~RequestQueue (void)
116 void RequestQueue::AddRequest (
118 RequestPriorityClass eRequestClass
,
119 bool /*bInsertWithHighestPriority*/)
121 ::osl::MutexGuard
aGuard (maMutex
);
123 OSL_ASSERT(eRequestClass
>=MIN__CLASS
&& eRequestClass
<=MAX__CLASS
);
125 // If the request is already a member of the queue then remove it so
126 // that the following insertion will use the new prioritization.
132 // The priority of the request inside its priority class is defined by
133 // the page number. This ensures a strict top-to-bottom, left-to-right
135 sal_Int32
nPriority (mpCacheContext
->GetPriority(aKey
));
136 Request
aRequest (aKey
, nPriority
, eRequestClass
);
137 mpRequestQueue
->insert(aRequest
);
139 SSCD_SET_REQUEST_CLASS(rRequestData
.GetPage(),eRequestClass
);
142 OSL_TRACE("%s request for page %d with priority class %d",
143 bRemoved
?"replaced":"added",
144 (rRequestData
.GetPage()->GetPageNum()-1)/2,
152 bool RequestQueue::RemoveRequest (
155 bool bRequestWasRemoved (false);
156 ::osl::MutexGuard
aGuard (maMutex
);
160 Container::const_iterator aRequestIterator
= ::std::find_if (
161 mpRequestQueue
->begin(),
162 mpRequestQueue
->end(),
163 Request::DataComparator(aKey
));
164 if (aRequestIterator
!= mpRequestQueue
->end())
166 if (aRequestIterator
->mnPriorityInClass
== mnMinimumPriority
+1)
168 else if (aRequestIterator
->mnPriorityInClass
== mnMaximumPriority
-1)
170 mpRequestQueue
->erase(aRequestIterator
);
171 bRequestWasRemoved
= true;
173 if (bRequestWasRemoved
)
175 SSCD_SET_STATUS(rRequest
.GetPage(),NONE
);
182 return bRequestWasRemoved
;
188 void RequestQueue::ChangeClass (
190 RequestPriorityClass eNewRequestClass
)
192 ::osl::MutexGuard
aGuard (maMutex
);
194 OSL_ASSERT(eNewRequestClass
>=MIN__CLASS
&& eNewRequestClass
<=MAX__CLASS
);
196 Container::const_iterator
iRequest (
198 mpRequestQueue
->begin(),
199 mpRequestQueue
->end(),
200 Request::DataComparator(aKey
)));
201 if (iRequest
!=mpRequestQueue
->end() && iRequest
->meClass
!=eNewRequestClass
)
203 AddRequest(aKey
, eNewRequestClass
, true);
204 SSCD_SET_REQUEST_CLASS(rRequestData
.GetPage(),eNewRequestClass
);
211 CacheKey
RequestQueue::GetFront (void)
213 ::osl::MutexGuard
aGuard (maMutex
);
215 if (mpRequestQueue
->empty())
216 throw ::com::sun::star::uno::RuntimeException(
217 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
218 "RequestQueue::GetFront(): queue is empty")),
221 return mpRequestQueue
->begin()->maKey
;
227 RequestPriorityClass
RequestQueue::GetFrontPriorityClass (void)
229 ::osl::MutexGuard
aGuard (maMutex
);
231 if (mpRequestQueue
->empty())
232 throw ::com::sun::star::uno::RuntimeException(
233 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
234 "RequestQueue::GetFrontPriorityClass(): queue is empty")),
237 return mpRequestQueue
->begin()->meClass
;
243 void RequestQueue::PopFront (void)
245 ::osl::MutexGuard
aGuard (maMutex
);
247 if ( ! mpRequestQueue
->empty())
249 SSCD_SET_STATUS(maRequestQueue
.begin()->mpData
->GetPage(),NONE
);
251 mpRequestQueue
->erase(mpRequestQueue
->begin());
253 // Reset the priority counter if possible.
254 if (mpRequestQueue
->empty())
256 mnMinimumPriority
= 0;
257 mnMaximumPriority
= 1;
265 bool RequestQueue::IsEmpty (void)
267 ::osl::MutexGuard
aGuard (maMutex
);
268 return mpRequestQueue
->empty();
274 void RequestQueue::Clear (void)
276 ::osl::MutexGuard
aGuard (maMutex
);
278 mpRequestQueue
->clear();
279 mnMinimumPriority
= 0;
280 mnMaximumPriority
= 1;
286 ::osl::Mutex
& RequestQueue::GetMutex (void)
292 } } } // end of namespace ::sd::slidesorter::cache