My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // Manually defining RTL_DISABLE_FAST_STRING allows to force turning fast string concatenation off
35 // (e.g. for debugging).
36 #ifndef RTL_DISABLE_FAST_STRING
37 #ifndef HAVE_SFINAE_ANONYMOUS_BROKEN
38 // This feature is not part of public API and is meant to be used only internally by LibreOffice.
39 #ifdef LIBO_INTERNAL_ONLY
40 // Enable fast string concatenation.
41 #define RTL_FAST_STRING
42 #endif
43 #endif
44 #endif
45 
46 // The unittest uses slightly different code to help check that the proper
47 // calls are made. The class is put into a different namespace to make
48 // sure the compiler generates a different (if generating also non-inline)
49 // copy of the function and does not merge them together. The class
50 // is "brought" into the proper rtl namespace by a typedef below.
51 #ifdef RTL_STRING_UNITTEST
52 #define rtl rtlunittest
53 #endif
54 
55 namespace rtl
56 {
57 
58 #ifdef RTL_STRING_UNITTEST
59 #undef rtl
60 #endif
61 
62 namespace internal
63 {
64 /*
65 These templates use SFINAE (Substitution failure is not an error) to help distinguish the various
66 plain C string types: char*, const char*, char[N], const char[N], char[] and const char[].
67 There are 2 cases:
68 1) Only string literal (i.e. const char[N]) is wanted, not any of the others.
69  In this case it is necessary to distinguish between const char[N] and char[N], as the latter
70  would be automatically converted to the const variant, which is not wanted (not a string literal
71  with known size of the content). In this case ConstCharArrayDetector is used to ensure the function
72  is called only with const char[N] arguments. There's no other plain C string type overload.
73 2) All plain C string types are wanted, and const char[N] needs to be handled differently.
74  In this case const char[N] would match const char* argument type (not exactly sure why, but it's
75  consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type
76  avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer
77  arguments. The const in the argument is necessary to handle the case when something is explicitly
78  cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference
79  being const, it would also match const char[N], so another overload with a reference to non-const
80  and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N].
81 Additionally, char[] and const char[] (i.e. size unknown) are rather tricky. Their usage with 'T&' would
82 mean it would be 'char(&)[]', which seems to be invalid. But gcc and clang somehow manage when it is
83 a template. while msvc complains about no conversion from char[] to char[1]. And the reference cannot
84 be avoided, because 'const char[]' as argument type would match also 'const char[N]'
85 So char[] and const char[] should always be used with their contents specified (which automatically
86 turns them into char[N] or const char[N]), or char* and const char* should be used.
87 */
88 struct Dummy {};
89 template< typename T1, typename T2 >
91 {
92 };
93 template< typename T >
94 struct CharPtrDetector< const char*, T >
95 {
96  typedef T Type;
97 };
98 template< typename T >
99 struct CharPtrDetector< char*, T >
100 {
101  typedef T Type;
102 };
103 
104 template< typename T1, typename T2 >
106 {
107 };
108 template< typename T, int N >
109 struct NonConstCharArrayDetector< char[ N ], T >
110 {
111  typedef T Type;
112 };
113 #ifdef RTL_STRING_UNITTEST
114 // never use, until all compilers handle this
115 template< typename T >
116 struct NonConstCharArrayDetector< char[], T >
117 {
118  typedef T Type;
119 };
120 template< typename T >
121 struct NonConstCharArrayDetector< const char[], T >
122 {
123  typedef T Type;
124 };
125 #endif
126 
127 template< typename T1, typename T2 = void >
129 {
130  static const bool ok = false;
131 };
132 template< int N, typename T >
133 struct ConstCharArrayDetector< const char[ N ], T >
134 {
135  typedef T Type;
136  static const int size = N;
137  static const bool ok = true;
138 };
139 
140 // this one is used to rule out only const char[N]
141 template< typename T >
143 {
144  typedef Dummy Type;
145 };
146 template< int N >
147 struct ExceptConstCharArrayDetector< const char[ N ] >
148 {
149 };
150 // this one is used to rule out only const char[N]
151 // (const will be brought in by 'const T&' in the function call)
152 // msvc needs const char[N] here (not sure whether gcc or msvc
153 // are right, it doesn't matter).
154 template< typename T >
156 {
157  typedef Dummy Type;
158 };
159 template< int N >
160 struct ExceptCharArrayDetector< char[ N ] >
161 {
162 };
163 template< int N >
164 struct ExceptCharArrayDetector< const char[ N ] >
165 {
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: */