1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _COMPHELPER_STLTYPES_HXX_
24 #define _COMPHELPER_STLTYPES_HXX_
25 
26 #if !defined(__SGI_STL_VECTOR_H) || !defined(__SGI_STL_MAP_H) || !defined(__SGI_STL_MULTIMAP_H)
27 
28 #include <vector>
29 #include <map>
30 
31 #include <stack>
32 #include <set>
33 
34 #ifdef _MSC_VER
35 # ifndef _USE_MATH_DEFINES
36 #  define _USE_MATH_DEFINES // needed by Visual C++ for math constants
37 # endif
38 #endif
39 
40 #include <math.h> // prevent conflict between exception and std::exception
41 #include <functional>
42 
43 
44 #include <rtl/ustring.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <com/sun/star/uno/Reference.hxx>
47 #include <com/sun/star/beans/PropertyValue.hpp>
48 
49 //... namespace comphelper ................................................
50 namespace comphelper
51 {
52 //.........................................................................
53 
54 //========================================================================
55 // comparison functors
56 
57 //------------------------------------------------------------------------
58 struct UStringLess : public ::std::binary_function< ::rtl::OUString, ::rtl::OUString, bool>
59 {
operator ()comphelper::UStringLess60     bool operator() (const ::rtl::OUString& x, const ::rtl::OUString& y) const { return x < y ? true : false;}      // construct prevents a MSVC6 warning
61 };
62 //------------------------------------------------------------------------
63 struct UStringMixLess : public ::std::binary_function< ::rtl::OUString, ::rtl::OUString, bool>
64 {
65     bool m_bCaseSensitive;
66 public:
UStringMixLesscomphelper::UStringMixLess67     UStringMixLess(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
operator ()comphelper::UStringMixLess68     bool operator() (const ::rtl::OUString& x, const ::rtl::OUString& y) const
69     {
70         if (m_bCaseSensitive)
71             return rtl_ustr_compare(x.getStr(), y.getStr()) < 0 ? true : false;
72         else
73             return rtl_ustr_compareIgnoreAsciiCase(x.getStr(), y.getStr()) < 0 ? true : false;
74     }
75 
isCaseSensitivecomphelper::UStringMixLess76     bool isCaseSensitive() const {return m_bCaseSensitive;}
77 };
78 //------------------------------------------------------------------------
79 struct UStringEqual
80 {
operator ()comphelper::UStringEqual81     sal_Bool operator() (const ::rtl::OUString& lhs, const ::rtl::OUString& rhs) const { return lhs.equals( rhs );}
82 };
83 
84 //------------------------------------------------------------------------
85 struct UStringIEqual
86 {
operator ()comphelper::UStringIEqual87     sal_Bool operator() (const ::rtl::OUString& lhs, const ::rtl::OUString& rhs) const { return lhs.equalsIgnoreAsciiCase( rhs );}
88 };
89 
90 //------------------------------------------------------------------------
91 struct UStringHash
92 {
operator ()comphelper::UStringHash93     size_t operator() (const ::rtl::OUString& rStr) const {return rStr.hashCode();}
94 };
95 
96 //------------------------------------------------------------------------
97 class UStringMixEqual
98 {
99     sal_Bool m_bCaseSensitive;
100 
101 public:
UStringMixEqual(sal_Bool bCaseSensitive=sal_True)102     UStringMixEqual(sal_Bool bCaseSensitive = sal_True):m_bCaseSensitive(bCaseSensitive){}
operator ()(const::rtl::OUString & lhs,const::rtl::OUString & rhs) const103     sal_Bool operator() (const ::rtl::OUString& lhs, const ::rtl::OUString& rhs) const
104     {
105         return m_bCaseSensitive ? lhs.equals( rhs ) : lhs.equalsIgnoreAsciiCase( rhs );
106     }
isCaseSensitive() const107     sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
108 };
109 //------------------------------------------------------------------------
110 class TStringMixEqualFunctor : public ::std::binary_function< ::rtl::OUString,::rtl::OUString,bool>
111 {
112     sal_Bool m_bCaseSensitive;
113 
114 public:
TStringMixEqualFunctor(sal_Bool bCaseSensitive=sal_True)115     TStringMixEqualFunctor(sal_Bool bCaseSensitive = sal_True)
116 		:m_bCaseSensitive(bCaseSensitive)
117 	{}
operator ()(const::rtl::OUString & lhs,const::rtl::OUString & rhs) const118     bool operator() (const ::rtl::OUString& lhs, const ::rtl::OUString& rhs) const
119     {
120         return !!(m_bCaseSensitive ? lhs.equals( rhs ) : lhs.equalsIgnoreAsciiCase( rhs ));
121     }
isCaseSensitive() const122     sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
123 };
124 //------------------------------------------------------------------------
125 class TPropertyValueEqualFunctor : public ::std::binary_function< ::com::sun::star::beans::PropertyValue,::rtl::OUString,bool>
126 {
127 public:
TPropertyValueEqualFunctor()128     TPropertyValueEqualFunctor()
129 	{}
operator ()(const::com::sun::star::beans::PropertyValue & lhs,const::rtl::OUString & rhs) const130     bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::rtl::OUString& rhs) const
131     {
132         return !!(lhs.Name == rhs);
133     }
134 };
135 //------------------------------------------------------------------------
136 class UStringMixHash
137 {
138     sal_Bool m_bCaseSensitive;
139 
140 public:
UStringMixHash(sal_Bool bCaseSensitive=sal_True)141     UStringMixHash(sal_Bool bCaseSensitive = sal_True):m_bCaseSensitive(bCaseSensitive){}
operator ()(const::rtl::OUString & rStr) const142     size_t operator() (const ::rtl::OUString& rStr) const
143     {
144         return m_bCaseSensitive ? rStr.hashCode() : rStr.toAsciiUpperCase().hashCode();
145     }
isCaseSensitive() const146     sal_Bool isCaseSensitive() const {return m_bCaseSensitive;}
147 };
148 
149 //=====================================================================
150 //= OInterfaceCompare
151 //=====================================================================
152 /** is stl-compliant structure for comparing Reference&lt; &lt;iface&gt; &gt; instances
153 */
154 template < class IAFCE >
155 struct OInterfaceCompare
156 	:public ::std::binary_function	<	::com::sun::star::uno::Reference< IAFCE >
157 									,	::com::sun::star::uno::Reference< IAFCE >
158 									,	bool
159 									>
160 {
operator ()comphelper::OInterfaceCompare161 	bool operator() (const ::com::sun::star::uno::Reference< IAFCE >& lhs, const ::com::sun::star::uno::Reference< IAFCE >& rhs) const
162 	{
163 		return lhs.get() < rhs.get();
164 			// this does not make any sense if you see the semantics of the pointer returned by get:
165 			// It's a pointer to a point in memory where an interface implementation lies.
166 			// But for our purpose (provide a reliable less-operator which can be used with the STL), this is
167 			// sufficient ....
168 	}
169 };
170 
171 template <class _Tp, class _Arg>
172 class mem_fun1_t : public ::std::binary_function<_Tp*,_Arg,void>
173 {
174 	typedef void (_Tp::*_fun_type)(_Arg);
175 public:
mem_fun1_t(_fun_type __pf)176 	explicit mem_fun1_t(_fun_type __pf) : _M_f(__pf) {}
operator ()(_Tp * __p,_Arg __x) const177 	void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
178 private:
179 	_fun_type _M_f;
180 };
181 
182 template <class _Tp, class _Arg>
mem_fun(void (_Tp::* __f)(_Arg))183 inline mem_fun1_t<_Tp,_Arg> mem_fun(void (_Tp::*__f)(_Arg))
184 {
185 	return mem_fun1_t<_Tp,_Arg>(__f);
186 }
187 
188 //.........................................................................
189 /** output iterator that appends OUStrings into an OUStringBuffer.
190  */
191 class OUStringBufferAppender :
192     public ::std::iterator< ::std::output_iterator_tag, void, void, void, void>
193 {
194 public:
195     typedef OUStringBufferAppender Self;
196     typedef ::std::output_iterator_tag iterator_category;
197     typedef void value_type;
198     typedef void reference;
199     typedef void pointer;
200     typedef size_t difference_type;
201 
OUStringBufferAppender(::rtl::OUStringBuffer & i_rBuffer)202     OUStringBufferAppender(::rtl::OUStringBuffer & i_rBuffer)
203         : m_rBuffer(i_rBuffer) { }
operator =(::rtl::OUString const & i_rStr)204     Self & operator=(::rtl::OUString const & i_rStr)
205     {
206         m_rBuffer.append( i_rStr );
207         return *this;
208     }
operator *()209     Self & operator*() { return *this; } // so operator= works
operator ++()210     Self & operator++() { return *this; }
operator ++(int)211     Self & operator++(int) { return *this; }
212 
213 private:
214     ::rtl::OUStringBuffer & m_rBuffer;
215 };
216 
217 //.........................................................................
218 /** algorithm similar to std::copy, but inserts a separator between elements.
219  */
220 template< typename ForwardIter, typename OutputIter, typename T >
intersperse(ForwardIter start,ForwardIter end,OutputIter out,T const & separator)221 OutputIter intersperse(
222     ForwardIter start, ForwardIter end, OutputIter out, T const & separator)
223 {
224     if (start != end) {
225         *out = *start;
226         ++start;
227         ++out;
228     }
229 
230     while (start != end) {
231         *out = separator;
232         ++out;
233         *out = *start;
234         ++start;
235         ++out;
236     }
237 
238     return out;
239 }
240 
241 //.........................................................................
242 }
243 //... namespace comphelper ................................................
244 
245 //==================================================================
246 // consistently defining stl-types
247 //==================================================================
248 
249 #define DECLARE_STL_ITERATORS(classname)                            \
250     typedef classname::iterator         classname##Iterator;        \
251     typedef classname::const_iterator   Const##classname##Iterator  \
252 
253 #define DECLARE_STL_MAP(keytype, valuetype, comparefct, classname)  \
254     typedef std::map< keytype, valuetype, comparefct >  classname;  \
255     DECLARE_STL_ITERATORS(classname)                                \
256 
257 #define DECLARE_STL_STDKEY_MAP(keytype, valuetype, classname)               \
258     DECLARE_STL_MAP(keytype, valuetype, std::less< keytype >, classname)    \
259 
260 #define DECLARE_STL_VECTOR(valuetyp, classname)     \
261     typedef std::vector< valuetyp >     classname;  \
262     DECLARE_STL_ITERATORS(classname)                \
263 
264 #define DECLARE_STL_USTRINGACCESS_MAP(valuetype, classname)                 \
265     DECLARE_STL_MAP(::rtl::OUString, valuetype, ::comphelper::UStringLess, classname)   \
266 
267 #define DECLARE_STL_STDKEY_SET(valuetype, classname)    \
268     typedef ::std::set< valuetype > classname;          \
269     DECLARE_STL_ITERATORS(classname)                    \
270 
271 #define DECLARE_STL_SET(valuetype, comparefct, classname)               \
272     typedef ::std::set< valuetype, comparefct > classname;  \
273     DECLARE_STL_ITERATORS(classname)                        \
274 
275 #endif
276 
277 #endif  // _COMPHELPER_STLTYPES_HXX_
278 
279