/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
if (m_manager->foundAtPrimaryProvider(uname))
{ switch (m_manager->getSort(uname, &entity, &cursor))
{ case codemaker::UnoType::Sort::Module:
produceModule(name, cursor); break;
case codemaker::UnoType::Sort::Enum:
produceEnum(name, dynamic_cast<unoidl::EnumTypeEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::PlainStruct:
producePlainStruct(name, dynamic_cast<unoidl::PlainStructTypeEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::PolymorphicStructTemplate:
producePolyStruct(
name, dynamic_cast<unoidl::PolymorphicStructTypeTemplateEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::Exception:
produceException(name, dynamic_cast<unoidl::ExceptionTypeEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::Interface:
produceInterface(name, dynamic_cast<unoidl::InterfaceTypeEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::Typedef:
produceTypedef(name, dynamic_cast<unoidl::TypedefEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::ConstantGroup:
produceConstantGroup(name, dynamic_cast<unoidl::ConstantGroupEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::SingleInterfaceBasedService:
produceService(
name, dynamic_cast<unoidl::SingleInterfaceBasedServiceEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::InterfaceBasedSingleton:
produceSingleton(
name, dynamic_cast<unoidl::InterfaceBasedSingletonEntity*>(entity.get())); break;
case codemaker::UnoType::Sort::AccumulationBasedService: case codemaker::UnoType::Sort::ServiceBasedSingleton: // old-style services and singletons not supported break;
default: throw CannotDumpException(u"entity '"_ustr + uname + u"' has unexpected type"_ustr);
}
} else
{ // type from --extra-types switch (m_manager->getSort(uname, &entity, &cursor))
{ case codemaker::UnoType::Sort::Typedef:
produceTypedef(name, dynamic_cast<unoidl::TypedefEntity*>(entity.get())); break;
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public enum ")
.append(getSafeIdentifier(typeName))
.endLine()
.beginBlock(); for (constauto& member : entity->getMembers())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
file.beginLine()
.append(getSafeIdentifier(member.name))
.append(" = ")
.append(OString::number(member.value))
.append(",")
.endLine();
}
file.endBlock();
if (hasNamespace)
file.endBlock();
file.closeFile();
}
void NetProducer::producePlainStruct(std::string_view name, const rtl::Reference<unoidl::PlainStructTypeEntity>& entity)
{ // produce referenced types constauto& base = entity->getDirectBase(); if (!base.isEmpty())
produceType(getBaseUnoName(base)); for (constauto& member : entity->getDirectMembers())
produceType(getBaseUnoName(member.type));
CSharpFile file(m_outputDir, name);
// verbose and dry run checks if (m_verbose)
std::cout << "[struct] " << name << " -> " << file.getPath() << '\n'; if (m_dryRun) return;
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
// generate struct and base structs list
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public class ")
.append(getSafeIdentifier(typeName)); if (!base.isEmpty())
file.append(" : ").append(getNetName(base));
file.endLine().beginBlock();
// generate default constructor
file.beginLine()
.append("public ")
.append(getSafeIdentifier(typeName))
.append("()")
.endLine()
.beginBlock()
.endBlock();
file.endLine(); // extra blank line
// generate full constructor
std::vector<unoidl::PlainStructTypeEntity::Member> baseFields;
{
OUString baseTypeName = base; while (!baseTypeName.isEmpty())
{
rtl::Reference<unoidl::Entity> baseEntity; if (m_manager->getSort(baseTypeName, &baseEntity)
!= codemaker::UnoType::Sort::PlainStruct) throw CannotDumpException("'" + b2u(name) + "' base type '" + baseTypeName
+ "' is not a plain struct");
// generate struct fields for (constauto& member : entity->getDirectMembers())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
file.beginLine()
.append("public ")
.append(getNetName(member.type))
.append(" ")
.append(getSafeIdentifier(member.name))
.append(";")
.endLine();
}
file.endBlock(); if (hasNamespace)
file.endBlock();
file.closeFile();
}
void NetProducer::producePolyStruct(
std::string_view name, const rtl::Reference<unoidl::PolymorphicStructTypeTemplateEntity>& entity)
{ // produce referenced types for (constauto& member : entity->getMembers()) if (!member.parameterized)
produceType(getBaseUnoName(member.type));
CSharpFile file(m_outputDir, name);
// verbose and dry run checks if (m_verbose)
std::cout << "[polystruct] " << name << " -> " << file.getPath() << '\n'; if (m_dryRun) return;
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
// generate struct and type parameters list
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public class ")
.append(getSafeIdentifier(typeName))
.append("<");
separatedForeach(entity->getTypeParameters(), [&file]() { file.append(", "); },
[&file](constauto& param) { file.append(param); });
file.append(">").endLine().beginBlock();
// generate default constructor
file.beginLine()
.append("public ")
.append(getSafeIdentifier(typeName))
.append("()")
.endLine()
.beginBlock()
.endBlock();
file.endLine(); // extra blank line
// generate full constructor
file.beginLine().append("public ").append(getSafeIdentifier(typeName)).append("(");
separatedForeach(entity->getMembers(), [&file]() { file.append(", "); },
[this, &file](constauto& member) {
file.append(member.parameterized ? u2b(member.type)
: getNetName(member.type))
.append(" ")
.append(getSafeIdentifier(member.name));
});
file.append(")").endLine();
file.beginBlock(); for (constauto& member : entity->getMembers())
{
file.beginLine()
.append("this.")
.append(getSafeIdentifier(member.name))
.append(" = ")
.append(getSafeIdentifier(member.name))
.append(";")
.endLine();
}
file.endBlock();
file.endLine(); // extra blank line
// generate struct fields for (constauto& member : entity->getMembers())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
file.beginLine()
.append("public ")
.append(member.parameterized ? u2b(member.type) : getNetName(member.type))
.append(" ")
.append(getSafeIdentifier(member.name))
.append(";")
.endLine();
}
file.endBlock(); if (hasNamespace)
file.endBlock();
file.closeFile();
}
void NetProducer::produceException(std::string_view name, const rtl::Reference<unoidl::ExceptionTypeEntity>& entity)
{ // produce referenced types constauto& base = entity->getDirectBase(); if (!base.isEmpty())
produceType(getBaseUnoName(base)); for (constauto& member : entity->getDirectMembers())
produceType(getBaseUnoName(member.type));
CSharpFile file(m_outputDir, name);
// verbose and dry run checks if (m_verbose)
std::cout << "[exception] " << name << " -> " << file.getPath() << '\n'; if (m_dryRun) return;
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
// generate exception and base exceptions list
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public class ")
.append(getSafeIdentifier(typeName)); if (!base.isEmpty())
file.append(" : ").append(getNetName(base));
file.endLine().beginBlock();
// generate default constructor
file.beginLine()
.append("public ")
.append(getSafeIdentifier(typeName))
.append("()")
.endLine()
.beginBlock()
.endBlock();
file.endLine(); // extra blank line
// generate full constructor
std::vector<unoidl::ExceptionTypeEntity::Member> baseFields;
{
OUString baseTypeName = base; while (!baseTypeName.isEmpty())
{
rtl::Reference<unoidl::Entity> baseEntity; if (m_manager->getSort(baseTypeName, &baseEntity)
!= codemaker::UnoType::Sort::Exception) throw CannotDumpException("'" + b2u(name) + "' base type '" + baseTypeName
+ "' is not an exception");
// generate exception fields for (constauto& member : entity->getDirectMembers())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
file.beginLine()
.append("public ")
.append(getNetName(member.type))
.append(" ")
.append(getSafeIdentifier(member.name))
.append(";")
.endLine();
}
file.endBlock(); if (hasNamespace)
file.endBlock();
file.closeFile();
}
void NetProducer::produceInterface(std::string_view name, const rtl::Reference<unoidl::InterfaceTypeEntity>& entity)
{ // produce referenced types for (constauto& base : entity->getDirectMandatoryBases())
produceType(getBaseUnoName(base.name)); for (constauto& member : entity->getDirectAttributes())
{
produceType(getBaseUnoName(member.type)); for (constauto& e : member.getExceptions)
produceType(getBaseUnoName(e)); for (constauto& e : member.setExceptions)
produceType(getBaseUnoName(e));
} for (constauto& member : entity->getDirectMethods())
{
produceType(getBaseUnoName(member.returnType)); for (constauto& e : member.exceptions)
produceType(getBaseUnoName(e)); for (constauto& p : member.parameters)
produceType(getBaseUnoName(p.type));
}
CSharpFile file(m_outputDir, name);
// verbose and dry run checks if (m_verbose)
std::cout << "[interface] " << name << " -> " << file.getPath() << '\n'; if (m_dryRun) return;
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
// generate interface and base interfaces list
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public interface ")
.append(getSafeIdentifier(typeName)); constauto& bases = entity->getDirectMandatoryBases(); if (!bases.empty())
{
file.append(" : ");
separatedForeach(bases, [&file]() { file.append(", "); },
[this, &file](constauto& b) { file.append(getNetName(b.name)); });
}
file.endLine().beginBlock();
// generate interface properties for (constauto& member : entity->getDirectAttributes())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine(); if (member.bound)
file.beginLine().append("[com.sun.star.uno.Bound]").endLine();
if (hasNamespace)
file.beginLine().append("namespace ").append(namespaceName).endLine().beginBlock();
file.beginLine()
.append("[com.sun.star.uno.UnoGenerated]")
.endLine()
.beginLine()
.append("public static class ")
.append(getSafeIdentifier(typeName))
.endLine()
.beginBlock(); for (constauto& member : entity->getMembers())
{ for (constauto& anno : member.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
OString type, value; switch (member.value.type)
{ case unoidl::ConstantValue::TYPE_BOOLEAN:
type = "bool"_ostr;
value = member.value.booleanValue ? "true"_ostr : "false"_ostr; break; case unoidl::ConstantValue::TYPE_BYTE:
type = "sbyte"_ostr;
value = OString::number(member.value.byteValue); break; case unoidl::ConstantValue::TYPE_SHORT:
type = "short"_ostr;
value = OString::number(member.value.shortValue); break; case unoidl::ConstantValue::TYPE_UNSIGNED_SHORT:
type = "ushort"_ostr;
value = OString::number(member.value.unsignedShortValue); break; case unoidl::ConstantValue::TYPE_LONG:
type = "int"_ostr;
value = OString::number(member.value.longValue); break; case unoidl::ConstantValue::TYPE_UNSIGNED_LONG:
type = "uint"_ostr;
value = OString::number(member.value.unsignedLongValue); break; case unoidl::ConstantValue::TYPE_HYPER:
type = "long"_ostr;
value = OString::number(member.value.hyperValue); break; case unoidl::ConstantValue::TYPE_UNSIGNED_HYPER:
type = "ulong"_ostr;
value = OString::number(member.value.unsignedHyperValue); break; case unoidl::ConstantValue::TYPE_FLOAT:
type = "float"_ostr;
value = OString::number(member.value.floatValue) + "f"; break; case unoidl::ConstantValue::TYPE_DOUBLE:
type = "double"_ostr;
value = OString::number(member.value.doubleValue); break;
}
file.beginLine()
.append("public const ")
.append(type)
.append(" ")
.append(getSafeIdentifier(member.name))
.append(" = ")
.append(value)
.append(";")
.endLine();
}
file.endBlock(); if (hasNamespace)
file.endBlock();
file.closeFile();
}
for (constauto& ctor : entity->getConstructors())
{ for (constauto& anno : ctor.annotations) if (anno == "deprecated")
file.beginLine().append("[System.Obsolete]").endLine();
for (constauto& e : ctor.exceptions)
{
file.beginLine()
.append("catch (")
.append(e)
.append(")")
.endLine()
.beginBlock()
.beginLine()
.append("throw;")
.endLine()
.endBlock();
}
file.beginLine()
.append("catch")
.endLine()
.beginBlock()
.beginLine()
.append("throw new com.sun.star.uno.DeploymentException(")
.append("\"Could not create service ")
.append(name)
.append(" from given XComponentContext\", ctx);")
.endLine()
.endBlock();
for (constauto& e : ctor.exceptions)
{
file.beginLine()
.append("catch (")
.append(getNetName(e))
.append(")")
.endLine()
.beginBlock()
.beginLine()
.append("throw;")
.endLine()
.endBlock();
}
file.beginLine()
.append("catch")
.endLine()
.beginBlock()
.beginLine()
.append("throw new com.sun.star.uno.DeploymentException(")
.append("\"Could not create service ")
.append(name)
.append(" from given XComponentContext\", ctx);")
.endLine()
.endBlock();
file.endBlock();
}
}
file.endBlock(); if (hasNamespace)
file.endBlock();
file.closeFile();
}
file.beginLine()
.append("catch")
.endLine()
.beginBlock()
.beginLine()
.append("throw new com.sun.star.uno.DeploymentException(\"Could not get singleton ")
.append(name)
.append(" from given XComponentContext\", ctx);")
.endLine()
.endBlock();
while (true)
{
OString baseName = getBaseUnoName(fullName); if (m_typedefs.contains(baseName))
fullName = fullName.replaceFirst(baseName, m_typedefs.at(baseName)); else break;
}
std::string_view fullNameView(fullName);
// if sequence, count dimensions int dimensions = 0; while (fullNameView.starts_with("[]"))
{
++dimensions;
fullNameView = fullNameView.substr(2);
}
// if polymorphic, process parameters too if (fullNameView.ends_with('>'))
{ const std::string_view::size_type nFirstAngle = fullNameView.find_first_of('<');
size_t start = (nFirstAngle != std::string_view::npos) ? (nFirstAngle + 1) : 0;
size_t end = fullNameView.size() - 1;
buffer.append(fullNameView.substr(0, start));
OString params(fullNameView.substr(start, end - start));
bool first = true; for (start = 0; start != std::string_view::npos;)
{
std::string_view param(o3tl::getToken(params, ',', start)); if (first)
first = false; else
buffer.append(", ");
buffer.append(getNetName(param));
}
buffer.append(">");
} else
{ // assumes basetypes are not polymorphic for a tiny optimization // if this is changed later, move this part out of the else block
fullName = OString(fullNameView);
OString baseName = getBaseUnoName(fullName); if (s_baseTypes.contains(baseName))
buffer.append(fullName.replaceFirst(baseName, s_baseTypes.at(baseName))); else
buffer.append(fullName);
}
// if sequence, add [] to make array while (dimensions--)
buffer.append("[]");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.