LibreOffice Module codemaker (master) 1
includes.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
21#include "includes.hxx"
22
23#include "dependencies.hxx"
24#include "dumputils.hxx"
25
26#include <codemaker/global.hxx>
28#include <codemaker/unotype.hxx>
29
30#include <osl/diagnose.h>
31#include <rtl/ref.hxx>
32#include <rtl/string.hxx>
33#include <rtl/ustring.hxx>
34#include <sal/types.h>
35
36#include <utility>
37#include <vector>
38
40
41Includes::Includes(
43 codemaker::cppumaker::Dependencies const & dependencies, bool hpp):
44 m_manager(std::move(manager)), m_map(dependencies.getMap()), m_hpp(hpp),
45 m_includeCassert(false),
46 m_includeAny(dependencies.hasAnyDependency()), m_includeReference(false),
47 m_includeSequence(dependencies.hasSequenceDependency()),
48 m_includeType(dependencies.hasTypeDependency()),
49 m_includeCppuMacrosHxx(false), m_includeCppuUnotypeHxx(false),
50 m_includeOslMutexHxx(false),
51 m_includeRtlStrbufHxx(false), m_includeRtlStringH(false),
52 m_includeRtlTextencH(false), m_includeRtlUstrbufHxx(false),
53 m_includeRtlUstringH(false),
54 m_includeRtlUstringHxx(dependencies.hasStringDependency()),
55 m_includeRtlInstanceHxx(false),
56 m_includeSalTypesH(
57 dependencies.hasBooleanDependency() || dependencies.hasByteDependency()
58 || dependencies.hasShortDependency()
59 || dependencies.hasUnsignedShortDependency()
60 || dependencies.hasLongDependency()
61 || dependencies.hasUnsignedLongDependency()
62 || dependencies.hasHyperDependency()
63 || dependencies.hasUnsignedHyperDependency()
64 || dependencies.hasCharDependency()),
65 m_includeTypelibTypeclassH(false),
66 m_includeTypelibTypedescriptionH(false)
67{}
68
70{}
71
72void Includes::add(OString const & entityName) {
73 sal_Int32 k;
74 std::vector< OString > args;
75 OUString n(b2u(codemaker::UnoType::decompose(entityName, &k, &args)));
76 if (k != 0) {
77 m_includeSequence = true;
78 }
79 switch (m_manager->getSort(n)) {
89 m_includeSalTypesH = true;
90 break;
93 break;
96 break;
98 m_includeType = true;
99 break;
101 m_includeAny = true;
102 break;
104 for (const OString& arg : args)
105 {
106 add(arg);
107 }
108 [[fallthrough]];
116 break;
117 default:
119 "unexpected type \"" + b2u(entityName)
120 + "\" in call to codemaker::cppumaker::Includes::add");
121 }
122}
123
124namespace {
125
126void dumpEmptyLineBeforeFirst(FileStream & out, bool * first) {
127 OSL_ASSERT(first != nullptr);
128 if (*first) {
129 out << "\n";
130 *first = false;
131 }
132}
133
134}
135
137 FileStream & out, OUString const * companionHdl, bool exceptions)
138{
139 OSL_ASSERT(companionHdl == nullptr || m_hpp);
140 if (!m_includeReference) {
141 for (const auto& pair : m_map)
142 {
143 if (isInterfaceType(u2b(pair.first))) {
144 m_includeReference = true;
145 break;
146 }
147 }
148 }
149 out << "#include \"sal/config.h\"\n";
150 if (m_includeCassert) {
151 out << "\n#include <cassert>\n";
152 }
153 if (companionHdl) {
154 out << "\n";
155 dumpInclude(out, u2b(*companionHdl), false);
156 }
157 bool first = true;
158 for (const auto& pair : m_map)
159 {
160 if (exceptions || pair.second != Dependencies::KIND_EXCEPTION) {
161 dumpEmptyLineBeforeFirst(out, &first);
162 if (m_hpp || pair.second == Dependencies::KIND_BASE
163 || !isInterfaceType(u2b(pair.first)))
164 {
165 // If we know our name, then avoid including ourselves.
166 if (!companionHdl || *companionHdl != pair.first) {
167 dumpInclude(out, u2b(pair.first), m_hpp);
168 }
169 } else {
170 bool ns = dumpNamespaceOpen(out, pair.first, false);
171 if (ns) {
172 out << " ";
173 }
174 out << "class ";
175 dumpTypeIdentifier(out, pair.first);
176 out << ";";
177 if (ns) {
178 out << " ";
179 }
180 dumpNamespaceClose(out, pair.first, false);
181 out << "\n";
182 }
183 }
184 }
185 static char const * hxxExtension[2] = { "h", "hxx" };
186 if (m_includeAny) {
187 dumpEmptyLineBeforeFirst(out, &first);
188 out << "#include \"com/sun/star/uno/Any." << hxxExtension[m_hpp]
189 << "\"\n";
190 }
191 if (m_includeReference) {
192 dumpEmptyLineBeforeFirst(out, &first);
193 out << "#include \"com/sun/star/uno/Reference." << hxxExtension[m_hpp]
194 << "\"\n";
195 }
196 if (m_includeSequence) {
197 dumpEmptyLineBeforeFirst(out, &first);
198 out << "#include \"com/sun/star/uno/Sequence." << hxxExtension[m_hpp]
199 << "\"\n";
200 }
201 if (m_includeType) {
202 dumpEmptyLineBeforeFirst(out, &first);
203 out << "#include \"com/sun/star/uno/Type." << hxxExtension[m_hpp]
204 << "\"\n";
205 }
207 dumpEmptyLineBeforeFirst(out, &first);
208 out << "#include \"cppu/macros.hxx\"\n";
209 }
211 dumpEmptyLineBeforeFirst(out, &first);
212 out << "#include \"cppu/unotype.hxx\"\n";
213 }
215 dumpEmptyLineBeforeFirst(out, &first);
216 out << "#include \"osl/mutex.hxx\"\n";
217 }
219 dumpEmptyLineBeforeFirst(out, &first);
220 out << "#include \"rtl/strbuf.hxx\"\n";
221 }
223 dumpEmptyLineBeforeFirst(out, &first);
224 out << "#include \"rtl/string.h\"\n";
225 }
227 dumpEmptyLineBeforeFirst(out, &first);
228 out << "#include \"rtl/textenc.h\"\n";
229 }
231 dumpEmptyLineBeforeFirst(out, &first);
232 out << "#include \"rtl/ustrbuf.hxx\"\n";
233 }
235 dumpEmptyLineBeforeFirst(out, &first);
236 out << "#include \"rtl/ustring.h\"\n";
237 }
239 dumpEmptyLineBeforeFirst(out, &first);
240 out << "#include \"rtl/ustring.hxx\"\n";
241 }
243 dumpEmptyLineBeforeFirst(out, &first);
244 out << "#include \"rtl/instance.hxx\"\n";
245 }
246 if (m_includeSalTypesH) {
247 dumpEmptyLineBeforeFirst(out, &first);
248 out << "#include \"sal/types.h\"\n";
249 }
251 dumpEmptyLineBeforeFirst(out, &first);
252 out << "#include \"typelib/typeclass.h\"\n";
253 }
255 dumpEmptyLineBeforeFirst(out, &first);
256 out << "#include \"typelib/typedescription.h\"\n";
257 }
258 for (OUString const & s : m_custom)
259 out << s << "\n";
260}
261
263 FileStream & out, OString const & entityName, bool hpp)
264{
265 out << "#include \"" << entityName.replace('.', '/') << "."
266 << (hpp ? "hpp" : "hdl") << "\"\n";
267}
268
269bool Includes::isInterfaceType(std::string_view entityName) const {
270 return m_manager->getSort(b2u(entityName)) == UnoType::Sort::Interface;
271}
272
273/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A simple class to track which other entities a given entity depends on.
void add(OString const &entityName)
Definition: includes.cxx:72
bool isInterfaceType(std::string_view entityName) const
Definition: includes.cxx:269
static void dumpInclude(FileStream &out, OString const &entityName, bool hpp)
Definition: includes.cxx:262
void dump(FileStream &out, OUString const *companionHdl, bool exceptions)
Definition: includes.cxx:136
rtl::Reference< TypeManager > m_manager
Definition: includes.hxx:74
Dependencies::Map m_map
Definition: includes.hxx:75
std::vector< OUString > m_custom
Definition: includes.hxx:95
t_map m_map
rtl::Reference< ParseManager > manager
sal_Int64 n
rtl::OString decompose(rtl::OString const &type, sal_Int32 *rank=nullptr, std::vector< rtl::OString > *arguments=nullptr)
Decomposes a UNO type name or UNO type registry name.
void dumpTypeIdentifier(FileStream &out, std::u16string_view entityName)
Definition: dumputils.cxx:73
bool dumpNamespaceClose(FileStream &out, std::u16string_view entityName, bool fullModuleType)
Definition: dumputils.cxx:51
bool dumpNamespaceOpen(FileStream &out, std::u16string_view entityName, bool fullModuleType)
Definition: dumputils.cxx:32
ns
constexpr OUStringLiteral first
args
OUString b2u(std::string_view s)
Definition: typemanager.hxx:71
OString u2b(std::u16string_view s)
Definition: typemanager.hxx:67