LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / include / comphelper / stl_types.hxx
blob5693a83338f5c2b62f32183b1f04171d853ed070
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 <algorithm>
25 #include <memory>
26 #include <string_view>
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
31 namespace com::sun::star::uno { template <typename > class Reference; }
33 namespace comphelper
36 // comparison functors
38 struct UStringMixLess
40 bool m_bCaseSensitive;
41 public:
42 UStringMixLess(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
43 bool operator() (const OUString& x, std::u16string_view y) const
45 if (m_bCaseSensitive)
46 return x.compareTo(y) < 0;
47 else
48 return x.compareToIgnoreAsciiCase(y) < 0;
51 bool isCaseSensitive() const {return m_bCaseSensitive;}
54 class UStringMixEqual
56 bool const m_bCaseSensitive;
58 public:
59 UStringMixEqual(bool bCaseSensitive = true):m_bCaseSensitive(bCaseSensitive){}
60 bool operator() (const OUString& lhs, std::u16string_view rhs) const
62 return m_bCaseSensitive ? lhs == rhs : lhs.equalsIgnoreAsciiCase( rhs );
64 bool isCaseSensitive() const {return m_bCaseSensitive;}
67 /// by-value less functor for std::set<std::unique_ptr<T>>
68 template<class T> struct UniquePtrValueLess
70 bool operator()(std::unique_ptr<T> const& lhs,
71 std::unique_ptr<T> const& rhs) const
73 assert(lhs.get());
74 assert(rhs.get());
75 return (*lhs) < (*rhs);
77 // The following are so we can search in std::set without allocating a temporary entry on the heap
78 typedef bool is_transparent;
79 bool operator()(T const& lhs,
80 std::unique_ptr<T> const& rhs) const
82 assert(rhs.get());
83 return lhs < (*rhs);
85 bool operator()(std::unique_ptr<T> const& lhs,
86 T const& rhs) const
88 assert(lhs.get());
89 return (*lhs) < rhs;
93 /// by-value implementation of std::foo<std::unique_ptr<T>>::operator==
94 template<template<typename, typename...> class C, typename T, typename... Etc>
95 bool ContainerUniquePtrEquals(
96 C<std::unique_ptr<T>, Etc...> const& lhs,
97 C<std::unique_ptr<T>, Etc...> const& rhs)
99 return lhs.size() == rhs.size()
100 && std::equal(lhs.begin(), lhs.end(), rhs.begin(),
101 [](const auto& p1, const auto& p2) { return *p1 == *p2; });
105 template <class Tp, class Arg>
106 class mem_fun1_t
108 typedef void (Tp::*_fun_type)(Arg);
109 public:
110 explicit mem_fun1_t(_fun_type pf) : M_f(pf) {}
111 void operator()(Tp* p, Arg x) const { (p->*M_f)(x); }
112 private:
113 _fun_type const M_f;
116 template <class Tp, class Arg>
117 inline mem_fun1_t<Tp,Arg> mem_fun(void (Tp::*f)(Arg))
119 return mem_fun1_t<Tp,Arg>(f);
122 /** output iterator that appends OUStrings into an OUStringBuffer.
124 class OUStringBufferAppender
126 public:
127 typedef OUStringBufferAppender Self;
128 typedef ::std::output_iterator_tag iterator_category;
129 typedef void value_type;
130 typedef void reference;
131 typedef void pointer;
132 typedef size_t difference_type;
134 OUStringBufferAppender(OUStringBuffer & i_rBuffer)
135 : m_rBuffer(&i_rBuffer) { }
136 Self & operator=(std::u16string_view i_rStr)
138 m_rBuffer->append( i_rStr );
139 return *this;
141 Self & operator*() { return *this; } // so operator= works
142 Self & operator++() { return *this; }
144 private:
145 OUStringBuffer * m_rBuffer;
148 /** algorithm similar to std::copy, but inserts a separator between elements.
150 template< typename ForwardIter, typename OutputIter, typename T >
151 OutputIter intersperse(
152 ForwardIter start, ForwardIter end, OutputIter out, T const & separator)
154 if (start != end) {
155 *out = *start;
156 ++start;
157 ++out;
160 while (start != end) {
161 *out = separator;
162 ++out;
163 *out = *start;
164 ++start;
165 ++out;
168 return out;
173 #endif // INCLUDED_COMPHELPER_STL_TYPES_HXX
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */