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