LibreOffice Module codemaker (master) 1
commoncpp.cxx
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
23
24#include <codemaker/options.hxx>
26#include <codemaker/unotype.hxx>
27
28#include <rtl/strbuf.hxx>
29#include <rtl/string.hxx>
30#include <rtl/ustring.hxx>
31#include <sal/types.h>
32#include <o3tl/string_view.hxx>
33
34#include <vector>
35
36namespace codemaker::cpp {
37
38OString scopedCppName(OString const & type, bool ns_alias)
39{
40 char c('/');
41 sal_Int32 nPos = type.lastIndexOf( c );
42 if (nPos == -1) {
43 nPos = type.lastIndexOf( '.' );
44 if (nPos == -1)
45 return type;
46
47 c = '.';
48 }
49
50 OStringBuffer tmpBuf(type.getLength()*2);
51 nPos = 0;
52 do
53 {
54 tmpBuf.append(OString::Concat("::") + o3tl::getToken(type, 0, c, nPos));
55 } while( nPos != -1 );
56
57 OString s(tmpBuf.makeStringAndClear());
58 if (ns_alias && s.startsWith("::com::sun::star::", &s))
59 {
60 s = "::css::" + s; // nicer shorthand
61 }
62
63 return s;
64}
65
67 codemaker::UnoType::Sort sort, std::u16string_view nucleus)
68{
69 OStringBuffer buf;
71 static char const * const cppTypes[static_cast<int>(codemaker::UnoType::Sort::Any) + 1] = {
72 "void", "::sal_Bool", "::sal_Int8", "::sal_Int16", "::sal_uInt16",
73 "::sal_Int32", "::sal_uInt32", "::sal_Int64", "::sal_uInt64",
74 "float", "double", "::sal_Unicode", "rtl::OUString",
75 "::css::uno::Type", "::css::uno::Any" };
76 buf.append(cppTypes[static_cast<int>(sort)]);
77 } else {
79 && nucleus == u"com.sun.star.uno.XInterface")
80 {
81 buf.append("::css::uno::XInterface");
82 } else {
83 //TODO: check that nucleus is a valid (UTF-8) identifier
84 buf.append(u2b(nucleus));
85 }
86 }
87 return buf.makeStringAndClear();
88}
89
91 OString const & unoIdentifier, std::string_view prefix,
92 IdentifierTranslationMode transmode, OString const * forbidden)
93{
94 if (// Keywords:
95 unoIdentifier == "asm"
96 || unoIdentifier == "auto"
97 || unoIdentifier == "bool"
98 || unoIdentifier == "break"
99 || unoIdentifier == "case"
100 || unoIdentifier == "catch"
101 || unoIdentifier == "char"
102 || unoIdentifier == "class"
103 || unoIdentifier == "const"
104 /* unoIdentifier == "const_cast" */
105 || unoIdentifier == "continue"
106 || unoIdentifier == "default"
107 || unoIdentifier == "delete"
108 || unoIdentifier == "do"
109 || unoIdentifier == "double"
110 /* unoIdentifier == "dynamic_cast" */
111 || unoIdentifier == "else"
112 || unoIdentifier == "enum"
113 || unoIdentifier == "explicit"
114 || unoIdentifier == "export"
115 || unoIdentifier == "extern"
116 || unoIdentifier == "false"
117 || unoIdentifier == "float"
118 || unoIdentifier == "for"
119 || unoIdentifier == "friend"
120 || unoIdentifier == "goto"
121 || unoIdentifier == "if"
122 || unoIdentifier == "inline"
123 || unoIdentifier == "int"
124 || unoIdentifier == "long"
125 || unoIdentifier == "mutable"
126 || unoIdentifier == "namespace"
127 || unoIdentifier == "new"
128 || unoIdentifier == "operator"
129 || unoIdentifier == "private"
130 || unoIdentifier == "protected"
131 || unoIdentifier == "public"
132 || unoIdentifier == "register"
133 /* unoIdentifier == "reinterpret_cast" */
134 || unoIdentifier == "return"
135 || unoIdentifier == "short"
136 || unoIdentifier == "signed"
137 || unoIdentifier == "sizeof"
138 || unoIdentifier == "static"
139 /* unoIdentifier == "static_cast" */
140 || unoIdentifier == "struct"
141 || unoIdentifier == "switch"
142 || unoIdentifier == "template"
143 || unoIdentifier == "this"
144 || unoIdentifier == "throw"
145 || unoIdentifier == "true"
146 || unoIdentifier == "try"
147 || unoIdentifier == "typedef"
148 || unoIdentifier == "typeid"
149 || unoIdentifier == "typename"
150 || unoIdentifier == "union"
151 || unoIdentifier == "unsigned"
152 || unoIdentifier == "using"
153 || unoIdentifier == "virtual"
154 || unoIdentifier == "void"
155 || unoIdentifier == "volatile"
156 /* unoIdentifier == "wchar_t" */
157 || unoIdentifier == "while"
158 // Alternative representations:
159 || unoIdentifier == "and"
160 /* unoIdentifier == "and_eq" */
161 || unoIdentifier == "bitand"
162 || unoIdentifier == "bitor"
163 || unoIdentifier == "compl"
164 || unoIdentifier == "not"
165 /* unoIdentifier == "not_eq" */
166 || unoIdentifier == "or"
167 /* unoIdentifier == "or_eq" */
168 || unoIdentifier == "xor"
169 /* unoIdentifier == "xor_eq" */
170 // Standard macros:
171 || (unoIdentifier == "BUFSIZ"
172 || unoIdentifier == "CLOCKS_PER_SEC"
173 || unoIdentifier == "EDOM"
174 || unoIdentifier == "EOF"
175 || unoIdentifier == "ERANGE"
176 || unoIdentifier == "EXIT_FAILURE"
177 || unoIdentifier == "EXIT_SUCCESS"
178 || unoIdentifier == "FILENAME_MAX"
179 || unoIdentifier == "FOPEN_MAX"
180 || unoIdentifier == "HUGE_VAL"
181 || unoIdentifier == "LC_ALL"
182 || unoIdentifier == "LC_COLLATE"
183 || unoIdentifier == "LC_CTYPE"
184 || unoIdentifier == "LC_MONETARY"
185 || unoIdentifier == "LC_NUMERIC"
186 || unoIdentifier == "LC_TIME"
187 || unoIdentifier == "L_tmpnam"
188 || unoIdentifier == "MB_CUR_MAX"
189 || unoIdentifier == "NULL"
190 || unoIdentifier == "RAND_MAX"
191 || unoIdentifier == "SEEK_CUR"
192 || unoIdentifier == "SEEK_END"
193 || unoIdentifier == "SEEK_SET"
194 || unoIdentifier == "SIGABRT"
195 || unoIdentifier == "SIGFPE"
196 || unoIdentifier == "SIGILL"
197 || unoIdentifier == "SIGINT"
198 || unoIdentifier == "SIGSEGV"
199 || unoIdentifier == "SIGTERM"
200 || unoIdentifier == "SIG_DFL"
201 || unoIdentifier == "SIG_ERR"
202 || unoIdentifier == "SIG_IGN"
203 || unoIdentifier == "TMP_MAX"
204 || unoIdentifier == "WCHAR_MAX"
205 || unoIdentifier == "WCHAR_MIN"
206 || unoIdentifier == "WEOF"
207 /* unoIdentifier == "_IOFBF" */
208 /* unoIdentifier == "_IOLBF" */
209 /* unoIdentifier == "_IONBF" */
210 || unoIdentifier == "assert"
211 || unoIdentifier == "errno"
212 || unoIdentifier == "offsetof"
213 || unoIdentifier == "setjmp"
214 || unoIdentifier == "stderr"
215 || unoIdentifier == "stdin"
216 || unoIdentifier == "stdout"
217 /* unoIdentifier == "va_arg" */
218 /* unoIdentifier == "va_end" */
219 /* unoIdentifier == "va_start" */
220 // Standard values:
221 || unoIdentifier == "CHAR_BIT"
222 || unoIdentifier == "CHAR_MAX"
223 || unoIdentifier == "CHAR_MIN"
224 || unoIdentifier == "DBL_DIG"
225 || unoIdentifier == "DBL_EPSILON"
226 || unoIdentifier == "DBL_MANT_DIG"
227 || unoIdentifier == "DBL_MAX"
228 || unoIdentifier == "DBL_MAX_10_EXP"
229 || unoIdentifier == "DBL_MAX_EXP"
230 || unoIdentifier == "DBL_MIN"
231 || unoIdentifier == "DBL_MIN_10_EXP"
232 || unoIdentifier == "DBL_MIN_EXP"
233 || unoIdentifier == "FLT_DIG"
234 || unoIdentifier == "FLT_EPSILON"
235 || unoIdentifier == "FLT_MANT_DIG"
236 || unoIdentifier == "FLT_MAX"
237 || unoIdentifier == "FLT_MAX_10_EXP"
238 || unoIdentifier == "FLT_MAX_EXP"
239 || unoIdentifier == "FLT_MIN"
240 || unoIdentifier == "FLT_MIN_10_EXP"
241 || unoIdentifier == "FLT_MIN_EXP"
242 || unoIdentifier == "FLT_RADIX"
243 || unoIdentifier == "FLT_ROUNDS"
244 || unoIdentifier == "INT_MAX"
245 || unoIdentifier == "INT_MIN"
246 || unoIdentifier == "LDBL_DIG"
247 || unoIdentifier == "LDBL_EPSILON"
248 || unoIdentifier == "LDBL_MANT_DIG"
249 || unoIdentifier == "LDBL_MAX"
250 || unoIdentifier == "LDBL_MAX_10_EXP"
251 || unoIdentifier == "LDBL_MAX_EXP"
252 || unoIdentifier == "LDBL_MIN"
253 || unoIdentifier == "LDBL_MIN_10_EXP"
254 || unoIdentifier == "LDBL_MIN_EXP"
255 || unoIdentifier == "LONG_MAX"
256 || unoIdentifier == "LONG_MIN"
257 || unoIdentifier == "MB_LEN_MAX"
258 || unoIdentifier == "SCHAR_MAX"
259 || unoIdentifier == "SCHAR_MIN"
260 || unoIdentifier == "SHRT_MAX"
261 || unoIdentifier == "SHRT_MIN"
262 || unoIdentifier == "UCHAR_MAX"
263 || unoIdentifier == "UINT_MAX"
264 || unoIdentifier == "ULONG_MAX"
265 || unoIdentifier == "USHRT_MAX")
266 || (transmode == IdentifierTranslationMode::Global
267 && (// Standard types:
268 /* unoIdentifier == "clock_t" */
269 /* unoIdentifier == "div_t" */
270 unoIdentifier == "FILE"
271 /* unoIdentifier == "fpos_t" */
272 /* unoIdentifier == "jmp_buf" */
273 || unoIdentifier == "lconv"
274 /* unoIdentifier == "ldiv_t" */
275 /* unoIdentifier == "mbstate_t" */
276 /* unoIdentifier == "ptrdiff_t" */
277 /* unoIdentifier == "sig_atomic_t" */
278 /* unoIdentifier == "size_t" */
279 /* unoIdentifier == "time_t" */
280 || unoIdentifier == "tm"
281 /* unoIdentifier == "va_list" */
282 /* unoIdentifier == "wctrans_t" */
283 /* unoIdentifier == "wctype_t" */
284 /* unoIdentifier == "wint_t" */
285 // Standard namespaces:
286 || unoIdentifier == "std"))
287 // Others:
288 || unoIdentifier == "NDEBUG"
289 || (forbidden != nullptr && unoIdentifier == *forbidden) )
290 {
291 return OString::Concat(prefix) + "_" + unoIdentifier;
292 } else {
293 return unoIdentifier;
294 }
295}
296
297}
298
299/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float u
sal_uInt16 nPos
Sort
An enumeration of all the sorts of relevant UNOIDL entities.
Definition: unotype.hxx:33
OString translateUnoToCppIdentifier(OString const &unoIdentifier, std::string_view prefix, IdentifierTranslationMode transmode, OString const *forbidden)
Definition: commoncpp.cxx:90
OString translateUnoToCppType(codemaker::UnoType::Sort sort, std::u16string_view nucleus)
Definition: commoncpp.cxx:66
OString scopedCppName(OString const &type, bool ns_alias)
Definition: commoncpp.cxx:38
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString u2b(std::u16string_view s)
Definition: typemanager.hxx:67
ResultType type