fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / inc / regexpmap.hxx
blobd1c68391d873b9512573537041ef79c2cd001d21
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
21 #define INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
23 #include "sal/config.h"
25 #include <list>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
30 #include "regexp.hxx"
32 namespace ucb_impl {
34 template< typename Val > class RegexpMap;
35 template< typename Val > class RegexpMapIter;
38 template< typename Val >
39 class RegexpMapEntry
41 public:
42 inline RegexpMapEntry(OUString const & rTheRegexp,
43 Val * pTheValue):
44 m_aRegexp(rTheRegexp), m_pValue(pTheValue) {}
46 OUString getRegexp() const { return m_aRegexp; }
48 Val const & getValue() const { return *m_pValue; }
50 Val & getValue() { return *m_pValue; }
52 private:
53 OUString m_aRegexp;
54 Val * m_pValue;
59 template< typename Val >
60 struct Entry
62 Regexp m_aRegexp;
63 Val m_aValue;
65 inline Entry(Regexp const & rTheRegexp, Val const & rTheValue):
66 m_aRegexp(rTheRegexp), m_aValue(rTheValue) {}
70 template< typename Val > class List: public std::list< Entry< Val > > {};
73 template< typename Val >
74 struct RegexpMapImpl
76 List< Val > m_aList[Regexp::KIND_DOMAIN + 1];
77 Entry< Val > * m_pDefault;
79 RegexpMapImpl(): m_pDefault(0) {}
81 ~RegexpMapImpl() { delete m_pDefault; }
85 template< typename Val >
86 class RegexpMapIterImpl
88 public:
89 typedef RegexpMapImpl< Val > MapImpl;
90 typedef typename List< Val >::iterator ListIterator;
92 // Solaris needs these for the ctor...
94 inline RegexpMapIterImpl();
96 inline RegexpMapIterImpl(MapImpl * pTheMap, int nTheList,
97 ListIterator aTheIndex);
99 RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap, bool bBegin);
101 RegexpMapIterImpl(RegexpMapIterImpl const & rOther);
103 RegexpMapIterImpl & operator =(RegexpMapIterImpl const & rOther);
105 bool operator ==(RegexpMapIterImpl const & rOther) const;
107 RegexpMapImpl< Val > const * getMap() const { return m_pMap; }
109 int getList() const { return m_nList; }
111 typename List< Val >::iterator const & getIndex() const { return m_aIndex; }
113 void next();
115 RegexpMapEntry< Val > & get();
117 private:
118 mutable RegexpMapEntry< Val > m_aEntry;
119 typename List< Val >::iterator m_aIndex;
120 RegexpMapImpl< Val > * m_pMap;
121 int m_nList;
122 mutable bool m_bEntrySet;
124 void setEntry() const;
127 template< typename Val >
128 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl():
129 m_aEntry(rtl::OUString(), 0),
130 m_pMap(0),
131 m_nList(-1),
132 m_bEntrySet(false)
135 template< typename Val >
136 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl(MapImpl * pTheMap,
137 int nTheList,
138 ListIterator aTheIndex):
139 m_aEntry(rtl::OUString(), 0),
140 m_aIndex(aTheIndex),
141 m_pMap(pTheMap),
142 m_nList(nTheList),
143 m_bEntrySet(false)
146 template< typename Val >
147 void RegexpMapIterImpl< Val >::setEntry() const
149 if (!m_bEntrySet)
151 Entry< Val > const & rTheEntry
152 = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
153 m_aEntry
154 = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(false),
155 const_cast< Val * >(&rTheEntry.m_aValue));
156 m_bEntrySet = true;
160 template< typename Val >
161 RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap,
162 bool bBegin):
163 m_aEntry(rtl::OUString(), 0),
164 m_pMap(pTheMap),
165 m_bEntrySet(false)
167 if (bBegin)
169 m_nList = -1;
170 if (!m_pMap->m_pDefault)
171 next();
173 else
175 m_nList = Regexp::KIND_DOMAIN;
176 m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
180 template< typename Val >
181 RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapIterImpl const & rOther):
182 m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
183 m_bEntrySet(rOther.m_bEntrySet)
185 if (m_nList != -1)
186 m_aIndex = rOther.m_aIndex;
189 template< typename Val >
190 RegexpMapIterImpl< Val > & RegexpMapIterImpl< Val >::operator =(
191 RegexpMapIterImpl const & rOther)
193 if (this != &rOther)
195 m_aEntry = rOther.m_aEntry;
196 m_pMap = rOther.m_pMap;
197 m_nList = rOther.m_nList;
198 m_bEntrySet = rOther.m_bEntrySet;
199 if (m_nList == -1)
200 m_aIndex = typename List< Val >::iterator();
201 else
202 m_aIndex = rOther.m_aIndex;
204 return *this;
207 template< typename Val >
208 bool RegexpMapIterImpl< Val >::operator ==(RegexpMapIterImpl const & rOther)
209 const
211 return m_pMap == rOther.m_pMap
212 && m_nList == rOther.m_nList
213 && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
216 template< typename Val >
217 void RegexpMapIterImpl< Val >::next()
219 switch (m_nList)
221 case Regexp::KIND_DOMAIN:
222 if (m_aIndex == m_pMap->m_aList[m_nList].end())
223 return;
224 //fall-through
225 default:
226 ++m_aIndex;
227 if (m_nList == Regexp::KIND_DOMAIN
228 || m_aIndex != m_pMap->m_aList[m_nList].end())
229 break;
230 //fall-through
231 case -1:
234 ++m_nList;
235 m_aIndex = m_pMap->m_aList[m_nList].begin();
237 while (m_nList < Regexp::KIND_DOMAIN
238 && m_aIndex == m_pMap->m_aList[m_nList].end());
239 break;
241 m_bEntrySet = false;
244 template< typename Val >
245 RegexpMapEntry< Val > & RegexpMapIterImpl< Val >::get()
247 setEntry();
248 return m_aEntry;
252 template< typename Val >
253 class RegexpMapConstIter
255 friend class RegexpMap< Val >; // to access m_pImpl, ctor
256 friend class RegexpMapIter< Val >; // to access m_pImpl, ctor
258 public:
259 RegexpMapConstIter();
261 RegexpMapConstIter(RegexpMapConstIter const & rOther);
263 ~RegexpMapConstIter();
265 RegexpMapConstIter & operator =(RegexpMapConstIter const & rOther);
267 RegexpMapConstIter & operator ++();
269 RegexpMapConstIter operator ++(int);
271 RegexpMapEntry< Val > const & operator *() const;
273 RegexpMapEntry< Val > const * operator ->() const;
275 bool equals(RegexpMapConstIter const & rOther) const;
276 // for free operator ==(), operator !=()
278 private:
279 RegexpMapIterImpl< Val > * m_pImpl;
281 RegexpMapConstIter(RegexpMapIterImpl< Val > * pTheImpl);
284 template< typename Val >
285 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapIterImpl< Val > *
286 pTheImpl):
287 m_pImpl(pTheImpl)
290 template< typename Val >
291 RegexpMapConstIter< Val >::RegexpMapConstIter():
292 m_pImpl(new RegexpMapIterImpl< Val >)
295 template< typename Val >
296 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
297 rOther):
298 m_pImpl(new RegexpMapIterImpl< Val >(*rOther.m_pImpl))
301 template< typename Val >
302 RegexpMapConstIter< Val >::~RegexpMapConstIter()
304 delete m_pImpl;
307 template< typename Val >
308 RegexpMapConstIter< Val > &
309 RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
311 *m_pImpl = *rOther.m_pImpl;
312 return *this;
315 template< typename Val >
316 RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
318 m_pImpl->next();
319 return *this;
322 template< typename Val >
323 RegexpMapConstIter< Val > RegexpMapConstIter< Val >::operator ++(int)
325 RegexpMapConstIter aTemp(*this);
326 m_pImpl->next();
327 return aTemp;
330 template< typename Val >
331 RegexpMapEntry< Val > const & RegexpMapConstIter< Val >::operator *() const
333 return m_pImpl->get();
336 template< typename Val >
337 RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
339 return &m_pImpl->get();
342 template< typename Val >
343 bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
344 const
346 return *m_pImpl == *rOther.m_pImpl;
350 template< typename Val >
351 class RegexpMapIter: public RegexpMapConstIter< Val >
353 friend class RegexpMap< Val >; // to access ctor
355 public:
356 RegexpMapIter() {}
358 RegexpMapIter & operator ++();
360 RegexpMapIter operator ++(int);
362 RegexpMapEntry< Val > & operator *();
364 RegexpMapEntry< Val > const & operator *() const;
366 RegexpMapEntry< Val > * operator ->();
368 RegexpMapEntry< Val > const * operator ->() const;
370 private:
371 RegexpMapIter(RegexpMapIterImpl< Val > * pTheImpl);
374 template< typename Val >
375 RegexpMapIter< Val >::RegexpMapIter(RegexpMapIterImpl< Val > * pTheImpl):
376 RegexpMapConstIter< Val >(pTheImpl)
379 template< typename Val >
380 RegexpMapIter< Val > & RegexpMapIter< Val >::operator ++()
382 this->m_pImpl->next();
383 return *this;
386 template< typename Val >
387 RegexpMapIter< Val > RegexpMapIter< Val >::operator ++(int)
389 RegexpMapIter aTemp(*this);
390 this->m_pImpl->next();
391 return aTemp;
394 template< typename Val >
395 RegexpMapEntry< Val > & RegexpMapIter< Val >::operator *()
397 return this->m_pImpl->get();
400 template< typename Val >
401 RegexpMapEntry< Val > const & RegexpMapIter< Val >::operator *() const
403 return this->m_pImpl->get();
406 template< typename Val >
407 RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
409 return &this->m_pImpl->get();
412 template< typename Val >
413 RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
415 return &this->m_pImpl->get();
419 template< typename Val >
420 class RegexpMap
422 public:
423 typedef sal_uInt32 size_type;
424 typedef RegexpMapIter< Val > iterator;
425 typedef RegexpMapConstIter< Val > const_iterator;
427 RegexpMap();
429 RegexpMap(RegexpMap const & rOther);
431 ~RegexpMap();
433 RegexpMap & operator =(RegexpMap const & rOther);
435 bool add(OUString const & rKey, Val const & rValue, bool bOverwrite,
436 OUString * pReverse = 0);
437 // throws com::sun::star::lang::IllegalArgumentException
439 iterator find(OUString const & rKey, OUString * pReverse = 0);
440 // throws com::sun::star::lang::IllegalArgumentException
442 void erase(iterator const & rPos);
444 iterator begin();
446 const_iterator begin() const;
448 iterator end();
450 const_iterator end() const;
452 bool empty() const;
454 size_type size() const;
456 Val const * map(OUString const & rString,
457 OUString * pTranslation = 0, bool * pTranslated = 0)
458 const;
460 private:
461 RegexpMapImpl< Val > * m_pImpl;
464 template< typename Val >
465 RegexpMap< Val >::RegexpMap():
466 m_pImpl(new RegexpMapImpl< Val >)
469 template< typename Val >
470 RegexpMap< Val >::RegexpMap(RegexpMap const & rOther):
471 m_pImpl(new RegexpMapImpl< Val >(*rOther.m_pImpl))
474 template< typename Val >
475 RegexpMap< Val >::~RegexpMap()
477 delete m_pImpl;
480 template< typename Val >
481 RegexpMap< Val > & RegexpMap< Val >::operator =(RegexpMap const & rOther)
483 *m_pImpl = *rOther.m_pImpl;
484 return *this;
487 template< typename Val >
488 bool RegexpMap< Val >::add(rtl::OUString const & rKey, Val const & rValue,
489 bool bOverwrite, rtl::OUString * pReverse)
491 Regexp aRegexp(Regexp::parse(rKey));
493 if (aRegexp.isDefault())
495 if (m_pImpl->m_pDefault)
497 if (!bOverwrite)
498 return false;
499 delete m_pImpl->m_pDefault;
501 m_pImpl->m_pDefault = new Entry< Val >(aRegexp, rValue);
503 else
505 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
507 typename List< Val >::iterator aEnd(rTheList.end());
508 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
510 if (aIt->m_aRegexp == aRegexp)
512 if (bOverwrite)
514 rTheList.erase(aIt);
515 break;
517 else
518 return false;
522 rTheList.push_back(Entry< Val >(aRegexp, rValue));
525 if (pReverse)
526 *pReverse = aRegexp.getRegexp(true);
528 return true;
531 template< typename Val >
532 typename RegexpMap< Val >::iterator RegexpMap< Val >::find(rtl::OUString const & rKey,
533 rtl::OUString * pReverse)
535 Regexp aRegexp(Regexp::parse(rKey));
537 if (pReverse)
538 *pReverse = aRegexp.getRegexp(true);
540 if (aRegexp.isDefault())
542 if (m_pImpl->m_pDefault)
543 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
544 true));
546 else
548 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
550 typename List< Val > ::iterator aEnd(rTheList.end());
551 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
552 if (aIt->m_aRegexp == aRegexp)
553 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(
554 m_pImpl,
555 aRegexp.getKind(), aIt));
558 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
561 template< typename Val >
562 void RegexpMap< Val >::erase(iterator const & rPos)
564 if (rPos.m_pImpl->getMap() == m_pImpl)
566 if (rPos.m_pImpl->getList() == -1)
568 if (m_pImpl->m_pDefault)
570 delete m_pImpl->m_pDefault;
571 m_pImpl->m_pDefault = 0;
574 else
575 m_pImpl->m_aList[rPos.m_pImpl->getList()].
576 erase(rPos.m_pImpl->getIndex());
580 template< typename Val >
581 typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
583 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, true));
586 template< typename Val >
587 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
589 return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
590 true));
593 template< typename Val >
594 typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
596 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
599 template< typename Val >
600 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
602 return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
603 false));
606 template< typename Val >
607 bool RegexpMap< Val >::empty() const
609 return !m_pImpl->m_pDefault
610 && m_pImpl->m_aList[Regexp::KIND_PREFIX].empty()
611 && m_pImpl->m_aList[Regexp::KIND_AUTHORITY].empty()
612 && m_pImpl->m_aList[Regexp::KIND_DOMAIN].empty();
615 template< typename Val >
616 typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
618 return (m_pImpl->m_pDefault ? 1 : 0)
619 + m_pImpl->m_aList[Regexp::KIND_PREFIX].size()
620 + m_pImpl->m_aList[Regexp::KIND_AUTHORITY].size()
621 + m_pImpl->m_aList[Regexp::KIND_DOMAIN].size();
624 template< typename Val >
625 Val const * RegexpMap< Val >::map(rtl::OUString const & rString,
626 rtl::OUString * pTranslation,
627 bool * pTranslated) const
629 for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
631 List< Val > const & rTheList = m_pImpl->m_aList[n];
633 typename List< Val >::const_iterator aEnd(rTheList.end());
634 for (typename List< Val >::const_iterator aIt(rTheList.begin()); aIt != aEnd;
635 ++aIt)
636 if (aIt->m_aRegexp.matches(rString, pTranslation, pTranslated))
637 return &aIt->m_aValue;
639 if (m_pImpl->m_pDefault
640 && m_pImpl->m_pDefault->m_aRegexp.matches(rString, pTranslation,
641 pTranslated))
642 return &m_pImpl->m_pDefault->m_aValue;
643 return 0;
649 template< typename Val >
650 inline bool operator ==(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
651 ucb_impl::RegexpMapConstIter< Val > const & rIter2)
653 return rIter1.equals(rIter2);
656 template< typename Val >
657 inline bool operator !=(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
658 ucb_impl::RegexpMapConstIter< Val > const & rIter2)
660 return !rIter1.equals(rIter2);
663 #endif // INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
665 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */