LibreOffice
LibreOffice 4.1 SDK C/C++ API Reference
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
rtl
stringutils.hxx
Go to the documentation of this file.
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
10
#ifndef _RTL_STRINGUTILS_HXX_
11
#define _RTL_STRINGUTILS_HXX_
12
13
#include "
sal/config.h
"
14
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
24
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
33
34
namespace
rtl
35
{
36
37
#ifdef RTL_STRING_UNITTEST
38
#undef rtl
39
#endif
40
41
namespace
internal
42
{
43
/*
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.
66
*/
67
struct
Dummy
{};
68
template
<
typename
T1,
typename
T2 =
void
>
69
struct
CharPtrDetector
70
{
71
static
const
bool
ok
=
false
;
72
};
73
template
<
typename
T >
74
struct
CharPtrDetector
< const char*, T >
75
{
76
typedef
T
Type
;
77
static
const
bool
ok =
true
;
78
};
79
template
<
typename
T >
80
struct
CharPtrDetector
< char*, T >
81
{
82
typedef
T
Type
;
83
static
const
bool
ok =
true
;
84
};
85
86
template
<
typename
T1,
typename
T2 >
87
struct
NonConstCharArrayDetector
88
{
89
};
90
template
<
typename
T,
int
N >
91
struct
NonConstCharArrayDetector
< char[ N ], T >
92
{
93
typedef
T
Type
;
94
};
95
#ifdef RTL_STRING_UNITTEST
96
// never use, until all compilers handle this
97
template
<
typename
T >
98
struct
NonConstCharArrayDetector
< char[], T >
99
{
100
typedef
T Type;
101
};
102
template
<
typename
T >
103
struct
NonConstCharArrayDetector< const char[], T >
104
{
105
typedef
T Type;
106
};
107
#endif
108
109
template
<
typename
T1,
typename
T2 =
void
>
110
struct
ConstCharArrayDetector
111
{
112
static
const
bool
ok
=
false
;
113
};
114
template
<
int
N,
typename
T >
115
struct
ConstCharArrayDetector
< const char[ N ], T >
116
{
117
typedef
T
Type
;
118
static
const
int
size = N;
119
static
const
bool
ok
=
true
;
120
};
121
122
// this one is used to rule out only const char[N]
123
template
<
typename
T >
124
struct
ExceptConstCharArrayDetector
125
{
126
typedef
Dummy
Type
;
127
};
128
template
<
int
N >
129
struct
ExceptConstCharArrayDetector
< const char[ N ] >
130
{
131
};
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
138
{
139
typedef
Dummy
Type
;
140
};
141
template
<
int
N >
142
struct
ExceptCharArrayDetector
< char[ N ] >
143
{
144
};
145
template
<
int
N >
146
struct
ExceptCharArrayDetector
< const char[ N ] >
147
{
148
};
149
150
template
<
typename
T1,
typename
T2 =
void
>
151
struct
SalUnicodePtrDetector
152
{
153
static
const
bool
ok
=
false
;
154
};
155
template
<
typename
T >
156
struct
SalUnicodePtrDetector
< const
sal_Unicode
*, T >
157
{
158
typedef
T
Type
;
159
static
const
bool
ok =
true
;
160
};
161
template
<
typename
T >
162
struct
SalUnicodePtrDetector
<
sal_Unicode
*, T >
163
{
164
typedef
T
Type
;
165
static
const
bool
ok =
true
;
166
};
167
168
// SFINAE helper class
169
template
<
typename
T,
bool
>
170
struct
Enable
171
{
172
};
173
174
template
<
typename
T >
175
struct
Enable
< T, true >
176
{
177
typedef
T
Type
;
178
};
179
180
181
}
/* Namespace */
182
183
}
/* Namespace */
184
185
#endif
/* _RTL_STRINGUTILS_HXX_ */
186
187
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Generated on Mon Oct 21 2013 19:45:27 for LibreOffice by
1.8.4