1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
19 #ifndef _COMPHELPER_STLTYPES_HXX_
20 #define _COMPHELPER_STLTYPES_HXX_
22 #include "sal/config.h"
30 #include <math.h> // prevent conflict between exception and std::exception
34 #include <rtl/ustring.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/NamedValue.hpp>
40 //... namespace comphelper ................................................
43 //.........................................................................
45 //========================================================================
46 // comparison functors
48 //------------------------------------------------------------------------
49 struct UStringLess
: public ::std::binary_function
< OUString
, OUString
, bool>
51 bool operator() (const OUString
& x
, const OUString
& y
) const { return x
< y
? true : false;} // construct prevents a MSVC6 warning
53 //------------------------------------------------------------------------
54 struct UStringMixLess
: public ::std::binary_function
< OUString
, OUString
, bool>
56 bool m_bCaseSensitive
;
58 UStringMixLess(bool bCaseSensitive
= true):m_bCaseSensitive(bCaseSensitive
){}
59 bool operator() (const OUString
& x
, const OUString
& y
) const
62 return rtl_ustr_compare(x
.getStr(), y
.getStr()) < 0 ? true : false;
64 return rtl_ustr_compareIgnoreAsciiCase(x
.getStr(), y
.getStr()) < 0 ? true : false;
67 bool isCaseSensitive() const {return m_bCaseSensitive
;}
69 //------------------------------------------------------------------------
72 sal_Bool
operator() (const OUString
& lhs
, const OUString
& rhs
) const { return lhs
.equals( rhs
);}
75 //------------------------------------------------------------------------
78 sal_Bool
operator() (const OUString
& lhs
, const OUString
& rhs
) const { return lhs
.equalsIgnoreAsciiCase( rhs
);}
81 //------------------------------------------------------------------------
84 sal_Bool m_bCaseSensitive
;
87 UStringMixEqual(sal_Bool bCaseSensitive
= sal_True
):m_bCaseSensitive(bCaseSensitive
){}
88 sal_Bool
operator() (const OUString
& lhs
, const OUString
& rhs
) const
90 return m_bCaseSensitive
? lhs
.equals( rhs
) : lhs
.equalsIgnoreAsciiCase( rhs
);
92 sal_Bool
isCaseSensitive() const {return m_bCaseSensitive
;}
94 //------------------------------------------------------------------------
95 class TStringMixEqualFunctor
: public ::std::binary_function
< OUString
,OUString
,bool>
97 sal_Bool m_bCaseSensitive
;
100 TStringMixEqualFunctor(sal_Bool bCaseSensitive
= sal_True
)
101 :m_bCaseSensitive(bCaseSensitive
)
103 bool operator() (const OUString
& lhs
, const OUString
& rhs
) const
105 return !!(m_bCaseSensitive
? lhs
.equals( rhs
) : lhs
.equalsIgnoreAsciiCase( rhs
));
107 sal_Bool
isCaseSensitive() const {return m_bCaseSensitive
;}
109 //------------------------------------------------------------------------
110 class TPropertyValueEqualFunctor
: public ::std::binary_function
< ::com::sun::star::beans::PropertyValue
,OUString
,bool>
113 TPropertyValueEqualFunctor()
115 bool operator() (const ::com::sun::star::beans::PropertyValue
& lhs
, const OUString
& rhs
) const
117 return !!(lhs
.Name
== rhs
);
120 //------------------------------------------------------------------------
121 class TNamedValueEqualFunctor
: public ::std::binary_function
< ::com::sun::star::beans::NamedValue
,OUString
,bool>
124 TNamedValueEqualFunctor()
126 bool operator() (const ::com::sun::star::beans::NamedValue
& lhs
, const OUString
& rhs
) const
128 return !!(lhs
.Name
== rhs
);
131 //------------------------------------------------------------------------
134 sal_Bool m_bCaseSensitive
;
137 UStringMixHash(sal_Bool bCaseSensitive
= sal_True
):m_bCaseSensitive(bCaseSensitive
){}
138 size_t operator() (const OUString
& rStr
) const
140 return m_bCaseSensitive
? rStr
.hashCode() : rStr
.toAsciiUpperCase().hashCode();
142 sal_Bool
isCaseSensitive() const {return m_bCaseSensitive
;}
145 //=====================================================================
146 //= OInterfaceCompare
147 //=====================================================================
148 /** is stl-compliant structure for comparing Reference< <iface> > instances
150 template < class IAFCE
>
151 struct OInterfaceCompare
152 :public ::std::binary_function
< ::com::sun::star::uno::Reference
< IAFCE
>
153 , ::com::sun::star::uno::Reference
< IAFCE
>
157 bool operator() (const ::com::sun::star::uno::Reference
< IAFCE
>& lhs
, const ::com::sun::star::uno::Reference
< IAFCE
>& rhs
) const
159 return lhs
.get() < rhs
.get();
160 // this does not make any sense if you see the semantics of the pointer returned by get:
161 // It's a pointer to a point in memory where an interface implementation lies.
162 // But for our purpose (provide a reliable less-operator which can be used with the STL), this is
167 template <class _Tp
, class _Arg
>
168 class mem_fun1_t
: public ::std::binary_function
<_Tp
*,_Arg
,void>
170 typedef void (_Tp::*_fun_type
)(_Arg
);
172 explicit mem_fun1_t(_fun_type __pf
) : _M_f(__pf
) {}
173 void operator()(_Tp
* __p
, _Arg __x
) const { (__p
->*_M_f
)(__x
); }
178 template <class _Tp
, class _Arg
>
179 inline mem_fun1_t
<_Tp
,_Arg
> mem_fun(void (_Tp::*__f
)(_Arg
))
181 return mem_fun1_t
<_Tp
,_Arg
>(__f
);
184 //.........................................................................
185 /** output iterator that appends OUStrings into an OUStringBuffer.
187 class OUStringBufferAppender
:
188 public ::std::iterator
< ::std::output_iterator_tag
, void, void, void, void>
191 typedef OUStringBufferAppender Self
;
192 typedef ::std::output_iterator_tag iterator_category
;
193 typedef void value_type
;
194 typedef void reference
;
195 typedef void pointer
;
196 typedef size_t difference_type
;
198 OUStringBufferAppender(OUStringBuffer
& i_rBuffer
)
199 : m_rBuffer(i_rBuffer
) { }
200 Self
& operator=(OUString
const & i_rStr
)
202 m_rBuffer
.append( i_rStr
);
205 Self
& operator*() { return *this; } // so operator= works
206 Self
& operator++() { return *this; }
207 Self
& operator++(int) { return *this; }
210 OUStringBuffer
& m_rBuffer
;
213 //.........................................................................
214 /** algorithm similar to std::copy, but inserts a separator between elements.
216 template< typename ForwardIter
, typename OutputIter
, typename T
>
217 OutputIter
intersperse(
218 ForwardIter start
, ForwardIter end
, OutputIter out
, T
const & separator
)
226 while (start
!= end
) {
237 //.........................................................................
239 //... namespace comphelper ................................................
241 //==================================================================
242 // consistently defining stl-types
243 //==================================================================
245 #define DECLARE_STL_ITERATORS(classname) \
246 typedef classname::iterator classname##Iterator; \
247 typedef classname::const_iterator Const##classname##Iterator \
249 #define DECLARE_STL_MAP(keytype, valuetype, comparefct, classname) \
250 typedef std::map< keytype, valuetype, comparefct > classname; \
251 DECLARE_STL_ITERATORS(classname) \
253 #define DECLARE_STL_STDKEY_MAP(keytype, valuetype, classname) \
254 DECLARE_STL_MAP(keytype, valuetype, std::less< keytype >, classname) \
256 #define DECLARE_STL_VECTOR(valuetyp, classname) \
257 typedef std::vector< valuetyp > classname; \
258 DECLARE_STL_ITERATORS(classname) \
260 #define DECLARE_STL_USTRINGACCESS_MAP(valuetype, classname) \
261 DECLARE_STL_MAP(OUString, valuetype, ::comphelper::UStringLess, classname) \
263 #define DECLARE_STL_STDKEY_SET(valuetype, classname) \
264 typedef ::std::set< valuetype > classname; \
265 DECLARE_STL_ITERATORS(classname) \
267 #define DECLARE_STL_SET(valuetype, comparefct, classname) \
268 typedef ::std::set< valuetype, comparefct > classname; \
269 DECLARE_STL_ITERATORS(classname) \
271 #endif // _COMPHELPER_STLTYPES_HXX_
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */