All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joynr.generator.cpp.communicationmodel.EnumHTemplate 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 EnumHTemplate extends EnumTemplate {
  @Inject
  @Extension
  private TemplateBase _templateBase;

  @Inject
  @Extension
  private JoynrCppGeneratorExtensions _joynrCppGeneratorExtensions;

  @Inject
  @Extension
  private CppStdTypeUtil _cppStdTypeUtil;

  @Inject
  @Extension
  private NamingUtil _namingUtil;

  @Inject
  public EnumHTemplate(@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();
    String _packagePathWithJoynrPrefix = this._joynrCppGeneratorExtensions.getPackagePathWithJoynrPrefix(this.type, "_", true, generateVersion);
    String _plus = (_packagePathWithJoynrPrefix + "_");
    String _plus_1 = (_plus + typeName);
    final String headerGuard = (_plus_1 + "_h").toUpperCase();
    _builder.newLineIfNotEmpty();
    CharSequence _warning = this._templateBase.warning();
    _builder.append(_warning);
    _builder.newLineIfNotEmpty();
    _builder.append("#ifndef ");
    _builder.append(headerGuard);
    _builder.newLineIfNotEmpty();
    _builder.append("#define ");
    _builder.append(headerGuard);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("#include ");
    _builder.newLine();
    _builder.append("#include ");
    _builder.newLine();
    _builder.append("#include ");
    _builder.newLine();
    _builder.newLine();
    _builder.append("#include \"joynr/Util.h\"");
    _builder.newLine();
    _builder.append("#include \"joynr/exceptions/JoynrException.h\"");
    _builder.newLine();
    _builder.append("#include \"joynr/serializer/Serializer.h\"");
    _builder.newLine();
    _builder.newLine();
    {
      boolean _hasExtendsDeclaration = this._cppStdTypeUtil.hasExtendsDeclaration(this.type);
      if (_hasExtendsDeclaration) {
        _builder.append("#include ");
        String _includeOf = this._cppStdTypeUtil.getIncludeOf(this._cppStdTypeUtil.getExtendedType(this.type), generateVersion);
        _builder.append(_includeOf);
        _builder.newLineIfNotEmpty();
        _builder.newLine();
      }
    }
    String _namespaceStarter = this._joynrCppGeneratorExtensions.getNamespaceStarter(this.type, true, generateVersion);
    _builder.append(_namespaceStarter);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @brief Enumeration wrapper class ");
    _builder.append(typeName, " ");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("*");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @version ");
    _builder.append(this.majorVersion, " ");
    _builder.append(".");
    _builder.append(this.minorVersion, " ");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("struct ");
    _builder.append(typeName);
    _builder.append(" : public joynr::exceptions::ApplicationExceptionError {");
    _builder.newLineIfNotEmpty();
    {
      boolean _hasExtendsDeclaration_1 = this._cppStdTypeUtil.hasExtendsDeclaration(this.type);
      if (_hasExtendsDeclaration_1) {
        _builder.append("\t");
        _builder.append("// This enum inherits enumeration values from ");
        String _typeName = this._cppStdTypeUtil.getTypeName(this._cppStdTypeUtil.getExtendedType(this.type), generateVersion);
        _builder.append(_typeName, "\t");
        _builder.append(".");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    _builder.append("\t");
    _builder.append("using ApplicationExceptionError::ApplicationExceptionError;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append(typeName, "\t");
    _builder.append("() = default;");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("~");
    _builder.append(typeName, "\t");
    _builder.append("() override = default;");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t");
    CharSequence _appendDoxygenSummaryAndWriteSeeAndDescription = this._joynrCppGeneratorExtensions.appendDoxygenSummaryAndWriteSeeAndDescription(this.type, " *");
    _builder.append(_appendDoxygenSummaryAndWriteSeeAndDescription, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("* @version ");
    _builder.append(this.majorVersion, "\t ");
    _builder.append(".");
    _builder.append(this.minorVersion, "\t ");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("enum ");
    String _nestedEnumName = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName, "\t");
    _builder.append(" : std::uint32_t {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    int ordinal = (-1);
    _builder.newLineIfNotEmpty();
    {
      EList _enumElementsAndBaseEnumElements = this._cppStdTypeUtil.getEnumElementsAndBaseEnumElements(this.type);
      boolean _hasElements = false;
      for(final FEnumerator enumtype : _enumElementsAndBaseEnumElements) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "\t\t");
        }
        _builder.append("\t\t");
        _builder.append("/**");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append(" ");
        _builder.append("* @brief ");
        CharSequence _appendDoxygenComment = this._joynrCppGeneratorExtensions.appendDoxygenComment(enumtype, "* ");
        _builder.append(_appendDoxygenComment, "\t\t ");
        _builder.newLineIfNotEmpty();
        _builder.append("\t\t");
        _builder.append(" ");
        _builder.append("*/");
        _builder.newLine();
        _builder.append("\t\t");
        String _xblockexpression = null;
        {
          Integer _xifexpression = null;
          String _enumeratorValue = this._cppStdTypeUtil.getEnumeratorValue(enumtype.getValue());
          boolean _tripleEquals = (_enumeratorValue == null);
          if (_tripleEquals) {
            _xifexpression = Integer.valueOf((ordinal + 1));
          } else {
            _xifexpression = Integer.valueOf(this._cppStdTypeUtil.getEnumeratorValue(enumtype.getValue()));
          }
          ordinal = (_xifexpression).intValue();
          _xblockexpression = "";
        }
        _builder.append(_xblockexpression, "\t\t");
        _builder.newLineIfNotEmpty();
        _builder.append("\t\t");
        String _joynrName = this._namingUtil.joynrName(enumtype);
        _builder.append(_joynrName, "\t\t");
        _builder.append(" = ");
        _builder.append(ordinal, "\t\t");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.append("};");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief MAJOR_VERSION The major version of this struct as specified in the");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* type collection or interface in the Franca model.");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static const std::int32_t MAJOR_VERSION;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief MINOR_VERSION The minor version of this struct as specified in the");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* type collection or interface in the Franca model.");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static const std::int32_t MINOR_VERSION;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief Copy constructor");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @param o the object to copy from");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append(typeName, "\t");
    _builder.append("(const ");
    _builder.append(typeName, "\t");
    _builder.append("& o) = delete;");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief Get the matching enum name for an ordinal number");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @param ");
    String _firstLower = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower, "\t ");
    _builder.append("Value The ordinal number");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("* @return The string representing the enum for the given ordinal number");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static std::string getLiteral(const ");
    _builder.append(typeName, "\t");
    _builder.append("::");
    String _nestedEnumName_1 = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName_1, "\t");
    _builder.append("& ");
    String _firstLower_1 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_1, "\t");
    _builder.append("Value);");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief Get the matching enum for a string");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @param ");
    String _firstLower_2 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_2, "\t ");
    _builder.append("String The string representing the enum value");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("* @return The enum value representing the string");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static ");
    _builder.append(typeName, "\t");
    _builder.append("::");
    String _nestedEnumName_2 = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName_2, "\t");
    _builder.append(" getEnum(const std::string& ");
    String _firstLower_3 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_3, "\t");
    _builder.append("String);");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief Get the matching ordinal number for an enum");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @param ");
    String _firstLower_4 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_4, "\t ");
    _builder.append("Value The enum");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("* @return The ordinal number representing the enum");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static std::uint32_t getOrdinal(");
    _builder.append(typeName, "\t");
    _builder.append("::");
    String _nestedEnumName_3 = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName_3, "\t");
    _builder.append(" ");
    String _firstLower_5 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_5, "\t");
    _builder.append("Value);");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief Get the typeName of the enumeration type");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @return The typeName of the enumeration type");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static std::string getTypeName();");
    _builder.newLine();
    _builder.append("};");
    _builder.newLine();
    _builder.newLine();
    _builder.append("// Printing ");
    _builder.append(typeName);
    _builder.append(" with google-test and google-mock.");
    _builder.newLineIfNotEmpty();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @brief Print values of MessagingQos object");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @param messagingQos The current object instance");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @param os The output stream to send the output to");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("void PrintTo(const ");
    String _typeName_1 = this._cppStdTypeUtil.getTypeName(this.type, generateVersion);
    _builder.append(_typeName_1);
    _builder.append("& ");
    String _firstLower_6 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_6);
    _builder.append("Value, ::std::ostream* os);");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    String _namespaceEnder = this._joynrCppGeneratorExtensions.getNamespaceEnder(this.type, true, generateVersion);
    _builder.append(_namespaceEnder);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("namespace std {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @brief Function object that implements a hash function for ");
    String _buildPackagePath = this._joynrCppGeneratorExtensions.buildPackagePath(this.type, "::", true, generateVersion);
    _builder.append(_buildPackagePath, " ");
    _builder.append("::");
    _builder.append(typeName, " ");
    _builder.append(".");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("*");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* Used by the unordered associative containers std::unordered_set, std::unordered_multiset,");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* std::unordered_map, std::unordered_multimap as default hash function.");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("template<>");
    _builder.newLine();
    _builder.append("struct hash<");
    String _buildPackagePath_1 = this._joynrCppGeneratorExtensions.buildPackagePath(this.type, "::", true, generateVersion);
    _builder.append(_buildPackagePath_1);
    _builder.append("::");
    _builder.append(typeName);
    _builder.append("::");
    String _nestedEnumName_4 = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName_4);
    _builder.append("> {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @brief method overriding default implementation of operator ()");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* @param ");
    String _firstLower_7 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_7, "\t ");
    _builder.append("Value the operators argument");
    _builder.newLineIfNotEmpty();
    _builder.append("\t ");
    _builder.append("* @return the ordinal number representing the enum value");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("std::size_t operator()(const ");
    String _buildPackagePath_2 = this._joynrCppGeneratorExtensions.buildPackagePath(this.type, "::", true, generateVersion);
    _builder.append(_buildPackagePath_2, "\t");
    _builder.append("::");
    _builder.append(typeName, "\t");
    _builder.append("::");
    String _nestedEnumName_5 = this._joynrCppGeneratorExtensions.getNestedEnumName();
    _builder.append(_nestedEnumName_5, "\t");
    _builder.append("& ");
    String _firstLower_8 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_8, "\t");
    _builder.append("Value) const {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("return ");
    String _buildPackagePath_3 = this._joynrCppGeneratorExtensions.buildPackagePath(this.type, "::", true, generateVersion);
    _builder.append(_buildPackagePath_3, "\t\t");
    _builder.append("::");
    _builder.append(typeName, "\t\t");
    _builder.append("::getOrdinal(");
    String _firstLower_9 = StringExtensions.toFirstLower(typeName);
    _builder.append(_firstLower_9, "\t\t");
    _builder.append("Value);");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("};");
    _builder.newLine();
    _builder.append("} // namespace std");
    _builder.newLine();
    _builder.newLine();
    _builder.append("MUESLI_REGISTER_POLYMORPHIC_TYPE(");
    String _typeNameOfContainingClass = this._cppStdTypeUtil.getTypeNameOfContainingClass(this.type, generateVersion);
    _builder.append(_typeNameOfContainingClass);
    _builder.append(", joynr::exceptions::ApplicationExceptionError, \"");
    String _replace = this._cppStdTypeUtil.getTypeNameOfContainingClass(this.type, generateVersion).replace("::", ".");
    _builder.append(_replace);
    _builder.append("\")");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("namespace muesli");
    _builder.newLine();
    _builder.append("{");
    _builder.newLine();
    _builder.append("template <>");
    _builder.newLine();
    _builder.append("struct EnumTraits<");
    String _typeName_2 = this._cppStdTypeUtil.getTypeName(this.type, generateVersion);
    _builder.append(_typeName_2);
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    _builder.append("{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("using Wrapper = ");
    String _typeNameOfContainingClass_1 = this._cppStdTypeUtil.getTypeNameOfContainingClass(this.type, generateVersion);
    _builder.append(_typeNameOfContainingClass_1, "\t");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("};");
    _builder.newLine();
    _builder.append("} // namespace muesli");
    _builder.newLine();
    _builder.newLine();
    _builder.append("#endif // ");
    _builder.append(headerGuard);
    _builder.newLineIfNotEmpty();
    return _builder;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy