Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef _COMPHELPER_STLTYPES_HXX_
20 : #define _COMPHELPER_STLTYPES_HXX_
21 :
22 : #include "sal/config.h"
23 :
24 : #include <vector>
25 : #include <map>
26 :
27 : #include <stack>
28 : #include <set>
29 :
30 : #include <math.h> // prevent conflict between exception and std::exception
31 : #include <functional>
32 :
33 :
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>
39 :
40 : //... namespace comphelper ................................................
41 : namespace comphelper
42 : {
43 : //.........................................................................
44 :
45 : //========================================================================
46 : // comparison functors
47 :
48 : //------------------------------------------------------------------------
49 : struct UStringLess : public ::std::binary_function< OUString, OUString, bool>
50 : {
51 2284862 : bool operator() (const OUString& x, const OUString& y) const { return x < y ? true : false;} // construct prevents a MSVC6 warning
52 : };
53 : //------------------------------------------------------------------------
54 : struct UStringMixLess : public ::std::binary_function< OUString, OUString, bool>
55 : {
56 : bool m_bCaseSensitive;
57 : public:
58 632 : UStringMixLess(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
59 69947 : bool operator() (const OUString& x, const OUString& y) const
60 : {
61 69947 : if (m_bCaseSensitive)
62 69947 : return rtl_ustr_compare(x.getStr(), y.getStr()) < 0 ? true : false;
63 : else
64 0 : return rtl_ustr_compareIgnoreAsciiCase(x.getStr(), y.getStr()) < 0 ? true : false;
65 : }
66 :
67 637 : bool isCaseSensitive() const {return m_bCaseSensitive;}
68 : };
69 : //------------------------------------------------------------------------
70 : struct UStringEqual
71 : {
72 4952 : sal_Bool operator() (const OUString& lhs, const OUString& rhs) const { return lhs.equals( rhs );}
73 : };
74 :
75 : //------------------------------------------------------------------------
76 : struct UStringIEqual
77 : {
78 : sal_Bool operator() (const OUString& lhs, const OUString& rhs) const { return lhs.equalsIgnoreAsciiCase( rhs );}
79 : };
80 :
81 : //------------------------------------------------------------------------
82 : class UStringMixEqual
83 : {
84 : sal_Bool m_bCaseSensitive;
85 :
86 : public:
87 4686 : UStringMixEqual(sal_Bool bCaseSensitive = sal_True):m_bCaseSensitive(bCaseSensitive){}
88 37434 : sal_Bool operator() (const OUString& lhs, const OUString& rhs) const
89 : {
90 37434 : return m_bCaseSensitive ? lhs.equals( rhs ) : lhs.equalsIgnoreAsciiCase( rhs );
91 : }
92 6 : sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
93 : };
94 : //------------------------------------------------------------------------
95 : class TStringMixEqualFunctor : public ::std::binary_function< OUString,OUString,bool>
96 : {
97 : sal_Bool m_bCaseSensitive;
98 :
99 : public:
100 25 : TStringMixEqualFunctor(sal_Bool bCaseSensitive = sal_True)
101 25 : :m_bCaseSensitive(bCaseSensitive)
102 25 : {}
103 0 : bool operator() (const OUString& lhs, const OUString& rhs) const
104 : {
105 0 : return !!(m_bCaseSensitive ? lhs.equals( rhs ) : lhs.equalsIgnoreAsciiCase( rhs ));
106 : }
107 : sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
108 : };
109 : //------------------------------------------------------------------------
110 : class TPropertyValueEqualFunctor : public ::std::binary_function< ::com::sun::star::beans::PropertyValue,OUString,bool>
111 : {
112 : public:
113 53 : TPropertyValueEqualFunctor()
114 53 : {}
115 33 : bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const OUString& rhs) const
116 : {
117 33 : return !!(lhs.Name == rhs);
118 : }
119 : };
120 : //------------------------------------------------------------------------
121 : class TNamedValueEqualFunctor : public ::std::binary_function< ::com::sun::star::beans::NamedValue,OUString,bool>
122 : {
123 : public:
124 0 : TNamedValueEqualFunctor()
125 0 : {}
126 0 : bool operator() (const ::com::sun::star::beans::NamedValue& lhs, const OUString& rhs) const
127 : {
128 0 : return !!(lhs.Name == rhs);
129 : }
130 : };
131 : //------------------------------------------------------------------------
132 : class UStringMixHash
133 : {
134 : sal_Bool m_bCaseSensitive;
135 :
136 : public:
137 : UStringMixHash(sal_Bool bCaseSensitive = sal_True):m_bCaseSensitive(bCaseSensitive){}
138 : size_t operator() (const OUString& rStr) const
139 : {
140 : return m_bCaseSensitive ? rStr.hashCode() : rStr.toAsciiUpperCase().hashCode();
141 : }
142 : sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
143 : };
144 :
145 : //=====================================================================
146 : //= OInterfaceCompare
147 : //=====================================================================
148 : /** is stl-compliant structure for comparing Reference< <iface> > instances
149 : */
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 >
154 : , bool
155 : >
156 : {
157 4449 : bool operator() (const ::com::sun::star::uno::Reference< IAFCE >& lhs, const ::com::sun::star::uno::Reference< IAFCE >& rhs) const
158 : {
159 4449 : 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
163 : // sufficient ....
164 : }
165 : };
166 :
167 : template <class _Tp, class _Arg>
168 : class mem_fun1_t : public ::std::binary_function<_Tp*,_Arg,void>
169 : {
170 : typedef void (_Tp::*_fun_type)(_Arg);
171 : public:
172 0 : explicit mem_fun1_t(_fun_type __pf) : _M_f(__pf) {}
173 0 : void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
174 : private:
175 : _fun_type _M_f;
176 : };
177 :
178 : template <class _Tp, class _Arg>
179 0 : inline mem_fun1_t<_Tp,_Arg> mem_fun(void (_Tp::*__f)(_Arg))
180 : {
181 0 : return mem_fun1_t<_Tp,_Arg>(__f);
182 : }
183 :
184 : //.........................................................................
185 : /** output iterator that appends OUStrings into an OUStringBuffer.
186 : */
187 : class OUStringBufferAppender :
188 : public ::std::iterator< ::std::output_iterator_tag, void, void, void, void>
189 : {
190 : public:
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;
197 :
198 71 : OUStringBufferAppender(OUStringBuffer & i_rBuffer)
199 71 : : m_rBuffer(i_rBuffer) { }
200 26 : Self & operator=(OUString const & i_rStr)
201 : {
202 26 : m_rBuffer.append( i_rStr );
203 26 : return *this;
204 : }
205 26 : Self & operator*() { return *this; } // so operator= works
206 26 : Self & operator++() { return *this; }
207 : Self & operator++(int) { return *this; }
208 :
209 : private:
210 : OUStringBuffer & m_rBuffer;
211 : };
212 :
213 : //.........................................................................
214 : /** algorithm similar to std::copy, but inserts a separator between elements.
215 : */
216 : template< typename ForwardIter, typename OutputIter, typename T >
217 71 : OutputIter intersperse(
218 : ForwardIter start, ForwardIter end, OutputIter out, T const & separator)
219 : {
220 71 : if (start != end) {
221 18 : *out = *start;
222 18 : ++start;
223 18 : ++out;
224 : }
225 :
226 146 : while (start != end) {
227 4 : *out = separator;
228 4 : ++out;
229 4 : *out = *start;
230 4 : ++start;
231 4 : ++out;
232 : }
233 :
234 71 : return out;
235 : }
236 :
237 : //.........................................................................
238 : }
239 : //... namespace comphelper ................................................
240 :
241 : //==================================================================
242 : // consistently defining stl-types
243 : //==================================================================
244 :
245 : #define DECLARE_STL_ITERATORS(classname) \
246 : typedef classname::iterator classname##Iterator; \
247 : typedef classname::const_iterator Const##classname##Iterator \
248 :
249 : #define DECLARE_STL_MAP(keytype, valuetype, comparefct, classname) \
250 : typedef std::map< keytype, valuetype, comparefct > classname; \
251 : DECLARE_STL_ITERATORS(classname) \
252 :
253 : #define DECLARE_STL_STDKEY_MAP(keytype, valuetype, classname) \
254 : DECLARE_STL_MAP(keytype, valuetype, std::less< keytype >, classname) \
255 :
256 : #define DECLARE_STL_VECTOR(valuetyp, classname) \
257 : typedef std::vector< valuetyp > classname; \
258 : DECLARE_STL_ITERATORS(classname) \
259 :
260 : #define DECLARE_STL_USTRINGACCESS_MAP(valuetype, classname) \
261 : DECLARE_STL_MAP(OUString, valuetype, ::comphelper::UStringLess, classname) \
262 :
263 : #define DECLARE_STL_STDKEY_SET(valuetype, classname) \
264 : typedef ::std::set< valuetype > classname; \
265 : DECLARE_STL_ITERATORS(classname) \
266 :
267 : #define DECLARE_STL_SET(valuetype, comparefct, classname) \
268 : typedef ::std::set< valuetype, comparefct > classname; \
269 : DECLARE_STL_ITERATORS(classname) \
270 :
271 : #endif // _COMPHELPER_STLTYPES_HXX_
272 :
273 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|