tdf#162912 i18npool: Updated CJK BreakIterator to use custom rules
[LibreOffice.git] / codemaker / source / cppumaker / includes.cxx
blobec8a9ea3c8c58be1f819a276dd216d0e6256a9df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "includes.hxx"
23 #include "cpputype.hxx"
24 #include "dependencies.hxx"
25 #include "dumputils.hxx"
27 #include <codemaker/global.hxx>
28 #include <codemaker/typemanager.hxx>
29 #include <codemaker/unotype.hxx>
31 #include <osl/diagnose.h>
32 #include <rtl/ref.hxx>
33 #include <rtl/string.hxx>
34 #include <rtl/ustring.hxx>
35 #include <sal/types.h>
37 #include <utility>
38 #include <vector>
40 using codemaker::cppumaker::Includes;
42 Includes::Includes(
43 rtl::Reference< TypeManager > manager,
44 codemaker::cppumaker::Dependencies const & dependencies, FileType eFileType):
45 m_manager(std::move(manager)), m_map(dependencies.getMap()), m_filetype(eFileType),
46 m_includeCassert(false),
47 m_includeAny(dependencies.hasAnyDependency()), m_includeReference(false),
48 m_includeSequence(dependencies.hasSequenceDependency()),
49 m_includeType(dependencies.hasTypeDependency()),
50 m_includeCppuMacrosHxx(false), m_includeCppuUnotypeHxx(false),
51 m_includeOslMutexHxx(false),
52 m_includeRtlStrbufHxx(false), m_includeRtlStringH(false),
53 m_includeRtlTextencH(false), m_includeRtlUstrbufHxx(false),
54 m_includeRtlUstringH(false),
55 m_includeRtlUstringHxx(dependencies.hasStringDependency()),
56 m_includeRtlInstanceHxx(false),
57 m_includeSalTypesH(
58 dependencies.hasBooleanDependency() || dependencies.hasByteDependency()
59 || dependencies.hasShortDependency()
60 || dependencies.hasUnsignedShortDependency()
61 || dependencies.hasLongDependency()
62 || dependencies.hasUnsignedLongDependency()
63 || dependencies.hasHyperDependency()
64 || dependencies.hasUnsignedHyperDependency()
65 || dependencies.hasCharDependency()),
66 m_includeTypelibTypeclassH(false),
67 m_includeTypelibTypedescriptionH(false)
70 Includes::~Includes()
73 void Includes::add(OString const & entityName) {
74 sal_Int32 k;
75 std::vector< OString > args;
76 OUString n(b2u(codemaker::UnoType::decompose(entityName, &k, &args)));
77 if (k != 0) {
78 m_includeSequence = true;
80 switch (m_manager->getSort(n)) {
81 case codemaker::UnoType::Sort::Boolean:
82 case codemaker::UnoType::Sort::Byte:
83 case codemaker::UnoType::Sort::Short:
84 case codemaker::UnoType::Sort::UnsignedShort:
85 case codemaker::UnoType::Sort::Long:
86 case codemaker::UnoType::Sort::UnsignedLong:
87 case codemaker::UnoType::Sort::Hyper:
88 case codemaker::UnoType::Sort::UnsignedHyper:
89 case codemaker::UnoType::Sort::Char:
90 m_includeSalTypesH = true;
91 break;
92 case codemaker::UnoType::Sort::Float:
93 case codemaker::UnoType::Sort::Double:
94 break;
95 case codemaker::UnoType::Sort::String:
96 m_includeRtlUstringHxx = true;
97 break;
98 case codemaker::UnoType::Sort::Type:
99 m_includeType = true;
100 break;
101 case codemaker::UnoType::Sort::Any:
102 m_includeAny = true;
103 break;
104 case codemaker::UnoType::Sort::PolymorphicStructTemplate:
105 for (const OString& arg : args)
107 add(arg);
109 [[fallthrough]];
110 case codemaker::UnoType::Sort::Sequence:
111 case codemaker::UnoType::Sort::Enum:
112 case codemaker::UnoType::Sort::PlainStruct:
113 case codemaker::UnoType::Sort::Exception:
114 case codemaker::UnoType::Sort::Interface:
115 case codemaker::UnoType::Sort::Typedef:
116 m_map.emplace(n, Dependencies::KIND_NORMAL);
117 break;
118 default:
119 throw CannotDumpException(
120 "unexpected type \"" + b2u(entityName)
121 + "\" in call to codemaker::cppumaker::Includes::add");
125 namespace {
127 void dumpEmptyLineBeforeFirst(FileStream & out, bool * first) {
128 assert(first != nullptr);
129 if (*first) {
130 out << "\n";
131 *first = false;
137 void Includes::dump(
138 FileStream & out, OUString const * companionHdl, bool exceptions)
140 OSL_ASSERT(companionHdl == nullptr || m_filetype == FileType::HPP);
141 if (!m_includeReference) {
142 for (const auto& pair : m_map)
144 if (isInterfaceType(u2b(pair.first))) {
145 m_includeReference = true;
146 break;
150 out << "#include \"sal/config.h\"\n";
151 if (m_includeCassert) {
152 out << "\n#include <cassert>\n";
154 if (companionHdl) {
155 out << "\n";
156 dumpInclude(out, u2b(*companionHdl), false);
158 bool first = true;
159 for (const auto& pair : m_map)
161 if (exceptions || pair.second != Dependencies::KIND_EXCEPTION) {
162 dumpEmptyLineBeforeFirst(out, &first);
163 if ((m_filetype == FileType::HPP) || pair.second == Dependencies::KIND_BASE
164 || !isInterfaceType(u2b(pair.first)))
166 // If we know our name, then avoid including ourselves.
167 if (!companionHdl || *companionHdl != pair.first) {
168 dumpInclude(out, u2b(pair.first), (m_filetype == FileType::HPP));
170 } else {
171 bool ns = dumpNamespaceOpen(out, pair.first, false);
172 if (ns) {
173 out << " ";
175 out << "class ";
176 dumpTypeIdentifier(out, pair.first);
177 out << ";";
178 if (ns) {
179 out << " ";
181 dumpNamespaceClose(out, pair.first, false);
182 out << "\n";
186 static char const * hxxExtension[2] = { "h", "hxx" };
187 if (m_includeAny) {
188 dumpEmptyLineBeforeFirst(out, &first);
189 out << "#include \"com/sun/star/uno/Any." << hxxExtension[(m_filetype == FileType::HPP)]
190 << "\"\n";
192 if (m_includeReference) {
193 dumpEmptyLineBeforeFirst(out, &first);
194 out << "#include \"com/sun/star/uno/Reference." << hxxExtension[(m_filetype == FileType::HPP)]
195 << "\"\n";
197 if (m_includeSequence) {
198 dumpEmptyLineBeforeFirst(out, &first);
199 out << "#include \"com/sun/star/uno/Sequence." << hxxExtension[(m_filetype == FileType::HPP)]
200 << "\"\n";
202 if (m_includeType) {
203 dumpEmptyLineBeforeFirst(out, &first);
204 out << "#include \"com/sun/star/uno/Type." << hxxExtension[(m_filetype == FileType::HPP)]
205 << "\"\n";
207 if (m_includeCppuMacrosHxx) {
208 dumpEmptyLineBeforeFirst(out, &first);
209 out << "#include \"cppu/macros.hxx\"\n";
211 if (m_includeCppuUnotypeHxx) {
212 dumpEmptyLineBeforeFirst(out, &first);
213 out << "#include \"cppu/unotype.hxx\"\n";
215 if (m_includeOslMutexHxx) {
216 dumpEmptyLineBeforeFirst(out, &first);
217 out << "#include \"osl/mutex.hxx\"\n";
219 if (m_includeRtlStrbufHxx) {
220 dumpEmptyLineBeforeFirst(out, &first);
221 out << "#include \"rtl/strbuf.hxx\"\n";
223 if (m_includeRtlStringH) {
224 dumpEmptyLineBeforeFirst(out, &first);
225 out << "#include \"rtl/string.h\"\n";
227 if (m_includeRtlTextencH) {
228 dumpEmptyLineBeforeFirst(out, &first);
229 out << "#include \"rtl/textenc.h\"\n";
231 if (m_includeRtlUstrbufHxx) {
232 dumpEmptyLineBeforeFirst(out, &first);
233 out << "#include \"rtl/ustrbuf.hxx\"\n";
235 if (m_includeRtlUstringH) {
236 dumpEmptyLineBeforeFirst(out, &first);
237 out << "#include \"rtl/ustring.h\"\n";
239 if (m_includeRtlUstringHxx) {
240 dumpEmptyLineBeforeFirst(out, &first);
241 out << "#include \"rtl/ustring.hxx\"\n";
243 if (m_includeRtlInstanceHxx) {
244 dumpEmptyLineBeforeFirst(out, &first);
245 out << "#include \"rtl/instance.hxx\"\n";
247 if (m_includeSalTypesH) {
248 dumpEmptyLineBeforeFirst(out, &first);
249 out << "#include \"sal/types.h\"\n";
251 if (m_includeTypelibTypeclassH) {
252 dumpEmptyLineBeforeFirst(out, &first);
253 out << "#include \"typelib/typeclass.h\"\n";
255 if (m_includeTypelibTypedescriptionH) {
256 dumpEmptyLineBeforeFirst(out, &first);
257 out << "#include \"typelib/typedescription.h\"\n";
259 for (OUString const & s : m_custom)
260 out << s << "\n";
263 void Includes::dumpInclude(
264 FileStream & out, OString const & entityName, bool hpp)
266 out << "#include \"" << entityName.replace('.', '/') << "."
267 << (hpp ? "hpp" : "hdl") << "\"\n";
270 bool Includes::isInterfaceType(std::string_view entityName) const {
271 return m_manager->getSort(b2u(entityName)) == UnoType::Sort::Interface;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */