Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / stl_types.hxx
blob594d6889cf219eed4e894f1b5dbcee5842bff3e0
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 .
19 #ifndef INCLUDED_COMPHELPER_STL_TYPES_HXX
20 #define INCLUDED_COMPHELPER_STL_TYPES_HXX
22 #include <sal/config.h>
24 #include <math.h>
25 #include <functional>
26 #include <memory>
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/beans/PropertyValue.hpp>
33 namespace comphelper
36 // comparison functors
38 struct UStringMixLess : public ::std::binary_function< OUString, OUString, bool>
40 bool m_bCaseSensitive;
41 public:
42 UStringMixLess(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
43 bool operator() (const OUString& x, const OUString& y) const
45 if (m_bCaseSensitive)
46 return rtl_ustr_compare(x.getStr(), y.getStr()) < 0;
47 else
48 return rtl_ustr_compareIgnoreAsciiCase(x.getStr(), y.getStr()) < 0;
51 bool isCaseSensitive() const {return m_bCaseSensitive;}
54 class UStringMixEqual: public std::binary_function<OUString, OUString, bool>
56 bool m_bCaseSensitive;
58 public:
59 UStringMixEqual(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
60 bool operator() (const OUString& lhs, const OUString& rhs) const
62 return m_bCaseSensitive ? lhs.equals( rhs ) : lhs.equalsIgnoreAsciiCase( rhs );
64 bool isCaseSensitive() const {return m_bCaseSensitive;}
67 class TPropertyValueEqualFunctor : public ::std::binary_function< css::beans::PropertyValue,OUString,bool>
69 public:
70 TPropertyValueEqualFunctor()
72 bool operator() (const css::beans::PropertyValue& lhs, const OUString& rhs) const
74 return !!(lhs.Name == rhs);
78 /// by-value less functor for std::set<std::unique_ptr<T>>
79 template<class T> struct UniquePtrValueLess
80 : public ::std::binary_function<std::unique_ptr<T>, std::unique_ptr<T>, bool>
82 bool operator()(std::unique_ptr<T> const& lhs,
83 std::unique_ptr<T> const& rhs) const
85 assert(lhs.get());
86 assert(rhs.get());
87 return (*lhs) < (*rhs);
91 /// by-value implementation of std::foo<std::unique_ptr<T>>::operator==
92 template<template<typename, typename...> class C, typename T, typename... Etc>
93 bool ContainerUniquePtrEquals(
94 C<std::unique_ptr<T>, Etc...> const& lhs,
95 C<std::unique_ptr<T>, Etc...> const& rhs)
97 if (lhs.size() != rhs.size())
99 return false;
101 for (auto iter1 = lhs.begin(), iter2 = rhs.begin();
102 iter1 != lhs.end();
103 ++iter1, ++iter2)
105 if (!(**iter1 == **iter2))
107 return false;
110 return true;
114 /** STL-compliant structure for comparing Reference&lt; &lt;iface&gt; &gt; instances
116 template < class IAFCE >
117 struct OInterfaceCompare
118 :public ::std::binary_function < css::uno::Reference< IAFCE >
119 , css::uno::Reference< IAFCE >
120 , bool
123 bool operator() (const css::uno::Reference< IAFCE >& lhs, const css::uno::Reference< IAFCE >& rhs) const
125 return lhs.get() < rhs.get();
126 // this does not make any sense if you see the semantics of the pointer returned by get:
127 // It's a pointer to a point in memory where an interface implementation lies.
128 // But for our purpose (provide a reliable less-operator which can be used with the STL), this is
129 // sufficient ....
133 template <class Tp, class Arg>
134 class mem_fun1_t : public ::std::binary_function<Tp*,Arg,void>
136 typedef void (Tp::*_fun_type)(Arg);
137 public:
138 explicit mem_fun1_t(_fun_type pf) : M_f(pf) {}
139 void operator()(Tp* p, Arg x) const { (p->*M_f)(x); }
140 private:
141 _fun_type M_f;
144 template <class Tp, class Arg>
145 inline mem_fun1_t<Tp,Arg> mem_fun(void (Tp::*f)(Arg))
147 return mem_fun1_t<Tp,Arg>(f);
150 /** output iterator that appends OUStrings into an OUStringBuffer.
152 class OUStringBufferAppender :
153 public ::std::iterator< ::std::output_iterator_tag, void, void, void, void>
155 public:
156 typedef OUStringBufferAppender Self;
157 typedef ::std::output_iterator_tag iterator_category;
158 typedef void value_type;
159 typedef void reference;
160 typedef void pointer;
161 typedef size_t difference_type;
163 OUStringBufferAppender(OUStringBuffer & i_rBuffer)
164 : m_rBuffer(i_rBuffer) { }
165 Self & operator=(Self const &)
166 { // MSVC 2013 with non-debug runtime requires this in xutility.hpp:289
167 return *this;
169 Self & operator=(OUString const & i_rStr)
171 m_rBuffer.append( i_rStr );
172 return *this;
174 Self & operator*() { return *this; } // so operator= works
175 Self & operator++() { return *this; }
177 private:
178 OUStringBuffer & m_rBuffer;
181 /** algorithm similar to std::copy, but inserts a separator between elements.
183 template< typename ForwardIter, typename OutputIter, typename T >
184 OutputIter intersperse(
185 ForwardIter start, ForwardIter end, OutputIter out, T const & separator)
187 if (start != end) {
188 *out = *start;
189 ++start;
190 ++out;
193 while (start != end) {
194 *out = separator;
195 ++out;
196 *out = *start;
197 ++start;
198 ++out;
201 return out;
206 #endif // INCLUDED_COMPHELPER_STL_TYPES_HXX
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */