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