Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / rtl / stringutils.hxx
blob114cb5dd9463622e1a1d21f64478029d6c00e412
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/.
8 */
10 #ifndef INCLUDED_RTL_STRINGUTILS_HXX
11 #define INCLUDED_RTL_STRINGUTILS_HXX
13 #include <sal/config.h>
15 // Manually defining RTL_DISABLE_FAST_STRING allows to force turning fast string concatenation off
16 // (e.g. for debugging).
17 #ifndef RTL_DISABLE_FAST_STRING
18 // This feature is not part of public API and is meant to be used only internally by LibreOffice.
19 #ifdef LIBO_INTERNAL_ONLY
20 // Enable fast string concatenation.
21 #define RTL_FAST_STRING
22 #endif
23 #endif
25 // The unittest uses slightly different code to help check that the proper
26 // calls are made. The class is put into a different namespace to make
27 // sure the compiler generates a different (if generating also non-inline)
28 // copy of the function and does not merge them together. The class
29 // is "brought" into the proper rtl namespace by a typedef below.
30 #ifdef RTL_STRING_UNITTEST
31 #define rtl rtlunittest
32 #endif
34 namespace rtl
37 #ifdef RTL_STRING_UNITTEST
38 #undef rtl
39 #endif
41 namespace internal
44 These templates use SFINAE (Substitution failure is not an error) to help distinguish the various
45 plain C string types: char*, const char*, char[N], const char[N], char[] and const char[].
46 There are 2 cases:
47 1) Only string literal (i.e. const char[N]) is wanted, not any of the others.
48 In this case it is necessary to distinguish between const char[N] and char[N], as the latter
49 would be automatically converted to the const variant, which is not wanted (not a string literal
50 with known size of the content). In this case ConstCharArrayDetector is used to ensure the function
51 is called only with const char[N] arguments. There's no other plain C string type overload.
52 2) All plain C string types are wanted, and const char[N] needs to be handled differently.
53 In this case const char[N] would match const char* argument type (not exactly sure why, but it's
54 consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type
55 avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer
56 arguments. The const in the argument is necessary to handle the case when something is explicitly
57 cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference
58 being const, it would also match const char[N], so another overload with a reference to non-const
59 and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N].
60 Additionally, char[] and const char[] (i.e. size unknown) are rather tricky. Their usage with 'T&' would
61 mean it would be 'char(&)[]', which seems to be invalid. But gcc and clang somehow manage when it is
62 a template. while msvc complains about no conversion from char[] to char[1]. And the reference cannot
63 be avoided, because 'const char[]' as argument type would match also 'const char[N]'
64 So char[] and const char[] should always be used with their contents specified (which automatically
65 turns them into char[N] or const char[N]), or char* and const char* should be used.
67 struct Dummy {};
68 template< typename T1, typename T2 = void >
69 struct CharPtrDetector
71 static const bool ok = false;
73 template< typename T >
74 struct CharPtrDetector< const char*, T >
76 typedef T Type;
77 static const bool ok = true;
79 template< typename T >
80 struct CharPtrDetector< char*, T >
82 typedef T Type;
83 static const bool ok = true;
86 template< typename T1, typename T2 >
87 struct NonConstCharArrayDetector
90 template< typename T, int N >
91 struct NonConstCharArrayDetector< char[ N ], T >
93 typedef T Type;
95 #ifdef RTL_STRING_UNITTEST
96 // never use, until all compilers handle this
97 template< typename T >
98 struct NonConstCharArrayDetector< char[], T >
100 typedef T Type;
102 template< typename T >
103 struct NonConstCharArrayDetector< const char[], T >
105 typedef T Type;
107 #endif
109 template< typename T1, typename T2 = void >
110 struct ConstCharArrayDetector
112 static const bool ok = false;
114 template< int N, typename T >
115 struct ConstCharArrayDetector< const char[ N ], T >
117 typedef T Type;
118 static const int size = N;
119 static const bool ok = true;
122 // this one is used to rule out only const char[N]
123 template< typename T >
124 struct ExceptConstCharArrayDetector
126 typedef Dummy Type;
128 template< int N >
129 struct ExceptConstCharArrayDetector< const char[ N ] >
132 // this one is used to rule out only const char[N]
133 // (const will be brought in by 'const T&' in the function call)
134 // msvc needs const char[N] here (not sure whether gcc or msvc
135 // are right, it doesn't matter).
136 template< typename T >
137 struct ExceptCharArrayDetector
139 typedef Dummy Type;
141 template< int N >
142 struct ExceptCharArrayDetector< char[ N ] >
145 template< int N >
146 struct ExceptCharArrayDetector< const char[ N ] >
150 template< typename T1, typename T2 = void >
151 struct SalUnicodePtrDetector
153 static const bool ok = false;
155 template< typename T >
156 struct SalUnicodePtrDetector< const sal_Unicode*, T >
158 typedef T Type;
159 static const bool ok = true;
161 template< typename T >
162 struct SalUnicodePtrDetector< sal_Unicode*, T >
164 typedef T Type;
165 static const bool ok = true;
168 // SFINAE helper class
169 template< typename T, bool >
170 struct Enable
174 template< typename T >
175 struct Enable< T, true >
177 typedef T Type;
181 } /* Namespace */
183 } /* Namespace */
185 #endif // INCLUDED_RTL_STRINGUTILS_HXX
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */