io.joynr.generator.cpp.communicationmodel.EnumCppTemplate Maven / Gradle / Ivy
package io.joynr.generator.cpp.communicationmodel;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import io.joynr.generator.cpp.util.CppStdTypeUtil;
import io.joynr.generator.cpp.util.JoynrCppGeneratorExtensions;
import io.joynr.generator.cpp.util.TemplateBase;
import io.joynr.generator.templates.EnumTemplate;
import io.joynr.generator.templates.util.NamingUtil;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.franca.core.franca.FEnumerationType;
import org.franca.core.franca.FEnumerator;
@SuppressWarnings("all")
public class EnumCppTemplate extends EnumTemplate {
@Inject
@Extension
private TemplateBase _templateBase;
@Inject
@Extension
private CppStdTypeUtil _cppStdTypeUtil;
@Inject
@Extension
private NamingUtil _namingUtil;
@Inject
@Extension
private JoynrCppGeneratorExtensions _joynrCppGeneratorExtensions;
@Inject
public EnumCppTemplate(@Assisted final FEnumerationType type) {
super(type);
}
@Override
public CharSequence generate(final boolean generateVersion) {
StringConcatenation _builder = new StringConcatenation();
final String typeName = this._namingUtil.joynrName(this.type);
_builder.newLineIfNotEmpty();
CharSequence _warning = this._templateBase.warning();
_builder.append(_warning);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("#include ");
String _includeOf = this._cppStdTypeUtil.getIncludeOf(this.type, generateVersion);
_builder.append(_includeOf);
_builder.newLineIfNotEmpty();
_builder.append("#include ");
_builder.newLine();
_builder.newLine();
String _namespaceStarter = this._joynrCppGeneratorExtensions.getNamespaceStarter(this.type, true, generateVersion);
_builder.append(_namespaceStarter);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("const std::int32_t ");
_builder.append(typeName);
_builder.append("::MAJOR_VERSION = ");
_builder.append(this.majorVersion);
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("const std::int32_t ");
_builder.append(typeName);
_builder.append("::MINOR_VERSION = ");
_builder.append(this.minorVersion);
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("std::string ");
_builder.append(typeName);
_builder.append("::getLiteral(const ");
_builder.append(typeName);
_builder.append("::");
String _nestedEnumName = this._joynrCppGeneratorExtensions.getNestedEnumName();
_builder.append(_nestedEnumName);
_builder.append("& ");
String _firstLower = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower);
_builder.append("Value) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("std::string literal;");
_builder.newLine();
_builder.append("\t");
_builder.append("switch (");
String _firstLower_1 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_1, "\t");
_builder.append("Value) {");
_builder.newLineIfNotEmpty();
{
EList _enumElementsAndBaseEnumElements = this._cppStdTypeUtil.getEnumElementsAndBaseEnumElements(this.type);
for(final FEnumerator literal : _enumElementsAndBaseEnumElements) {
_builder.append("\t");
_builder.append("case ");
String _joynrName = this._namingUtil.joynrName(literal);
_builder.append(_joynrName, "\t");
_builder.append(":");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("literal = std::string(\"");
String _joynrName_1 = this._namingUtil.joynrName(literal);
_builder.append(_joynrName_1, "\t\t");
_builder.append("\");");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("break;");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("if (literal.empty()) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("throw std::invalid_argument(\"");
_builder.append(typeName, "\t\t");
_builder.append(": No literal found for value \\\"\" + std::to_string(");
String _firstLower_2 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_2, "\t\t");
_builder.append("Value) + \"\\\"\");");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("return literal;");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(typeName);
_builder.append("::");
String _nestedEnumName_1 = this._joynrCppGeneratorExtensions.getNestedEnumName();
_builder.append(_nestedEnumName_1);
_builder.append(" ");
_builder.append(typeName);
_builder.append("::getEnum(const std::string& ");
String _firstLower_3 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_3);
_builder.append("String) {");
_builder.newLineIfNotEmpty();
{
EList _enumElementsAndBaseEnumElements_1 = this._cppStdTypeUtil.getEnumElementsAndBaseEnumElements(this.type);
for(final FEnumerator literal_1 : _enumElementsAndBaseEnumElements_1) {
_builder.append("\t");
_builder.append("if (");
String _firstLower_4 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_4, "\t");
_builder.append("String == std::string(\"");
String _joynrName_2 = this._namingUtil.joynrName(literal_1);
_builder.append(_joynrName_2, "\t");
_builder.append("\")) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("return ");
String _joynrName_3 = this._namingUtil.joynrName(literal_1);
_builder.append(_joynrName_3, "\t\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("std::stringstream errorMessage(");
String _firstLower_5 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_5, "\t");
_builder.append("String);");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("errorMessage << \" is unknown literal for ");
String _joynrName_4 = this._namingUtil.joynrName(this.type);
_builder.append(_joynrName_4, " ");
_builder.append("\";");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("throw std::invalid_argument(errorMessage.str());");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("std::string ");
_builder.append(typeName);
_builder.append("::getTypeName() {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("return \"");
String _buildPackagePath = this._joynrCppGeneratorExtensions.buildPackagePath(this.type, ".", true, generateVersion);
String _plus = (_buildPackagePath + ".");
String _joynrName_5 = this._namingUtil.joynrName(this.type);
String _plus_1 = (_plus + _joynrName_5);
_builder.append(_plus_1, "\t");
_builder.append("\";");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("std::uint32_t ");
_builder.append(typeName);
_builder.append("::getOrdinal(");
_builder.append(typeName);
_builder.append("::");
String _nestedEnumName_2 = this._joynrCppGeneratorExtensions.getNestedEnumName();
_builder.append(_nestedEnumName_2);
_builder.append(" ");
String _firstLower_6 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_6);
_builder.append("Value) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("return static_cast(");
String _firstLower_7 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_7, "\t");
_builder.append("Value);");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("// Printing ");
_builder.append(typeName);
_builder.append(" with google-test and google-mock.");
_builder.newLineIfNotEmpty();
_builder.append("void PrintTo(const ");
_builder.append(typeName);
_builder.append("::");
String _nestedEnumName_3 = this._joynrCppGeneratorExtensions.getNestedEnumName();
_builder.append(_nestedEnumName_3);
_builder.append("& ");
String _firstLower_8 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_8);
_builder.append("Value, ::std::ostream* os) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("*os << \"");
_builder.append(typeName, "\t");
_builder.append("::\" << ");
_builder.append(typeName, "\t");
_builder.append("::getLiteral(");
String _firstLower_9 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_9, "\t");
_builder.append("Value)");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("<< \" (\" << ");
_builder.append(typeName, "\t\t\t");
_builder.append("::getOrdinal(");
String _firstLower_10 = StringExtensions.toFirstLower(typeName);
_builder.append(_firstLower_10, "\t\t\t");
_builder.append("Value) << \")\";");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
_builder.newLine();
String _namespaceEnder = this._joynrCppGeneratorExtensions.getNamespaceEnder(this.type, true, generateVersion);
_builder.append(_namespaceEnder);
_builder.newLineIfNotEmpty();
return _builder;
}
}