My Project
UDK 3.2.7 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
* Version: MPL 1.1 / GPLv3+ / LGPLv3+
4
*
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License or as specified alternatively below. You may obtain a copy of
8
* the License at http://www.mozilla.org/MPL/
9
*
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
13
* License.
14
*
15
* Major Contributor(s):
16
* [ Copyright (C) 2012 Lubos Lunak <l.lunak@suse.cz> (initial developer) ]
17
*
18
* All Rights Reserved.
19
*
20
* For minor contributions see the git repository.
21
*
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24
* the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25
* in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26
* instead of those above.
27
*/
28
29
#ifndef _RTL_STRINGUTILS_HXX_
30
#define _RTL_STRINGUTILS_HXX_
31
32
#include "
sal/config.h
"
33
34
// The unittest uses slightly different code to help check that the proper
35
// calls are made. The class is put into a different namespace to make
36
// sure the compiler generates a different (if generating also non-inline)
37
// copy of the function and does not merge them together. The class
38
// is "brought" into the proper rtl namespace by a typedef below.
39
#ifdef RTL_STRING_UNITTEST
40
#define rtl rtlunittest
41
#endif
42
43
namespace
rtl
44
{
45
46
#ifdef RTL_STRING_UNITTEST
47
#undef rtl
48
#endif
49
namespace
internal
50
{
51
/*
52
These templates use SFINAE (Substitution failure is not an error) to help distinguish the various
53
plain C string types: char*, const char*, char[N], const char[N], char[] and const char[].
54
There are 2 cases:
55
1) Only string literal (i.e. const char[N]) is wanted, not any of the others.
56
In this case it is necessary to distinguish between const char[N] and char[N], as the latter
57
would be automatically converted to the const variant, which is not wanted (not a string literal
58
with known size of the content). In this case ConstCharArrayDetector is used to ensure the function
59
is called only with const char[N] arguments. There's no other plain C string type overload.
60
2) All plain C string types are wanted, and const char[N] needs to be handled differently.
61
In this case const char[N] would match const char* argument type (not exactly sure why, but it's
62
consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type
63
avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer
64
arguments. The const in the argument is necessary to handle the case when something is explicitly
65
cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference
66
being const, it would also match const char[N], so another overload with a reference to non-const
67
and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N].
68
Additionally, char[] and const char[] (i.e. size unknown) are rather tricky. Their usage with 'T&' would
69
mean it would be 'char(&)[]', which seems to be invalid. But gcc and clang somehow manage when it is
70
a template. while msvc complains about no conversion from char[] to char[1]. And the reference cannot
71
be avoided, because 'const char[]' as argument type would match also 'const char[N]'
72
So char[] and const char[] should always be used with their contents specified (which automatically
73
turns them into char[N] or const char[N]), or char* and const char* should be used.
74
*/
75
struct
Dummy
{};
76
template
<
typename
T1,
typename
T2 >
77
struct
CharPtrDetector
78
{
79
};
80
template
<
typename
T >
81
struct
CharPtrDetector
< const char*, T >
82
{
83
typedef
T
Type
;
84
};
85
template
<
typename
T >
86
struct
CharPtrDetector
< char*, T >
87
{
88
typedef
T
Type
;
89
};
90
91
template
<
typename
T1,
typename
T2 >
92
struct
NonConstCharArrayDetector
93
{
94
};
95
template
<
typename
T,
int
N >
96
struct
NonConstCharArrayDetector
< char[ N ], T >
97
{
98
typedef
T
Type
;
99
};
100
#ifdef RTL_STRING_UNITTEST
101
// never use, until all compilers handle this
102
template
<
typename
T >
103
struct
NonConstCharArrayDetector
< char[], T >
104
{
105
typedef
T Type;
106
};
107
template
<
typename
T >
108
struct
NonConstCharArrayDetector< const char[], T >
109
{
110
typedef
T Type;
111
};
112
#endif
113
114
template
<
typename
T1,
typename
T2 >
115
struct
ConstCharArrayDetector
116
{
117
};
118
template
<
int
N,
typename
T >
119
struct
ConstCharArrayDetector
< const char[ N ], T >
120
{
121
typedef
T
Type
;
122
static
const
int
size = N;
123
};
124
125
// this one is used to rule out only const char[N]
126
template
<
typename
T >
127
struct
ExceptConstCharArrayDetector
128
{
129
typedef
Dummy
Type
;
130
};
131
template
<
int
N >
132
struct
ExceptConstCharArrayDetector
< const char[ N ] >
133
{
134
};
135
// this one is used to rule out only const char[N]
136
// (const will be brought in by 'const T&' in the function call)
137
// msvc needs const char[N] here (not sure whether gcc or msvc
138
// are right, it doesn't matter).
139
template
<
typename
T >
140
struct
ExceptCharArrayDetector
141
{
142
typedef
Dummy
Type
;
143
};
144
template
<
int
N >
145
struct
ExceptCharArrayDetector
< char[ N ] >
146
{
147
};
148
template
<
int
N >
149
struct
ExceptCharArrayDetector
< const char[ N ] >
150
{
151
};
152
153
}
/* Namespace */
154
155
}
/* Namespace */
156
157
#endif
/* _RTL_STRINGUTILS_HXX_ */
158
159
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Generated on Mon Oct 8 2012 00:36:42 for My Project by
1.8.1.2